obstack.h 15 KB

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