/* lkstore.c - Allocated string storage module.

   Copyright (C) 1989-1995 Alan R. Baldwin
   721 Berkeley St., Kent, Ohio 44240

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/*
 * Allocated string storage module.
 *
 * 31-Oct-1997 by John Hartman
 */

#include <stdio.h>
#include <setjmp.h>
#include <string.h>
#include "aslink.h"

/*
 * Allocate space for "str", copy str into new space
 * Return a pointer to the allocated name, or NULL if out of memory
 */
char *StoreString( char *str )
{
   /* To avoid wasting memory headers on small allocations, we
   /  allocate a big chunk and parcel it out as required.
   /  These static variables remember our hunk
   */
   #define STR_STORE_HUNK 2000
   static char *pNextFree = NULL;
   static int  bytesLeft = 0;

   int  length;
   char *pStoredString;

   length = strlen( str ) + 1;  /* what we need, including null */

   if (length > bytesLeft)
   {
      /* no space.  Allocate a new hunk.  We lose the pointer to any
      /  old hunk.  We don't care, as the names are never deleted.
      */
      pNextFree = (char*)new( STR_STORE_HUNK );
      bytesLeft = STR_STORE_HUNK;
   }

   /* Copy the name and terminating null into the name store */
   pStoredString = pNextFree;
   memcpy( pStoredString, str, length );

   pNextFree += length;
   bytesLeft -= length;

   return pStoredString;
}
