* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / link / lkstore.c
1 /* lkstore.c - Allocated string storage module.
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 /*
20  * Allocated string storage module.
21  *
22  * 31-Oct-1997 by John Hartman
23  */
24
25 #include <stdio.h>
26 #include <setjmp.h>
27 #include <string.h>
28 #include "aslink.h"
29
30 /*
31  * Allocate space for "str", copy str into new space
32  * Return a pointer to the allocated name, or NULL if out of memory
33  */
34 char *StoreString( char *str )
35 {
36    /* To avoid wasting memory headers on small allocations, we
37    /  allocate a big chunk and parcel it out as required.
38    /  These static variables remember our hunk
39    */
40    #define STR_STORE_HUNK 2000
41    static char *pNextFree = NULL;
42    static int  bytesLeft = 0;
43
44    int  length;
45    char *pStoredString;
46
47    length = strlen( str ) + 1;  /* what we need, including null */
48
49    if (length > bytesLeft)
50    {
51       /* no space.  Allocate a new hunk.  We lose the pointer to any
52       /  old hunk.  We don't care, as the names are never deleted.
53       */
54       pNextFree = (char*)new( STR_STORE_HUNK );
55       bytesLeft = STR_STORE_HUNK;
56    }
57
58    /* Copy the name and terminating null into the name store */
59    pStoredString = pNextFree;
60    memcpy( pStoredString, str, length );
61
62    pNextFree += length;
63    bytesLeft -= length;
64
65    return pStoredString;
66 }