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