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