17c478bd9Sstevel@tonic-gate #ifndef stringmem_h
27c478bd9Sstevel@tonic-gate #define stringmem_h
37c478bd9Sstevel@tonic-gate /*
47c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
5*1da57d55SToomas Soome  *
67c478bd9Sstevel@tonic-gate  * All rights reserved.
7*1da57d55SToomas Soome  *
87c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
97c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
107c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
117c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
127c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
137c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
147c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
157c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
167c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
17*1da57d55SToomas Soome  *
187c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
217c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
227c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
237c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
247c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
257c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
267c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27*1da57d55SToomas Soome  *
287c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
297c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
307c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
317c478bd9Sstevel@tonic-gate  * of the copyright holder.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate typedef struct StringMem StringMem;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Applications that dynamically allocate lots of small strings
387c478bd9Sstevel@tonic-gate  * run the risk of significantly fragmenting the heap. This module
397c478bd9Sstevel@tonic-gate  * aims to reduce this risk by allocating large arrays of small fixed
407c478bd9Sstevel@tonic-gate  * length strings, arranging them as a free-list and allowing
417c478bd9Sstevel@tonic-gate  * callers to allocate from the list. Strings that are too long
427c478bd9Sstevel@tonic-gate  * to be allocated from the free-list are allocated from the heap.
437c478bd9Sstevel@tonic-gate  * Since typical implementations of malloc() eat up a minimum of
447c478bd9Sstevel@tonic-gate  * 16 bytes per call to malloc() [because of alignment and space
457c478bd9Sstevel@tonic-gate  * management constraints] it makes sense to set the free-list
467c478bd9Sstevel@tonic-gate  * string size to 16 bytes. Note that unlike malloc() which typically
477c478bd9Sstevel@tonic-gate  * keeps 8 bytes per allocation for its own use, our allocator will
487c478bd9Sstevel@tonic-gate  * return all but one of the 16 bytes for use. One hidden byte of overhead
497c478bd9Sstevel@tonic-gate  * is reserved for flagging whether the string was allocated directly
507c478bd9Sstevel@tonic-gate  * from malloc or from the free-list.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Set the length of each free-list string. The longest string that
557c478bd9Sstevel@tonic-gate  * will be returned without calling malloc() will be one less than
567c478bd9Sstevel@tonic-gate  * this number.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate #define SM_STRLEN 16
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Create a string free-list container and the first block of its free-list.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate StringMem *_new_StringMem(unsigned blocking_factor);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Delete a string free-list.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate StringMem *_del_StringMem(StringMem *sm, int force);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Allocate an array of 'length' chars.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate char *_new_StringMemString(StringMem *sm, size_t size);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Free a string that was previously returned by _new_StringMemString().
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate char *_del_StringMemString(StringMem *sm, char *s);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #endif
81