xref: /titanic_50/usr/src/lib/libtecla/common/strngmem.h (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 #ifndef stringmem_h
2 #define stringmem_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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 typedef struct StringMem StringMem;
37 
38 /*
39  * Applications that dynamically allocate lots of small strings
40  * run the risk of significantly fragmenting the heap. This module
41  * aims to reduce this risk by allocating large arrays of small fixed
42  * length strings, arranging them as a free-list and allowing
43  * callers to allocate from the list. Strings that are too long
44  * to be allocated from the free-list are allocated from the heap.
45  * Since typical implementations of malloc() eat up a minimum of
46  * 16 bytes per call to malloc() [because of alignment and space
47  * management constraints] it makes sense to set the free-list
48  * string size to 16 bytes. Note that unlike malloc() which typically
49  * keeps 8 bytes per allocation for its own use, our allocator will
50  * return all but one of the 16 bytes for use. One hidden byte of overhead
51  * is reserved for flagging whether the string was allocated directly
52  * from malloc or from the free-list.
53  */
54 
55 /*
56  * Set the length of each free-list string. The longest string that
57  * will be returned without calling malloc() will be one less than
58  * this number.
59  */
60 #define SM_STRLEN 16
61 
62 /*
63  * Create a string free-list container and the first block of its free-list.
64  */
65 StringMem *_new_StringMem(unsigned blocking_factor);
66 
67 /*
68  * Delete a string free-list.
69  */
70 StringMem *_del_StringMem(StringMem *sm, int force);
71 
72 /*
73  * Allocate an array of 'length' chars.
74  */
75 char *_new_StringMemString(StringMem *sm, size_t size);
76 
77 /*
78  * Free a string that was previously returned by _new_StringMemString().
79  */
80 char *_del_StringMemString(StringMem *sm, char *s);
81 
82 #endif
83