xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/objalloc.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* objalloc.c -- routines to allocate memory for objects
2*8feb0f0bSmrg    Copyright (C) 1997-2020 Free Software Foundation, Inc.
31debfc3dSmrg    Written by Ian Lance Taylor, Cygnus Solutions.
41debfc3dSmrg 
51debfc3dSmrg This program is free software; you can redistribute it and/or modify it
61debfc3dSmrg under the terms of the GNU General Public License as published by the
71debfc3dSmrg Free Software Foundation; either version 2, or (at your option) any
81debfc3dSmrg later version.
91debfc3dSmrg 
101debfc3dSmrg This program is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131debfc3dSmrg GNU General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU General Public License
161debfc3dSmrg along with this program; if not, write to the Free Software
171debfc3dSmrg Foundation, 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA.  */
191debfc3dSmrg 
201debfc3dSmrg #include "config.h"
211debfc3dSmrg #include "ansidecl.h"
221debfc3dSmrg 
231debfc3dSmrg #include "objalloc.h"
241debfc3dSmrg 
251debfc3dSmrg /* Get a definition for NULL.  */
261debfc3dSmrg #include <stdio.h>
271debfc3dSmrg 
281debfc3dSmrg #if VMS
291debfc3dSmrg #include <stdlib.h>
301debfc3dSmrg #include <unixlib.h>
311debfc3dSmrg #else
321debfc3dSmrg 
331debfc3dSmrg /* Get a definition for size_t.  */
341debfc3dSmrg #include <stddef.h>
351debfc3dSmrg 
361debfc3dSmrg #ifdef HAVE_STDLIB_H
371debfc3dSmrg #include <stdlib.h>
381debfc3dSmrg #else
391debfc3dSmrg /* For systems with larger pointers than ints, this must be declared.  */
401debfc3dSmrg extern PTR malloc (size_t);
411debfc3dSmrg extern void free (PTR);
421debfc3dSmrg #endif
431debfc3dSmrg 
441debfc3dSmrg #endif
451debfc3dSmrg 
461debfc3dSmrg /* These routines allocate space for an object.  Freeing allocated
471debfc3dSmrg    space may or may not free all more recently allocated space.
481debfc3dSmrg 
491debfc3dSmrg    We handle large and small allocation requests differently.  If we
501debfc3dSmrg    don't have enough space in the current block, and the allocation
511debfc3dSmrg    request is for more than 512 bytes, we simply pass it through to
521debfc3dSmrg    malloc.  */
531debfc3dSmrg 
541debfc3dSmrg /* The objalloc structure is defined in objalloc.h.  */
551debfc3dSmrg 
561debfc3dSmrg /* This structure appears at the start of each chunk.  */
571debfc3dSmrg 
581debfc3dSmrg struct objalloc_chunk
591debfc3dSmrg {
601debfc3dSmrg   /* Next chunk.  */
611debfc3dSmrg   struct objalloc_chunk *next;
621debfc3dSmrg   /* If this chunk contains large objects, this is the value of
631debfc3dSmrg      current_ptr when this chunk was allocated.  If this chunk
641debfc3dSmrg      contains small objects, this is NULL.  */
651debfc3dSmrg   char *current_ptr;
661debfc3dSmrg };
671debfc3dSmrg 
681debfc3dSmrg /* The aligned size of objalloc_chunk.  */
691debfc3dSmrg 
701debfc3dSmrg #define CHUNK_HEADER_SIZE					\
711debfc3dSmrg   ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1)	\
721debfc3dSmrg    &~ (OBJALLOC_ALIGN - 1))
731debfc3dSmrg 
741debfc3dSmrg /* We ask for this much memory each time we create a chunk which is to
751debfc3dSmrg    hold small objects.  */
761debfc3dSmrg 
771debfc3dSmrg #define CHUNK_SIZE (4096 - 32)
781debfc3dSmrg 
791debfc3dSmrg /* A request for this amount or more is just passed through to malloc.  */
801debfc3dSmrg 
811debfc3dSmrg #define BIG_REQUEST (512)
821debfc3dSmrg 
831debfc3dSmrg /* Create an objalloc structure.  */
841debfc3dSmrg 
851debfc3dSmrg struct objalloc *
objalloc_create(void)861debfc3dSmrg objalloc_create (void)
871debfc3dSmrg {
881debfc3dSmrg   struct objalloc *ret;
891debfc3dSmrg   struct objalloc_chunk *chunk;
901debfc3dSmrg 
911debfc3dSmrg   ret = (struct objalloc *) malloc (sizeof *ret);
921debfc3dSmrg   if (ret == NULL)
931debfc3dSmrg     return NULL;
941debfc3dSmrg 
951debfc3dSmrg   ret->chunks = (PTR) malloc (CHUNK_SIZE);
961debfc3dSmrg   if (ret->chunks == NULL)
971debfc3dSmrg     {
981debfc3dSmrg       free (ret);
991debfc3dSmrg       return NULL;
1001debfc3dSmrg     }
1011debfc3dSmrg 
1021debfc3dSmrg   chunk = (struct objalloc_chunk *) ret->chunks;
1031debfc3dSmrg   chunk->next = NULL;
1041debfc3dSmrg   chunk->current_ptr = NULL;
1051debfc3dSmrg 
1061debfc3dSmrg   ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
1071debfc3dSmrg   ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
1081debfc3dSmrg 
1091debfc3dSmrg   return ret;
1101debfc3dSmrg }
1111debfc3dSmrg 
1121debfc3dSmrg /* Allocate space from an objalloc structure.  */
1131debfc3dSmrg 
1141debfc3dSmrg PTR
_objalloc_alloc(struct objalloc * o,unsigned long original_len)1151debfc3dSmrg _objalloc_alloc (struct objalloc *o, unsigned long original_len)
1161debfc3dSmrg {
1171debfc3dSmrg   unsigned long len = original_len;
1181debfc3dSmrg 
1191debfc3dSmrg   /* We avoid confusion from zero sized objects by always allocating
1201debfc3dSmrg      at least 1 byte.  */
1211debfc3dSmrg   if (len == 0)
1221debfc3dSmrg     len = 1;
1231debfc3dSmrg 
1241debfc3dSmrg   len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
1251debfc3dSmrg 
1261debfc3dSmrg   /* Check for overflow in the alignment operation above and the
1271debfc3dSmrg      malloc argument below. */
1281debfc3dSmrg   if (len + CHUNK_HEADER_SIZE < original_len)
1291debfc3dSmrg     return NULL;
1301debfc3dSmrg 
1311debfc3dSmrg   if (len <= o->current_space)
1321debfc3dSmrg     {
1331debfc3dSmrg       o->current_ptr += len;
1341debfc3dSmrg       o->current_space -= len;
1351debfc3dSmrg       return (PTR) (o->current_ptr - len);
1361debfc3dSmrg     }
1371debfc3dSmrg 
1381debfc3dSmrg   if (len >= BIG_REQUEST)
1391debfc3dSmrg     {
1401debfc3dSmrg       char *ret;
1411debfc3dSmrg       struct objalloc_chunk *chunk;
1421debfc3dSmrg 
1431debfc3dSmrg       ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
1441debfc3dSmrg       if (ret == NULL)
1451debfc3dSmrg 	return NULL;
1461debfc3dSmrg 
1471debfc3dSmrg       chunk = (struct objalloc_chunk *) ret;
1481debfc3dSmrg       chunk->next = (struct objalloc_chunk *) o->chunks;
1491debfc3dSmrg       chunk->current_ptr = o->current_ptr;
1501debfc3dSmrg 
1511debfc3dSmrg       o->chunks = (PTR) chunk;
1521debfc3dSmrg 
1531debfc3dSmrg       return (PTR) (ret + CHUNK_HEADER_SIZE);
1541debfc3dSmrg     }
1551debfc3dSmrg   else
1561debfc3dSmrg     {
1571debfc3dSmrg       struct objalloc_chunk *chunk;
1581debfc3dSmrg 
1591debfc3dSmrg       chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
1601debfc3dSmrg       if (chunk == NULL)
1611debfc3dSmrg 	return NULL;
1621debfc3dSmrg       chunk->next = (struct objalloc_chunk *) o->chunks;
1631debfc3dSmrg       chunk->current_ptr = NULL;
1641debfc3dSmrg 
1651debfc3dSmrg       o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
1661debfc3dSmrg       o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
1671debfc3dSmrg 
1681debfc3dSmrg       o->chunks = (PTR) chunk;
1691debfc3dSmrg 
1701debfc3dSmrg       return objalloc_alloc (o, len);
1711debfc3dSmrg     }
1721debfc3dSmrg }
1731debfc3dSmrg 
1741debfc3dSmrg /* Free an entire objalloc structure.  */
1751debfc3dSmrg 
1761debfc3dSmrg void
objalloc_free(struct objalloc * o)1771debfc3dSmrg objalloc_free (struct objalloc *o)
1781debfc3dSmrg {
1791debfc3dSmrg   struct objalloc_chunk *l;
1801debfc3dSmrg 
1811debfc3dSmrg   l = (struct objalloc_chunk *) o->chunks;
1821debfc3dSmrg   while (l != NULL)
1831debfc3dSmrg     {
1841debfc3dSmrg       struct objalloc_chunk *next;
1851debfc3dSmrg 
1861debfc3dSmrg       next = l->next;
1871debfc3dSmrg       free (l);
1881debfc3dSmrg       l = next;
1891debfc3dSmrg     }
1901debfc3dSmrg 
1911debfc3dSmrg   free (o);
1921debfc3dSmrg }
1931debfc3dSmrg 
1941debfc3dSmrg /* Free a block from an objalloc structure.  This also frees all more
1951debfc3dSmrg    recently allocated blocks.  */
1961debfc3dSmrg 
1971debfc3dSmrg void
objalloc_free_block(struct objalloc * o,PTR block)1981debfc3dSmrg objalloc_free_block (struct objalloc *o, PTR block)
1991debfc3dSmrg {
2001debfc3dSmrg   struct objalloc_chunk *p, *small;
2011debfc3dSmrg   char *b = (char *) block;
2021debfc3dSmrg 
2031debfc3dSmrg   /* First set P to the chunk which contains the block we are freeing,
2041debfc3dSmrg      and set Q to the last small object chunk we see before P.  */
2051debfc3dSmrg   small = NULL;
2061debfc3dSmrg   for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
2071debfc3dSmrg     {
2081debfc3dSmrg       if (p->current_ptr == NULL)
2091debfc3dSmrg 	{
2101debfc3dSmrg 	  if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
2111debfc3dSmrg 	    break;
2121debfc3dSmrg 	  small = p;
2131debfc3dSmrg 	}
2141debfc3dSmrg       else
2151debfc3dSmrg 	{
2161debfc3dSmrg 	  if (b == (char *) p + CHUNK_HEADER_SIZE)
2171debfc3dSmrg 	    break;
2181debfc3dSmrg 	}
2191debfc3dSmrg     }
2201debfc3dSmrg 
2211debfc3dSmrg   /* If we can't find the chunk, the caller has made a mistake.  */
2221debfc3dSmrg   if (p == NULL)
2231debfc3dSmrg     abort ();
2241debfc3dSmrg 
2251debfc3dSmrg   if (p->current_ptr == NULL)
2261debfc3dSmrg     {
2271debfc3dSmrg       struct objalloc_chunk *q;
2281debfc3dSmrg       struct objalloc_chunk *first;
2291debfc3dSmrg 
2301debfc3dSmrg       /* The block is in a chunk containing small objects.  We can
2311debfc3dSmrg 	 free every chunk through SMALL, because they have certainly
2321debfc3dSmrg 	 been allocated more recently.  After SMALL, we will not see
2331debfc3dSmrg 	 any chunks containing small objects; we can free any big
2341debfc3dSmrg 	 chunk if the current_ptr is greater than or equal to B.  We
2351debfc3dSmrg 	 can then reset the new current_ptr to B.  */
2361debfc3dSmrg 
2371debfc3dSmrg       first = NULL;
2381debfc3dSmrg       q = (struct objalloc_chunk *) o->chunks;
2391debfc3dSmrg       while (q != p)
2401debfc3dSmrg 	{
2411debfc3dSmrg 	  struct objalloc_chunk *next;
2421debfc3dSmrg 
2431debfc3dSmrg 	  next = q->next;
2441debfc3dSmrg 	  if (small != NULL)
2451debfc3dSmrg 	    {
2461debfc3dSmrg 	      if (small == q)
2471debfc3dSmrg 		small = NULL;
2481debfc3dSmrg 	      free (q);
2491debfc3dSmrg 	    }
2501debfc3dSmrg 	  else if (q->current_ptr > b)
2511debfc3dSmrg 	    free (q);
2521debfc3dSmrg 	  else if (first == NULL)
2531debfc3dSmrg 	    first = q;
2541debfc3dSmrg 
2551debfc3dSmrg 	  q = next;
2561debfc3dSmrg 	}
2571debfc3dSmrg 
2581debfc3dSmrg       if (first == NULL)
2591debfc3dSmrg 	first = p;
2601debfc3dSmrg       o->chunks = (PTR) first;
2611debfc3dSmrg 
2621debfc3dSmrg       /* Now start allocating from this small block again.  */
2631debfc3dSmrg       o->current_ptr = b;
2641debfc3dSmrg       o->current_space = ((char *) p + CHUNK_SIZE) - b;
2651debfc3dSmrg     }
2661debfc3dSmrg   else
2671debfc3dSmrg     {
2681debfc3dSmrg       struct objalloc_chunk *q;
2691debfc3dSmrg       char *current_ptr;
2701debfc3dSmrg 
2711debfc3dSmrg       /* This block is in a large chunk by itself.  We can free
2721debfc3dSmrg          everything on the list up to and including this block.  We
2731debfc3dSmrg          then start allocating from the next chunk containing small
2741debfc3dSmrg          objects, setting current_ptr from the value stored with the
2751debfc3dSmrg          large chunk we are freeing.  */
2761debfc3dSmrg 
2771debfc3dSmrg       current_ptr = p->current_ptr;
2781debfc3dSmrg       p = p->next;
2791debfc3dSmrg 
2801debfc3dSmrg       q = (struct objalloc_chunk *) o->chunks;
2811debfc3dSmrg       while (q != p)
2821debfc3dSmrg 	{
2831debfc3dSmrg 	  struct objalloc_chunk *next;
2841debfc3dSmrg 
2851debfc3dSmrg 	  next = q->next;
2861debfc3dSmrg 	  free (q);
2871debfc3dSmrg 	  q = next;
2881debfc3dSmrg 	}
2891debfc3dSmrg 
2901debfc3dSmrg       o->chunks = (PTR) p;
2911debfc3dSmrg 
2921debfc3dSmrg       while (p->current_ptr != NULL)
2931debfc3dSmrg 	p = p->next;
2941debfc3dSmrg 
2951debfc3dSmrg       o->current_ptr = current_ptr;
2961debfc3dSmrg       o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
2971debfc3dSmrg     }
2981debfc3dSmrg }
299