malloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* Memory allocator `malloc'.
  2. Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  3. Written May 1989 by Mike Haertel.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This library 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 GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA.
  16. The author may be reached (Email) at the address mike@ai.mit.edu,
  17. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  18. #ifndef _MALLOC_INTERNAL
  19. #define _MALLOC_INTERNAL
  20. #include <malloc.h>
  21. #endif
  22. /* How to really get more memory. */
  23. __ptr_t (*__morecore) __P ((ptrdiff_t __size)) = __default_morecore;
  24. /* Debugging hook for `malloc'. */
  25. __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
  26. /* Pointer to the base of the first block. */
  27. char *_heapbase;
  28. /* Block information table. Allocated with align/__free (not malloc/free). */
  29. malloc_info *_heapinfo;
  30. /* Number of info entries. */
  31. static __malloc_size_t heapsize;
  32. /* Search index in the info table. */
  33. __malloc_size_t _heapindex;
  34. /* Limit of valid info table indices. */
  35. __malloc_size_t _heaplimit;
  36. /* Free lists for each fragment size. */
  37. struct list _fraghead[BLOCKLOG];
  38. /* Instrumentation. */
  39. __malloc_size_t _chunks_used;
  40. __malloc_size_t _bytes_used;
  41. __malloc_size_t _chunks_free;
  42. __malloc_size_t _bytes_free;
  43. /* Are you experienced? */
  44. int __malloc_initialized;
  45. void (*__malloc_initialize_hook) __P ((void));
  46. void (*__after_morecore_hook) __P ((void));
  47. /* Aligned allocation. */
  48. static __ptr_t align __P ((__malloc_size_t));
  49. static __ptr_t
  50. align (size)
  51. __malloc_size_t size;
  52. {
  53. __ptr_t result;
  54. unsigned long int adj;
  55. result = (*__morecore) (size);
  56. adj = (unsigned long int) ((unsigned long int) ((char *) result -
  57. (char *) NULL)) % BLOCKSIZE;
  58. if (adj != 0)
  59. {
  60. adj = BLOCKSIZE - adj;
  61. (void) (*__morecore) (adj);
  62. result = (char *) result + adj;
  63. }
  64. if (__after_morecore_hook)
  65. (*__after_morecore_hook) ();
  66. return result;
  67. }
  68. /* Set everything up and remember that we have. */
  69. static int initialize __P ((void));
  70. static int
  71. initialize ()
  72. {
  73. if (__malloc_initialize_hook)
  74. (*__malloc_initialize_hook) ();
  75. heapsize = HEAP / BLOCKSIZE;
  76. _heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
  77. if (_heapinfo == NULL)
  78. return 0;
  79. memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
  80. _heapinfo[0].free.size = 0;
  81. _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  82. _heapindex = 0;
  83. _heapbase = (char *) _heapinfo;
  84. /* Account for the _heapinfo block itself in the statistics. */
  85. _bytes_used = heapsize * sizeof (malloc_info);
  86. _chunks_used = 1;
  87. __malloc_initialized = 1;
  88. return 1;
  89. }
  90. /* Get neatly aligned memory, initializing or
  91. growing the heap info table as necessary. */
  92. static __ptr_t morecore __P ((__malloc_size_t));
  93. static __ptr_t
  94. morecore (size)
  95. __malloc_size_t size;
  96. {
  97. __ptr_t result;
  98. malloc_info *newinfo, *oldinfo;
  99. __malloc_size_t newsize;
  100. result = align (size);
  101. if (result == NULL)
  102. return NULL;
  103. /* Check if we need to grow the info table. */
  104. if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
  105. {
  106. newsize = heapsize;
  107. while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize)
  108. newsize *= 2;
  109. newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
  110. if (newinfo == NULL)
  111. {
  112. (*__morecore) (-size);
  113. return NULL;
  114. }
  115. memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
  116. memset (&newinfo[heapsize], 0,
  117. (newsize - heapsize) * sizeof (malloc_info));
  118. oldinfo = _heapinfo;
  119. newinfo[BLOCK (oldinfo)].busy.type = 0;
  120. newinfo[BLOCK (oldinfo)].busy.info.size
  121. = BLOCKIFY (heapsize * sizeof (malloc_info));
  122. _heapinfo = newinfo;
  123. /* Account for the _heapinfo block itself in the statistics. */
  124. _bytes_used += newsize * sizeof (malloc_info);
  125. ++_chunks_used;
  126. _free_internal (oldinfo);
  127. heapsize = newsize;
  128. }
  129. _heaplimit = BLOCK ((char *) result + size);
  130. return result;
  131. }
  132. /* Allocate memory from the heap. */
  133. __ptr_t
  134. malloc (size)
  135. __malloc_size_t size;
  136. {
  137. __ptr_t result;
  138. __malloc_size_t block, blocks, lastblocks, start;
  139. register __malloc_size_t i;
  140. struct list *next;
  141. /* ANSI C allows `malloc (0)' to either return NULL, or to return a
  142. valid address you can realloc and free (though not dereference).
  143. It turns out that some extant code (sunrpc, at least Ultrix's version)
  144. expects `malloc (0)' to return non-NULL and breaks otherwise.
  145. Be compatible. */
  146. #if 0
  147. if (size == 0)
  148. return NULL;
  149. #endif
  150. if (__malloc_hook != NULL)
  151. return (*__malloc_hook) (size);
  152. if (!__malloc_initialized)
  153. if (!initialize ())
  154. return NULL;
  155. if (size < sizeof (struct list))
  156. size = sizeof (struct list);
  157. #ifdef SUNOS_LOCALTIME_BUG
  158. if (size < 16)
  159. size = 16;
  160. #endif
  161. /* Determine the allocation policy based on the request size. */
  162. if (size <= BLOCKSIZE / 2)
  163. {
  164. /* Small allocation to receive a fragment of a block.
  165. Determine the logarithm to base two of the fragment size. */
  166. register __malloc_size_t log = 1;
  167. --size;
  168. while ((size /= 2) != 0)
  169. ++log;
  170. /* Look in the fragment lists for a
  171. free fragment of the desired size. */
  172. next = _fraghead[log].next;
  173. if (next != NULL)
  174. {
  175. /* There are free fragments of this size.
  176. Pop a fragment out of the fragment list and return it.
  177. Update the block's nfree and first counters. */
  178. result = (__ptr_t) next;
  179. next->prev->next = next->next;
  180. if (next->next != NULL)
  181. next->next->prev = next->prev;
  182. block = BLOCK (result);
  183. if (--_heapinfo[block].busy.info.frag.nfree != 0)
  184. _heapinfo[block].busy.info.frag.first = (unsigned long int)
  185. ((unsigned long int) ((char *) next->next - (char *) NULL)
  186. % BLOCKSIZE) >> log;
  187. /* Update the statistics. */
  188. ++_chunks_used;
  189. _bytes_used += 1 << log;
  190. --_chunks_free;
  191. _bytes_free -= 1 << log;
  192. }
  193. else
  194. {
  195. /* No free fragments of the desired size, so get a new block
  196. and break it into fragments, returning the first. */
  197. result = malloc (BLOCKSIZE);
  198. if (result == NULL)
  199. return NULL;
  200. /* Link all fragments but the first into the free list. */
  201. for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
  202. {
  203. next = (struct list *) ((char *) result + (i << log));
  204. next->next = _fraghead[log].next;
  205. next->prev = &_fraghead[log];
  206. next->prev->next = next;
  207. if (next->next != NULL)
  208. next->next->prev = next;
  209. }
  210. /* Initialize the nfree and first counters for this block. */
  211. block = BLOCK (result);
  212. _heapinfo[block].busy.type = log;
  213. _heapinfo[block].busy.info.frag.nfree = i - 1;
  214. _heapinfo[block].busy.info.frag.first = i - 1;
  215. _chunks_free += (BLOCKSIZE >> log) - 1;
  216. _bytes_free += BLOCKSIZE - (1 << log);
  217. _bytes_used -= BLOCKSIZE - (1 << log);
  218. }
  219. }
  220. else
  221. {
  222. /* Large allocation to receive one or more blocks.
  223. Search the free list in a circle starting at the last place visited.
  224. If we loop completely around without finding a large enough
  225. space we will have to get more memory from the system. */
  226. blocks = BLOCKIFY (size);
  227. start = block = _heapindex;
  228. while (_heapinfo[block].free.size < blocks)
  229. {
  230. block = _heapinfo[block].free.next;
  231. if (block == start)
  232. {
  233. /* Need to get more from the system. Check to see if
  234. the new core will be contiguous with the final free
  235. block; if so we don't need to get as much. */
  236. block = _heapinfo[0].free.prev;
  237. lastblocks = _heapinfo[block].free.size;
  238. if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
  239. (*__morecore) (0) == ADDRESS (block + lastblocks) &&
  240. (morecore ((blocks - lastblocks) * BLOCKSIZE)) != NULL)
  241. {
  242. /* Which block we are extending (the `final free
  243. block' referred to above) might have changed, if
  244. it got combined with a freed info table. */
  245. block = _heapinfo[0].free.prev;
  246. _heapinfo[block].free.size += (blocks - lastblocks);
  247. _bytes_free += (blocks - lastblocks) * BLOCKSIZE;
  248. continue;
  249. }
  250. result = morecore (blocks * BLOCKSIZE);
  251. if (result == NULL)
  252. return NULL;
  253. block = BLOCK (result);
  254. _heapinfo[block].busy.type = 0;
  255. _heapinfo[block].busy.info.size = blocks;
  256. ++_chunks_used;
  257. _bytes_used += blocks * BLOCKSIZE;
  258. return result;
  259. }
  260. }
  261. /* At this point we have found a suitable free list entry.
  262. Figure out how to remove what we need from the list. */
  263. result = ADDRESS (block);
  264. if (_heapinfo[block].free.size > blocks)
  265. {
  266. /* The block we found has a bit left over,
  267. so relink the tail end back into the free list. */
  268. _heapinfo[block + blocks].free.size
  269. = _heapinfo[block].free.size - blocks;
  270. _heapinfo[block + blocks].free.next
  271. = _heapinfo[block].free.next;
  272. _heapinfo[block + blocks].free.prev
  273. = _heapinfo[block].free.prev;
  274. _heapinfo[_heapinfo[block].free.prev].free.next
  275. = _heapinfo[_heapinfo[block].free.next].free.prev
  276. = _heapindex = block + blocks;
  277. }
  278. else
  279. {
  280. /* The block exactly matches our requirements,
  281. so just remove it from the list. */
  282. _heapinfo[_heapinfo[block].free.next].free.prev
  283. = _heapinfo[block].free.prev;
  284. _heapinfo[_heapinfo[block].free.prev].free.next
  285. = _heapindex = _heapinfo[block].free.next;
  286. --_chunks_free;
  287. }
  288. _heapinfo[block].busy.type = 0;
  289. _heapinfo[block].busy.info.size = blocks;
  290. ++_chunks_used;
  291. _bytes_used += blocks * BLOCKSIZE;
  292. _bytes_free -= blocks * BLOCKSIZE;
  293. /* Mark all the blocks of the object just allocated except for the
  294. first with a negative number so you can find the first block by
  295. adding that adjustment. */
  296. while (--blocks > 0)
  297. _heapinfo[block + blocks].busy.info.size = -blocks;
  298. }
  299. return result;
  300. }
  301. #ifndef _LIBC
  302. /* On some ANSI C systems, some libc functions call _malloc, _free
  303. and _realloc. Make them use the GNU functions. */
  304. __ptr_t
  305. _malloc (size)
  306. __malloc_size_t size;
  307. {
  308. return malloc (size);
  309. }
  310. void
  311. _free (ptr)
  312. __ptr_t ptr;
  313. {
  314. free (ptr);
  315. }
  316. __ptr_t
  317. _realloc (ptr, size)
  318. __ptr_t ptr;
  319. __malloc_size_t size;
  320. {
  321. return realloc (ptr, size);
  322. }
  323. #endif