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