Initial revision
[fw/sdcc] / as / mcs51 / lkstore.c
1 /* lkstore.c */
2
3 /* 
4  * Allocated string storage module.
5  *
6  * 31-Oct-1997 by John Hartman
7  */
8
9 #include <stdio.h>
10 #include <setjmp.h>
11 #include <string.h>
12 #include <alloc.h>
13 #include "aslink.h"
14
15 /*
16  * Allocate space for "str", copy str into new space
17  * Return a pointer to the allocated name, or NULL if out of memory
18  */
19 char *StoreString( char *str )
20 {
21    /* To avoid wasting memory headers on small allocations, we
22    /  allocate a big chunk and parcel it out as required.
23    /  These static variables remember our hunk
24    */
25    #define STR_STORE_HUNK 2000
26    static char *pNextFree = NULL;
27    static int  bytesLeft = 0;
28    
29    int  length;
30    char *pStoredString;
31    
32    length = strlen( str ) + 1;  /* what we need, including null */
33
34    if (length > bytesLeft)
35    {
36       /* no space.  Allocate a new hunk.  We lose the pointer to any
37       /  old hunk.  We don't care, as the names are never deleted.
38       */
39       pNextFree = (char*)new( STR_STORE_HUNK );
40       bytesLeft = STR_STORE_HUNK;
41    }
42
43    /* Copy the name and terminating null into the name store */
44    pStoredString = pNextFree;
45    memcpy( pStoredString, str, length );
46
47    pNextFree += length;
48    bytesLeft -= length;
49
50    return pStoredString;
51 }