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