2
/********************************************
4
copyright 1991, Michael D. Brennan
6
This is a source file for mawk, an implementation of
7
the AWK programming language.
9
Mawk is distributed without warranty under the terms of
10
the GNU General Public License, version 2, 1991.
11
********************************************/
14
* Revision 1.6 1995/06/06 00:18:35 mike
15
* change mawk_exit(1) to mawk_exit(2)
17
* Revision 1.5 1995/03/08 00:06:26 mike
20
* Revision 1.4 1993/07/14 12:45:15 mike
23
* Revision 1.3 1993/07/07 00:07:54 mike
26
* Revision 1.2 1993/07/03 21:15:35 mike
29
* Revision 1.1.1.1 1993/07/03 18:58:23 mike
32
* Revision 5.4 1993/02/13 21:57:38 mike
35
* Revision 5.3 1993/01/14 13:12:33 mike
36
* casts in front of malloc
38
* Revision 5.1.1.1 1993/02/06 11:12:19 mike
39
* fix bug in reuse of parser table memory
40
* for most users ifdef the mess out
42
* Revision 5.1 1991/12/05 07:56:35 brennan
54
zmalloc() gets mem from malloc() in CHUNKS of 2048 bytes
55
and cuts these blocks into smaller pieces that are multiples
56
of eight bytes. When a piece is returned via zfree(), it goes
57
on a linked linear list indexed by its size. The lists are
60
E.g., if you ask for 22 bytes with p = zmalloc(22), you actually get
61
a piece of size 24. When you free it with zfree(p,22) , it is added
62
to the list at pool[2].
68
/* number of blocks to get from malloc */
70
static void PROTO(out_of_mem, (void)) ;
76
static char out[] = "out of memory" ;
78
if (mawk_state == EXECUTION) rt_error(out) ;
81
/* I don't think this will ever happen */
82
compile_error(out) ; mawk_exit(2) ;
89
char dummy[ZBLOCKSZ] ;
93
/* ZBLOCKS of sizes 1, 2, ... 16
94
which is bytes of sizes 8, 16, ... , 128
95
are stored on the linked linear lists in
96
pool[0], pool[1], ... , pool[15]
99
static ZBLOCK *pool[POOLSZ] ;
101
/* zmalloc() is a macro in front of bmalloc "BLOCK malloc" */
105
register unsigned blocks ;
108
static unsigned amt_avail ;
109
static ZBLOCK *avail ;
113
p = (ZBLOCK *) malloc(blocks << ZSHIFT) ;
114
if (!p) out_of_mem() ;
118
if (p = pool[blocks - 1])
120
pool[blocks - 1] = p->link ;
124
if (blocks > amt_avail)
126
if (amt_avail != 0) /* free avail */
128
avail->link = pool[--amt_avail] ;
129
pool[amt_avail] = avail ;
132
if (!(avail = (ZBLOCK *) malloc(CHUNK * ZBLOCKSZ)))
134
/* if we get here, almost out of memory */
136
p = (ZBLOCK *) malloc(blocks << ZSHIFT) ;
137
if (!p) out_of_mem() ;
140
else amt_avail = CHUNK ;
143
/* get p from the avail pile */
144
p = avail ; avail += blocks ; amt_avail -= blocks ;
151
register unsigned blocks ;
154
if (blocks > POOLSZ) free(p) ;
157
((ZBLOCK *) p)->link = pool[--blocks] ;
158
pool[blocks] = (ZBLOCK *) p ;
163
zrealloc(p, old_size, new_size)
165
unsigned old_size, new_size ;
169
if (new_size > (POOLSZ << ZSHIFT) &&
170
old_size > (POOLSZ << ZSHIFT))
172
if (!(q = realloc(p, new_size))) out_of_mem() ;
176
q = zmalloc(new_size) ;
177
memcpy(q, p, old_size < new_size ? old_size : new_size) ;
186
/* pacifier for Bison , this is really dead code */
191
/* hell just froze over */