obstack.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* obstack.c - subroutines used implicitly by object stack macros
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously and
  27. appropriately publish on each copy a valid copyright notice "Copyright
  28. (C) 1986 Free Software Foundation, Inc."; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License. You may charge a distribution fee for the
  31. physical act of transferring a copy.
  32. 2. You may modify your copy or copies of this source file or
  33. any portion of it, and copy and distribute such modifications under
  34. the terms of Paragraph 1 above, provided that you also do the following:
  35. a) cause the modified files to carry prominent notices stating
  36. that you changed the files and the date of any change; and
  37. b) cause the whole of any work that you distribute or publish,
  38. that in whole or in part contains or is a derivative of this
  39. program or any part thereof, to be licensed at no charge to all
  40. third parties on terms identical to those contained in this
  41. License Agreement (except that you may choose to grant more
  42. extensive warranty protection to third parties, at your option).
  43. c) You may charge a distribution fee for the physical act of
  44. transferring a copy, and you may at your option offer warranty
  45. protection in exchange for a fee.
  46. 3. You may copy and distribute this program or any portion of it in
  47. compiled, executable or object code form under the terms of Paragraphs
  48. 1 and 2 above provided that you do the following:
  49. a) cause each such copy to be accompanied by the
  50. corresponding machine-readable source code, which must
  51. be distributed under the terms of Paragraphs 1 and 2 above; or,
  52. b) cause each such copy to be accompanied by a
  53. written offer, with no time limit, to give any third party
  54. free (except for a nominal shipping charge) a machine readable
  55. copy of the corresponding source code, to be distributed
  56. under the terms of Paragraphs 1 and 2 above; or,
  57. c) in the case of a recipient of this program in compiled, executable
  58. or object code form (without the corresponding source code) you
  59. shall cause copies you distribute to be accompanied by a copy
  60. of the written offer of source code which you received along
  61. with the copy you received.
  62. 4. You may not copy, sublicense, distribute or transfer this program
  63. except as expressly provided under this License Agreement. Any attempt
  64. otherwise to copy, sublicense, distribute or transfer this program is void and
  65. your rights to use the program under this License agreement shall be
  66. automatically terminated. However, parties who have received computer
  67. software programs from you with this License Agreement will not have
  68. their licenses terminated so long as such parties remain in full compliance.
  69. 5. If you wish to incorporate parts of this program into other free
  70. programs whose distribution conditions are different, write to the Free
  71. Software Foundation at 1000 Mass Ave, Cambridge, MA 02138. We have not yet
  72. worked out a simple rule that can be stated here, but we will often permit
  73. this. We will be guided by the two goals of preserving the free status of
  74. all derivatives our free software and of promoting the sharing and reuse of
  75. software.
  76. In other words, you are welcome to use, share and improve this program.
  77. You are forbidden to forbid anyone else to use, share and improve
  78. what you give them. Help stamp out software-hoarding! */
  79. #include <obstack.h>
  80. void
  81. _obstack_begin (h, chunkfun)
  82. struct obstack *h;
  83. int (*chunkfun) ();
  84. {
  85. register _Ll* chunk; /* points to new chunk */
  86. chunk = h->chunk =
  87. (_Ll*) (*chunkfun) (h->chunk_size);
  88. h->next_free = h->object_base = chunk->obstack_l_0;
  89. h->chunk_limit = chunk->obstack_l_limit
  90. = (char *) chunk + h->chunk_size;
  91. chunk->obstack_l_prev = 0;
  92. }
  93. /* Allocate a new current chunk for the obstack *H
  94. on the assumption that LENGTH bytes need to be added
  95. to the current object, or a new object of length LENGTH allocated.
  96. Copies any partial object from the end of the old chunk
  97. to the beginning of the new one. */
  98. void
  99. _obstack_newchunk (h, chunkfun, length)
  100. struct obstack *h;
  101. int (*chunkfun) ();
  102. int length;
  103. {
  104. register _Ll* old_chunk = h->chunk;
  105. register _Ll* new_chunk;
  106. register long new_size;
  107. register int obj_size = h->next_free - h->object_base;
  108. /* Compute size for new chunk. */
  109. new_size = (obj_size + length) << 1;
  110. if (new_size < h->chunk_size)
  111. new_size = h->chunk_size;
  112. /* Allocate and initialize the new chunk. */
  113. new_chunk = h->chunk = (_Ll*) (*chunkfun) (new_size);
  114. new_chunk->obstack_l_prev = old_chunk;
  115. new_chunk->obstack_l_limit = h->chunk_limit = (char *) new_chunk + new_size;
  116. /* Move the existing object to the new chunk. */
  117. bcopy (h->object_base, new_chunk->obstack_l_0, obj_size);
  118. h->object_base = new_chunk->obstack_l_0;
  119. h->next_free = h->object_base + obj_size;
  120. };
  121. void
  122. _obstack_free (h, freechunkfun, obj)
  123. struct obstack *h;
  124. void (*freechunkfun) ();
  125. char *obj;
  126. {
  127. register _Ll* lp; /* below addr of any objects in this chunk */
  128. register _Ll* plp; /* point to previous chunk if any */
  129. lp = (h)->chunk;
  130. while (lp != 0 && ((char *)lp > obj || (lp)->obstack_l_limit < obj))
  131. {
  132. plp = lp -> obstack_l_prev;
  133. (*freechunkfun) (lp);
  134. lp = plp;
  135. }
  136. if (lp)
  137. {
  138. (h)->object_base = (h)->next_free = (char *)(obj);
  139. (h)->chunk_limit = lp->obstack_l_limit;
  140. (h)->chunk = lp;
  141. }
  142. else if (obj != 0)
  143. /* obj is not in any of the chunks! */
  144. abort ();
  145. }