Commit | Line | Data |
---|---|---|
24448022 C |
1 | /* obstack.h - object stack macros |
2 | Copyright (C) 1988 Free Software Foundation, Inc. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify it | |
5 | under the terms of the GNU General Public License as published by the | |
6 | Free Software Foundation; either version 1, or (at your option) any | |
7 | later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | ||
18 | ||
19 | In other words, you are welcome to use, share and improve this program. | |
20 | You are forbidden to forbid anyone else to use, share and improve | |
21 | what you give them. Help stamp out software-hoarding! */ | |
22 | ||
23 | ||
24 | /* Summary: | |
25 | ||
26 | All the apparent functions defined here are macros. The idea | |
27 | is that you would use these pre-tested macros to solve a | |
28 | very specific set of problems, and they would run fast. | |
29 | Caution: no side-effects in arguments please!! They may be | |
30 | evaluated MANY times!! | |
31 | ||
32 | These macros operate a stack of objects. Each object starts life | |
33 | small, and may grow to maturity. (Consider building a word syllable | |
34 | by syllable.) An object can move while it is growing. Once it has | |
35 | been "finished" it never changes address again. So the "top of the | |
36 | stack" is typically an immature growing object, while the rest of the | |
37 | stack is of mature, fixed size and fixed address objects. | |
38 | ||
39 | These routines grab large chunks of memory, using a function you | |
40 | supply, called `obstack_chunk_alloc'. On occasion, they free chunks, | |
41 | by calling `obstack_chunk_free'. You must define them and declare | |
42 | them before using any obstack macros. | |
43 | ||
44 | Each independent stack is represented by a `struct obstack'. | |
45 | Each of the obstack macros expects a pointer to such a structure | |
46 | as the first argument. | |
47 | ||
48 | One motivation for this package is the problem of growing char strings | |
49 | in symbol tables. Unless you are "facist pig with a read-only mind" | |
50 | [Gosper's immortal quote from HAKMEM item 154, out of context] you | |
51 | would not like to put any arbitrary upper limit on the length of your | |
52 | symbols. | |
53 | ||
54 | In practice this often means you will build many short symbols and a | |
55 | few long symbols. At the time you are reading a symbol you don't know | |
56 | how long it is. One traditional method is to read a symbol into a | |
57 | buffer, realloc()ating the buffer every time you try to read a symbol | |
58 | that is longer than the buffer. This is beaut, but you still will | |
59 | want to copy the symbol from the buffer to a more permanent | |
60 | symbol-table entry say about half the time. | |
61 | ||
62 | With obstacks, you can work differently. Use one obstack for all symbol | |
63 | names. As you read a symbol, grow the name in the obstack gradually. | |
64 | When the name is complete, finalize it. Then, if the symbol exists already, | |
65 | free the newly read name. | |
66 | ||
67 | The way we do this is to take a large chunk, allocating memory from | |
68 | low addresses. When you want to build a aymbol in the chunk you just | |
69 | add chars above the current "high water mark" in the chunk. When you | |
70 | have finished adding chars, because you got to the end of the symbol, | |
71 | you know how long the chars are, and you can create a new object. | |
72 | Mostly the chars will not burst over the highest address of the chunk, | |
73 | because you would typically expect a chunk to be (say) 100 times as | |
74 | long as an average object. | |
75 | ||
76 | In case that isn't clear, when we have enough chars to make up | |
77 | the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed) | |
78 | so we just point to it where it lies. No moving of chars is | |
79 | needed and this is the second win: potentially long strings need | |
80 | never be explicitly shuffled. Once an object is formed, it does not | |
81 | change its address during its lifetime. | |
82 | ||
83 | When the chars burst over a chunk boundary, we allocate a larger | |
84 | chunk, and then copy the partly formed object from the end of the old | |
85 | chunk to the beggining of the new larger chunk. We then carry on | |
86 | accreting characters to the end of the object as we normaly would. | |
87 | ||
88 | A special macro is provided to add a single char at a time to a | |
89 | growing object. This allows the use of register variables, which | |
90 | break the ordinary 'growth' macro. | |
91 | ||
92 | Summary: | |
93 | We allocate large chunks. | |
94 | We carve out one object at a time from the current chunk. | |
95 | Once carved, an object never moves. | |
96 | We are free to append data of any size to the currently | |
97 | growing object. | |
98 | Exactly one object is growing in an obstack at any one time. | |
99 | You can run one obstack per control block. | |
100 | You may have as many control blocks as you dare. | |
101 | Because of the way we do it, you can `unwind' a obstack | |
102 | back to a previous state. (You may remove objects much | |
103 | as you would with a stack.) | |
104 | */ | |
105 | ||
106 | ||
107 | /* Don't do the contents of this file more than once. */ | |
108 | ||
109 | #ifndef __OBSTACKS__ | |
110 | #define __OBSTACKS__ | |
111 | \f | |
112 | /* We use subtraction of (char *)0 instead of casting to int | |
113 | because on word-addressable machines a simple cast to int | |
114 | may ignore the byte-within-word field of the pointer. */ | |
115 | ||
116 | #ifndef __PTR_TO_INT | |
117 | #define __PTR_TO_INT(P) ((P) - (char *)0) | |
118 | #endif | |
119 | ||
120 | #ifndef __INT_TO_PTR | |
121 | #define __INT_TO_PTR(P) ((P) + (char *)0) | |
122 | #endif | |
123 | ||
124 | struct _obstack_chunk /* Lives at front of each chunk. */ | |
125 | { | |
126 | char *limit; /* 1 past end of this chunk */ | |
127 | struct _obstack_chunk *prev; /* address of prior chunk or NULL */ | |
128 | char contents[4]; /* objects begin here */ | |
129 | }; | |
130 | ||
131 | struct obstack /* control current object in current chunk */ | |
132 | { | |
133 | long chunk_size; /* preferred size to allocate chunks in */ | |
134 | struct _obstack_chunk* chunk; /* address of current struct obstack_chunk */ | |
135 | char *object_base; /* address of object we are building */ | |
136 | char *next_free; /* where to add next char to current object */ | |
137 | char *chunk_limit; /* address of char after current chunk */ | |
138 | int temp; /* Temporary for some macros. */ | |
139 | int alignment_mask; /* Mask of alignment for each object. */ | |
140 | struct _obstack_chunk *(*chunkfun) (); /* User's fcn to allocate a chunk. */ | |
141 | void (*freefun) (); /* User's function to free a chunk. */ | |
142 | }; | |
143 | \f | |
144 | #ifdef __STDC__ | |
145 | ||
146 | /* Do the function-declarations after the structs | |
147 | but before defining the macros. */ | |
148 | ||
149 | void obstack_init (struct obstack *obstack); | |
150 | ||
151 | void * obstack_alloc (struct obstack *obstack, int size); | |
152 | ||
153 | void * obstack_copy (struct obstack *obstack, void *address, int size); | |
154 | void * obstack_copy0 (struct obstack *obstack, void *address, int size); | |
155 | ||
156 | void obstack_free (struct obstack *obstack, void *block); | |
157 | ||
158 | void obstack_blank (struct obstack *obstack, int size); | |
159 | ||
160 | void obstack_grow (struct obstack *obstack, void *data, int size); | |
161 | void obstack_grow0 (struct obstack *obstack, void *data, int size); | |
162 | ||
163 | void obstack_1grow (struct obstack *obstack, int data_char); | |
164 | ||
165 | void * obstack_finish (struct obstack *obstack); | |
166 | ||
167 | int obstack_object_size (struct obstack *obstack); | |
168 | ||
169 | int obstack_room (struct obstack *obstack); | |
170 | void obstack_1grow_fast (struct obstack *obstack, int data_char); | |
171 | void obstack_blank_fast (struct obstack *obstack, int size); | |
172 | ||
173 | void * obstack_base (struct obstack *obstack); | |
174 | void * obstack_next_free (struct obstack *obstack); | |
175 | int obstack_alignment_mask (struct obstack *obstack); | |
176 | int obstack_chunk_size (struct obstack *obstack); | |
177 | ||
178 | #endif /* __STDC__ */ | |
179 | ||
180 | /* Non-ANSI C cannot really support alternative functions for these macros, | |
181 | so we do not declare them. */ | |
182 | \f | |
183 | /* Pointer to beginning of object being allocated or to be allocated next. | |
184 | Note that this might not be the final address of the object | |
185 | because a new chunk might be needed to hold the final size. */ | |
186 | ||
187 | #define obstack_base(h) ((h)->object_base) | |
188 | ||
189 | /* Size for allocating ordinary chunks. */ | |
190 | ||
191 | #define obstack_chunk_size(h) ((h)->chunk_size) | |
192 | ||
193 | /* Pointer to next byte not yet allocated in current chunk. */ | |
194 | ||
195 | #define obstack_next_free(h) ((h)->next_free) | |
196 | ||
197 | /* Mask specifying low bits that should be clear in address of an object. */ | |
198 | ||
199 | #define obstack_alignment_mask(h) ((h)->alignment_mask) | |
200 | ||
201 | #define obstack_init(h) \ | |
202 | _obstack_begin ((h), 0, 0, obstack_chunk_alloc, obstack_chunk_free) | |
203 | ||
204 | #define obstack_begin(h, size) \ | |
205 | _obstack_begin ((h), (size), 0, obstack_chunk_alloc, obstack_chunk_free) | |
206 | ||
207 | #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar) | |
208 | ||
209 | #define obstack_blank_fast(h,n) ((h)->next_free += (n)) | |
210 | \f | |
211 | #if defined (__GNUC__) && defined (__STDC__) | |
212 | ||
213 | /* For GNU C, if not -traditional, | |
214 | we can define these macros to compute all args only once | |
215 | without using a global variable. | |
216 | Also, we can avoid using the `temp' slot, to make faster code. */ | |
217 | ||
218 | #define obstack_object_size(OBSTACK) \ | |
219 | ({ struct obstack *__o = (OBSTACK); \ | |
220 | (unsigned) (__o->next_free - __o->object_base); }) | |
221 | ||
222 | #define obstack_room(OBSTACK) \ | |
223 | ({ struct obstack *__o = (OBSTACK); \ | |
224 | (unsigned) (__o->chunk_limit - __o->next_free); }) | |
225 | ||
226 | #define obstack_grow(OBSTACK,where,length) \ | |
227 | ({ struct obstack *__o = (OBSTACK); \ | |
228 | int __len = (length); \ | |
229 | ((__o->next_free + __len > __o->chunk_limit) \ | |
230 | ? _obstack_newchunk (__o, __len) : 0); \ | |
231 | bcopy (where, __o->next_free, __len); \ | |
232 | __o->next_free += __len; \ | |
233 | (void) 0; }) | |
234 | ||
235 | #define obstack_grow0(OBSTACK,where,length) \ | |
236 | ({ struct obstack *__o = (OBSTACK); \ | |
237 | int __len = (length); \ | |
238 | ((__o->next_free + __len + 1 > __o->chunk_limit) \ | |
239 | ? _obstack_newchunk (__o, __len + 1) : 0), \ | |
240 | bcopy (where, __o->next_free, __len), \ | |
241 | __o->next_free += __len, \ | |
242 | *(__o->next_free)++ = 0; \ | |
243 | (void) 0; }) | |
244 | ||
245 | #define obstack_1grow(OBSTACK,datum) \ | |
246 | ({ struct obstack *__o = (OBSTACK); \ | |
247 | ((__o->next_free + 1 > __o->chunk_limit) \ | |
248 | ? _obstack_newchunk (__o, 1) : 0), \ | |
249 | *(__o->next_free)++ = (datum); \ | |
250 | (void) 0; }) | |
251 | ||
252 | #define obstack_blank(OBSTACK,length) \ | |
253 | ({ struct obstack *__o = (OBSTACK); \ | |
254 | int __len = (length); \ | |
255 | ((__o->next_free + __len > __o->chunk_limit) \ | |
256 | ? _obstack_newchunk (__o, __len) : 0); \ | |
257 | __o->next_free += __len; \ | |
258 | (void) 0; }) | |
259 | ||
260 | #define obstack_alloc(OBSTACK,length) \ | |
261 | ({ struct obstack *__h = (OBSTACK); \ | |
262 | obstack_blank (__h, (length)); \ | |
263 | obstack_finish (__h); }) | |
264 | ||
265 | #define obstack_copy(OBSTACK,where,length) \ | |
266 | ({ struct obstack *__h = (OBSTACK); \ | |
267 | obstack_grow (__h, (where), (length)); \ | |
268 | obstack_finish (__h); }) | |
269 | ||
270 | #define obstack_copy0(OBSTACK,where,length) \ | |
271 | ({ struct obstack *__h = (OBSTACK); \ | |
272 | obstack_grow0 (__h, (where), (length)); \ | |
273 | obstack_finish (__h); }) | |
274 | ||
275 | #define obstack_finish(OBSTACK) \ | |
276 | ({ struct obstack *__o = (OBSTACK); \ | |
277 | void *value = (void *) __o->object_base; \ | |
278 | __o->next_free \ | |
279 | = __INT_TO_PTR ((__PTR_TO_INT (__o->next_free)+__o->alignment_mask)\ | |
280 | & ~ (__o->alignment_mask)); \ | |
281 | ((__o->next_free - (char *)__o->chunk \ | |
282 | > __o->chunk_limit - (char *)__o->chunk) \ | |
283 | ? (__o->next_free = __o->chunk_limit) : 0); \ | |
284 | __o->object_base = __o->next_free; \ | |
285 | value; }) | |
286 | ||
287 | #define obstack_free(OBSTACK, OBJ) \ | |
288 | ({ struct obstack *__o = (OBSTACK); \ | |
289 | void *__obj = (OBJ); \ | |
290 | if (__obj >= (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \ | |
291 | __o->next_free = __o->object_base = __obj; \ | |
292 | else (obstack_free) (__o, __obj); }) | |
293 | \f | |
294 | #else /* not __GNUC__ or not __STDC__ */ | |
295 | ||
296 | /* The non-GNU macros copy the obstack-pointer into this global variable | |
297 | to avoid multiple evaluation. */ | |
298 | ||
299 | extern struct obstack *_obstack; | |
300 | ||
301 | #define obstack_object_size(h) \ | |
302 | (unsigned) (_obstack = (h), (h)->next_free - (h)->object_base) | |
303 | ||
304 | #define obstack_room(h) \ | |
305 | (unsigned) (_obstack = (h), (h)->chunk_limit - (h)->next_free) | |
306 | ||
307 | #define obstack_grow(h,where,length) \ | |
308 | ( (h)->temp = (length), \ | |
309 | (((h)->next_free + (h)->temp > (h)->chunk_limit) \ | |
310 | ? _obstack_newchunk ((h), (h)->temp) : 0), \ | |
311 | bcopy (where, (h)->next_free, (h)->temp), \ | |
312 | (h)->next_free += (h)->temp) | |
313 | ||
314 | #define obstack_grow0(h,where,length) \ | |
315 | ( (h)->temp = (length), \ | |
316 | (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \ | |
317 | ? _obstack_newchunk ((h), (h)->temp + 1) : 0), \ | |
318 | bcopy (where, (h)->next_free, (h)->temp), \ | |
319 | (h)->next_free += (h)->temp, \ | |
320 | *((h)->next_free)++ = 0) | |
321 | ||
322 | #define obstack_1grow(h,datum) \ | |
323 | ( (((h)->next_free + 1 > (h)->chunk_limit) \ | |
324 | ? _obstack_newchunk ((h), 1) : 0), \ | |
325 | *((h)->next_free)++ = (datum)) | |
326 | ||
327 | #define obstack_blank(h,length) \ | |
328 | ( (h)->temp = (length), \ | |
329 | (((h)->next_free + (h)->temp > (h)->chunk_limit) \ | |
330 | ? _obstack_newchunk ((h), (h)->temp) : 0), \ | |
331 | (h)->next_free += (h)->temp) | |
332 | ||
333 | #define obstack_alloc(h,length) \ | |
334 | (obstack_blank ((h), (length)), obstack_finish ((h))) | |
335 | ||
336 | #define obstack_copy(h,where,length) \ | |
337 | (obstack_grow ((h), (where), (length)), obstack_finish ((h))) | |
338 | ||
339 | #define obstack_copy0(h,where,length) \ | |
340 | (obstack_grow0 ((h), (where), (length)), obstack_finish ((h))) | |
341 | ||
342 | #define obstack_finish(h) \ | |
343 | ( (h)->temp = __PTR_TO_INT ((h)->object_base), \ | |
344 | (h)->next_free \ | |
345 | = __INT_TO_PTR ((__PTR_TO_INT ((h)->next_free)+(h)->alignment_mask) \ | |
346 | & ~ ((h)->alignment_mask)), \ | |
347 | (((h)->next_free - (char *)(h)->chunk \ | |
348 | > (h)->chunk_limit - (char *)(h)->chunk) \ | |
349 | ? ((h)->next_free = (h)->chunk_limit) : 0), \ | |
350 | (h)->object_base = (h)->next_free, \ | |
351 | __INT_TO_PTR ((h)->temp)) | |
352 | ||
353 | #ifdef __STDC__ | |
354 | #define obstack_free(h,obj) \ | |
355 | ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \ | |
356 | (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\ | |
357 | ? (int) ((h)->next_free = (h)->object_base \ | |
358 | = (h)->temp + (char *) (h)->chunk) \ | |
359 | : ((obstack_free) ((h), (h)->temp + (char *) (h)->chunk), 0))) | |
360 | #else | |
361 | #define obstack_free(h,obj) \ | |
362 | ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \ | |
363 | (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\ | |
364 | ? (int) ((h)->next_free = (h)->object_base \ | |
365 | = (h)->temp + (char *) (h)->chunk) \ | |
366 | : (int) _obstack_free ((h), (h)->temp + (char *) (h)->chunk))) | |
367 | #endif | |
368 | ||
369 | #endif /* not __GNUC__ or not __STDC__ */ | |
370 | ||
371 | #endif /* not __OBSTACKS__ */ | |
372 |