CARMA C++
stringmem.h
1 #ifndef stringmem_h
2 #define stringmem_h
3 
4 typedef struct StringMem StringMem;
5 
6 /*
7  * Applications that dynamically allocate lots of small strings
8  * run the risk of significantly fragmenting the heap. This module
9  * aims to reduce this risk by allocating large arrays of small fixed
10  * length strings, arranging them as a free-list and allowing
11  * callers to allocate from the list. Strings that are too long
12  * to be allocated from the free-list are allocated from the heap.
13  * Since typical implementations of malloc() eat up a minimum of
14  * 16 bytes per call to malloc() [because of alignment and space
15  * management constraints] it makes sense to set the free-list
16  * string size to 16 bytes. Note that unlike malloc() which typically
17  * keeps 8 bytes per allocation for its own use, our allocator will
18  * return all but one of the 16 bytes for use. One hidden byte of overhead
19  * is reserved for flagging whether the string was allocated directly
20  * from malloc or from the free-list.
21  */
22 
23 /*
24  * Set the length of each free-list string. The longest string that
25  * will be returned without calling malloc() will be one less than
26  * this number.
27  */
28 #define SM_STRLEN 16
29 
30 /*
31  * Create a string free-list container and the first block of its free-list.
32  */
33 StringMem *new_StringMem(const char *caller, unsigned blocking_factor);
34 
35 /*
36  * Delete a string free-list.
37  */
38 StringMem *del_StringMem(const char *caller, StringMem *sm, int force);
39 
40 /*
41  * Allocate an array of 'length' chars.
42  */
43 char *new_StringMemString(const char *caller, StringMem *sm, size_t size);
44 
45 /*
46  * Free a string that was previously returned by new_StringMemString().
47  */
48 char *del_StringMemString(const char *caller, StringMem *sm, char *s);
49 
50 #endif