obstack.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. */
  67. #include "obstack.h"
  68. void
  69. _obstack_begin (h, chunkfun)
  70. struct obstack *h;
  71. int (*chunkfun) ();
  72. {
  73. register _Ll* chunk; /* points to new chunk */
  74. chunk = h->chunk =
  75. (_Ll*) (*chunkfun) (h->chunk_size);
  76. h->next_free = h->object_base = chunk->obstack_l_0;
  77. h->chunk_limit = chunk->obstack_l_limit
  78. = (char *) chunk + h->chunk_size;
  79. chunk->obstack_l_prev = 0;
  80. }
  81. /* Allocate a new current chunk for the obstack *H
  82. on the assumption that LENGTH bytes need to be added
  83. to the current object, or a new object of length LENGTH allocated.
  84. Copies any partial object from the end of the old chunk
  85. to the beginning of the new one. */
  86. void
  87. _obstack_newchunk (h, chunkfun, length)
  88. struct obstack *h;
  89. int (*chunkfun) ();
  90. int length;
  91. {
  92. register _Ll* old_chunk = h->chunk;
  93. register _Ll* new_chunk;
  94. register long new_size;
  95. register int obj_size = h->next_free - h->object_base;
  96. /* Compute size for new chunk. */
  97. new_size = (obj_size + length) << 1;
  98. if (new_size < h->chunk_size)
  99. new_size = h->chunk_size;
  100. /* Allocate and initialize the new chunk. */
  101. new_chunk = h->chunk = (_Ll*) (*chunkfun) (new_size);
  102. new_chunk->obstack_l_prev = old_chunk;
  103. new_chunk->obstack_l_limit = h->chunk_limit = (char *) new_chunk + new_size;
  104. /* Move the existing object to the new chunk. */
  105. bcopy (h->object_base, new_chunk->obstack_l_0, obj_size);
  106. h->object_base = new_chunk->obstack_l_0;
  107. h->next_free = h->object_base + obj_size;
  108. };
  109. void
  110. _obstack_free (h, freechunkfun, obj)
  111. struct obstack *h;
  112. void (*freechunkfun) ();
  113. char *obj;
  114. {
  115. register _Ll* lp; /* below addr of any objects in this chunk */
  116. register _Ll* plp; /* point to previous chunk if any */
  117. lp = (h)->chunk;
  118. while (lp != 0 && ((char *)lp > obj || (lp)->obstack_l_limit < obj))
  119. {
  120. plp = lp -> obstack_l_prev;
  121. (*freechunkfun) (lp);
  122. lp = plp;
  123. }
  124. if (lp)
  125. {
  126. (h)->object_base = (h)->next_free = (char *)(obj);
  127. (h)->chunk_limit = lp->obstack_l_limit;
  128. (h)->chunk = lp;
  129. }
  130. else if (obj != 0)
  131. /* obj is not in any of the chunks! */
  132. abort ();
  133. }