1*7c478bd9Sstevel@tonic-gate #ifndef stringmem_h 2*7c478bd9Sstevel@tonic-gate #define stringmem_h 3*7c478bd9Sstevel@tonic-gate /* 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd. 5*7c478bd9Sstevel@tonic-gate * 6*7c478bd9Sstevel@tonic-gate * All rights reserved. 7*7c478bd9Sstevel@tonic-gate * 8*7c478bd9Sstevel@tonic-gate * Permission is hereby granted, free of charge, to any person obtaining a 9*7c478bd9Sstevel@tonic-gate * copy of this software and associated documentation files (the 10*7c478bd9Sstevel@tonic-gate * "Software"), to deal in the Software without restriction, including 11*7c478bd9Sstevel@tonic-gate * without limitation the rights to use, copy, modify, merge, publish, 12*7c478bd9Sstevel@tonic-gate * distribute, and/or sell copies of the Software, and to permit persons 13*7c478bd9Sstevel@tonic-gate * to whom the Software is furnished to do so, provided that the above 14*7c478bd9Sstevel@tonic-gate * copyright notice(s) and this permission notice appear in all copies of 15*7c478bd9Sstevel@tonic-gate * the Software and that both the above copyright notice(s) and this 16*7c478bd9Sstevel@tonic-gate * permission notice appear in supporting documentation. 17*7c478bd9Sstevel@tonic-gate * 18*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19*7c478bd9Sstevel@tonic-gate * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20*7c478bd9Sstevel@tonic-gate * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 21*7c478bd9Sstevel@tonic-gate * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22*7c478bd9Sstevel@tonic-gate * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 23*7c478bd9Sstevel@tonic-gate * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 24*7c478bd9Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 25*7c478bd9Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 26*7c478bd9Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 27*7c478bd9Sstevel@tonic-gate * 28*7c478bd9Sstevel@tonic-gate * Except as contained in this notice, the name of a copyright holder 29*7c478bd9Sstevel@tonic-gate * shall not be used in advertising or otherwise to promote the sale, use 30*7c478bd9Sstevel@tonic-gate * or other dealings in this Software without prior written authorization 31*7c478bd9Sstevel@tonic-gate * of the copyright holder. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate typedef struct StringMem StringMem; 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate /* 39*7c478bd9Sstevel@tonic-gate * Applications that dynamically allocate lots of small strings 40*7c478bd9Sstevel@tonic-gate * run the risk of significantly fragmenting the heap. This module 41*7c478bd9Sstevel@tonic-gate * aims to reduce this risk by allocating large arrays of small fixed 42*7c478bd9Sstevel@tonic-gate * length strings, arranging them as a free-list and allowing 43*7c478bd9Sstevel@tonic-gate * callers to allocate from the list. Strings that are too long 44*7c478bd9Sstevel@tonic-gate * to be allocated from the free-list are allocated from the heap. 45*7c478bd9Sstevel@tonic-gate * Since typical implementations of malloc() eat up a minimum of 46*7c478bd9Sstevel@tonic-gate * 16 bytes per call to malloc() [because of alignment and space 47*7c478bd9Sstevel@tonic-gate * management constraints] it makes sense to set the free-list 48*7c478bd9Sstevel@tonic-gate * string size to 16 bytes. Note that unlike malloc() which typically 49*7c478bd9Sstevel@tonic-gate * keeps 8 bytes per allocation for its own use, our allocator will 50*7c478bd9Sstevel@tonic-gate * return all but one of the 16 bytes for use. One hidden byte of overhead 51*7c478bd9Sstevel@tonic-gate * is reserved for flagging whether the string was allocated directly 52*7c478bd9Sstevel@tonic-gate * from malloc or from the free-list. 53*7c478bd9Sstevel@tonic-gate */ 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate /* 56*7c478bd9Sstevel@tonic-gate * Set the length of each free-list string. The longest string that 57*7c478bd9Sstevel@tonic-gate * will be returned without calling malloc() will be one less than 58*7c478bd9Sstevel@tonic-gate * this number. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate #define SM_STRLEN 16 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate /* 63*7c478bd9Sstevel@tonic-gate * Create a string free-list container and the first block of its free-list. 64*7c478bd9Sstevel@tonic-gate */ 65*7c478bd9Sstevel@tonic-gate StringMem *_new_StringMem(unsigned blocking_factor); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate /* 68*7c478bd9Sstevel@tonic-gate * Delete a string free-list. 69*7c478bd9Sstevel@tonic-gate */ 70*7c478bd9Sstevel@tonic-gate StringMem *_del_StringMem(StringMem *sm, int force); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate /* 73*7c478bd9Sstevel@tonic-gate * Allocate an array of 'length' chars. 74*7c478bd9Sstevel@tonic-gate */ 75*7c478bd9Sstevel@tonic-gate char *_new_StringMemString(StringMem *sm, size_t size); 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* 78*7c478bd9Sstevel@tonic-gate * Free a string that was previously returned by _new_StringMemString(). 79*7c478bd9Sstevel@tonic-gate */ 80*7c478bd9Sstevel@tonic-gate char *_del_StringMemString(StringMem *sm, char *s); 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate #endif 83