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