17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*1da57d55SToomas Soome  *
47c478bd9Sstevel@tonic-gate  * All rights reserved.
5*1da57d55SToomas Soome  *
67c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
77c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
87c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
97c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
107c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
117c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
127c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
137c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
147c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*1da57d55SToomas Soome  *
167c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
187c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
197c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
207c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
217c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
227c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
237c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
247c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*1da57d55SToomas Soome  *
267c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
277c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
287c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
297c478bd9Sstevel@tonic-gate  * of the copyright holder.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "freelist.h"
437c478bd9Sstevel@tonic-gate #include "stringrp.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * StringSegment objects store lots of small strings in larger
477c478bd9Sstevel@tonic-gate  * character arrays. Since the total length of all of the strings can't
487c478bd9Sstevel@tonic-gate  * be known in advance, an extensible list of large character arrays,
497c478bd9Sstevel@tonic-gate  * called string-segments are used.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate typedef struct StringSegment StringSegment;
527c478bd9Sstevel@tonic-gate struct StringSegment {
537c478bd9Sstevel@tonic-gate   StringSegment *next; /* A pointer to the next segment in the list */
547c478bd9Sstevel@tonic-gate   char *block;         /* An array of characters to be shared between strings */
557c478bd9Sstevel@tonic-gate   int unused;          /* The amount of unused space at the end of block[] */
567c478bd9Sstevel@tonic-gate };
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * StringGroup is typedef'd in stringrp.h.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate struct StringGroup {
627c478bd9Sstevel@tonic-gate   FreeList *node_mem;  /* The StringSegment free-list */
637c478bd9Sstevel@tonic-gate   int block_size;      /* The dimension of each character array block */
647c478bd9Sstevel@tonic-gate   StringSegment *head; /* The list of character arrays */
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Specify how many StringSegment's to allocate at a time.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate #define STR_SEG_BLK 20
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*.......................................................................
737c478bd9Sstevel@tonic-gate  * Create a new StringGroup object.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Input:
767c478bd9Sstevel@tonic-gate  *  segment_size    int    The length of each of the large character
777c478bd9Sstevel@tonic-gate  *                         arrays in which multiple strings will be
787c478bd9Sstevel@tonic-gate  *                         stored. This sets the length of longest
797c478bd9Sstevel@tonic-gate  *                         string that can be stored, and for efficiency
807c478bd9Sstevel@tonic-gate  *                         should be at least 10 times as large as
817c478bd9Sstevel@tonic-gate  *                         the average string that will be stored.
827c478bd9Sstevel@tonic-gate  * Output:
837c478bd9Sstevel@tonic-gate  *  return  StringGroup *  The new object, or NULL on error.
847c478bd9Sstevel@tonic-gate  */
_new_StringGroup(int segment_size)857c478bd9Sstevel@tonic-gate StringGroup *_new_StringGroup(int segment_size)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate   StringGroup *sg;    /* The object to be returned */
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Check the arguments.
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate   if(segment_size < 1) {
927c478bd9Sstevel@tonic-gate     errno = EINVAL;
937c478bd9Sstevel@tonic-gate     return NULL;
947c478bd9Sstevel@tonic-gate   };
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Allocate the container.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate   sg = (StringGroup *) malloc(sizeof(StringGroup));
997c478bd9Sstevel@tonic-gate   if(!sg) {
1007c478bd9Sstevel@tonic-gate     errno = ENOMEM;
1017c478bd9Sstevel@tonic-gate     return NULL;
1027c478bd9Sstevel@tonic-gate   };
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
1057c478bd9Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
1067c478bd9Sstevel@tonic-gate  * to _del_StringGroup().
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate   sg->node_mem = NULL;
1097c478bd9Sstevel@tonic-gate   sg->head = NULL;
1107c478bd9Sstevel@tonic-gate   sg->block_size = segment_size;
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Allocate the free list that is used to allocate list nodes.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate   sg->node_mem = _new_FreeList(sizeof(StringSegment), STR_SEG_BLK);
1157c478bd9Sstevel@tonic-gate   if(!sg->node_mem)
1167c478bd9Sstevel@tonic-gate     return _del_StringGroup(sg);
1177c478bd9Sstevel@tonic-gate   return sg;
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*.......................................................................
1217c478bd9Sstevel@tonic-gate  * Delete a StringGroup object.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * Input:
1247c478bd9Sstevel@tonic-gate  *  sg     StringGroup *  The object to be deleted.
1257c478bd9Sstevel@tonic-gate  * Output:
1267c478bd9Sstevel@tonic-gate  *  return StringGroup *  The deleted object (always NULL).
1277c478bd9Sstevel@tonic-gate  */
_del_StringGroup(StringGroup * sg)1287c478bd9Sstevel@tonic-gate StringGroup *_del_StringGroup(StringGroup *sg)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate   if(sg) {
1317c478bd9Sstevel@tonic-gate     StringSegment *node;
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Delete the character arrays.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate     for(node=sg->head; node; node=node->next) {
1367c478bd9Sstevel@tonic-gate       if(node->block)
1377c478bd9Sstevel@tonic-gate 	free(node->block);
1387c478bd9Sstevel@tonic-gate       node->block = NULL;
1397c478bd9Sstevel@tonic-gate     };
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Delete the list nodes that contained the string segments.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate     sg->node_mem = _del_FreeList(sg->node_mem, 1);
1447c478bd9Sstevel@tonic-gate     sg->head = NULL; /* Already deleted by deleting sg->node_mem */
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Delete the container.
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate     free(sg);
1497c478bd9Sstevel@tonic-gate   };
1507c478bd9Sstevel@tonic-gate   return NULL;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*.......................................................................
1547c478bd9Sstevel@tonic-gate  * Make a copy of a string in the specified string group, and return
155*1da57d55SToomas Soome  * a pointer to the copy.
1567c478bd9Sstevel@tonic-gate  *
1577c478bd9Sstevel@tonic-gate  * Input:
1587c478bd9Sstevel@tonic-gate  *  sg      StringGroup *  The group to store the string in.
1597c478bd9Sstevel@tonic-gate  *  string   const char *  The string to be recorded.
1607c478bd9Sstevel@tonic-gate  *  remove_escapes  int    If true, omit backslashes which escape
1617c478bd9Sstevel@tonic-gate  *                         other characters when making the copy.
1627c478bd9Sstevel@tonic-gate  * Output:
1637c478bd9Sstevel@tonic-gate  *  return         char *  The pointer to the copy of the string,
1647c478bd9Sstevel@tonic-gate  *                         or NULL if there was insufficient memory.
1657c478bd9Sstevel@tonic-gate  */
_sg_store_string(StringGroup * sg,const char * string,int remove_escapes)1667c478bd9Sstevel@tonic-gate char *_sg_store_string(StringGroup *sg, const char *string, int remove_escapes)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate   char *copy;           /* The recorded copy of string[] */
1697c478bd9Sstevel@tonic-gate   size_t len;
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Check the arguments.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate   if(!sg || !string)
1747c478bd9Sstevel@tonic-gate     return NULL;
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Get memory for the string.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate   len = strlen(string);
1797c478bd9Sstevel@tonic-gate   copy = _sg_alloc_string(sg, len);
1807c478bd9Sstevel@tonic-gate   if(copy) {
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * If needed, remove backslash escapes while copying the input string
1837c478bd9Sstevel@tonic-gate  * into the cache string.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate     if(remove_escapes) {
1867c478bd9Sstevel@tonic-gate       int escaped = 0;             /* True if the next character should be */
1877c478bd9Sstevel@tonic-gate                                    /*  escaped. */
1887c478bd9Sstevel@tonic-gate       const char *src = string;    /* A pointer into the input string */
1897c478bd9Sstevel@tonic-gate       char *dst = copy;            /* A pointer into the cached copy of the */
1907c478bd9Sstevel@tonic-gate                                    /*  string. */
1917c478bd9Sstevel@tonic-gate       while(*src) {
1927c478bd9Sstevel@tonic-gate 	if(!escaped && *src == '\\') {
1937c478bd9Sstevel@tonic-gate 	  escaped = 1;
1947c478bd9Sstevel@tonic-gate 	  src++;
1957c478bd9Sstevel@tonic-gate 	} else {
1967c478bd9Sstevel@tonic-gate 	  escaped = 0;
1977c478bd9Sstevel@tonic-gate 	  *dst++ = *src++;
1987c478bd9Sstevel@tonic-gate 	};
1997c478bd9Sstevel@tonic-gate       };
2007c478bd9Sstevel@tonic-gate       *dst = '\0';
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * If escapes have already been removed, copy the input string directly
2037c478bd9Sstevel@tonic-gate  * into the cache.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate     } else {
2067c478bd9Sstevel@tonic-gate       strlcpy(copy, string, len + 1);
2077c478bd9Sstevel@tonic-gate     };
2087c478bd9Sstevel@tonic-gate   };
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Return a pointer to the copy of the string (or NULL if the allocation
2117c478bd9Sstevel@tonic-gate  * failed).
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate   return copy;
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*.......................................................................
2177c478bd9Sstevel@tonic-gate  * Allocate memory for a string of a given length.
2187c478bd9Sstevel@tonic-gate  *
2197c478bd9Sstevel@tonic-gate  * Input:
2207c478bd9Sstevel@tonic-gate  *  sg      StringGroup *  The group to store the string in.
2217c478bd9Sstevel@tonic-gate  *  length          int    The required length of the string.
2227c478bd9Sstevel@tonic-gate  * Output:
2237c478bd9Sstevel@tonic-gate  *  return         char *  The pointer to the copy of the string,
2247c478bd9Sstevel@tonic-gate  *                         or NULL if there was insufficient memory.
2257c478bd9Sstevel@tonic-gate  */
_sg_alloc_string(StringGroup * sg,int length)2267c478bd9Sstevel@tonic-gate char *_sg_alloc_string(StringGroup *sg, int length)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate   StringSegment *node;  /* A node of the list of string segments */
2297c478bd9Sstevel@tonic-gate   char *copy;           /* The allocated string */
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * If the string is longer than block_size, then we can't record it.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate   if(length > sg->block_size || length < 0)
2347c478bd9Sstevel@tonic-gate     return NULL;
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * See if there is room to record the string in one of the existing
2377c478bd9Sstevel@tonic-gate  * string segments. Do this by advancing the node pointer until we find
2387c478bd9Sstevel@tonic-gate  * a node with length+1 bytes unused, or we get to the end of the list.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate   for(node=sg->head; node && node->unused <= length; node=node->next)
2417c478bd9Sstevel@tonic-gate     ;
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * If there wasn't room, allocate a new string segment.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate   if(!node) {
2467c478bd9Sstevel@tonic-gate     node = (StringSegment *) _new_FreeListNode(sg->node_mem);
2477c478bd9Sstevel@tonic-gate     if(!node)
2487c478bd9Sstevel@tonic-gate       return NULL;
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Initialize the segment.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate     node->next = NULL;
2537c478bd9Sstevel@tonic-gate     node->block = NULL;
2547c478bd9Sstevel@tonic-gate     node->unused = sg->block_size;
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Attempt to allocate the string segment character array.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate     node->block = (char *) malloc(sg->block_size);
2597c478bd9Sstevel@tonic-gate     if(!node->block)
2607c478bd9Sstevel@tonic-gate       return NULL;
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * Prepend the node to the list.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate     node->next = sg->head;
2657c478bd9Sstevel@tonic-gate     sg->head = node;
2667c478bd9Sstevel@tonic-gate   };
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Get memory for the string.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate   copy = node->block + sg->block_size - node->unused;
2717c478bd9Sstevel@tonic-gate   node->unused -= length + 1;
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Return a pointer to the string memory.
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate   return copy;
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*.......................................................................
2797c478bd9Sstevel@tonic-gate  * Delete all of the strings that are currently stored by a specified
2807c478bd9Sstevel@tonic-gate  * StringGroup object.
2817c478bd9Sstevel@tonic-gate  *
2827c478bd9Sstevel@tonic-gate  * Input:
2837c478bd9Sstevel@tonic-gate  *  sg   StringGroup *   The group of strings to clear.
2847c478bd9Sstevel@tonic-gate  */
_clr_StringGroup(StringGroup * sg)2857c478bd9Sstevel@tonic-gate void _clr_StringGroup(StringGroup *sg)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate   StringSegment *node;   /* A node in the list of string segments */
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Mark all of the string segments as unoccupied.
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate   for(node=sg->head; node; node=node->next)
2927c478bd9Sstevel@tonic-gate     node->unused = sg->block_size;
2937c478bd9Sstevel@tonic-gate   return;
2947c478bd9Sstevel@tonic-gate }
295