152cb4975SJohn Polstra /* objalloc.h -- routines to allocate memory for objects 2*dbbf32ddSDavid E. O'Brien Copyright 1997, 2001 Free Software Foundation, Inc. 352cb4975SJohn Polstra Written by Ian Lance Taylor, Cygnus Solutions. 452cb4975SJohn Polstra 552cb4975SJohn Polstra This program is free software; you can redistribute it and/or modify it 652cb4975SJohn Polstra under the terms of the GNU General Public License as published by the 752cb4975SJohn Polstra Free Software Foundation; either version 2, or (at your option) any 852cb4975SJohn Polstra later version. 952cb4975SJohn Polstra 1052cb4975SJohn Polstra This program is distributed in the hope that it will be useful, 1152cb4975SJohn Polstra but WITHOUT ANY WARRANTY; without even the implied warranty of 1252cb4975SJohn Polstra MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1352cb4975SJohn Polstra GNU General Public License for more details. 1452cb4975SJohn Polstra 1552cb4975SJohn Polstra You should have received a copy of the GNU General Public License 1652cb4975SJohn Polstra along with this program; if not, write to the Free Software 1778ce21c1SDavid E. O'Brien Foundation, 51 Franklin Street - Fifth Floor, 1878ce21c1SDavid E. O'Brien Boston, MA 02110-1301, USA. */ 1952cb4975SJohn Polstra 2052cb4975SJohn Polstra #ifndef OBJALLOC_H 2152cb4975SJohn Polstra #define OBJALLOC_H 2252cb4975SJohn Polstra 2352cb4975SJohn Polstra #include "ansidecl.h" 2452cb4975SJohn Polstra 2552cb4975SJohn Polstra /* These routines allocate space for an object. The assumption is 2652cb4975SJohn Polstra that the object will want to allocate space as it goes along, but 2752cb4975SJohn Polstra will never want to free any particular block. There is a function 2852cb4975SJohn Polstra to free a block, which also frees all more recently allocated 2952cb4975SJohn Polstra blocks. There is also a function to free all the allocated space. 3052cb4975SJohn Polstra 3152cb4975SJohn Polstra This is essentially a specialization of obstacks. The main 3252cb4975SJohn Polstra difference is that a block may not be allocated a bit at a time. 3352cb4975SJohn Polstra Another difference is that these routines are always built on top 3452cb4975SJohn Polstra of malloc, and always pass an malloc failure back to the caller, 3552cb4975SJohn Polstra unlike more recent versions of obstacks. */ 3652cb4975SJohn Polstra 3752cb4975SJohn Polstra /* This is what an objalloc structure looks like. Callers should not 3852cb4975SJohn Polstra refer to these fields, nor should they allocate these structure 3952cb4975SJohn Polstra themselves. Instead, they should only create them via 4052cb4975SJohn Polstra objalloc_init, and only access them via the functions and macros 4152cb4975SJohn Polstra listed below. The structure is only defined here so that we can 4252cb4975SJohn Polstra access it via macros. */ 4352cb4975SJohn Polstra 4452cb4975SJohn Polstra struct objalloc 4552cb4975SJohn Polstra { 4652cb4975SJohn Polstra char *current_ptr; 4752cb4975SJohn Polstra unsigned int current_space; 4852cb4975SJohn Polstra void *chunks; 4952cb4975SJohn Polstra }; 5052cb4975SJohn Polstra 5152cb4975SJohn Polstra /* Work out the required alignment. */ 5252cb4975SJohn Polstra 5352cb4975SJohn Polstra struct objalloc_align { char x; double d; }; 5452cb4975SJohn Polstra 5552cb4975SJohn Polstra #if defined (__STDC__) && __STDC__ 5652cb4975SJohn Polstra #ifndef offsetof 5752cb4975SJohn Polstra #include <stddef.h> 5852cb4975SJohn Polstra #endif 5952cb4975SJohn Polstra #endif 60*dbbf32ddSDavid E. O'Brien #ifndef offsetof 61*dbbf32ddSDavid E. O'Brien #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) 62*dbbf32ddSDavid E. O'Brien #endif 63*dbbf32ddSDavid E. O'Brien #define OBJALLOC_ALIGN offsetof (struct objalloc_align, d) 6452cb4975SJohn Polstra 6552cb4975SJohn Polstra /* Create an objalloc structure. Returns NULL if malloc fails. */ 6652cb4975SJohn Polstra 6752cb4975SJohn Polstra extern struct objalloc *objalloc_create (void); 6852cb4975SJohn Polstra 6952cb4975SJohn Polstra /* Allocate space from an objalloc structure. Returns NULL if malloc 7052cb4975SJohn Polstra fails. */ 7152cb4975SJohn Polstra 7252cb4975SJohn Polstra extern void *_objalloc_alloc (struct objalloc *, unsigned long); 7352cb4975SJohn Polstra 7452cb4975SJohn Polstra /* The macro version of objalloc_alloc. We only define this if using 7552cb4975SJohn Polstra gcc, because otherwise we would have to evaluate the arguments 7652cb4975SJohn Polstra multiple times, or use a temporary field as obstack.h does. */ 7752cb4975SJohn Polstra 7852cb4975SJohn Polstra #if defined (__GNUC__) && defined (__STDC__) && __STDC__ 7952cb4975SJohn Polstra 8052cb4975SJohn Polstra /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and 8152cb4975SJohn Polstra does not implement __extension__. But that compiler doesn't define 8252cb4975SJohn Polstra __GNUC_MINOR__. */ 8352cb4975SJohn Polstra #if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__) 8452cb4975SJohn Polstra #define __extension__ 8552cb4975SJohn Polstra #endif 8652cb4975SJohn Polstra 8752cb4975SJohn Polstra #define objalloc_alloc(o, l) \ 8852cb4975SJohn Polstra __extension__ \ 8952cb4975SJohn Polstra ({ struct objalloc *__o = (o); \ 9052cb4975SJohn Polstra unsigned long __len = (l); \ 9152cb4975SJohn Polstra if (__len == 0) \ 9252cb4975SJohn Polstra __len = 1; \ 9352cb4975SJohn Polstra __len = (__len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1); \ 9452cb4975SJohn Polstra (__len <= __o->current_space \ 9552cb4975SJohn Polstra ? (__o->current_ptr += __len, \ 9652cb4975SJohn Polstra __o->current_space -= __len, \ 9752cb4975SJohn Polstra (void *) (__o->current_ptr - __len)) \ 9852cb4975SJohn Polstra : _objalloc_alloc (__o, __len)); }) 9952cb4975SJohn Polstra 10052cb4975SJohn Polstra #else /* ! __GNUC__ */ 10152cb4975SJohn Polstra 10252cb4975SJohn Polstra #define objalloc_alloc(o, l) _objalloc_alloc ((o), (l)) 10352cb4975SJohn Polstra 10452cb4975SJohn Polstra #endif /* ! __GNUC__ */ 10552cb4975SJohn Polstra 10652cb4975SJohn Polstra /* Free an entire objalloc structure. */ 10752cb4975SJohn Polstra 10852cb4975SJohn Polstra extern void objalloc_free (struct objalloc *); 10952cb4975SJohn Polstra 11052cb4975SJohn Polstra /* Free a block allocated by objalloc_alloc. This also frees all more 11152cb4975SJohn Polstra recently allocated blocks. */ 11252cb4975SJohn Polstra 11352cb4975SJohn Polstra extern void objalloc_free_block (struct objalloc *, void *); 11452cb4975SJohn Polstra 11552cb4975SJohn Polstra #endif /* OBJALLOC_H */ 116