|
xorp
|
00001 #ifndef stringmem_h 00002 #define stringmem_h 00003 /* 00004 * Copyright (c) 2000, 2001 by Martin C. Shepherd. 00005 * 00006 * All rights reserved. 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a 00009 * copy of this software and associated documentation files (the 00010 * "Software"), to deal in the Software without restriction, including 00011 * without limitation the rights to use, copy, modify, merge, publish, 00012 * distribute, and/or sell copies of the Software, and to permit persons 00013 * to whom the Software is furnished to do so, provided that the above 00014 * copyright notice(s) and this permission notice appear in all copies of 00015 * the Software and that both the above copyright notice(s) and this 00016 * permission notice appear in supporting documentation. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00019 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00020 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 00021 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00022 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 00023 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 00024 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00025 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00026 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00027 * 00028 * Except as contained in this notice, the name of a copyright holder 00029 * shall not be used in advertising or otherwise to promote the sale, use 00030 * or other dealings in this Software without prior written authorization 00031 * of the copyright holder. 00032 */ 00033 00034 typedef struct StringMem StringMem; 00035 00036 /* 00037 * Applications that dynamically allocate lots of small strings 00038 * run the risk of significantly fragmenting the heap. This module 00039 * aims to reduce this risk by allocating large arrays of small fixed 00040 * length strings, arranging them as a free-list and allowing 00041 * callers to allocate from the list. Strings that are too long 00042 * to be allocated from the free-list are allocated from the heap. 00043 * Since typical implementations of malloc() eat up a minimum of 00044 * 16 bytes per call to malloc() [because of alignment and space 00045 * management constraints] it makes sense to set the free-list 00046 * string size to 16 bytes. Note that unlike malloc() which typically 00047 * keeps 8 bytes per allocation for its own use, our allocator will 00048 * return all but one of the 16 bytes for use. One hidden byte of overhead 00049 * is reserved for flagging whether the string was allocated directly 00050 * from malloc or from the free-list. 00051 */ 00052 00053 /* 00054 * Set the length of each free-list string. The longest string that 00055 * will be returned without calling malloc() will be one less than 00056 * this number. 00057 */ 00058 #define SM_STRLEN 16 00059 00060 /* 00061 * Create a string free-list container and the first block of its free-list. 00062 */ 00063 StringMem *_new_StringMem(const char *caller, unsigned blocking_factor); 00064 00065 /* 00066 * Delete a string free-list. 00067 */ 00068 StringMem *_del_StringMem(const char *caller, StringMem *sm, int force); 00069 00070 /* 00071 * Allocate an array of 'length' chars. 00072 */ 00073 char *_new_StringMemString(StringMem *sm, size_t size); 00074 00075 /* 00076 * Free a string that was previously returned by _new_StringMemString(). 00077 */ 00078 char *_del_StringMemString(StringMem *sm, char *s); 00079 00080 #endif