obstacks.texi revision 1.1.1.5 1 1.1.1.2 christos @node Obstacks
2 1.1.1.3 christos @subsection Obstacks
3 1.1 christos @cindex obstacks
4 1.1 christos
5 1.1 christos An @dfn{obstack} is a pool of memory containing a stack of objects. You
6 1.1 christos can create any number of separate obstacks, and then allocate objects in
7 1.1 christos specified obstacks. Within each obstack, the last object allocated must
8 1.1 christos always be the first one freed, but distinct obstacks are independent of
9 1.1 christos each other.
10 1.1 christos
11 1.1 christos Aside from this one constraint of order of freeing, obstacks are totally
12 1.1 christos general: an obstack can contain any number of objects of any size. They
13 1.1 christos are implemented with macros, so allocation is usually very fast as long as
14 1.1 christos the objects are usually small. And the only space overhead per object is
15 1.1 christos the padding needed to start each object on a suitable boundary.
16 1.1 christos
17 1.1 christos @menu
18 1.1.1.3 christos * Creating Obstacks:: How to declare an obstack in your program.
19 1.1.1.3 christos * Preparing for Obstacks:: Preparations needed before you can
20 1.1.1.3 christos use obstacks.
21 1.1 christos * Allocation in an Obstack:: Allocating objects in an obstack.
22 1.1 christos * Freeing Obstack Objects:: Freeing objects in an obstack.
23 1.1.1.4 christos * Obstack Functions:: The obstack functions are really macros.
24 1.1 christos * Growing Objects:: Making an object bigger by stages.
25 1.1.1.3 christos * Extra Fast Growing:: Extra-high-efficiency (though more
26 1.1.1.3 christos complicated) growing objects.
27 1.1 christos * Status of an Obstack:: Inquiries about the status of an obstack.
28 1.1 christos * Obstacks Data Alignment:: Controlling alignment of objects in obstacks.
29 1.1 christos * Obstack Chunks:: How obstacks obtain and release chunks;
30 1.1.1.3 christos efficiency considerations.
31 1.1 christos * Summary of Obstacks::
32 1.1 christos @end menu
33 1.1 christos
34 1.1 christos @node Creating Obstacks
35 1.1.1.3 christos @subsubsection Creating Obstacks
36 1.1 christos
37 1.1 christos The utilities for manipulating obstacks are declared in the header
38 1.1 christos file @file{obstack.h}.
39 1.1 christos @pindex obstack.h
40 1.1 christos
41 1.1 christos @comment obstack.h
42 1.1 christos @comment GNU
43 1.1 christos @deftp {Data Type} {struct obstack}
44 1.1 christos An obstack is represented by a data structure of type @code{struct
45 1.1 christos obstack}. This structure has a small fixed size; it records the status
46 1.1 christos of the obstack and how to find the space in which objects are allocated.
47 1.1 christos It does not contain any of the objects themselves. You should not try
48 1.1.1.4 christos to access the contents of the structure directly; use only the macros
49 1.1 christos described in this chapter.
50 1.1 christos @end deftp
51 1.1 christos
52 1.1 christos You can declare variables of type @code{struct obstack} and use them as
53 1.1 christos obstacks, or you can allocate obstacks dynamically like any other kind
54 1.1 christos of object. Dynamic allocation of obstacks allows your program to have a
55 1.1 christos variable number of different stacks. (You can even allocate an
56 1.1 christos obstack structure in another obstack, but this is rarely useful.)
57 1.1 christos
58 1.1.1.4 christos All the macros that work with obstacks require you to specify which
59 1.1 christos obstack to use. You do this with a pointer of type @code{struct obstack
60 1.1 christos *}. In the following, we often say ``an obstack'' when strictly
61 1.1 christos speaking the object at hand is such a pointer.
62 1.1 christos
63 1.1 christos The objects in the obstack are packed into large blocks called
64 1.1 christos @dfn{chunks}. The @code{struct obstack} structure points to a chain of
65 1.1 christos the chunks currently in use.
66 1.1 christos
67 1.1 christos The obstack library obtains a new chunk whenever you allocate an object
68 1.1 christos that won't fit in the previous chunk. Since the obstack library manages
69 1.1 christos chunks automatically, you don't need to pay much attention to them, but
70 1.1 christos you do need to supply a function which the obstack library should use to
71 1.1 christos get a chunk. Usually you supply a function which uses @code{malloc}
72 1.1 christos directly or indirectly. You must also supply a function to free a chunk.
73 1.1 christos These matters are described in the following section.
74 1.1 christos
75 1.1 christos @node Preparing for Obstacks
76 1.1.1.3 christos @subsubsection Preparing for Using Obstacks
77 1.1 christos
78 1.1.1.4 christos Each source file in which you plan to use obstacks
79 1.1 christos must include the header file @file{obstack.h}, like this:
80 1.1 christos
81 1.1 christos @smallexample
82 1.1 christos #include <obstack.h>
83 1.1 christos @end smallexample
84 1.1 christos
85 1.1 christos @findex obstack_chunk_alloc
86 1.1 christos @findex obstack_chunk_free
87 1.1 christos Also, if the source file uses the macro @code{obstack_init}, it must
88 1.1.1.4 christos declare or define two macros that will be called by the
89 1.1 christos obstack library. One, @code{obstack_chunk_alloc}, is used to allocate
90 1.1 christos the chunks of memory into which objects are packed. The other,
91 1.1 christos @code{obstack_chunk_free}, is used to return chunks when the objects in
92 1.1 christos them are freed. These macros should appear before any use of obstacks
93 1.1 christos in the source file.
94 1.1 christos
95 1.1 christos Usually these are defined to use @code{malloc} via the intermediary
96 1.1 christos @code{xmalloc} (@pxref{Unconstrained Allocation, , , libc, The GNU C Library Reference Manual}). This is done with
97 1.1 christos the following pair of macro definitions:
98 1.1 christos
99 1.1 christos @smallexample
100 1.1 christos #define obstack_chunk_alloc xmalloc
101 1.1 christos #define obstack_chunk_free free
102 1.1 christos @end smallexample
103 1.1 christos
104 1.1 christos @noindent
105 1.1 christos Though the memory you get using obstacks really comes from @code{malloc},
106 1.1 christos using obstacks is faster because @code{malloc} is called less often, for
107 1.1 christos larger blocks of memory. @xref{Obstack Chunks}, for full details.
108 1.1 christos
109 1.1 christos At run time, before the program can use a @code{struct obstack} object
110 1.1 christos as an obstack, it must initialize the obstack by calling
111 1.1.1.4 christos @code{obstack_init} or one of its variants, @code{obstack_begin},
112 1.1.1.4 christos @code{obstack_specify_allocation}, or
113 1.1.1.4 christos @code{obstack_specify_allocation_with_arg}.
114 1.1 christos
115 1.1 christos @comment obstack.h
116 1.1 christos @comment GNU
117 1.1 christos @deftypefun int obstack_init (struct obstack *@var{obstack-ptr})
118 1.1 christos Initialize obstack @var{obstack-ptr} for allocation of objects. This
119 1.1.1.4 christos macro calls the obstack's @code{obstack_chunk_alloc} function. If
120 1.1 christos allocation of memory fails, the function pointed to by
121 1.1 christos @code{obstack_alloc_failed_handler} is called. The @code{obstack_init}
122 1.1.1.4 christos macro always returns 1 (Compatibility notice: Former versions of
123 1.1 christos obstack returned 0 if allocation failed).
124 1.1 christos @end deftypefun
125 1.1 christos
126 1.1 christos Here are two examples of how to allocate the space for an obstack and
127 1.1 christos initialize it. First, an obstack that is a static variable:
128 1.1 christos
129 1.1 christos @smallexample
130 1.1 christos static struct obstack myobstack;
131 1.1 christos @dots{}
132 1.1 christos obstack_init (&myobstack);
133 1.1 christos @end smallexample
134 1.1 christos
135 1.1 christos @noindent
136 1.1 christos Second, an obstack that is itself dynamically allocated:
137 1.1 christos
138 1.1 christos @smallexample
139 1.1 christos struct obstack *myobstack_ptr
140 1.1 christos = (struct obstack *) xmalloc (sizeof (struct obstack));
141 1.1 christos
142 1.1 christos obstack_init (myobstack_ptr);
143 1.1 christos @end smallexample
144 1.1 christos
145 1.1 christos @comment obstack.h
146 1.1 christos @comment GNU
147 1.1.1.4 christos @deftypefun int obstack_begin (struct obstack *@var{obstack-ptr}, size_t chunk_size)
148 1.1.1.4 christos Like @code{obstack_init}, but specify chunks to be at least
149 1.1.1.4 christos @var{chunk_size} bytes in size.
150 1.1.1.4 christos @end deftypefun
151 1.1.1.4 christos
152 1.1.1.4 christos @comment obstack.h
153 1.1.1.4 christos @comment GNU
154 1.1.1.4 christos @deftypefun int obstack_specify_allocation (struct obstack *@var{obstack-ptr}, size_t chunk_size, size_t alignment, void *(*chunkfun) (size_t), void (*freefun) (void *))
155 1.1.1.4 christos Like @code{obstack_init}, specifying chunk size, chunk
156 1.1.1.4 christos alignment, and memory allocation functions. A @var{chunk_size} or
157 1.1.1.4 christos @var{alignment} of zero results in the default size or alignment
158 1.1.1.4 christos respectively being used.
159 1.1.1.4 christos @end deftypefun
160 1.1.1.4 christos
161 1.1.1.4 christos @comment obstack.h
162 1.1.1.4 christos @comment GNU
163 1.1.1.4 christos @deftypefun int obstack_specify_allocation_with_arg (struct obstack *@var{obstack-ptr}, size_t chunk_size, size_t alignment, void *(*chunkfun) (void *, size_t), void (*freefun) (void *, void *), void *arg)
164 1.1.1.4 christos Like @code{obstack_specify_allocation}, but specifying memory
165 1.1.1.4 christos allocation functions that take an extra first argument, @var{arg}.
166 1.1.1.4 christos @end deftypefun
167 1.1.1.4 christos
168 1.1.1.4 christos @comment obstack.h
169 1.1.1.4 christos @comment GNU
170 1.1 christos @defvar obstack_alloc_failed_handler
171 1.1 christos The value of this variable is a pointer to a function that
172 1.1 christos @code{obstack} uses when @code{obstack_chunk_alloc} fails to allocate
173 1.1 christos memory. The default action is to print a message and abort.
174 1.1 christos You should supply a function that either calls @code{exit}
175 1.1.1.5 christos (@pxref{Program Termination, , , libc, The GNU C Library Reference Manual})
176 1.1.1.5 christos or @code{longjmp} and doesn't return.
177 1.1 christos
178 1.1 christos @smallexample
179 1.1 christos void my_obstack_alloc_failed (void)
180 1.1 christos @dots{}
181 1.1 christos obstack_alloc_failed_handler = &my_obstack_alloc_failed;
182 1.1 christos @end smallexample
183 1.1 christos
184 1.1 christos @end defvar
185 1.1 christos
186 1.1 christos @node Allocation in an Obstack
187 1.1.1.3 christos @subsubsection Allocation in an Obstack
188 1.1 christos @cindex allocation (obstacks)
189 1.1 christos
190 1.1 christos The most direct way to allocate an object in an obstack is with
191 1.1 christos @code{obstack_alloc}, which is invoked almost like @code{malloc}.
192 1.1 christos
193 1.1 christos @comment obstack.h
194 1.1 christos @comment GNU
195 1.1.1.4 christos @deftypefun {void *} obstack_alloc (struct obstack *@var{obstack-ptr}, size_t @var{size})
196 1.1 christos This allocates an uninitialized block of @var{size} bytes in an obstack
197 1.1 christos and returns its address. Here @var{obstack-ptr} specifies which obstack
198 1.1 christos to allocate the block in; it is the address of the @code{struct obstack}
199 1.1.1.4 christos object which represents the obstack. Each obstack macro
200 1.1 christos requires you to specify an @var{obstack-ptr} as the first argument.
201 1.1 christos
202 1.1.1.4 christos This macro calls the obstack's @code{obstack_chunk_alloc} function if
203 1.1 christos it needs to allocate a new chunk of memory; it calls
204 1.1 christos @code{obstack_alloc_failed_handler} if allocation of memory by
205 1.1 christos @code{obstack_chunk_alloc} failed.
206 1.1 christos @end deftypefun
207 1.1 christos
208 1.1 christos For example, here is a function that allocates a copy of a string @var{str}
209 1.1 christos in a specific obstack, which is in the variable @code{string_obstack}:
210 1.1 christos
211 1.1 christos @smallexample
212 1.1 christos struct obstack string_obstack;
213 1.1 christos
214 1.1 christos char *
215 1.1 christos copystring (char *string)
216 1.1 christos @{
217 1.1 christos size_t len = strlen (string) + 1;
218 1.1 christos char *s = (char *) obstack_alloc (&string_obstack, len);
219 1.1 christos memcpy (s, string, len);
220 1.1 christos return s;
221 1.1 christos @}
222 1.1 christos @end smallexample
223 1.1 christos
224 1.1.1.4 christos To allocate a block with specified contents, use the macro @code{obstack_copy}.
225 1.1 christos
226 1.1 christos @comment obstack.h
227 1.1 christos @comment GNU
228 1.1.1.4 christos @deftypefun {void *} obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
229 1.1 christos This allocates a block and initializes it by copying @var{size}
230 1.1 christos bytes of data starting at @var{address}. It calls
231 1.1 christos @code{obstack_alloc_failed_handler} if allocation of memory by
232 1.1 christos @code{obstack_chunk_alloc} failed.
233 1.1 christos @end deftypefun
234 1.1 christos
235 1.1 christos @comment obstack.h
236 1.1 christos @comment GNU
237 1.1.1.4 christos @deftypefun {void *} obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
238 1.1 christos Like @code{obstack_copy}, but appends an extra byte containing a null
239 1.1 christos character. This extra byte is not counted in the argument @var{size}.
240 1.1 christos @end deftypefun
241 1.1 christos
242 1.1.1.4 christos The @code{obstack_copy0} macro is convenient for copying a sequence
243 1.1 christos of characters into an obstack as a null-terminated string. Here is an
244 1.1 christos example of its use:
245 1.1 christos
246 1.1 christos @smallexample
247 1.1 christos char *
248 1.1.1.4 christos obstack_savestring (char *addr, size_t size)
249 1.1 christos @{
250 1.1 christos return obstack_copy0 (&myobstack, addr, size);
251 1.1 christos @}
252 1.1 christos @end smallexample
253 1.1 christos
254 1.1 christos @noindent
255 1.1 christos Contrast this with the previous example of @code{savestring} using
256 1.1 christos @code{malloc} (@pxref{Basic Allocation, , , libc, The GNU C Library Reference Manual}).
257 1.1 christos
258 1.1 christos @node Freeing Obstack Objects
259 1.1.1.3 christos @subsubsection Freeing Objects in an Obstack
260 1.1 christos @cindex freeing (obstacks)
261 1.1 christos
262 1.1.1.4 christos To free an object allocated in an obstack, use the macro
263 1.1 christos @code{obstack_free}. Since the obstack is a stack of objects, freeing
264 1.1 christos one object automatically frees all other objects allocated more recently
265 1.1 christos in the same obstack.
266 1.1 christos
267 1.1 christos @comment obstack.h
268 1.1 christos @comment GNU
269 1.1 christos @deftypefun void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
270 1.1 christos If @var{object} is a null pointer, everything allocated in the obstack
271 1.1 christos is freed. Otherwise, @var{object} must be the address of an object
272 1.1 christos allocated in the obstack. Then @var{object} is freed, along with
273 1.1 christos everything allocated in @var{obstack} since @var{object}.
274 1.1 christos @end deftypefun
275 1.1 christos
276 1.1 christos Note that if @var{object} is a null pointer, the result is an
277 1.1 christos uninitialized obstack. To free all memory in an obstack but leave it
278 1.1 christos valid for further allocation, call @code{obstack_free} with the address
279 1.1 christos of the first object allocated on the obstack:
280 1.1 christos
281 1.1 christos @smallexample
282 1.1 christos obstack_free (obstack_ptr, first_object_allocated_ptr);
283 1.1 christos @end smallexample
284 1.1 christos
285 1.1 christos Recall that the objects in an obstack are grouped into chunks. When all
286 1.1 christos the objects in a chunk become free, the obstack library automatically
287 1.1 christos frees the chunk (@pxref{Preparing for Obstacks}). Then other
288 1.1 christos obstacks, or non-obstack allocation, can reuse the space of the chunk.
289 1.1 christos
290 1.1 christos @node Obstack Functions
291 1.1.1.3 christos @subsubsection Obstack Functions and Macros
292 1.1 christos @cindex macros
293 1.1 christos
294 1.1.1.4 christos The interfaces for using obstacks are shown here as functions to
295 1.1.1.4 christos specify the return type and argument types, but they are really
296 1.1.1.4 christos defined as macros. This means that the arguments don't actually have
297 1.1.1.4 christos types, but they generally behave as if they have the types shown.
298 1.1.1.4 christos You can call these macros like functions, but you cannot use them in
299 1.1.1.4 christos any other way (for example, you cannot take their address).
300 1.1 christos
301 1.1 christos Calling the macros requires a special precaution: namely, the first
302 1.1 christos operand (the obstack pointer) may not contain any side effects, because
303 1.1 christos it may be computed more than once. For example, if you write this:
304 1.1 christos
305 1.1 christos @smallexample
306 1.1 christos obstack_alloc (get_obstack (), 4);
307 1.1 christos @end smallexample
308 1.1 christos
309 1.1 christos @noindent
310 1.1 christos you will find that @code{get_obstack} may be called several times.
311 1.1 christos If you use @code{*obstack_list_ptr++} as the obstack pointer argument,
312 1.1 christos you will get very strange results since the incrementation may occur
313 1.1 christos several times.
314 1.1 christos
315 1.1 christos If you use the GNU C compiler, this precaution is not necessary, because
316 1.1 christos various language extensions in GNU C permit defining the macros so as to
317 1.1 christos compute each argument only once.
318 1.1 christos
319 1.1.1.4 christos Note that arguments other than the first will only be evaluated once,
320 1.1.1.4 christos even when not using GNU C.
321 1.1.1.4 christos
322 1.1.1.4 christos @code{obstack.h} does declare a number of functions,
323 1.1.1.4 christos @code{_obstack_begin}, @code{_obstack_begin_1},
324 1.1.1.4 christos @code{_obstack_newchunk}, @code{_obstack_free}, and
325 1.1.1.4 christos @code{_obstack_memory_used}. You should not call these directly.
326 1.1.1.4 christos
327 1.1 christos @node Growing Objects
328 1.1.1.3 christos @subsubsection Growing Objects
329 1.1 christos @cindex growing objects (in obstacks)
330 1.1 christos @cindex changing the size of a block (obstacks)
331 1.1 christos
332 1.1 christos Because memory in obstack chunks is used sequentially, it is possible to
333 1.1 christos build up an object step by step, adding one or more bytes at a time to the
334 1.1 christos end of the object. With this technique, you do not need to know how much
335 1.1 christos data you will put in the object until you come to the end of it. We call
336 1.1.1.4 christos this the technique of @dfn{growing objects}. The special macros
337 1.1 christos for adding data to the growing object are described in this section.
338 1.1 christos
339 1.1 christos You don't need to do anything special when you start to grow an object.
340 1.1.1.4 christos Using one of the macros to add data to the object automatically
341 1.1 christos starts it. However, it is necessary to say explicitly when the object is
342 1.1.1.4 christos finished. This is done with @code{obstack_finish}.
343 1.1 christos
344 1.1 christos The actual address of the object thus built up is not known until the
345 1.1 christos object is finished. Until then, it always remains possible that you will
346 1.1 christos add so much data that the object must be copied into a new chunk.
347 1.1 christos
348 1.1 christos While the obstack is in use for a growing object, you cannot use it for
349 1.1 christos ordinary allocation of another object. If you try to do so, the space
350 1.1 christos already added to the growing object will become part of the other object.
351 1.1 christos
352 1.1 christos @comment obstack.h
353 1.1 christos @comment GNU
354 1.1.1.4 christos @deftypefun void obstack_blank (struct obstack *@var{obstack-ptr}, size_t @var{size})
355 1.1.1.4 christos The most basic macro for adding to a growing object is
356 1.1 christos @code{obstack_blank}, which adds space without initializing it.
357 1.1 christos @end deftypefun
358 1.1 christos
359 1.1 christos @comment obstack.h
360 1.1 christos @comment GNU
361 1.1.1.4 christos @deftypefun void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{data}, size_t @var{size})
362 1.1 christos To add a block of initialized space, use @code{obstack_grow}, which is
363 1.1 christos the growing-object analogue of @code{obstack_copy}. It adds @var{size}
364 1.1 christos bytes of data to the growing object, copying the contents from
365 1.1 christos @var{data}.
366 1.1 christos @end deftypefun
367 1.1 christos
368 1.1 christos @comment obstack.h
369 1.1 christos @comment GNU
370 1.1.1.4 christos @deftypefun void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{data}, size_t @var{size})
371 1.1 christos This is the growing-object analogue of @code{obstack_copy0}. It adds
372 1.1 christos @var{size} bytes copied from @var{data}, followed by an additional null
373 1.1 christos character.
374 1.1 christos @end deftypefun
375 1.1 christos
376 1.1 christos @comment obstack.h
377 1.1 christos @comment GNU
378 1.1 christos @deftypefun void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{c})
379 1.1.1.4 christos To add one character at a time, use @code{obstack_1grow}.
380 1.1 christos It adds a single byte containing @var{c} to the growing object.
381 1.1 christos @end deftypefun
382 1.1 christos
383 1.1 christos @comment obstack.h
384 1.1 christos @comment GNU
385 1.1 christos @deftypefun void obstack_ptr_grow (struct obstack *@var{obstack-ptr}, void *@var{data})
386 1.1.1.4 christos Adding the value of a pointer one can use
387 1.1 christos @code{obstack_ptr_grow}. It adds @code{sizeof (void *)} bytes
388 1.1 christos containing the value of @var{data}.
389 1.1 christos @end deftypefun
390 1.1 christos
391 1.1 christos @comment obstack.h
392 1.1 christos @comment GNU
393 1.1 christos @deftypefun void obstack_int_grow (struct obstack *@var{obstack-ptr}, int @var{data})
394 1.1.1.4 christos A single value of type @code{int} can be added by using
395 1.1.1.4 christos @code{obstack_int_grow}. It adds @code{sizeof (int)} bytes to
396 1.1 christos the growing object and initializes them with the value of @var{data}.
397 1.1 christos @end deftypefun
398 1.1 christos
399 1.1 christos @comment obstack.h
400 1.1 christos @comment GNU
401 1.1 christos @deftypefun {void *} obstack_finish (struct obstack *@var{obstack-ptr})
402 1.1.1.4 christos When you are finished growing the object, use
403 1.1 christos @code{obstack_finish} to close it off and return its final address.
404 1.1 christos
405 1.1 christos Once you have finished the object, the obstack is available for ordinary
406 1.1 christos allocation or for growing another object.
407 1.1 christos @end deftypefun
408 1.1 christos
409 1.1 christos When you build an object by growing it, you will probably need to know
410 1.1 christos afterward how long it became. You need not keep track of this as you grow
411 1.1.1.4 christos the object, because you can find out the length from the obstack
412 1.1.1.4 christos with @code{obstack_object_size}, before finishing the object.
413 1.1 christos
414 1.1 christos @comment obstack.h
415 1.1 christos @comment GNU
416 1.1.1.4 christos @deftypefun size_t obstack_object_size (struct obstack *@var{obstack-ptr})
417 1.1.1.4 christos This macro returns the current size of the growing object, in bytes.
418 1.1.1.4 christos Remember to call @code{obstack_object_size} @emph{before} finishing the object.
419 1.1 christos After it is finished, @code{obstack_object_size} will return zero.
420 1.1 christos @end deftypefun
421 1.1 christos
422 1.1 christos If you have started growing an object and wish to cancel it, you should
423 1.1 christos finish it and then free it, like this:
424 1.1 christos
425 1.1 christos @smallexample
426 1.1 christos obstack_free (obstack_ptr, obstack_finish (obstack_ptr));
427 1.1 christos @end smallexample
428 1.1 christos
429 1.1 christos @noindent
430 1.1 christos This has no effect if no object was growing.
431 1.1 christos
432 1.1 christos @node Extra Fast Growing
433 1.1.1.3 christos @subsubsection Extra Fast Growing Objects
434 1.1 christos @cindex efficiency and obstacks
435 1.1 christos
436 1.1.1.4 christos The usual macros for growing objects incur overhead for checking
437 1.1 christos whether there is room for the new growth in the current chunk. If you
438 1.1 christos are frequently constructing objects in small steps of growth, this
439 1.1 christos overhead can be significant.
440 1.1 christos
441 1.1 christos You can reduce the overhead by using special ``fast growth''
442 1.1.1.4 christos macros that grow the object without checking. In order to have a
443 1.1 christos robust program, you must do the checking yourself. If you do this checking
444 1.1 christos in the simplest way each time you are about to add data to the object, you
445 1.1 christos have not saved anything, because that is what the ordinary growth
446 1.1.1.4 christos macros do. But if you can arrange to check less often, or check
447 1.1 christos more efficiently, then you make the program faster.
448 1.1 christos
449 1.1.1.4 christos @code{obstack_room} returns the amount of room available
450 1.1.1.4 christos in the current chunk.
451 1.1 christos
452 1.1 christos @comment obstack.h
453 1.1 christos @comment GNU
454 1.1.1.4 christos @deftypefun size_t obstack_room (struct obstack *@var{obstack-ptr})
455 1.1 christos This returns the number of bytes that can be added safely to the current
456 1.1 christos growing object (or to an object about to be started) in obstack
457 1.1.1.4 christos @var{obstack} using the fast growth macros.
458 1.1 christos @end deftypefun
459 1.1 christos
460 1.1.1.4 christos While you know there is room, you can use these fast growth macros
461 1.1 christos for adding data to a growing object:
462 1.1 christos
463 1.1 christos @comment obstack.h
464 1.1 christos @comment GNU
465 1.1 christos @deftypefun void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{c})
466 1.1.1.4 christos @code{obstack_1grow_fast} adds one byte containing the
467 1.1 christos character @var{c} to the growing object in obstack @var{obstack-ptr}.
468 1.1 christos @end deftypefun
469 1.1 christos
470 1.1 christos @comment obstack.h
471 1.1 christos @comment GNU
472 1.1 christos @deftypefun void obstack_ptr_grow_fast (struct obstack *@var{obstack-ptr}, void *@var{data})
473 1.1.1.4 christos @code{obstack_ptr_grow_fast} adds @code{sizeof (void *)}
474 1.1 christos bytes containing the value of @var{data} to the growing object in
475 1.1 christos obstack @var{obstack-ptr}.
476 1.1 christos @end deftypefun
477 1.1 christos
478 1.1 christos @comment obstack.h
479 1.1 christos @comment GNU
480 1.1 christos @deftypefun void obstack_int_grow_fast (struct obstack *@var{obstack-ptr}, int @var{data})
481 1.1.1.4 christos @code{obstack_int_grow_fast} adds @code{sizeof (int)} bytes
482 1.1 christos containing the value of @var{data} to the growing object in obstack
483 1.1 christos @var{obstack-ptr}.
484 1.1 christos @end deftypefun
485 1.1 christos
486 1.1 christos @comment obstack.h
487 1.1 christos @comment GNU
488 1.1.1.4 christos @deftypefun void obstack_blank_fast (struct obstack *@var{obstack-ptr}, size_t @var{size})
489 1.1.1.4 christos @code{obstack_blank_fast} adds @var{size} bytes to the
490 1.1 christos growing object in obstack @var{obstack-ptr} without initializing them.
491 1.1 christos @end deftypefun
492 1.1 christos
493 1.1 christos When you check for space using @code{obstack_room} and there is not
494 1.1.1.4 christos enough room for what you want to add, the fast growth macros
495 1.1 christos are not safe. In this case, simply use the corresponding ordinary
496 1.1.1.4 christos growth macro instead. Very soon this will copy the object to a
497 1.1 christos new chunk; then there will be lots of room available again.
498 1.1 christos
499 1.1.1.4 christos So, each time you use an ordinary growth macro, check afterward for
500 1.1 christos sufficient space using @code{obstack_room}. Once the object is copied
501 1.1 christos to a new chunk, there will be plenty of space again, so the program will
502 1.1.1.4 christos start using the fast growth macros again.
503 1.1 christos
504 1.1 christos Here is an example:
505 1.1 christos
506 1.1 christos @smallexample
507 1.1 christos @group
508 1.1 christos void
509 1.1.1.4 christos add_string (struct obstack *obstack, const char *ptr, size_t len)
510 1.1 christos @{
511 1.1 christos while (len > 0)
512 1.1 christos @{
513 1.1.1.4 christos size_t room = obstack_room (obstack);
514 1.1 christos if (room == 0)
515 1.1 christos @{
516 1.1.1.4 christos /* @r{Not enough room. Add one character slowly,}
517 1.1 christos @r{which may copy to a new chunk and make room.} */
518 1.1 christos obstack_1grow (obstack, *ptr++);
519 1.1 christos len--;
520 1.1 christos @}
521 1.1 christos else
522 1.1 christos @{
523 1.1 christos if (room > len)
524 1.1 christos room = len;
525 1.1 christos /* @r{Add fast as much as we have room for.} */
526 1.1 christos len -= room;
527 1.1 christos while (room-- > 0)
528 1.1 christos obstack_1grow_fast (obstack, *ptr++);
529 1.1 christos @}
530 1.1 christos @}
531 1.1 christos @}
532 1.1 christos @end group
533 1.1 christos @end smallexample
534 1.1 christos
535 1.1.1.4 christos @cindex shrinking objects
536 1.1.1.4 christos You can use @code{obstack_blank_fast} with a ``negative'' size
537 1.1.1.4 christos argument to make the current object smaller. Just don't try to shrink
538 1.1.1.4 christos it beyond zero length---there's no telling what will happen if you do
539 1.1.1.4 christos that. Earlier versions of obstacks allowed you to use
540 1.1.1.4 christos @code{obstack_blank} to shrink objects. This will no longer work.
541 1.1.1.4 christos
542 1.1 christos @node Status of an Obstack
543 1.1.1.3 christos @subsubsection Status of an Obstack
544 1.1 christos @cindex obstack status
545 1.1 christos @cindex status of obstack
546 1.1 christos
547 1.1.1.4 christos Here are macros that provide information on the current status of
548 1.1 christos allocation in an obstack. You can use them to learn about an object while
549 1.1 christos still growing it.
550 1.1 christos
551 1.1 christos @comment obstack.h
552 1.1 christos @comment GNU
553 1.1 christos @deftypefun {void *} obstack_base (struct obstack *@var{obstack-ptr})
554 1.1.1.4 christos This macro returns the tentative address of the beginning of the
555 1.1 christos currently growing object in @var{obstack-ptr}. If you finish the object
556 1.1 christos immediately, it will have that address. If you make it larger first, it
557 1.1 christos may outgrow the current chunk---then its address will change!
558 1.1 christos
559 1.1 christos If no object is growing, this value says where the next object you
560 1.1 christos allocate will start (once again assuming it fits in the current
561 1.1 christos chunk).
562 1.1 christos @end deftypefun
563 1.1 christos
564 1.1 christos @comment obstack.h
565 1.1 christos @comment GNU
566 1.1 christos @deftypefun {void *} obstack_next_free (struct obstack *@var{obstack-ptr})
567 1.1.1.4 christos This macro returns the address of the first free byte in the current
568 1.1 christos chunk of obstack @var{obstack-ptr}. This is the end of the currently
569 1.1 christos growing object. If no object is growing, @code{obstack_next_free}
570 1.1 christos returns the same value as @code{obstack_base}.
571 1.1 christos @end deftypefun
572 1.1 christos
573 1.1 christos @comment obstack.h
574 1.1 christos @comment GNU
575 1.1.1.4 christos @deftypefun size_t obstack_object_size (struct obstack *@var{obstack-ptr})
576 1.1.1.4 christos This macro returns the size in bytes of the currently growing object.
577 1.1 christos This is equivalent to
578 1.1 christos
579 1.1 christos @smallexample
580 1.1.1.4 christos ((size_t) (obstack_next_free (@var{obstack-ptr}) - obstack_base (@var{obstack-ptr})))
581 1.1 christos @end smallexample
582 1.1 christos @end deftypefun
583 1.1 christos
584 1.1 christos @node Obstacks Data Alignment
585 1.1.1.3 christos @subsubsection Alignment of Data in Obstacks
586 1.1 christos @cindex alignment (in obstacks)
587 1.1 christos
588 1.1 christos Each obstack has an @dfn{alignment boundary}; each object allocated in
589 1.1 christos the obstack automatically starts on an address that is a multiple of the
590 1.1.1.3 christos specified boundary. By default, this boundary is aligned so that
591 1.1.1.3 christos the object can hold any type of data.
592 1.1 christos
593 1.1 christos To access an obstack's alignment boundary, use the macro
594 1.1.1.4 christos @code{obstack_alignment_mask}.
595 1.1 christos
596 1.1 christos @comment obstack.h
597 1.1 christos @comment GNU
598 1.1.1.4 christos @deftypefn Macro size_t obstack_alignment_mask (struct obstack *@var{obstack-ptr})
599 1.1 christos The value is a bit mask; a bit that is 1 indicates that the corresponding
600 1.1 christos bit in the address of an object should be 0. The mask value should be one
601 1.1 christos less than a power of 2; the effect is that all object addresses are
602 1.1.1.3 christos multiples of that power of 2. The default value of the mask is a value
603 1.1.1.3 christos that allows aligned objects to hold any type of data: for example, if
604 1.1.1.3 christos its value is 3, any type of data can be stored at locations whose
605 1.1 christos addresses are multiples of 4. A mask value of 0 means an object can start
606 1.1 christos on any multiple of 1 (that is, no alignment is required).
607 1.1 christos
608 1.1 christos The expansion of the macro @code{obstack_alignment_mask} is an lvalue,
609 1.1 christos so you can alter the mask by assignment. For example, this statement:
610 1.1 christos
611 1.1 christos @smallexample
612 1.1 christos obstack_alignment_mask (obstack_ptr) = 0;
613 1.1 christos @end smallexample
614 1.1 christos
615 1.1 christos @noindent
616 1.1 christos has the effect of turning off alignment processing in the specified obstack.
617 1.1 christos @end deftypefn
618 1.1 christos
619 1.1 christos Note that a change in alignment mask does not take effect until
620 1.1 christos @emph{after} the next time an object is allocated or finished in the
621 1.1 christos obstack. If you are not growing an object, you can make the new
622 1.1 christos alignment mask take effect immediately by calling @code{obstack_finish}.
623 1.1 christos This will finish a zero-length object and then do proper alignment for
624 1.1 christos the next object.
625 1.1 christos
626 1.1 christos @node Obstack Chunks
627 1.1.1.3 christos @subsubsection Obstack Chunks
628 1.1 christos @cindex efficiency of chunks
629 1.1 christos @cindex chunks
630 1.1 christos
631 1.1 christos Obstacks work by allocating space for themselves in large chunks, and
632 1.1 christos then parceling out space in the chunks to satisfy your requests. Chunks
633 1.1 christos are normally 4096 bytes long unless you specify a different chunk size.
634 1.1 christos The chunk size includes 8 bytes of overhead that are not actually used
635 1.1 christos for storing objects. Regardless of the specified size, longer chunks
636 1.1 christos will be allocated when necessary for long objects.
637 1.1 christos
638 1.1 christos The obstack library allocates chunks by calling the function
639 1.1 christos @code{obstack_chunk_alloc}, which you must define. When a chunk is no
640 1.1 christos longer needed because you have freed all the objects in it, the obstack
641 1.1 christos library frees the chunk by calling @code{obstack_chunk_free}, which you
642 1.1 christos must also define.
643 1.1 christos
644 1.1 christos These two must be defined (as macros) or declared (as functions) in each
645 1.1 christos source file that uses @code{obstack_init} (@pxref{Creating Obstacks}).
646 1.1 christos Most often they are defined as macros like this:
647 1.1 christos
648 1.1 christos @smallexample
649 1.1 christos #define obstack_chunk_alloc malloc
650 1.1 christos #define obstack_chunk_free free
651 1.1 christos @end smallexample
652 1.1 christos
653 1.1 christos Note that these are simple macros (no arguments). Macro definitions with
654 1.1 christos arguments will not work! It is necessary that @code{obstack_chunk_alloc}
655 1.1 christos or @code{obstack_chunk_free}, alone, expand into a function name if it is
656 1.1 christos not itself a function name.
657 1.1 christos
658 1.1 christos If you allocate chunks with @code{malloc}, the chunk size should be a
659 1.1 christos power of 2. The default chunk size, 4096, was chosen because it is long
660 1.1 christos enough to satisfy many typical requests on the obstack yet short enough
661 1.1 christos not to waste too much memory in the portion of the last chunk not yet used.
662 1.1 christos
663 1.1 christos @comment obstack.h
664 1.1 christos @comment GNU
665 1.1.1.4 christos @deftypefn Macro size_t obstack_chunk_size (struct obstack *@var{obstack-ptr})
666 1.1 christos This returns the chunk size of the given obstack.
667 1.1 christos @end deftypefn
668 1.1 christos
669 1.1 christos Since this macro expands to an lvalue, you can specify a new chunk size by
670 1.1 christos assigning it a new value. Doing so does not affect the chunks already
671 1.1 christos allocated, but will change the size of chunks allocated for that particular
672 1.1 christos obstack in the future. It is unlikely to be useful to make the chunk size
673 1.1 christos smaller, but making it larger might improve efficiency if you are
674 1.1 christos allocating many objects whose size is comparable to the chunk size. Here
675 1.1 christos is how to do so cleanly:
676 1.1 christos
677 1.1 christos @smallexample
678 1.1 christos if (obstack_chunk_size (obstack_ptr) < @var{new-chunk-size})
679 1.1 christos obstack_chunk_size (obstack_ptr) = @var{new-chunk-size};
680 1.1 christos @end smallexample
681 1.1 christos
682 1.1 christos @node Summary of Obstacks
683 1.1.1.4 christos @subsubsection Summary of Obstack Macros
684 1.1 christos
685 1.1.1.4 christos Here is a summary of all the macros associated with obstacks. Each
686 1.1 christos takes the address of an obstack (@code{struct obstack *}) as its first
687 1.1 christos argument.
688 1.1 christos
689 1.1 christos @table @code
690 1.1.1.4 christos @item int obstack_init (struct obstack *@var{obstack-ptr})
691 1.1 christos Initialize use of an obstack. @xref{Creating Obstacks}.
692 1.1 christos
693 1.1.1.4 christos @item int obstack_begin (struct obstack *@var{obstack-ptr}, size_t chunk_size)
694 1.1.1.4 christos Initialize use of an obstack, with an initial chunk of
695 1.1.1.4 christos @var{chunk_size} bytes.
696 1.1.1.4 christos
697 1.1.1.4 christos @item int obstack_specify_allocation (struct obstack *@var{obstack-ptr}, size_t chunk_size, size_t alignment, void *(*chunkfun) (size_t), void (*freefun) (void *))
698 1.1.1.4 christos Initialize use of an obstack, specifying intial chunk size, chunk
699 1.1.1.4 christos alignment, and memory allocation functions.
700 1.1.1.4 christos
701 1.1.1.4 christos @item int obstack_specify_allocation_with_arg (struct obstack *@var{obstack-ptr}, size_t chunk_size, size_t alignment, void *(*chunkfun) (void *, size_t), void (*freefun) (void *, void *), void *arg)
702 1.1.1.4 christos Like @code{obstack_specify_allocation}, but specifying memory
703 1.1.1.4 christos allocation functions that take an extra first argument, @var{arg}.
704 1.1.1.4 christos
705 1.1.1.4 christos @item void *obstack_alloc (struct obstack *@var{obstack-ptr}, size_t @var{size})
706 1.1 christos Allocate an object of @var{size} uninitialized bytes.
707 1.1 christos @xref{Allocation in an Obstack}.
708 1.1 christos
709 1.1.1.4 christos @item void *obstack_copy (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
710 1.1 christos Allocate an object of @var{size} bytes, with contents copied from
711 1.1 christos @var{address}. @xref{Allocation in an Obstack}.
712 1.1 christos
713 1.1.1.4 christos @item void *obstack_copy0 (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
714 1.1 christos Allocate an object of @var{size}+1 bytes, with @var{size} of them copied
715 1.1 christos from @var{address}, followed by a null character at the end.
716 1.1 christos @xref{Allocation in an Obstack}.
717 1.1 christos
718 1.1 christos @item void obstack_free (struct obstack *@var{obstack-ptr}, void *@var{object})
719 1.1 christos Free @var{object} (and everything allocated in the specified obstack
720 1.1 christos more recently than @var{object}). @xref{Freeing Obstack Objects}.
721 1.1 christos
722 1.1.1.4 christos @item void obstack_blank (struct obstack *@var{obstack-ptr}, size_t @var{size})
723 1.1 christos Add @var{size} uninitialized bytes to a growing object.
724 1.1 christos @xref{Growing Objects}.
725 1.1 christos
726 1.1.1.4 christos @item void obstack_grow (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
727 1.1 christos Add @var{size} bytes, copied from @var{address}, to a growing object.
728 1.1 christos @xref{Growing Objects}.
729 1.1 christos
730 1.1.1.4 christos @item void obstack_grow0 (struct obstack *@var{obstack-ptr}, void *@var{address}, size_t @var{size})
731 1.1 christos Add @var{size} bytes, copied from @var{address}, to a growing object,
732 1.1 christos and then add another byte containing a null character. @xref{Growing
733 1.1 christos Objects}.
734 1.1 christos
735 1.1 christos @item void obstack_1grow (struct obstack *@var{obstack-ptr}, char @var{data-char})
736 1.1 christos Add one byte containing @var{data-char} to a growing object.
737 1.1 christos @xref{Growing Objects}.
738 1.1 christos
739 1.1 christos @item void *obstack_finish (struct obstack *@var{obstack-ptr})
740 1.1 christos Finalize the object that is growing and return its permanent address.
741 1.1 christos @xref{Growing Objects}.
742 1.1 christos
743 1.1.1.4 christos @item size_t obstack_object_size (struct obstack *@var{obstack-ptr})
744 1.1 christos Get the current size of the currently growing object. @xref{Growing
745 1.1 christos Objects}.
746 1.1 christos
747 1.1.1.4 christos @item void obstack_blank_fast (struct obstack *@var{obstack-ptr}, size_t @var{size})
748 1.1 christos Add @var{size} uninitialized bytes to a growing object without checking
749 1.1 christos that there is enough room. @xref{Extra Fast Growing}.
750 1.1 christos
751 1.1 christos @item void obstack_1grow_fast (struct obstack *@var{obstack-ptr}, char @var{data-char})
752 1.1 christos Add one byte containing @var{data-char} to a growing object without
753 1.1 christos checking that there is enough room. @xref{Extra Fast Growing}.
754 1.1 christos
755 1.1.1.4 christos @item size_t obstack_room (struct obstack *@var{obstack-ptr})
756 1.1 christos Get the amount of room now available for growing the current object.
757 1.1 christos @xref{Extra Fast Growing}.
758 1.1 christos
759 1.1.1.4 christos @item size_t obstack_alignment_mask (struct obstack *@var{obstack-ptr})
760 1.1 christos The mask used for aligning the beginning of an object. This is an
761 1.1 christos lvalue. @xref{Obstacks Data Alignment}.
762 1.1 christos
763 1.1.1.4 christos @item size_t obstack_chunk_size (struct obstack *@var{obstack-ptr})
764 1.1 christos The size for allocating chunks. This is an lvalue. @xref{Obstack Chunks}.
765 1.1 christos
766 1.1 christos @item void *obstack_base (struct obstack *@var{obstack-ptr})
767 1.1 christos Tentative starting address of the currently growing object.
768 1.1 christos @xref{Status of an Obstack}.
769 1.1 christos
770 1.1 christos @item void *obstack_next_free (struct obstack *@var{obstack-ptr})
771 1.1 christos Address just after the end of the currently growing object.
772 1.1 christos @xref{Status of an Obstack}.
773 1.1 christos @end table
774 1.1 christos
775