xref: /dflybsd-src/contrib/gdb-7/libiberty/objalloc.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* objalloc.c -- routines to allocate memory for objects
2*ef5ccd6cSJohn Marino    Copyright 1997-2012 Free Software Foundation, Inc.
35796c8dcSSimon Schubert    Written by Ian Lance Taylor, Cygnus Solutions.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify it
65796c8dcSSimon Schubert under the terms of the GNU General Public License as published by the
75796c8dcSSimon Schubert Free Software Foundation; either version 2, or (at your option) any
85796c8dcSSimon Schubert later version.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
115796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
125796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
135796c8dcSSimon Schubert GNU General Public License for more details.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
165796c8dcSSimon Schubert along with this program; if not, write to the Free Software
175796c8dcSSimon Schubert Foundation, 51 Franklin Street - Fifth Floor,
185796c8dcSSimon Schubert Boston, MA 02110-1301, USA.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "config.h"
215796c8dcSSimon Schubert #include "ansidecl.h"
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "objalloc.h"
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert /* Get a definition for NULL.  */
265796c8dcSSimon Schubert #include <stdio.h>
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert #if VMS
295796c8dcSSimon Schubert #include <stdlib.h>
305796c8dcSSimon Schubert #include <unixlib.h>
315796c8dcSSimon Schubert #else
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert /* Get a definition for size_t.  */
345796c8dcSSimon Schubert #include <stddef.h>
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert #ifdef HAVE_STDLIB_H
375796c8dcSSimon Schubert #include <stdlib.h>
385796c8dcSSimon Schubert #else
395796c8dcSSimon Schubert /* For systems with larger pointers than ints, this must be declared.  */
405796c8dcSSimon Schubert extern PTR malloc (size_t);
415796c8dcSSimon Schubert extern void free (PTR);
425796c8dcSSimon Schubert #endif
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert #endif
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* These routines allocate space for an object.  Freeing allocated
475796c8dcSSimon Schubert    space may or may not free all more recently allocated space.
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert    We handle large and small allocation requests differently.  If we
505796c8dcSSimon Schubert    don't have enough space in the current block, and the allocation
515796c8dcSSimon Schubert    request is for more than 512 bytes, we simply pass it through to
525796c8dcSSimon Schubert    malloc.  */
535796c8dcSSimon Schubert 
545796c8dcSSimon Schubert /* The objalloc structure is defined in objalloc.h.  */
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert /* This structure appears at the start of each chunk.  */
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert struct objalloc_chunk
595796c8dcSSimon Schubert {
605796c8dcSSimon Schubert   /* Next chunk.  */
615796c8dcSSimon Schubert   struct objalloc_chunk *next;
625796c8dcSSimon Schubert   /* If this chunk contains large objects, this is the value of
635796c8dcSSimon Schubert      current_ptr when this chunk was allocated.  If this chunk
645796c8dcSSimon Schubert      contains small objects, this is NULL.  */
655796c8dcSSimon Schubert   char *current_ptr;
665796c8dcSSimon Schubert };
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert /* The aligned size of objalloc_chunk.  */
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert #define CHUNK_HEADER_SIZE					\
715796c8dcSSimon Schubert   ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1)	\
725796c8dcSSimon Schubert    &~ (OBJALLOC_ALIGN - 1))
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert /* We ask for this much memory each time we create a chunk which is to
755796c8dcSSimon Schubert    hold small objects.  */
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert #define CHUNK_SIZE (4096 - 32)
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert /* A request for this amount or more is just passed through to malloc.  */
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert #define BIG_REQUEST (512)
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert /* Create an objalloc structure.  */
845796c8dcSSimon Schubert 
855796c8dcSSimon Schubert struct objalloc *
objalloc_create(void)865796c8dcSSimon Schubert objalloc_create (void)
875796c8dcSSimon Schubert {
885796c8dcSSimon Schubert   struct objalloc *ret;
895796c8dcSSimon Schubert   struct objalloc_chunk *chunk;
905796c8dcSSimon Schubert 
915796c8dcSSimon Schubert   ret = (struct objalloc *) malloc (sizeof *ret);
925796c8dcSSimon Schubert   if (ret == NULL)
935796c8dcSSimon Schubert     return NULL;
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert   ret->chunks = (PTR) malloc (CHUNK_SIZE);
965796c8dcSSimon Schubert   if (ret->chunks == NULL)
975796c8dcSSimon Schubert     {
985796c8dcSSimon Schubert       free (ret);
995796c8dcSSimon Schubert       return NULL;
1005796c8dcSSimon Schubert     }
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert   chunk = (struct objalloc_chunk *) ret->chunks;
1035796c8dcSSimon Schubert   chunk->next = NULL;
1045796c8dcSSimon Schubert   chunk->current_ptr = NULL;
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert   ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
1075796c8dcSSimon Schubert   ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert   return ret;
1105796c8dcSSimon Schubert }
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert /* Allocate space from an objalloc structure.  */
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert PTR
_objalloc_alloc(struct objalloc * o,unsigned long original_len)115*ef5ccd6cSJohn Marino _objalloc_alloc (struct objalloc *o, unsigned long original_len)
1165796c8dcSSimon Schubert {
117*ef5ccd6cSJohn Marino   unsigned long len = original_len;
118*ef5ccd6cSJohn Marino 
1195796c8dcSSimon Schubert   /* We avoid confusion from zero sized objects by always allocating
1205796c8dcSSimon Schubert      at least 1 byte.  */
1215796c8dcSSimon Schubert   if (len == 0)
1225796c8dcSSimon Schubert     len = 1;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
1255796c8dcSSimon Schubert 
126*ef5ccd6cSJohn Marino   /* Check for overflow in the alignment operation above and the
127*ef5ccd6cSJohn Marino      malloc argument below. */
128*ef5ccd6cSJohn Marino   if (len + CHUNK_HEADER_SIZE < original_len)
129*ef5ccd6cSJohn Marino     return NULL;
130*ef5ccd6cSJohn Marino 
1315796c8dcSSimon Schubert   if (len <= o->current_space)
1325796c8dcSSimon Schubert     {
1335796c8dcSSimon Schubert       o->current_ptr += len;
1345796c8dcSSimon Schubert       o->current_space -= len;
1355796c8dcSSimon Schubert       return (PTR) (o->current_ptr - len);
1365796c8dcSSimon Schubert     }
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   if (len >= BIG_REQUEST)
1395796c8dcSSimon Schubert     {
1405796c8dcSSimon Schubert       char *ret;
1415796c8dcSSimon Schubert       struct objalloc_chunk *chunk;
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert       ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
1445796c8dcSSimon Schubert       if (ret == NULL)
1455796c8dcSSimon Schubert 	return NULL;
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert       chunk = (struct objalloc_chunk *) ret;
1485796c8dcSSimon Schubert       chunk->next = (struct objalloc_chunk *) o->chunks;
1495796c8dcSSimon Schubert       chunk->current_ptr = o->current_ptr;
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert       o->chunks = (PTR) chunk;
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert       return (PTR) (ret + CHUNK_HEADER_SIZE);
1545796c8dcSSimon Schubert     }
1555796c8dcSSimon Schubert   else
1565796c8dcSSimon Schubert     {
1575796c8dcSSimon Schubert       struct objalloc_chunk *chunk;
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert       chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
1605796c8dcSSimon Schubert       if (chunk == NULL)
1615796c8dcSSimon Schubert 	return NULL;
1625796c8dcSSimon Schubert       chunk->next = (struct objalloc_chunk *) o->chunks;
1635796c8dcSSimon Schubert       chunk->current_ptr = NULL;
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert       o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
1665796c8dcSSimon Schubert       o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
1675796c8dcSSimon Schubert 
1685796c8dcSSimon Schubert       o->chunks = (PTR) chunk;
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert       return objalloc_alloc (o, len);
1715796c8dcSSimon Schubert     }
1725796c8dcSSimon Schubert }
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert /* Free an entire objalloc structure.  */
1755796c8dcSSimon Schubert 
1765796c8dcSSimon Schubert void
objalloc_free(struct objalloc * o)1775796c8dcSSimon Schubert objalloc_free (struct objalloc *o)
1785796c8dcSSimon Schubert {
1795796c8dcSSimon Schubert   struct objalloc_chunk *l;
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   l = (struct objalloc_chunk *) o->chunks;
1825796c8dcSSimon Schubert   while (l != NULL)
1835796c8dcSSimon Schubert     {
1845796c8dcSSimon Schubert       struct objalloc_chunk *next;
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert       next = l->next;
1875796c8dcSSimon Schubert       free (l);
1885796c8dcSSimon Schubert       l = next;
1895796c8dcSSimon Schubert     }
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert   free (o);
1925796c8dcSSimon Schubert }
1935796c8dcSSimon Schubert 
1945796c8dcSSimon Schubert /* Free a block from an objalloc structure.  This also frees all more
1955796c8dcSSimon Schubert    recently allocated blocks.  */
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert void
objalloc_free_block(struct objalloc * o,PTR block)1985796c8dcSSimon Schubert objalloc_free_block (struct objalloc *o, PTR block)
1995796c8dcSSimon Schubert {
2005796c8dcSSimon Schubert   struct objalloc_chunk *p, *small;
2015796c8dcSSimon Schubert   char *b = (char *) block;
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert   /* First set P to the chunk which contains the block we are freeing,
2045796c8dcSSimon Schubert      and set Q to the last small object chunk we see before P.  */
2055796c8dcSSimon Schubert   small = NULL;
2065796c8dcSSimon Schubert   for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
2075796c8dcSSimon Schubert     {
2085796c8dcSSimon Schubert       if (p->current_ptr == NULL)
2095796c8dcSSimon Schubert 	{
2105796c8dcSSimon Schubert 	  if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
2115796c8dcSSimon Schubert 	    break;
2125796c8dcSSimon Schubert 	  small = p;
2135796c8dcSSimon Schubert 	}
2145796c8dcSSimon Schubert       else
2155796c8dcSSimon Schubert 	{
2165796c8dcSSimon Schubert 	  if (b == (char *) p + CHUNK_HEADER_SIZE)
2175796c8dcSSimon Schubert 	    break;
2185796c8dcSSimon Schubert 	}
2195796c8dcSSimon Schubert     }
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert   /* If we can't find the chunk, the caller has made a mistake.  */
2225796c8dcSSimon Schubert   if (p == NULL)
2235796c8dcSSimon Schubert     abort ();
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert   if (p->current_ptr == NULL)
2265796c8dcSSimon Schubert     {
2275796c8dcSSimon Schubert       struct objalloc_chunk *q;
2285796c8dcSSimon Schubert       struct objalloc_chunk *first;
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert       /* The block is in a chunk containing small objects.  We can
2315796c8dcSSimon Schubert 	 free every chunk through SMALL, because they have certainly
2325796c8dcSSimon Schubert 	 been allocated more recently.  After SMALL, we will not see
2335796c8dcSSimon Schubert 	 any chunks containing small objects; we can free any big
2345796c8dcSSimon Schubert 	 chunk if the current_ptr is greater than or equal to B.  We
2355796c8dcSSimon Schubert 	 can then reset the new current_ptr to B.  */
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert       first = NULL;
2385796c8dcSSimon Schubert       q = (struct objalloc_chunk *) o->chunks;
2395796c8dcSSimon Schubert       while (q != p)
2405796c8dcSSimon Schubert 	{
2415796c8dcSSimon Schubert 	  struct objalloc_chunk *next;
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert 	  next = q->next;
2445796c8dcSSimon Schubert 	  if (small != NULL)
2455796c8dcSSimon Schubert 	    {
2465796c8dcSSimon Schubert 	      if (small == q)
2475796c8dcSSimon Schubert 		small = NULL;
2485796c8dcSSimon Schubert 	      free (q);
2495796c8dcSSimon Schubert 	    }
2505796c8dcSSimon Schubert 	  else if (q->current_ptr > b)
2515796c8dcSSimon Schubert 	    free (q);
2525796c8dcSSimon Schubert 	  else if (first == NULL)
2535796c8dcSSimon Schubert 	    first = q;
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert 	  q = next;
2565796c8dcSSimon Schubert 	}
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert       if (first == NULL)
2595796c8dcSSimon Schubert 	first = p;
2605796c8dcSSimon Schubert       o->chunks = (PTR) first;
2615796c8dcSSimon Schubert 
2625796c8dcSSimon Schubert       /* Now start allocating from this small block again.  */
2635796c8dcSSimon Schubert       o->current_ptr = b;
2645796c8dcSSimon Schubert       o->current_space = ((char *) p + CHUNK_SIZE) - b;
2655796c8dcSSimon Schubert     }
2665796c8dcSSimon Schubert   else
2675796c8dcSSimon Schubert     {
2685796c8dcSSimon Schubert       struct objalloc_chunk *q;
2695796c8dcSSimon Schubert       char *current_ptr;
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert       /* This block is in a large chunk by itself.  We can free
2725796c8dcSSimon Schubert          everything on the list up to and including this block.  We
2735796c8dcSSimon Schubert          then start allocating from the next chunk containing small
2745796c8dcSSimon Schubert          objects, setting current_ptr from the value stored with the
2755796c8dcSSimon Schubert          large chunk we are freeing.  */
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert       current_ptr = p->current_ptr;
2785796c8dcSSimon Schubert       p = p->next;
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert       q = (struct objalloc_chunk *) o->chunks;
2815796c8dcSSimon Schubert       while (q != p)
2825796c8dcSSimon Schubert 	{
2835796c8dcSSimon Schubert 	  struct objalloc_chunk *next;
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert 	  next = q->next;
2865796c8dcSSimon Schubert 	  free (q);
2875796c8dcSSimon Schubert 	  q = next;
2885796c8dcSSimon Schubert 	}
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert       o->chunks = (PTR) p;
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert       while (p->current_ptr != NULL)
2935796c8dcSSimon Schubert 	p = p->next;
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert       o->current_ptr = current_ptr;
2965796c8dcSSimon Schubert       o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
2975796c8dcSSimon Schubert     }
2985796c8dcSSimon Schubert }
299