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