1 /* objalloc.c -- routines to allocate memory for objects
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Solutions.
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
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.
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. */
25 /* Get a definition for NULL. */
33 /* Get a definition for size_t. */
39 /* For systems with larger pointers than ints, this must be declared. */
40 extern PTR malloc (size_t);
41 extern void free (PTR);
46 /* These routines allocate space for an object. Freeing allocated
47 space may or may not free all more recently allocated space.
49 We handle large and small allocation requests differently. If we
50 don't have enough space in the current block, and the allocation
51 request is for more than 512 bytes, we simply pass it through to
54 /* The objalloc structure is defined in objalloc.h. */
56 /* This structure appears at the start of each chunk. */
61 struct objalloc_chunk *next;
62 /* If this chunk contains large objects, this is the value of
63 current_ptr when this chunk was allocated. If this chunk
64 contains small objects, this is NULL. */
68 /* The aligned size of objalloc_chunk. */
70 #define CHUNK_HEADER_SIZE \
71 ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
72 &~ (OBJALLOC_ALIGN - 1))
74 /* We ask for this much memory each time we create a chunk which is to
75 hold small objects. */
77 #define CHUNK_SIZE (4096 - 32)
79 /* A request for this amount or more is just passed through to malloc. */
81 #define BIG_REQUEST (512)
83 /* Create an objalloc structure. */
86 objalloc_create (void)
89 struct objalloc_chunk *chunk;
91 ret = (struct objalloc *) malloc (sizeof *ret);
95 ret->chunks = (PTR) malloc (CHUNK_SIZE);
96 if (ret->chunks == NULL)
102 chunk = (struct objalloc_chunk *) ret->chunks;
104 chunk->current_ptr = NULL;
106 ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
107 ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
112 /* Allocate space from an objalloc structure. */
115 _objalloc_alloc (struct objalloc *o, unsigned long original_len)
117 unsigned long len = original_len;
119 /* We avoid confusion from zero sized objects by always allocating
124 len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
126 /* Check for overflow in the alignment operation above and the
127 malloc argument below. */
128 if (len + CHUNK_HEADER_SIZE < original_len)
131 if (len <= o->current_space)
133 o->current_ptr += len;
134 o->current_space -= len;
135 return (PTR) (o->current_ptr - len);
138 if (len >= BIG_REQUEST)
141 struct objalloc_chunk *chunk;
143 ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
147 chunk = (struct objalloc_chunk *) ret;
148 chunk->next = (struct objalloc_chunk *) o->chunks;
149 chunk->current_ptr = o->current_ptr;
151 o->chunks = (PTR) chunk;
153 return (PTR) (ret + CHUNK_HEADER_SIZE);
157 struct objalloc_chunk *chunk;
159 chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
162 chunk->next = (struct objalloc_chunk *) o->chunks;
163 chunk->current_ptr = NULL;
165 o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
166 o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
168 o->chunks = (PTR) chunk;
170 return objalloc_alloc (o, len);
174 /* Free an entire objalloc structure. */
177 objalloc_free (struct objalloc *o)
179 struct objalloc_chunk *l;
181 l = (struct objalloc_chunk *) o->chunks;
184 struct objalloc_chunk *next;
194 /* Free a block from an objalloc structure. This also frees all more
195 recently allocated blocks. */
198 objalloc_free_block (struct objalloc *o, PTR block)
200 struct objalloc_chunk *p, *small;
201 char *b = (char *) block;
203 /* First set P to the chunk which contains the block we are freeing,
204 and set Q to the last small object chunk we see before P. */
206 for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
208 if (p->current_ptr == NULL)
210 if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
216 if (b == (char *) p + CHUNK_HEADER_SIZE)
221 /* If we can't find the chunk, the caller has made a mistake. */
225 if (p->current_ptr == NULL)
227 struct objalloc_chunk *q;
228 struct objalloc_chunk *first;
230 /* The block is in a chunk containing small objects. We can
231 free every chunk through SMALL, because they have certainly
232 been allocated more recently. After SMALL, we will not see
233 any chunks containing small objects; we can free any big
234 chunk if the current_ptr is greater than or equal to B. We
235 can then reset the new current_ptr to B. */
238 q = (struct objalloc_chunk *) o->chunks;
241 struct objalloc_chunk *next;
250 else if (q->current_ptr > b)
252 else if (first == NULL)
260 o->chunks = (PTR) first;
262 /* Now start allocating from this small block again. */
264 o->current_space = ((char *) p + CHUNK_SIZE) - b;
268 struct objalloc_chunk *q;
271 /* This block is in a large chunk by itself. We can free
272 everything on the list up to and including this block. We
273 then start allocating from the next chunk containing small
274 objects, setting current_ptr from the value stored with the
275 large chunk we are freeing. */
277 current_ptr = p->current_ptr;
280 q = (struct objalloc_chunk *) o->chunks;
283 struct objalloc_chunk *next;
292 while (p->current_ptr != NULL)
295 o->current_ptr = current_ptr;
296 o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;