objalloc.h revision 169695
1169695Skan/* objalloc.h -- routines to allocate memory for objects 2169695Skan Copyright 1997, 2001 Free Software Foundation, Inc. 3169695Skan Written by Ian Lance Taylor, Cygnus Solutions. 4169695Skan 5169695SkanThis program is free software; you can redistribute it and/or modify it 6169695Skanunder the terms of the GNU General Public License as published by the 7169695SkanFree Software Foundation; either version 2, or (at your option) any 8169695Skanlater version. 9169695Skan 10169695SkanThis program is distributed in the hope that it will be useful, 11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13169695SkanGNU General Public License for more details. 14169695Skan 15169695SkanYou should have received a copy of the GNU General Public License 16169695Skanalong with this program; if not, write to the Free Software 17169695SkanFoundation, 51 Franklin Street - Fifth Floor, 18169695SkanBoston, MA 02110-1301, USA. */ 19169695Skan 20169695Skan#ifndef OBJALLOC_H 21169695Skan#define OBJALLOC_H 22169695Skan 23169695Skan#include "ansidecl.h" 24169695Skan 25169695Skan/* These routines allocate space for an object. The assumption is 26169695Skan that the object will want to allocate space as it goes along, but 27169695Skan will never want to free any particular block. There is a function 28169695Skan to free a block, which also frees all more recently allocated 29169695Skan blocks. There is also a function to free all the allocated space. 30169695Skan 31169695Skan This is essentially a specialization of obstacks. The main 32169695Skan difference is that a block may not be allocated a bit at a time. 33169695Skan Another difference is that these routines are always built on top 34169695Skan of malloc, and always pass an malloc failure back to the caller, 35169695Skan unlike more recent versions of obstacks. */ 36169695Skan 37169695Skan/* This is what an objalloc structure looks like. Callers should not 38169695Skan refer to these fields, nor should they allocate these structure 39169695Skan themselves. Instead, they should only create them via 40169695Skan objalloc_init, and only access them via the functions and macros 41169695Skan listed below. The structure is only defined here so that we can 42169695Skan access it via macros. */ 43169695Skan 44169695Skanstruct objalloc 45169695Skan{ 46169695Skan char *current_ptr; 47169695Skan unsigned int current_space; 48169695Skan void *chunks; 49169695Skan}; 50169695Skan 51169695Skan/* Work out the required alignment. */ 52169695Skan 53169695Skanstruct objalloc_align { char x; double d; }; 54169695Skan 55169695Skan#if defined (__STDC__) && __STDC__ 56169695Skan#ifndef offsetof 57169695Skan#include <stddef.h> 58169695Skan#endif 59169695Skan#endif 60169695Skan#ifndef offsetof 61169695Skan#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) 62169695Skan#endif 63169695Skan#define OBJALLOC_ALIGN offsetof (struct objalloc_align, d) 64169695Skan 65169695Skan/* Create an objalloc structure. Returns NULL if malloc fails. */ 66169695Skan 67169695Skanextern struct objalloc *objalloc_create (void); 68169695Skan 69169695Skan/* Allocate space from an objalloc structure. Returns NULL if malloc 70169695Skan fails. */ 71169695Skan 72169695Skanextern void *_objalloc_alloc (struct objalloc *, unsigned long); 73169695Skan 74169695Skan/* The macro version of objalloc_alloc. We only define this if using 75169695Skan gcc, because otherwise we would have to evaluate the arguments 76169695Skan multiple times, or use a temporary field as obstack.h does. */ 77169695Skan 78169695Skan#if defined (__GNUC__) && defined (__STDC__) && __STDC__ 79169695Skan 80169695Skan/* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and 81169695Skan does not implement __extension__. But that compiler doesn't define 82169695Skan __GNUC_MINOR__. */ 83169695Skan#if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__) 84169695Skan#define __extension__ 85169695Skan#endif 86169695Skan 87169695Skan#define objalloc_alloc(o, l) \ 88169695Skan __extension__ \ 89169695Skan ({ struct objalloc *__o = (o); \ 90169695Skan unsigned long __len = (l); \ 91169695Skan if (__len == 0) \ 92169695Skan __len = 1; \ 93169695Skan __len = (__len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1); \ 94169695Skan (__len <= __o->current_space \ 95169695Skan ? (__o->current_ptr += __len, \ 96169695Skan __o->current_space -= __len, \ 97169695Skan (void *) (__o->current_ptr - __len)) \ 98169695Skan : _objalloc_alloc (__o, __len)); }) 99169695Skan 100169695Skan#else /* ! __GNUC__ */ 101169695Skan 102169695Skan#define objalloc_alloc(o, l) _objalloc_alloc ((o), (l)) 103169695Skan 104169695Skan#endif /* ! __GNUC__ */ 105169695Skan 106169695Skan/* Free an entire objalloc structure. */ 107169695Skan 108169695Skanextern void objalloc_free (struct objalloc *); 109169695Skan 110169695Skan/* Free a block allocated by objalloc_alloc. This also frees all more 111169695Skan recently allocated blocks. */ 112169695Skan 113169695Skanextern void objalloc_free_block (struct objalloc *, void *); 114169695Skan 115169695Skan#endif /* OBJALLOC_H */ 116