1 #ifndef stringrp_h
2 #define stringrp_h
3 /*
4  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
5  *
6  * All rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, and/or sell copies of the Software, and to permit persons
13  * to whom the Software is furnished to do so, provided that the above
14  * copyright notice(s) and this permission notice appear in all copies of
15  * the Software and that both the above copyright notice(s) and this
16  * permission notice appear in supporting documentation.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
21  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
23  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
24  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
25  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
26  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  *
28  * Except as contained in this notice, the name of a copyright holder
29  * shall not be used in advertising or otherwise to promote the sale, use
30  * or other dealings in this Software without prior written authorization
31  * of the copyright holder.
32  */
33 
34 /*
35  * StringGroup objects provide memory for modules that need to
36  * allocate lots of small strings without needing to free any of them
37  * individually, but rather is happy to free them all at the same
38  * time. Taking advantage of these properties, StringGroup objects
39  * avoid the heap fragmentation that tends to occur when lots of small
40  * strings are allocated directly from the heap and later free'd. They
41  * do this by allocating a list of large character arrays in each of
42  * which multiple strings are stored. Thus instead of allocating lots
43  * of small strings, a few large character arrays are allocated. When
44  * the strings are free'd on mass, this list of character arrays is
45  * maintained, ready for subsequent use in recording another set of
46  * strings.
47  */
48 typedef struct StringGroup StringGroup;
49 
50 /*
51  * The following constructor allocates a string-allocation object.
52  * The segment_size argument specifies how long each string segment
53  * array should be. This should be at least 10 times the length of
54  * the average string to be recorded in the string group, and
55  * sets the length of the longest string that can be stored.
56  */
57 StringGroup *_new_StringGroup(int segment_size);
58 
59 /*
60  * Delete all of the strings that are currently stored by a specified
61  * StringGroup object.
62  */
63 void _clr_StringGroup(StringGroup *sg);
64 
65 /*
66  * Make a copy of the specified string, returning a pointer to
67  * the copy, or NULL if there was insufficient memory. If the
68  * remove_escapes argument is non-zero, backslashes that escape
69  * other characters will be removed.
70  */
71 char *_sg_store_string(StringGroup *sg, const char *string, int remove_escapes);
72 
73 /*
74  * Allocate memory for a string of a given length.
75  */
76 char *_sg_alloc_string(StringGroup *sg, int length);
77 
78 /*
79  * Delete a StringGroup object (and all of the strings that it
80  * contains).
81  */
82 StringGroup *_del_StringGroup(StringGroup *sg);
83 
84 #endif
85