-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmalloc.c
176 lines (146 loc) · 3.57 KB
/
zmalloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/********************************************
zmalloc.c
copyright 1991,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
#include "mawk.h"
#include "zmalloc.h"
static void
out_of_mem( void )
{
const char * out = "out of memory";
if ( mawk_state == EXECUTION )
rt_error( out );
else {
/* I don't think this will ever happen */
compile_error( out );
mawk_exit( 2 );
}
}
void *
emalloc( size_t sz )
{
void * ret = malloc( sz );
if ( ret == 0 )
out_of_mem();
return ret;
}
void *
erealloc( void * p, size_t sz )
{
void * ret = realloc( p, sz );
if ( ret == 0 )
out_of_mem();
return ret;
}
/* if we are valgrinding or purifying */
#ifdef MEM_CHECK
void *
zmalloc( size_t sz )
{
return emalloc( sz );
}
void *
zrealloc( void * p, size_t old, size_t new )
{
return erealloc( p, new );
}
void
zfree( void * p, size_t sz )
{
free( p );
}
#else /* usual case */
/*
zmalloc() gets mem from emalloc() in chunks of ZSIZE * AVAIL_SZ
and cuts these blocks into smaller pieces that are multiples ZSIZE.
When a piece is returned via zfree(), it goes
on a linked linear list indexed by its size. The lists are
an array, pool[].
*/
/* block sizes are set by this #define */
#define ZSZ ( 4 * sizeof( long ) )
typedef union zblock {
union zblock * link;
double align;
char filler[ZSZ];
} ZBlock;
#define ZSIZE sizeof( ZBlock )
#define bytes_to_blocks( b ) ( ( ( b ) + ZSIZE - 1 ) / ZSIZE )
/* memory from emalloc goes here to be partitioned into
smaller pieces that end up in pool[]
*/
static ZBlock * avail;
static size_t amt_avail;
#define AVAIL_SZ 1024 /* number of ZBlocks to get from emalloc */
static void
fill_avail( void )
{
avail = (ZBlock *)emalloc( ZSIZE * AVAIL_SZ );
amt_avail = AVAIL_SZ;
}
#define POOL_SZ 16
static ZBlock * pool[POOL_SZ];
/* size of biggest block in pool[] */
#define zmalloc_limit ( 16 * ZSIZE )
void *
zmalloc( size_t sz )
{
if ( sz > zmalloc_limit ) {
return emalloc( sz );
}
{
size_t blks = bytes_to_blocks( sz );
ZBlock * p = pool[blks - 1];
if ( p ) {
/* get mem from pool */
pool[blks - 1] = p->link;
return p;
}
if ( blks > amt_avail ) {
if ( amt_avail > 0 ) {
avail->link = pool[amt_avail - 1];
pool[amt_avail - 1] = avail;
}
fill_avail();
}
/* cut a piece off the avail block */
p = avail;
avail += blks;
amt_avail -= blks;
return p;
}
}
void
zfree( void * p, size_t sz )
{
if ( sz > zmalloc_limit ) {
free( p );
}
else {
/* put p in pool[] */
size_t blks = bytes_to_blocks( sz );
ZBlock * zp = (ZBlock *)p;
zp->link = pool[blks - 1];
pool[blks - 1] = zp;
}
}
void *
zrealloc( void * p, size_t old_size, size_t new_size )
{
if ( new_size > zmalloc_limit && old_size > zmalloc_limit ) {
return erealloc( p, new_size );
}
else {
void * ret = zmalloc( new_size );
memcpy( ret, p, old_size < new_size ? old_size : new_size );
zfree( p, old_size );
return ret;
}
}
#endif