]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/obstack.h
1 /* obstack.h - object stack macros
2 Copyright (C) 1986, 1988 Free Software Foundation, Inc.
6 BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
7 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
8 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
9 RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
10 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
13 AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
14 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
17 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
18 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
19 WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
20 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
21 OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
22 USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
23 DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
24 A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
25 PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
28 GENERAL PUBLIC LICENSE TO COPY
30 1. You may copy and distribute verbatim copies of this source file
31 as you receive it, in any medium, provided that you conspicuously and
32 appropriately publish on each copy a valid copyright notice "Copyright
33 (C) 1988 Free Software Foundation, Inc."; and include following the
34 copyright notice a verbatim copy of the above disclaimer of warranty
35 and of this License. You may charge a distribution fee for the
36 physical act of transferring a copy.
38 2. You may modify your copy or copies of this source file or
39 any portion of it, and copy and distribute such modifications under
40 the terms of Paragraph 1 above, provided that you also do the following:
42 a) cause the modified files to carry prominent notices stating
43 that you changed the files and the date of any change; and
45 b) cause the whole of any work that you distribute or publish,
46 that in whole or in part contains or is a derivative of this
47 program or any part thereof, to be licensed at no charge to all
48 third parties on terms identical to those contained in this
49 License Agreement (except that you may choose to grant more extensive
50 warranty protection to some or all third parties, at your option).
52 c) You may charge a distribution fee for the physical act of
53 transferring a copy, and you may at your option offer warranty
54 protection in exchange for a fee.
56 Mere aggregation of another unrelated program with this program (or its
57 derivative) on a volume of a storage or distribution medium does not bring
58 the other program under the scope of these terms.
60 3. You may copy and distribute this program (or a portion or derivative
61 of it, under Paragraph 2) in object code or executable form under the terms
62 of Paragraphs 1 and 2 above provided that you also do one of the following:
64 a) accompany it with the complete corresponding machine-readable
65 source code, which must be distributed under the terms of
66 Paragraphs 1 and 2 above; or,
68 b) accompany it with a written offer, valid for at least three
69 years, to give any third party free (except for a nominal
70 shipping charge) a complete machine-readable copy of the
71 corresponding source code, to be distributed under the terms of
72 Paragraphs 1 and 2 above; or,
74 c) accompany it with the information you received as to where the
75 corresponding source code may be obtained. (This alternative is
76 allowed only for noncommercial distribution and only if you
77 received the program in object code or executable form alone.)
79 For an executable file, complete source code means all the source code for
80 all modules it contains; but, as a special exception, it need not include
81 source code for modules which are standard libraries that accompany the
82 operating system on which the executable file runs.
84 4. You may not copy, sublicense, distribute or transfer this program
85 except as expressly provided under this License Agreement. Any attempt
86 otherwise to copy, sublicense, distribute or transfer this program is void and
87 your rights to use the program under this License agreement shall be
88 automatically terminated. However, parties who have received computer
89 software programs from you with this License Agreement will not have
90 their licenses terminated so long as such parties remain in full compliance.
92 5. If you wish to incorporate parts of this program into other free
93 programs whose distribution conditions are different, write to the Free
94 Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
95 worked out a simple rule that can be stated here, but we will often permit
96 this. We will be guided by the two goals of preserving the free status of
97 all derivatives our free software and of promoting the sharing and reuse of
101 In other words, you are welcome to use, share and improve this program.
102 You are forbidden to forbid anyone else to use, share and improve
103 what you give them. Help stamp out software-hoarding! */
108 All the apparent functions defined here are macros. The idea
109 is that you would use these pre-tested macros to solve a
110 very specific set of problems, and they would run fast.
111 Caution: no side-effects in arguments please!! They may be
112 evaluated MANY times!!
114 These macros operate a stack of objects. Each object starts life
115 small, and may grow to maturity. (Consider building a word syllable
116 by syllable.) An object can move while it is growing. Once it has
117 been "finished" it never changes address again. So the "top of the
118 stack" is typically an immature growing object, while the rest of the
119 stack is of mature, fixed size and fixed address objects.
121 These routines grab large chunks of memory, using a function you
122 supply, called `obstack_chunk_alloc'. On occasion, they free chunks,
123 by calling `obstack_chunk_free'. You must define them and declare
124 them before using any obstack macros.
126 Each independent stack is represented by a `struct obstack'.
127 Each of the obstack macros expects a pointer to such a structure
128 as the first argument.
130 One motivation for this package is the problem of growing char strings
131 in symbol tables. Unless you are "facist pig with a read-only mind"
132 [Gosper's immortal quote from HAKMEM item 154, out of context] you
133 would not like to put any arbitrary upper limit on the length of your
136 In practice this often means you will build many short symbols and a
137 few long symbols. At the time you are reading a symbol you don't know
138 how long it is. One traditional method is to read a symbol into a
139 buffer, realloc()ating the buffer every time you try to read a symbol
140 that is longer than the buffer. This is beaut, but you still will
141 want to copy the symbol from the buffer to a more permanent
142 symbol-table entry say about half the time.
144 With obstacks, you can work differently. Use one obstack for all symbol
145 names. As you read a symbol, grow the name in the obstack gradually.
146 When the name is complete, finalize it. Then, if the symbol exists already,
147 free the newly read name.
149 The way we do this is to take a large chunk, allocating memory from
150 low addresses. When you want to build a aymbol in the chunk you just
151 add chars above the current "high water mark" in the chunk. When you
152 have finished adding chars, because you got to the end of the symbol,
153 you know how long the chars are, and you can create a new object.
154 Mostly the chars will not burst over the highest address of the chunk,
155 because you would typically expect a chunk to be (say) 100 times as
156 long as an average object.
158 In case that isn't clear, when we have enough chars to make up
159 the object, THEY ARE ALREADY CONTIGUOUS IN THE CHUNK (guaranteed)
160 so we just point to it where it lies. No moving of chars is
161 needed and this is the second win: potentially long strings need
162 never be explicitly shuffled. Once an object is formed, it does not
163 change its address during its lifetime.
165 When the chars burst over a chunk boundary, we allocate a larger
166 chunk, and then copy the partly formed object from the end of the old
167 chunk to the beggining of the new larger chunk. We then carry on
168 accreting characters to the end of the object as we normaly would.
170 A special macro is provided to add a single char at a time to a
171 growing object. This allows the use of register variables, which
172 break the ordinary 'growth' macro.
175 We allocate large chunks.
176 We carve out one object at a time from the current chunk.
177 Once carved, an object never moves.
178 We are free to append data of any size to the currently
180 Exactly one object is growing in an obstack at any one time.
181 You can run one obstack per control block.
182 You may have as many control blocks as you dare.
183 Because of the way we do it, you can `unwind' a obstack
184 back to a previous state. (You may remove objects much
185 as you would with a stack.)
189 /* Don't do the contents of this file more than once. */
196 /* Do the function-declarations before defining the macros. */
198 void obstack_init (struct obstack
*obstack
);
200 void * obstack_alloc (struct obstack
*obstack
, int size
);
202 void * obstack_copy (struct obstack
*obstack
, void *address
, int size
);
203 void * obstack_copy0 (struct obstack
*obstack
, void *address
, int size
);
205 void obstack_free (struct obstack
*obstack
, void *block
);
207 void obstack_blank (struct obstack
*obstack
, int size
);
209 void obstack_grow (struct obstack
*obstack
, void *data
, int size
);
210 void obstack_grow0 (struct obstack
*obstack
, void *data
, int size
);
212 void obstack_1grow (struct obstack
*obstack
, int data_char
);
214 void * obstack_finish (struct obstack
*obstack
);
216 int obstack_object_size (struct obstack
*obstack
);
218 int obstack_room (struct obstack
*obstack
);
219 void obstack_1grow_fast (struct obstack
*obstack
, int data_char
);
220 void obstack_blank_fast (struct obstack
*obstack
, int size
);
222 void * object_base (struct obstack
*obstack
);
223 void * object_next_free (struct obstack
*obstack
);
224 int obstack_alignment_mask (struct obstack
*obstack
);
225 int obstack_chunk_size (struct obstack
*obstack
);
227 #endif /* __STDC__ */
229 /* Non-ANSI C cannot really support alternative functions for these macros,
230 so we do not declare them. */
232 struct _obstack_chunk
/* Lives at front of each chunk. */
234 char *limit
; /* 1 past end of this chunk */
235 struct _obstack_chunk
*prev
; /* address of prior chunk or NULL */
236 char contents
[4]; /* objects begin here */
239 struct obstack
/* control current object in current chunk */
241 long chunk_size
; /* preferred size to allocate chunks in */
242 struct _obstack_chunk
* chunk
; /* address of current struct obstack_chunk */
243 char *object_base
; /* address of object we are building */
244 char *next_free
; /* where to add next char to current object */
245 char *chunk_limit
; /* address of char after current chunk */
246 int temp
; /* Temporary for some macros. */
247 int alignment_mask
; /* Mask of alignment for each object. */
248 struct _obstack_chunk
*(*chunkfun
) (); /* User's fcn to allocate a chunk. */
249 void (*freefun
) (); /* User's function to free a chunk. */
252 /* Pointer to beginning of object being allocated or to be allocated next.
253 Note that this might not be the final address of the object
254 because a new chunk might be needed to hold the final size. */
256 #define obstack_base(h) ((h)->object_base)
258 /* Size for allocating ordinary chunks. */
260 #define obstack_chunk_size(h) ((h)->chunk_size)
262 /* Pointer to next byte not yet allocated in current chunk. */
264 #define obstack_next_free(h) ((h)->next_free)
266 /* Mask specifying low bits that should be clear in address of an object. */
268 #define obstack_alignment_mask(h) ((h)->alignment_mask)
270 #define obstack_init(h) \
271 _obstack_begin ((h), 4096 - 4, 4, obstack_chunk_alloc, obstack_chunk_free)
273 #define obstack_begin(h, size) \
274 _obstack_begin ((h), (size), 4, obstack_chunk_alloc, obstack_chunk_free)
276 #define obstack_1grow_fast(h,achar) (*((h)->next_free)++ = achar)
278 #define obstack_blank_fast(h,n) ((h)->next_free += (n))
280 #if defined (__GNU__) && defined (__STDC__)
282 /* For GNU C we can define these macros to compute all args only once
283 without using a global variable.
284 Also, we can avoid using the `temp' slot, to make faster code.
286 By checking both __GNU__ and __STDC__, we can make sure
287 both GNU language extensions and ANSI preprocessing are available. */
289 #define obstack_object_size(OBSTACK) \
290 ({ struct obstack *__o = (OBSTACK); \
291 (unsigned) (__o->next_free - __o->object_base); })
293 #define obstack_room(OBSTACK) \
294 ({ struct obstack *__o = (OBSTACK); \
295 (unsigned) (__o->chunk_limit - __o->next_free); })
297 #define obstack_grow(OBSTACK,where,length) \
298 ({ struct obstack *__o = (OBSTACK); \
299 int __len = (length); \
300 ((__o->next_free + __len > __o->chunk_limit) \
301 ? _obstack_newchunk (__o, __len) : 0); \
302 bcopy (where, __o->next_free, __len); \
303 __o->next_free += __len; \
306 #define obstack_grow0(OBSTACK,where,length) \
307 ({ struct obstack *__o = (OBSTACK); \
308 int __len = (length); \
309 ((__o->next_free + __len + 1 > __o->chunk_limit) \
310 ? _obstack_newchunk (__o, __len + 1) : 0), \
311 bcopy (where, __o->next_free, __len), \
312 __o->next_free += __len, \
313 *(__o->next_free)++ = 0; \
316 #define obstack_1grow(OBSTACK,datum) \
317 ({ struct obstack *__o = (OBSTACK); \
318 ((__o->next_free + 1 > __o->chunk_limit) \
319 ? _obstack_newchunk (__o, 1) : 0), \
320 *(__o->next_free)++ = (datum); \
323 #define obstack_blank(OBSTACK,length) \
324 ({ struct obstack *__o = (OBSTACK); \
325 int __len = (length); \
326 ((__o->next_free + __len > __o->chunk_limit) \
327 ? _obstack_newchunk (__o, __len) : 0); \
328 __o->next_free += __len; \
331 #define obstack_alloc(OBSTACK,length) \
332 ({ struct obstack *__h = (OBSTACK); \
333 obstack_blank (__h, (length)); \
334 obstack_finish (__h); })
336 #define obstack_copy(OBSTACK,where,length) \
337 ({ struct obstack *__h = (OBSTACK); \
338 obstack_grow (__h, (where), (length)); \
339 obstack_finish (__h); })
341 #define obstack_copy0(OBSTACK,where,length) \
342 ({ struct obstack *__h = (OBSTACK); \
343 obstack_grow0 (__h, (where), (length)); \
344 obstack_finish (__h); })
346 #define obstack_finish(OBSTACK) \
347 ({ struct obstack *__o = (OBSTACK); \
348 void *value = (void *) __o->object_base; \
350 = (char*)((int)(__o->next_free+__o->alignment_mask) \
351 & ~ (__o->alignment_mask)); \
352 ((__o->next_free - (char *)__o->chunk \
353 > __o->chunk_limit - (char *)__o->chunk) \
354 ? __o->next_free = __o->chunk_limit : 0); \
355 __o->object_base = __o->next_free; \
358 #define obstack_free(OBSTACK, OBJ) \
359 ({ struct obstack *__o = (OBSTACK); \
360 void *__obj = (OBJ); \
361 if (__obj >= (void *)__o->chunk && __obj < (void *)__o->chunk_limit) \
362 __o->next_free = __o->object_base = __obj; \
363 else (obstack_free) (__o, __obj); })
365 #else /* not __GNU__ */
367 /* The non-GNU macros copy the obstack-pointer into this global variable
368 to avoid multiple evaluation. */
370 extern struct obstack
*_obstack
;
372 #define obstack_object_size(h) \
373 (unsigned) (_obstack = (h), (h)->next_free - (h)->object_base)
375 #define obstack_room(h) \
376 (unsigned) (_obstack = (h), (h)->chunk_limit - (h)->next_free)
378 #define obstack_grow(h,where,length) \
379 ( (h)->temp = (length), \
380 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
381 ? _obstack_newchunk ((h), (h)->temp) : 0), \
382 bcopy (where, (h)->next_free, (h)->temp), \
383 (h)->next_free += (h)->temp)
385 #define obstack_grow0(h,where,length) \
386 ( (h)->temp = (length), \
387 (((h)->next_free + (h)->temp + 1 > (h)->chunk_limit) \
388 ? _obstack_newchunk ((h), (h)->temp + 1) : 0), \
389 bcopy (where, (h)->next_free, (h)->temp), \
390 (h)->next_free += (h)->temp, \
391 *((h)->next_free)++ = 0)
393 #define obstack_1grow(h,datum) \
394 ( (((h)->next_free + 1 > (h)->chunk_limit) \
395 ? _obstack_newchunk ((h), 1) : 0), \
396 *((h)->next_free)++ = (datum))
398 #define obstack_blank(h,length) \
399 ( (h)->temp = (length), \
400 (((h)->next_free + (h)->temp > (h)->chunk_limit) \
401 ? _obstack_newchunk ((h), (h)->temp) : 0), \
402 (h)->next_free += (h)->temp)
404 #define obstack_alloc(h,length) \
405 (obstack_blank ((h), (length)), obstack_finish ((h)))
407 #define obstack_copy(h,where,length) \
408 (obstack_grow ((h), (where), (length)), obstack_finish ((h)))
410 #define obstack_copy0(h,where,length) \
411 (obstack_grow0 ((h), (where), (length)), obstack_finish ((h)))
413 #define obstack_finish(h) \
414 ( (h)->temp = (int) (h)->object_base, \
416 = (char*)((int)((h)->next_free+(h)->alignment_mask) \
417 & ~ ((h)->alignment_mask)), \
418 (((h)->next_free - (char *)(h)->chunk \
419 > (h)->chunk_limit - (char *)(h)->chunk) \
420 ? (h)->next_free = (h)->chunk_limit : 0), \
421 (h)->object_base = (h)->next_free, \
425 #define obstack_free(h,obj) \
426 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
427 (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
428 ? (int) ((h)->next_free = (h)->object_base \
429 = (h)->temp + (char *) (h)->chunk) \
430 : (int) (obstack_free) ((h), (h)->temp + (char *) (h)->chunk)))
432 #define obstack_free(h,obj) \
433 ( (h)->temp = (char *)(obj) - (char *) (h)->chunk, \
434 (((h)->temp >= 0 && (h)->temp < (h)->chunk_limit - (char *) (h)->chunk)\
435 ? (int) ((h)->next_free = (h)->object_base \
436 = (h)->temp + (char *) (h)->chunk) \
437 : (int) _obstack_free ((h), (h)->temp + (char *) (h)->chunk)))
440 #endif /* not __GNU__ */
442 #endif /* not __OBSTACKS__ */