063e89c4d88cca7f6345bf14fe817cf4ea620b76
[fw/sdcc] / as / link / 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 "aslink.h"
13
14 /*
15  * Allocate space for "str", copy str into new space
16  * Return a pointer to the allocated name, or NULL if out of memory
17  */
18 char *StoreString( char *str )
19 {
20    /* To avoid wasting memory headers on small allocations, we
21    /  allocate a big chunk and parcel it out as required.
22    /  These static variables remember our hunk
23    */
24    #define STR_STORE_HUNK 2000
25    static char *pNextFree = NULL;
26    static int  bytesLeft = 0;
27    
28    int  length;
29    char *pStoredString;
30    
31    length = strlen( str ) + 1;  /* what we need, including null */
32
33    if (length > bytesLeft)
34    {
35       /* no space.  Allocate a new hunk.  We lose the pointer to any
36       /  old hunk.  We don't care, as the names are never deleted.
37       */
38       pNextFree = (char*)new( STR_STORE_HUNK );
39       bytesLeft = STR_STORE_HUNK;
40    }
41
42    /* Copy the name and terminating null into the name store */
43    pStoredString = pNextFree;
44    memcpy( pStoredString, str, length );
45
46    pNextFree += length;
47    bytesLeft -= length;
48
49    return pStoredString;
50 }