xref: /openbsd-src/gnu/usr.bin/binutils/include/objalloc.h (revision d2201f2f89f0be1a0be6f7568000ed297414a06d)
1fddef416Sniklas /* objalloc.h -- routines to allocate memory for objects
2*d2201f2fSdrahn    Copyright 1997, 2001 Free Software Foundation, Inc.
3fddef416Sniklas    Written by Ian Lance Taylor, Cygnus Solutions.
4fddef416Sniklas 
5fddef416Sniklas This program is free software; you can redistribute it and/or modify it
6fddef416Sniklas under the terms of the GNU General Public License as published by the
7fddef416Sniklas Free Software Foundation; either version 2, or (at your option) any
8fddef416Sniklas later version.
9fddef416Sniklas 
10fddef416Sniklas This program is distributed in the hope that it will be useful,
11fddef416Sniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
12fddef416Sniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13fddef416Sniklas GNU General Public License for more details.
14fddef416Sniklas 
15fddef416Sniklas You should have received a copy of the GNU General Public License
16fddef416Sniklas along with this program; if not, write to the Free Software
17f7cc78ecSespie Foundation, 59 Temple Place - Suite 330,
18f7cc78ecSespie Boston, MA 02111-1307, USA.  */
19fddef416Sniklas 
20fddef416Sniklas #ifndef OBJALLOC_H
21fddef416Sniklas #define OBJALLOC_H
22fddef416Sniklas 
23fddef416Sniklas #include "ansidecl.h"
24fddef416Sniklas 
25fddef416Sniklas /* These routines allocate space for an object.  The assumption is
26fddef416Sniklas    that the object will want to allocate space as it goes along, but
27fddef416Sniklas    will never want to free any particular block.  There is a function
28fddef416Sniklas    to free a block, which also frees all more recently allocated
29fddef416Sniklas    blocks.  There is also a function to free all the allocated space.
30fddef416Sniklas 
31fddef416Sniklas    This is essentially a specialization of obstacks.  The main
32fddef416Sniklas    difference is that a block may not be allocated a bit at a time.
33fddef416Sniklas    Another difference is that these routines are always built on top
34fddef416Sniklas    of malloc, and always pass an malloc failure back to the caller,
35fddef416Sniklas    unlike more recent versions of obstacks.  */
36fddef416Sniklas 
37fddef416Sniklas /* This is what an objalloc structure looks like.  Callers should not
38fddef416Sniklas    refer to these fields, nor should they allocate these structure
39fddef416Sniklas    themselves.  Instead, they should only create them via
40fddef416Sniklas    objalloc_init, and only access them via the functions and macros
41fddef416Sniklas    listed below.  The structure is only defined here so that we can
42fddef416Sniklas    access it via macros.  */
43fddef416Sniklas 
44fddef416Sniklas struct objalloc
45fddef416Sniklas {
46fddef416Sniklas   char *current_ptr;
47fddef416Sniklas   unsigned int current_space;
48fddef416Sniklas   PTR chunks;
49fddef416Sniklas };
50fddef416Sniklas 
51fddef416Sniklas /* Work out the required alignment.  */
52fddef416Sniklas 
53fddef416Sniklas struct objalloc_align { char x; double d; };
54fddef416Sniklas 
55fddef416Sniklas #if defined (__STDC__) && __STDC__
56fddef416Sniklas #ifndef offsetof
57fddef416Sniklas #include <stddef.h>
58fddef416Sniklas #endif
59fddef416Sniklas #endif
60*d2201f2fSdrahn #ifndef offsetof
61*d2201f2fSdrahn #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
62*d2201f2fSdrahn #endif
63*d2201f2fSdrahn #define OBJALLOC_ALIGN offsetof (struct objalloc_align, d)
64fddef416Sniklas 
65fddef416Sniklas /* Create an objalloc structure.  Returns NULL if malloc fails.  */
66fddef416Sniklas 
67fddef416Sniklas extern struct objalloc *objalloc_create PARAMS ((void));
68fddef416Sniklas 
69fddef416Sniklas /* Allocate space from an objalloc structure.  Returns NULL if malloc
70fddef416Sniklas    fails.  */
71fddef416Sniklas 
72fddef416Sniklas extern PTR _objalloc_alloc PARAMS ((struct objalloc *, unsigned long));
73fddef416Sniklas 
74fddef416Sniklas /* The macro version of objalloc_alloc.  We only define this if using
75fddef416Sniklas    gcc, because otherwise we would have to evaluate the arguments
76fddef416Sniklas    multiple times, or use a temporary field as obstack.h does.  */
77fddef416Sniklas 
78fddef416Sniklas #if defined (__GNUC__) && defined (__STDC__) && __STDC__
79fddef416Sniklas 
80fddef416Sniklas /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
81fddef416Sniklas    does not implement __extension__.  But that compiler doesn't define
82fddef416Sniklas    __GNUC_MINOR__.  */
83fddef416Sniklas #if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
84fddef416Sniklas #define __extension__
85fddef416Sniklas #endif
86fddef416Sniklas 
87fddef416Sniklas #define objalloc_alloc(o, l)						\
88fddef416Sniklas   __extension__								\
89fddef416Sniklas   ({ struct objalloc *__o = (o);					\
90fddef416Sniklas      unsigned long __len = (l);						\
91fddef416Sniklas      if (__len == 0)							\
92fddef416Sniklas        __len = 1;							\
93fddef416Sniklas      __len = (__len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);	\
94fddef416Sniklas      (__len <= __o->current_space					\
95fddef416Sniklas       ? (__o->current_ptr += __len,					\
96fddef416Sniklas 	 __o->current_space -= __len,					\
97fddef416Sniklas 	 (PTR) (__o->current_ptr - __len))				\
98fddef416Sniklas       : _objalloc_alloc (__o, __len)); })
99fddef416Sniklas 
100fddef416Sniklas #else /* ! __GNUC__ */
101fddef416Sniklas 
102fddef416Sniklas #define objalloc_alloc(o, l) _objalloc_alloc ((o), (l))
103fddef416Sniklas 
104fddef416Sniklas #endif /* ! __GNUC__ */
105fddef416Sniklas 
106fddef416Sniklas /* Free an entire objalloc structure.  */
107fddef416Sniklas 
108fddef416Sniklas extern void objalloc_free PARAMS ((struct objalloc *));
109fddef416Sniklas 
110fddef416Sniklas /* Free a block allocated by objalloc_alloc.  This also frees all more
111fddef416Sniklas    recently allocated blocks.  */
112fddef416Sniklas 
113fddef416Sniklas extern void objalloc_free_block PARAMS ((struct objalloc *, PTR));
114fddef416Sniklas 
115fddef416Sniklas #endif /* OBJALLOC_H */
116