objalloc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* objalloc.c -- routines to allocate memory for objects
  2. Copyright 1997-2012 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Solutions.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "config.h"
  17. #include "ansidecl.h"
  18. #include "objalloc.h"
  19. /* Get a definition for NULL. */
  20. #include <stdio.h>
  21. #if VMS
  22. #include <stdlib.h>
  23. #include <unixlib.h>
  24. #else
  25. /* Get a definition for size_t. */
  26. #include <stddef.h>
  27. #ifdef HAVE_STDLIB_H
  28. #include <stdlib.h>
  29. #else
  30. /* For systems with larger pointers than ints, this must be declared. */
  31. extern PTR malloc (size_t);
  32. extern void free (PTR);
  33. #endif
  34. #endif
  35. /* These routines allocate space for an object. Freeing allocated
  36. space may or may not free all more recently allocated space.
  37. We handle large and small allocation requests differently. If we
  38. don't have enough space in the current block, and the allocation
  39. request is for more than 512 bytes, we simply pass it through to
  40. malloc. */
  41. /* The objalloc structure is defined in objalloc.h. */
  42. /* This structure appears at the start of each chunk. */
  43. struct objalloc_chunk
  44. {
  45. /* Next chunk. */
  46. struct objalloc_chunk *next;
  47. /* If this chunk contains large objects, this is the value of
  48. current_ptr when this chunk was allocated. If this chunk
  49. contains small objects, this is NULL. */
  50. char *current_ptr;
  51. };
  52. /* The aligned size of objalloc_chunk. */
  53. #define CHUNK_HEADER_SIZE \
  54. ((sizeof (struct objalloc_chunk) + OBJALLOC_ALIGN - 1) \
  55. &~ (OBJALLOC_ALIGN - 1))
  56. /* We ask for this much memory each time we create a chunk which is to
  57. hold small objects. */
  58. #define CHUNK_SIZE (4096 - 32)
  59. /* A request for this amount or more is just passed through to malloc. */
  60. #define BIG_REQUEST (512)
  61. /* Create an objalloc structure. */
  62. struct objalloc *
  63. objalloc_create (void)
  64. {
  65. struct objalloc *ret;
  66. struct objalloc_chunk *chunk;
  67. ret = (struct objalloc *) malloc (sizeof *ret);
  68. if (ret == NULL)
  69. return NULL;
  70. ret->chunks = (PTR) malloc (CHUNK_SIZE);
  71. if (ret->chunks == NULL)
  72. {
  73. free (ret);
  74. return NULL;
  75. }
  76. chunk = (struct objalloc_chunk *) ret->chunks;
  77. chunk->next = NULL;
  78. chunk->current_ptr = NULL;
  79. ret->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
  80. ret->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
  81. return ret;
  82. }
  83. /* Allocate space from an objalloc structure. */
  84. PTR
  85. _objalloc_alloc (struct objalloc *o, unsigned long original_len)
  86. {
  87. unsigned long len = original_len;
  88. /* We avoid confusion from zero sized objects by always allocating
  89. at least 1 byte. */
  90. if (len == 0)
  91. len = 1;
  92. len = (len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);
  93. /* Check for overflow in the alignment operation above and the
  94. malloc argument below. */
  95. if (len + CHUNK_HEADER_SIZE < original_len)
  96. return NULL;
  97. if (len <= o->current_space)
  98. {
  99. o->current_ptr += len;
  100. o->current_space -= len;
  101. return (PTR) (o->current_ptr - len);
  102. }
  103. if (len >= BIG_REQUEST)
  104. {
  105. char *ret;
  106. struct objalloc_chunk *chunk;
  107. ret = (char *) malloc (CHUNK_HEADER_SIZE + len);
  108. if (ret == NULL)
  109. return NULL;
  110. chunk = (struct objalloc_chunk *) ret;
  111. chunk->next = (struct objalloc_chunk *) o->chunks;
  112. chunk->current_ptr = o->current_ptr;
  113. o->chunks = (PTR) chunk;
  114. return (PTR) (ret + CHUNK_HEADER_SIZE);
  115. }
  116. else
  117. {
  118. struct objalloc_chunk *chunk;
  119. chunk = (struct objalloc_chunk *) malloc (CHUNK_SIZE);
  120. if (chunk == NULL)
  121. return NULL;
  122. chunk->next = (struct objalloc_chunk *) o->chunks;
  123. chunk->current_ptr = NULL;
  124. o->current_ptr = (char *) chunk + CHUNK_HEADER_SIZE;
  125. o->current_space = CHUNK_SIZE - CHUNK_HEADER_SIZE;
  126. o->chunks = (PTR) chunk;
  127. return objalloc_alloc (o, len);
  128. }
  129. }
  130. /* Free an entire objalloc structure. */
  131. void
  132. objalloc_free (struct objalloc *o)
  133. {
  134. struct objalloc_chunk *l;
  135. l = (struct objalloc_chunk *) o->chunks;
  136. while (l != NULL)
  137. {
  138. struct objalloc_chunk *next;
  139. next = l->next;
  140. free (l);
  141. l = next;
  142. }
  143. free (o);
  144. }
  145. /* Free a block from an objalloc structure. This also frees all more
  146. recently allocated blocks. */
  147. void
  148. objalloc_free_block (struct objalloc *o, PTR block)
  149. {
  150. struct objalloc_chunk *p, *small;
  151. char *b = (char *) block;
  152. /* First set P to the chunk which contains the block we are freeing,
  153. and set Q to the last small object chunk we see before P. */
  154. small = NULL;
  155. for (p = (struct objalloc_chunk *) o->chunks; p != NULL; p = p->next)
  156. {
  157. if (p->current_ptr == NULL)
  158. {
  159. if (b > (char *) p && b < (char *) p + CHUNK_SIZE)
  160. break;
  161. small = p;
  162. }
  163. else
  164. {
  165. if (b == (char *) p + CHUNK_HEADER_SIZE)
  166. break;
  167. }
  168. }
  169. /* If we can't find the chunk, the caller has made a mistake. */
  170. if (p == NULL)
  171. abort ();
  172. if (p->current_ptr == NULL)
  173. {
  174. struct objalloc_chunk *q;
  175. struct objalloc_chunk *first;
  176. /* The block is in a chunk containing small objects. We can
  177. free every chunk through SMALL, because they have certainly
  178. been allocated more recently. After SMALL, we will not see
  179. any chunks containing small objects; we can free any big
  180. chunk if the current_ptr is greater than or equal to B. We
  181. can then reset the new current_ptr to B. */
  182. first = NULL;
  183. q = (struct objalloc_chunk *) o->chunks;
  184. while (q != p)
  185. {
  186. struct objalloc_chunk *next;
  187. next = q->next;
  188. if (small != NULL)
  189. {
  190. if (small == q)
  191. small = NULL;
  192. free (q);
  193. }
  194. else if (q->current_ptr > b)
  195. free (q);
  196. else if (first == NULL)
  197. first = q;
  198. q = next;
  199. }
  200. if (first == NULL)
  201. first = p;
  202. o->chunks = (PTR) first;
  203. /* Now start allocating from this small block again. */
  204. o->current_ptr = b;
  205. o->current_space = ((char *) p + CHUNK_SIZE) - b;
  206. }
  207. else
  208. {
  209. struct objalloc_chunk *q;
  210. char *current_ptr;
  211. /* This block is in a large chunk by itself. We can free
  212. everything on the list up to and including this block. We
  213. then start allocating from the next chunk containing small
  214. objects, setting current_ptr from the value stored with the
  215. large chunk we are freeing. */
  216. current_ptr = p->current_ptr;
  217. p = p->next;
  218. q = (struct objalloc_chunk *) o->chunks;
  219. while (q != p)
  220. {
  221. struct objalloc_chunk *next;
  222. next = q->next;
  223. free (q);
  224. q = next;
  225. }
  226. o->chunks = (PTR) p;
  227. while (p->current_ptr != NULL)
  228. p = p->next;
  229. o->current_ptr = current_ptr;
  230. o->current_space = ((char *) p + CHUNK_SIZE) - current_ptr;
  231. }
  232. }