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