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