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