obstacks.texi 30 KB

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