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