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