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