obstack.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* obstack.c - subroutines used implicitly by 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. #include "obstack.h"
  18. #ifdef __STDC__
  19. #define POINTER void *
  20. #else
  21. #define POINTER char *
  22. #endif
  23. /* Determine default alignment. */
  24. struct fooalign {char x; double d;};
  25. #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
  26. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  27. But in fact it might be less smart and round addresses to as much as
  28. DEFAULT_ROUNDING. So we prepare for it to do that. */
  29. union fooround {long x; double d;};
  30. #define DEFAULT_ROUNDING (sizeof (union fooround))
  31. /* When we copy a long block of data, this is the unit to do it with.
  32. On some machines, copying successive ints does not work;
  33. in such a case, redefine COPYING_UNIT to `long' (if that works)
  34. or `char' as a last resort. */
  35. #ifndef COPYING_UNIT
  36. #define COPYING_UNIT int
  37. #endif
  38. /* The non-GNU-C macros copy the obstack into this global variable
  39. to avoid multiple evaluation. */
  40. struct obstack *_obstack;
  41. /* Initialize an obstack H for use. Specify chunk size SIZE (0 means default).
  42. Objects start on multiples of ALIGNMENT (0 means use default).
  43. CHUNKFUN is the function to use to allocate chunks,
  44. and FREEFUN the function to free them. */
  45. void
  46. _obstack_begin (h, size, alignment, chunkfun, freefun)
  47. struct obstack *h;
  48. int size;
  49. int alignment;
  50. POINTER (*chunkfun) ();
  51. void (*freefun) ();
  52. {
  53. register struct _obstack_chunk* chunk; /* points to new chunk */
  54. if (alignment == 0)
  55. alignment = DEFAULT_ALIGNMENT;
  56. if (size == 0)
  57. /* Default size is what GNU malloc can fit in a 4096-byte block.
  58. Pick a number small enough that when rounded up to DEFAULT_ROUNDING
  59. it is still smaller than 4096 - 4. */
  60. {
  61. int extra = 4;
  62. if (extra < DEFAULT_ROUNDING)
  63. extra = DEFAULT_ROUNDING;
  64. size = 4096 - extra;
  65. }
  66. h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  67. h->freefun = freefun;
  68. h->chunk_size = size;
  69. h->alignment_mask = alignment - 1;
  70. chunk = h->chunk = (*h->chunkfun) (h->chunk_size);
  71. h->next_free = h->object_base = chunk->contents;
  72. h->chunk_limit = chunk->limit
  73. = (char *) chunk + h->chunk_size;
  74. chunk->prev = 0;
  75. }
  76. /* Allocate a new current chunk for the obstack *H
  77. on the assumption that LENGTH bytes need to be added
  78. to the current object, or a new object of length LENGTH allocated.
  79. Copies any partial object from the end of the old chunk
  80. to the beginning of the new one. */
  81. void
  82. _obstack_newchunk (h, length)
  83. struct obstack *h;
  84. int length;
  85. {
  86. register struct _obstack_chunk* old_chunk = h->chunk;
  87. register struct _obstack_chunk* new_chunk;
  88. register long new_size;
  89. register int obj_size = h->next_free - h->object_base;
  90. register int i;
  91. int already;
  92. /* Compute size for new chunk. */
  93. new_size = (obj_size + length) + (obj_size >> 3) + 100;
  94. if (new_size < h->chunk_size)
  95. new_size = h->chunk_size;
  96. /* Allocate and initialize the new chunk. */
  97. new_chunk = h->chunk = (*h->chunkfun) (new_size);
  98. new_chunk->prev = old_chunk;
  99. new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  100. /* Move the existing object to the new chunk.
  101. Word at a time is fast and is safe if the object
  102. is sufficiently aligned. */
  103. if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  104. {
  105. for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  106. i >= 0; i--)
  107. ((COPYING_UNIT *)new_chunk->contents)[i]
  108. = ((COPYING_UNIT *)h->object_base)[i];
  109. /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  110. but that can cross a page boundary on a machine
  111. which does not do strict alignment for COPYING_UNITS. */
  112. already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  113. }
  114. else
  115. already = 0;
  116. /* Copy remaining bytes one by one. */
  117. for (i = already; i < obj_size; i++)
  118. new_chunk->contents[i] = h->object_base[i];
  119. h->object_base = new_chunk->contents;
  120. h->next_free = h->object_base + obj_size;
  121. }
  122. /* Return nonzero if object OBJ has been allocated from obstack H.
  123. This is here for debugging.
  124. If you use it in a program, you are probably losing. */
  125. int
  126. _obstack_allocated_p (h, obj)
  127. struct obstack *h;
  128. POINTER obj;
  129. {
  130. register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
  131. register struct _obstack_chunk* plp; /* point to previous chunk if any */
  132. lp = (h)->chunk;
  133. while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  134. {
  135. plp = lp -> prev;
  136. lp = plp;
  137. }
  138. return lp != 0;
  139. }
  140. /* Free objects in obstack H, including OBJ and everything allocate
  141. more recently than OBJ. If OBJ is zero, free everything in H. */
  142. void
  143. #ifdef __STDC__
  144. #undef obstack_free
  145. obstack_free (struct obstack *h, POINTER obj)
  146. #else
  147. _obstack_free (h, obj)
  148. struct obstack *h;
  149. POINTER obj;
  150. #endif
  151. {
  152. register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
  153. register struct _obstack_chunk* plp; /* point to previous chunk if any */
  154. lp = (h)->chunk;
  155. while (lp != 0 && ((POINTER)lp > obj || (POINTER)(lp)->limit < obj))
  156. {
  157. plp = lp -> prev;
  158. (*h->freefun) (lp);
  159. lp = plp;
  160. }
  161. if (lp)
  162. {
  163. (h)->object_base = (h)->next_free = (char *)(obj);
  164. (h)->chunk_limit = lp->limit;
  165. (h)->chunk = lp;
  166. }
  167. else if (obj != 0)
  168. /* obj is not in any of the chunks! */
  169. abort ();
  170. }
  171. /* Let same .o link with output of gcc and other compilers. */
  172. #ifdef __STDC__
  173. void
  174. _obstack_free (h, obj)
  175. struct obstack *h;
  176. POINTER obj;
  177. {
  178. obstack_free (h, obj);
  179. }
  180. #endif
  181. #if 0
  182. /* These are now turned off because the applications do not use it
  183. and it uses bcopy via obstack_grow, which causes trouble on sysV. */
  184. /* Now define the functional versions of the obstack macros.
  185. Define them to simply use the corresponding macros to do the job. */
  186. #ifdef __STDC__
  187. /* These function definitions do not work with non-ANSI preprocessors;
  188. they won't pass through the macro names in parentheses. */
  189. /* The function names appear in parentheses in order to prevent
  190. the macro-definitions of the names from being expanded there. */
  191. POINTER (obstack_base) (obstack)
  192. struct obstack *obstack;
  193. {
  194. return obstack_base (obstack);
  195. }
  196. POINTER (obstack_next_free) (obstack)
  197. struct obstack *obstack;
  198. {
  199. return obstack_next_free (obstack);
  200. }
  201. int (obstack_object_size) (obstack)
  202. struct obstack *obstack;
  203. {
  204. return obstack_object_size (obstack);
  205. }
  206. int (obstack_room) (obstack)
  207. struct obstack *obstack;
  208. {
  209. return obstack_room (obstack);
  210. }
  211. void (obstack_grow) (obstack, pointer, length)
  212. struct obstack *obstack;
  213. POINTER pointer;
  214. int length;
  215. {
  216. obstack_grow (obstack, pointer, length);
  217. }
  218. void (obstack_grow0) (obstack, pointer, length)
  219. struct obstack *obstack;
  220. POINTER pointer;
  221. int length;
  222. {
  223. obstack_grow0 (obstack, pointer, length);
  224. }
  225. void (obstack_1grow) (obstack, character)
  226. struct obstack *obstack;
  227. int character;
  228. {
  229. obstack_1grow (obstack, character);
  230. }
  231. void (obstack_blank) (obstack, length)
  232. struct obstack *obstack;
  233. int length;
  234. {
  235. obstack_blank (obstack, length);
  236. }
  237. void (obstack_1grow_fast) (obstack, character)
  238. struct obstack *obstack;
  239. int character;
  240. {
  241. obstack_1grow_fast (obstack, character);
  242. }
  243. void (obstack_blank_fast) (obstack, length)
  244. struct obstack *obstack;
  245. int length;
  246. {
  247. obstack_blank_fast (obstack, length);
  248. }
  249. POINTER (obstack_finish) (obstack)
  250. struct obstack *obstack;
  251. {
  252. return obstack_finish (obstack);
  253. }
  254. POINTER (obstack_alloc) (obstack, length)
  255. struct obstack *obstack;
  256. int length;
  257. {
  258. return obstack_alloc (obstack, length);
  259. }
  260. POINTER (obstack_copy) (obstack, pointer, length)
  261. struct obstack *obstack;
  262. POINTER pointer;
  263. int length;
  264. {
  265. return obstack_copy (obstack, pointer, length);
  266. }
  267. POINTER (obstack_copy0) (obstack, pointer, length)
  268. struct obstack *obstack;
  269. POINTER pointer;
  270. int length;
  271. {
  272. return obstack_copy0 (obstack, pointer, length);
  273. }
  274. #endif /* __STDC__ */
  275. #endif /* 0 */