obstacks.texi 31 KB

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