100bf4279Sespie /* objalloc.c -- routines to allocate memory for objects
2*483f9b85Sbluhm Copyright 1997-2012 Free Software Foundation, Inc.
300bf4279Sespie Written by Ian Lance Taylor, Cygnus Solutions.
400bf4279Sespie
500bf4279Sespie This program is free software; you can redistribute it and/or modify it
600bf4279Sespie under the terms of the GNU General Public License as published by the
700bf4279Sespie Free Software Foundation; either version 2, or (at your option) any
800bf4279Sespie later version.
900bf4279Sespie
1000bf4279Sespie This program is distributed in the hope that it will be useful,
1100bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1200bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1300bf4279Sespie GNU General Public License for more details.
1400bf4279Sespie
1500bf4279Sespie You should have received a copy of the GNU General Public License
1600bf4279Sespie along with this program; if not, write to the Free Software
17150b7e42Smiod Foundation, 51 Franklin Street - Fifth Floor,
18150b7e42Smiod Boston, MA 02110-1301, USA. */
1900bf4279Sespie
20f5dd06f4Sespie #include "config.h"
21150b7e42Smiod #include "ansidecl.h"
22f5dd06f4Sespie
2300bf4279Sespie #include "objalloc.h"
2400bf4279Sespie
2500bf4279Sespie /* Get a definition for NULL. */
2600bf4279Sespie #include <stdio.h>
2700bf4279Sespie
2800bf4279Sespie #if VMS
2900bf4279Sespie #include <stdlib.h>
3000bf4279Sespie #include <unixlib.h>
3100bf4279Sespie #else
3200bf4279Sespie
3300bf4279Sespie /* Get a definition for size_t. */
3400bf4279Sespie #include <stddef.h>
3500bf4279Sespie
36f5dd06f4Sespie #ifdef HAVE_STDLIB_H
37f5dd06f4Sespie #include <stdlib.h>
38f5dd06f4Sespie #else
3900bf4279Sespie /* For systems with larger pointers than ints, this must be declared. */
40150b7e42Smiod extern PTR malloc (size_t);
41150b7e42Smiod extern void free (PTR);
4200bf4279Sespie #endif
4300bf4279Sespie
44f5dd06f4Sespie #endif
45f5dd06f4Sespie
4600bf4279Sespie /* These routines allocate space for an object. Freeing allocated
4700bf4279Sespie space may or may not free all more recently allocated space.
4800bf4279Sespie
4900bf4279Sespie We handle large and small allocation requests differently. If we
5000bf4279Sespie don't have enough space in the current block, and the allocation
5100bf4279Sespie request is for more than 512 bytes, we simply pass it through to
5200bf4279Sespie malloc. */
5300bf4279Sespie
5400bf4279Sespie /* The objalloc structure is defined in objalloc.h. */
5500bf4279Sespie
5600bf4279Sespie /* This structure appears at the start of each chunk. */
5700bf4279Sespie
5800bf4279Sespie struct objalloc_chunk
5900bf4279Sespie {
6000bf4279Sespie /* Next chunk. */
6100bf4279Sespie struct objalloc_chunk *next;
6200bf4279Sespie /* If this chunk contains large objects, this is the value of
6300bf4279Sespie current_ptr when this chunk was allocated. If this chunk
6400bf4279Sespie contains small objects, this is NULL. */
6500bf4279Sespie char *current_ptr;
6600bf4279Sespie };
6700bf4279Sespie
6800bf4279Sespie /* The aligned size of objalloc_chunk. */
6900bf4279Sespie
7000bf4279Sespie #define CHUNK_HEADER_SIZE \
7100bf4279Sespie ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
7200bf4279Sespie &~ (OBJALLOC_ALIGN - 1))
7300bf4279Sespie
7400bf4279Sespie /* We ask for this much memory each time we create a chunk which is to
7500bf4279Sespie hold small objects. */
7600bf4279Sespie
7700bf4279Sespie #define CHUNK_SIZE (4096 - 32)
7800bf4279Sespie
7900bf4279Sespie /* A request for this amount or more is just passed through to malloc. */
8000bf4279Sespie
8100bf4279Sespie #define BIG_REQUEST (512)
8200bf4279Sespie
8300bf4279Sespie /* Create an objalloc structure. */
8400bf4279Sespie
8500bf4279Sespie struct objalloc *
objalloc_create(void)86150b7e42Smiod objalloc_create (void)
8700bf4279Sespie {
8800bf4279Sespie struct objalloc *ret;
8900bf4279Sespie struct objalloc_chunk *chunk;
9000bf4279Sespie
9100bf4279Sespie ret = (struct objalloc *) malloc (sizeof *ret);
9200bf4279Sespie if (ret == NULL)
9300bf4279Sespie return NULL;
9400bf4279Sespie
9500bf4279Sespie ret->chunks = (PTR) malloc (CHUNK_SIZE);
9600bf4279Sespie if (ret->chunks == NULL)
9700bf4279Sespie {
9800bf4279Sespie free (ret);
9900bf4279Sespie return NULL;
10000bf4279Sespie }
10100bf4279Sespie
10200bf4279Sespie chunk = (struct objalloc_chunk *) ret->chunks;
10300bf4279Sespie chunk->next = NULL;
10400bf4279Sespie chunk->current_ptr = NULL;
10500bf4279Sespie
10600bf4279Sespie ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
10700bf4279Sespie ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
10800bf4279Sespie
10900bf4279Sespie return ret;
11000bf4279Sespie }
11100bf4279Sespie
11200bf4279Sespie /* Allocate space from an objalloc structure. */
11300bf4279Sespie
11400bf4279Sespie PTR
_objalloc_alloc(struct objalloc * o,unsigned long original_len)115*483f9b85Sbluhm _objalloc_alloc (struct objalloc *o, unsigned long original_len)
11600bf4279Sespie {
117*483f9b85Sbluhm unsigned long len = original_len;
118*483f9b85Sbluhm
11900bf4279Sespie /* We avoid confusion from zero sized objects by always allocating
12000bf4279Sespie at least 1 byte. */
12100bf4279Sespie if (len == 0)
12200bf4279Sespie len = 1;
12300bf4279Sespie
12400bf4279Sespie len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
12500bf4279Sespie
126*483f9b85Sbluhm /* CVE-2012-3509: Check for overflow in the alignment operation above
127*483f9b85Sbluhm * and then malloc argument below. */
128*483f9b85Sbluhm if (len + CHUNK_HEADER_SIZE < original_len)
129*483f9b85Sbluhm return NULL;
130*483f9b85Sbluhm
13100bf4279Sespie if (len <= o->current_space)
13200bf4279Sespie {
13300bf4279Sespie o->current_ptr += len;
13400bf4279Sespie o->current_space -= len;
13500bf4279Sespie return (PTR) (o->current_ptr - len);
13600bf4279Sespie }
13700bf4279Sespie
13800bf4279Sespie if (len >= BIG_REQUEST)
13900bf4279Sespie {
14000bf4279Sespie char *ret;
14100bf4279Sespie struct objalloc_chunk *chunk;
14200bf4279Sespie
14300bf4279Sespie ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
14400bf4279Sespie if (ret == NULL)
14500bf4279Sespie return NULL;
14600bf4279Sespie
14700bf4279Sespie chunk = (struct objalloc_chunk *) ret;
14800bf4279Sespie chunk->next = (struct objalloc_chunk *) o->chunks;
14900bf4279Sespie chunk->current_ptr = o->current_ptr;
15000bf4279Sespie
15100bf4279Sespie o->chunks = (PTR) chunk;
15200bf4279Sespie
15300bf4279Sespie return (PTR) (ret + CHUNK_HEADER_SIZE);
15400bf4279Sespie }
15500bf4279Sespie else
15600bf4279Sespie {
15700bf4279Sespie struct objalloc_chunk *chunk;
15800bf4279Sespie
15900bf4279Sespie chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
16000bf4279Sespie if (chunk == NULL)
16100bf4279Sespie return NULL;
16200bf4279Sespie chunk->next = (struct objalloc_chunk *) o->chunks;
16300bf4279Sespie chunk->current_ptr = NULL;
16400bf4279Sespie
16500bf4279Sespie o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
16600bf4279Sespie o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
16700bf4279Sespie
16800bf4279Sespie o->chunks = (PTR) chunk;
16900bf4279Sespie
17000bf4279Sespie return objalloc_alloc (o, len);
17100bf4279Sespie }
17200bf4279Sespie }
17300bf4279Sespie
17400bf4279Sespie /* Free an entire objalloc structure. */
17500bf4279Sespie
17600bf4279Sespie void
objalloc_free(struct objalloc * o)177150b7e42Smiod objalloc_free (struct objalloc *o)
17800bf4279Sespie {
17900bf4279Sespie struct objalloc_chunk *l;
18000bf4279Sespie
18100bf4279Sespie l = (struct objalloc_chunk *) o->chunks;
18200bf4279Sespie while (l != NULL)
18300bf4279Sespie {
18400bf4279Sespie struct objalloc_chunk *next;
18500bf4279Sespie
18600bf4279Sespie next = l->next;
18700bf4279Sespie free (l);
18800bf4279Sespie l = next;
18900bf4279Sespie }
19000bf4279Sespie
19100bf4279Sespie free (o);
19200bf4279Sespie }
19300bf4279Sespie
19400bf4279Sespie /* Free a block from an objalloc structure. This also frees all more
19500bf4279Sespie recently allocated blocks. */
19600bf4279Sespie
19700bf4279Sespie void
objalloc_free_block(struct objalloc * o,PTR block)198150b7e42Smiod objalloc_free_block (struct objalloc *o, PTR block)
19900bf4279Sespie {
20000bf4279Sespie struct objalloc_chunk *p, *small;
20100bf4279Sespie char *b = (char *) block;
20200bf4279Sespie
20300bf4279Sespie /* First set P to the chunk which contains the block we are freeing,
20400bf4279Sespie and set Q to the last small object chunk we see before P. */
20500bf4279Sespie small = NULL;
20600bf4279Sespie for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
20700bf4279Sespie {
20800bf4279Sespie if (p->current_ptr == NULL)
20900bf4279Sespie {
21000bf4279Sespie if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
21100bf4279Sespie break;
21200bf4279Sespie small = p;
21300bf4279Sespie }
21400bf4279Sespie else
21500bf4279Sespie {
21600bf4279Sespie if (b == (char *) p + CHUNK_HEADER_SIZE)
21700bf4279Sespie break;
21800bf4279Sespie }
21900bf4279Sespie }
22000bf4279Sespie
22100bf4279Sespie /* If we can't find the chunk, the caller has made a mistake. */
22200bf4279Sespie if (p == NULL)
22300bf4279Sespie abort ();
22400bf4279Sespie
22500bf4279Sespie if (p->current_ptr == NULL)
22600bf4279Sespie {
22700bf4279Sespie struct objalloc_chunk *q;
22800bf4279Sespie struct objalloc_chunk *first;
22900bf4279Sespie
23000bf4279Sespie /* The block is in a chunk containing small objects. We can
23100bf4279Sespie free every chunk through SMALL, because they have certainly
23200bf4279Sespie been allocated more recently. After SMALL, we will not see
23300bf4279Sespie any chunks containing small objects; we can free any big
23400bf4279Sespie chunk if the current_ptr is greater than or equal to B. We
23500bf4279Sespie can then reset the new current_ptr to B. */
23600bf4279Sespie
23700bf4279Sespie first = NULL;
23800bf4279Sespie q = (struct objalloc_chunk *) o->chunks;
23900bf4279Sespie while (q != p)
24000bf4279Sespie {
24100bf4279Sespie struct objalloc_chunk *next;
24200bf4279Sespie
24300bf4279Sespie next = q->next;
24400bf4279Sespie if (small != NULL)
24500bf4279Sespie {
24600bf4279Sespie if (small == q)
24700bf4279Sespie small = NULL;
24800bf4279Sespie free (q);
24900bf4279Sespie }
25000bf4279Sespie else if (q->current_ptr > b)
25100bf4279Sespie free (q);
25200bf4279Sespie else if (first == NULL)
25300bf4279Sespie first = q;
25400bf4279Sespie
25500bf4279Sespie q = next;
25600bf4279Sespie }
25700bf4279Sespie
25800bf4279Sespie if (first == NULL)
25900bf4279Sespie first = p;
26000bf4279Sespie o->chunks = (PTR) first;
26100bf4279Sespie
26200bf4279Sespie /* Now start allocating from this small block again. */
26300bf4279Sespie o->current_ptr = b;
26400bf4279Sespie o->current_space = ((char *) p + CHUNK_SIZE) - b;
26500bf4279Sespie }
26600bf4279Sespie else
26700bf4279Sespie {
26800bf4279Sespie struct objalloc_chunk *q;
26900bf4279Sespie char *current_ptr;
27000bf4279Sespie
27100bf4279Sespie /* This block is in a large chunk by itself. We can free
27200bf4279Sespie everything on the list up to and including this block. We
27300bf4279Sespie then start allocating from the next chunk containing small
27400bf4279Sespie objects, setting current_ptr from the value stored with the
27500bf4279Sespie large chunk we are freeing. */
27600bf4279Sespie
27700bf4279Sespie current_ptr = p->current_ptr;
27800bf4279Sespie p = p->next;
27900bf4279Sespie
28000bf4279Sespie q = (struct objalloc_chunk *) o->chunks;
28100bf4279Sespie while (q != p)
28200bf4279Sespie {
28300bf4279Sespie struct objalloc_chunk *next;
28400bf4279Sespie
28500bf4279Sespie next = q->next;
28600bf4279Sespie free (q);
28700bf4279Sespie q = next;
28800bf4279Sespie }
28900bf4279Sespie
29000bf4279Sespie o->chunks = (PTR) p;
29100bf4279Sespie
29200bf4279Sespie while (p->current_ptr != NULL)
29300bf4279Sespie p = p->next;
29400bf4279Sespie
29500bf4279Sespie o->current_ptr = current_ptr;
29600bf4279Sespie o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
29700bf4279Sespie }
29800bf4279Sespie }
299