1*fae548d3Szrj /* objalloc.c -- routines to allocate memory for objects
2*fae548d3Szrj Copyright (C) 1997-2020 Free Software Foundation, Inc.
3*fae548d3Szrj Written by Ian Lance Taylor, Cygnus Solutions.
4*fae548d3Szrj
5*fae548d3Szrj This program is free software; you can redistribute it and/or modify it
6*fae548d3Szrj under the terms of the GNU General Public License as published by the
7*fae548d3Szrj Free Software Foundation; either version 2, or (at your option) any
8*fae548d3Szrj later version.
9*fae548d3Szrj
10*fae548d3Szrj This program is distributed in the hope that it will be useful,
11*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*fae548d3Szrj GNU General Public License for more details.
14*fae548d3Szrj
15*fae548d3Szrj You should have received a copy of the GNU General Public License
16*fae548d3Szrj along with this program; if not, write to the Free Software
17*fae548d3Szrj Foundation, 51 Franklin Street - Fifth Floor,
18*fae548d3Szrj Boston, MA 02110-1301, USA. */
19*fae548d3Szrj
20*fae548d3Szrj #include "config.h"
21*fae548d3Szrj #include "ansidecl.h"
22*fae548d3Szrj
23*fae548d3Szrj #include "objalloc.h"
24*fae548d3Szrj
25*fae548d3Szrj /* Get a definition for NULL. */
26*fae548d3Szrj #include <stdio.h>
27*fae548d3Szrj
28*fae548d3Szrj #if VMS
29*fae548d3Szrj #include <stdlib.h>
30*fae548d3Szrj #include <unixlib.h>
31*fae548d3Szrj #else
32*fae548d3Szrj
33*fae548d3Szrj /* Get a definition for size_t. */
34*fae548d3Szrj #include <stddef.h>
35*fae548d3Szrj
36*fae548d3Szrj #ifdef HAVE_STDLIB_H
37*fae548d3Szrj #include <stdlib.h>
38*fae548d3Szrj #else
39*fae548d3Szrj /* For systems with larger pointers than ints, this must be declared. */
40*fae548d3Szrj extern PTR malloc (size_t);
41*fae548d3Szrj extern void free (PTR);
42*fae548d3Szrj #endif
43*fae548d3Szrj
44*fae548d3Szrj #endif
45*fae548d3Szrj
46*fae548d3Szrj /* These routines allocate space for an object. Freeing allocated
47*fae548d3Szrj space may or may not free all more recently allocated space.
48*fae548d3Szrj
49*fae548d3Szrj We handle large and small allocation requests differently. If we
50*fae548d3Szrj don't have enough space in the current block, and the allocation
51*fae548d3Szrj request is for more than 512 bytes, we simply pass it through to
52*fae548d3Szrj malloc. */
53*fae548d3Szrj
54*fae548d3Szrj /* The objalloc structure is defined in objalloc.h. */
55*fae548d3Szrj
56*fae548d3Szrj /* This structure appears at the start of each chunk. */
57*fae548d3Szrj
58*fae548d3Szrj struct objalloc_chunk
59*fae548d3Szrj {
60*fae548d3Szrj /* Next chunk. */
61*fae548d3Szrj struct objalloc_chunk *next;
62*fae548d3Szrj /* If this chunk contains large objects, this is the value of
63*fae548d3Szrj current_ptr when this chunk was allocated. If this chunk
64*fae548d3Szrj contains small objects, this is NULL. */
65*fae548d3Szrj char *current_ptr;
66*fae548d3Szrj };
67*fae548d3Szrj
68*fae548d3Szrj /* The aligned size of objalloc_chunk. */
69*fae548d3Szrj
70*fae548d3Szrj #define CHUNK_HEADER_SIZE \
71*fae548d3Szrj ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
72*fae548d3Szrj &~ (OBJALLOC_ALIGN - 1))
73*fae548d3Szrj
74*fae548d3Szrj /* We ask for this much memory each time we create a chunk which is to
75*fae548d3Szrj hold small objects. */
76*fae548d3Szrj
77*fae548d3Szrj #define CHUNK_SIZE (4096 - 32)
78*fae548d3Szrj
79*fae548d3Szrj /* A request for this amount or more is just passed through to malloc. */
80*fae548d3Szrj
81*fae548d3Szrj #define BIG_REQUEST (512)
82*fae548d3Szrj
83*fae548d3Szrj /* Create an objalloc structure. */
84*fae548d3Szrj
85*fae548d3Szrj struct objalloc *
objalloc_create(void)86*fae548d3Szrj objalloc_create (void)
87*fae548d3Szrj {
88*fae548d3Szrj struct objalloc *ret;
89*fae548d3Szrj struct objalloc_chunk *chunk;
90*fae548d3Szrj
91*fae548d3Szrj ret = (struct objalloc *) malloc (sizeof *ret);
92*fae548d3Szrj if (ret == NULL)
93*fae548d3Szrj return NULL;
94*fae548d3Szrj
95*fae548d3Szrj ret->chunks = (PTR) malloc (CHUNK_SIZE);
96*fae548d3Szrj if (ret->chunks == NULL)
97*fae548d3Szrj {
98*fae548d3Szrj free (ret);
99*fae548d3Szrj return NULL;
100*fae548d3Szrj }
101*fae548d3Szrj
102*fae548d3Szrj chunk = (struct objalloc_chunk *) ret->chunks;
103*fae548d3Szrj chunk->next = NULL;
104*fae548d3Szrj chunk->current_ptr = NULL;
105*fae548d3Szrj
106*fae548d3Szrj ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
107*fae548d3Szrj ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
108*fae548d3Szrj
109*fae548d3Szrj return ret;
110*fae548d3Szrj }
111*fae548d3Szrj
112*fae548d3Szrj /* Allocate space from an objalloc structure. */
113*fae548d3Szrj
114*fae548d3Szrj PTR
_objalloc_alloc(struct objalloc * o,unsigned long original_len)115*fae548d3Szrj _objalloc_alloc (struct objalloc *o, unsigned long original_len)
116*fae548d3Szrj {
117*fae548d3Szrj unsigned long len = original_len;
118*fae548d3Szrj
119*fae548d3Szrj /* We avoid confusion from zero sized objects by always allocating
120*fae548d3Szrj at least 1 byte. */
121*fae548d3Szrj if (len == 0)
122*fae548d3Szrj len = 1;
123*fae548d3Szrj
124*fae548d3Szrj len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
125*fae548d3Szrj
126*fae548d3Szrj /* Check for overflow in the alignment operation above and the
127*fae548d3Szrj malloc argument below. */
128*fae548d3Szrj if (len + CHUNK_HEADER_SIZE < original_len)
129*fae548d3Szrj return NULL;
130*fae548d3Szrj
131*fae548d3Szrj if (len <= o->current_space)
132*fae548d3Szrj {
133*fae548d3Szrj o->current_ptr += len;
134*fae548d3Szrj o->current_space -= len;
135*fae548d3Szrj return (PTR) (o->current_ptr - len);
136*fae548d3Szrj }
137*fae548d3Szrj
138*fae548d3Szrj if (len >= BIG_REQUEST)
139*fae548d3Szrj {
140*fae548d3Szrj char *ret;
141*fae548d3Szrj struct objalloc_chunk *chunk;
142*fae548d3Szrj
143*fae548d3Szrj ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
144*fae548d3Szrj if (ret == NULL)
145*fae548d3Szrj return NULL;
146*fae548d3Szrj
147*fae548d3Szrj chunk = (struct objalloc_chunk *) ret;
148*fae548d3Szrj chunk->next = (struct objalloc_chunk *) o->chunks;
149*fae548d3Szrj chunk->current_ptr = o->current_ptr;
150*fae548d3Szrj
151*fae548d3Szrj o->chunks = (PTR) chunk;
152*fae548d3Szrj
153*fae548d3Szrj return (PTR) (ret + CHUNK_HEADER_SIZE);
154*fae548d3Szrj }
155*fae548d3Szrj else
156*fae548d3Szrj {
157*fae548d3Szrj struct objalloc_chunk *chunk;
158*fae548d3Szrj
159*fae548d3Szrj chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
160*fae548d3Szrj if (chunk == NULL)
161*fae548d3Szrj return NULL;
162*fae548d3Szrj chunk->next = (struct objalloc_chunk *) o->chunks;
163*fae548d3Szrj chunk->current_ptr = NULL;
164*fae548d3Szrj
165*fae548d3Szrj o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
166*fae548d3Szrj o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
167*fae548d3Szrj
168*fae548d3Szrj o->chunks = (PTR) chunk;
169*fae548d3Szrj
170*fae548d3Szrj return objalloc_alloc (o, len);
171*fae548d3Szrj }
172*fae548d3Szrj }
173*fae548d3Szrj
174*fae548d3Szrj /* Free an entire objalloc structure. */
175*fae548d3Szrj
176*fae548d3Szrj void
objalloc_free(struct objalloc * o)177*fae548d3Szrj objalloc_free (struct objalloc *o)
178*fae548d3Szrj {
179*fae548d3Szrj struct objalloc_chunk *l;
180*fae548d3Szrj
181*fae548d3Szrj l = (struct objalloc_chunk *) o->chunks;
182*fae548d3Szrj while (l != NULL)
183*fae548d3Szrj {
184*fae548d3Szrj struct objalloc_chunk *next;
185*fae548d3Szrj
186*fae548d3Szrj next = l->next;
187*fae548d3Szrj free (l);
188*fae548d3Szrj l = next;
189*fae548d3Szrj }
190*fae548d3Szrj
191*fae548d3Szrj free (o);
192*fae548d3Szrj }
193*fae548d3Szrj
194*fae548d3Szrj /* Free a block from an objalloc structure. This also frees all more
195*fae548d3Szrj recently allocated blocks. */
196*fae548d3Szrj
197*fae548d3Szrj void
objalloc_free_block(struct objalloc * o,PTR block)198*fae548d3Szrj objalloc_free_block (struct objalloc *o, PTR block)
199*fae548d3Szrj {
200*fae548d3Szrj struct objalloc_chunk *p, *small;
201*fae548d3Szrj char *b = (char *) block;
202*fae548d3Szrj
203*fae548d3Szrj /* First set P to the chunk which contains the block we are freeing,
204*fae548d3Szrj and set Q to the last small object chunk we see before P. */
205*fae548d3Szrj small = NULL;
206*fae548d3Szrj for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
207*fae548d3Szrj {
208*fae548d3Szrj if (p->current_ptr == NULL)
209*fae548d3Szrj {
210*fae548d3Szrj if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
211*fae548d3Szrj break;
212*fae548d3Szrj small = p;
213*fae548d3Szrj }
214*fae548d3Szrj else
215*fae548d3Szrj {
216*fae548d3Szrj if (b == (char *) p + CHUNK_HEADER_SIZE)
217*fae548d3Szrj break;
218*fae548d3Szrj }
219*fae548d3Szrj }
220*fae548d3Szrj
221*fae548d3Szrj /* If we can't find the chunk, the caller has made a mistake. */
222*fae548d3Szrj if (p == NULL)
223*fae548d3Szrj abort ();
224*fae548d3Szrj
225*fae548d3Szrj if (p->current_ptr == NULL)
226*fae548d3Szrj {
227*fae548d3Szrj struct objalloc_chunk *q;
228*fae548d3Szrj struct objalloc_chunk *first;
229*fae548d3Szrj
230*fae548d3Szrj /* The block is in a chunk containing small objects. We can
231*fae548d3Szrj free every chunk through SMALL, because they have certainly
232*fae548d3Szrj been allocated more recently. After SMALL, we will not see
233*fae548d3Szrj any chunks containing small objects; we can free any big
234*fae548d3Szrj chunk if the current_ptr is greater than or equal to B. We
235*fae548d3Szrj can then reset the new current_ptr to B. */
236*fae548d3Szrj
237*fae548d3Szrj first = NULL;
238*fae548d3Szrj q = (struct objalloc_chunk *) o->chunks;
239*fae548d3Szrj while (q != p)
240*fae548d3Szrj {
241*fae548d3Szrj struct objalloc_chunk *next;
242*fae548d3Szrj
243*fae548d3Szrj next = q->next;
244*fae548d3Szrj if (small != NULL)
245*fae548d3Szrj {
246*fae548d3Szrj if (small == q)
247*fae548d3Szrj small = NULL;
248*fae548d3Szrj free (q);
249*fae548d3Szrj }
250*fae548d3Szrj else if (q->current_ptr > b)
251*fae548d3Szrj free (q);
252*fae548d3Szrj else if (first == NULL)
253*fae548d3Szrj first = q;
254*fae548d3Szrj
255*fae548d3Szrj q = next;
256*fae548d3Szrj }
257*fae548d3Szrj
258*fae548d3Szrj if (first == NULL)
259*fae548d3Szrj first = p;
260*fae548d3Szrj o->chunks = (PTR) first;
261*fae548d3Szrj
262*fae548d3Szrj /* Now start allocating from this small block again. */
263*fae548d3Szrj o->current_ptr = b;
264*fae548d3Szrj o->current_space = ((char *) p + CHUNK_SIZE) - b;
265*fae548d3Szrj }
266*fae548d3Szrj else
267*fae548d3Szrj {
268*fae548d3Szrj struct objalloc_chunk *q;
269*fae548d3Szrj char *current_ptr;
270*fae548d3Szrj
271*fae548d3Szrj /* This block is in a large chunk by itself. We can free
272*fae548d3Szrj everything on the list up to and including this block. We
273*fae548d3Szrj then start allocating from the next chunk containing small
274*fae548d3Szrj objects, setting current_ptr from the value stored with the
275*fae548d3Szrj large chunk we are freeing. */
276*fae548d3Szrj
277*fae548d3Szrj current_ptr = p->current_ptr;
278*fae548d3Szrj p = p->next;
279*fae548d3Szrj
280*fae548d3Szrj q = (struct objalloc_chunk *) o->chunks;
281*fae548d3Szrj while (q != p)
282*fae548d3Szrj {
283*fae548d3Szrj struct objalloc_chunk *next;
284*fae548d3Szrj
285*fae548d3Szrj next = q->next;
286*fae548d3Szrj free (q);
287*fae548d3Szrj q = next;
288*fae548d3Szrj }
289*fae548d3Szrj
290*fae548d3Szrj o->chunks = (PTR) p;
291*fae548d3Szrj
292*fae548d3Szrj while (p->current_ptr != NULL)
293*fae548d3Szrj p = p->next;
294*fae548d3Szrj
295*fae548d3Szrj o->current_ptr = current_ptr;
296*fae548d3Szrj o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
297*fae548d3Szrj }
298*fae548d3Szrj }
299