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