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