malloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* malloc.c - C standard library routine.
  2. Copyright (c) 1989, 1993 Michael J. Haertel
  3. You may redistribute this library under the terms of the
  4. GNU Library General Public License (version 2 or any later
  5. version) as published by the Free Software Foundation.
  6. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED
  7. WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR
  8. WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  9. SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */
  10. #define _GNU_SOURCE
  11. #include <features.h>
  12. #include <limits.h>
  13. #include <stddef.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include "malloc.h"
  19. #ifdef __UCLIBC_HAS_THREADS__
  20. #include <pthread.h>
  21. pthread_mutex_t __malloclock = PTHREAD_MUTEX_INITIALIZER;
  22. # define LOCK pthread_mutex_lock(&__malloclock)
  23. # define UNLOCK pthread_mutex_unlock(&__malloclock);
  24. #else
  25. # define LOCK
  26. # define UNLOCK
  27. #endif
  28. /* Stuff that is shared across .o files */
  29. /* Pointer to the base of the first block. */
  30. char *_heapbase;
  31. /* Block information table. */
  32. union info *_heapinfo;
  33. /* Search index in the info table. */
  34. size_t _heapindex;
  35. /* Limit of valid info table indices. */
  36. size_t _heaplimit;
  37. /* List of blocks allocated with memalign or valloc */
  38. struct alignlist *_aligned_blocks;
  39. /* Stuff that is local to this .o file only */
  40. /* How to really get more memory. */
  41. static void * __morecore(long size);
  42. /* Number of info entries. */
  43. static size_t heapsize;
  44. /* Count of large blocks allocated for each fragment size. */
  45. static size_t _fragblocks[BLOCKLOG];
  46. /* Free lists for each fragment size. */
  47. static struct list _fraghead[BLOCKLOG];
  48. /* Are we experienced? */
  49. static int initialized;
  50. /* Aligned allocation.
  51. * Called within the lock in initialize() and morecore(),
  52. * so no explicit locking needed... */
  53. static void * align(size_t size)
  54. {
  55. void *result;
  56. unsigned int adj;
  57. result = __morecore(size);
  58. adj = (unsigned int) ((char *) result - (char *) NULL) % BLOCKSIZE;
  59. if (adj != 0) {
  60. __morecore(adj = BLOCKSIZE - adj);
  61. result = (char *) result + adj;
  62. }
  63. return result;
  64. }
  65. /* Set everything up and remember that we have.
  66. * Called within the lock in malloc(), so no
  67. * explicit locking needed... */
  68. static int initialize(void)
  69. {
  70. heapsize = HEAP / BLOCKSIZE;
  71. _heapinfo = align(heapsize * sizeof (union info));
  72. if (!_heapinfo) {
  73. return 0;
  74. }
  75. memset(_heapinfo, 0, heapsize * sizeof (union info));
  76. _heapinfo[0].free.size = 0;
  77. _heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
  78. _heapindex = 0;
  79. _heapbase = (char *) _heapinfo;
  80. initialized = 1;
  81. return 1;
  82. }
  83. /* Get neatly aligned memory, initializing or growing the
  84. * heap info table as necessary.
  85. * Called within a lock in malloc() and free(),
  86. * so no explicit locking needed... */
  87. static void * morecore(size_t size)
  88. {
  89. void *result;
  90. union info *newinfo, *oldinfo;
  91. size_t newsize;
  92. result = align(size);
  93. if (!result)
  94. return NULL;
  95. /* Check if we need to grow the info table. */
  96. if (BLOCK((char *) result + size) > heapsize) {
  97. newsize = heapsize;
  98. while (BLOCK((char *) result + size) > newsize)
  99. newsize *= 2;
  100. newinfo = align(newsize * sizeof (union info));
  101. if (!newinfo) {
  102. __morecore(-size);
  103. return NULL;
  104. }
  105. memset(newinfo, 0, newsize * sizeof (union info));
  106. memcpy(newinfo, _heapinfo, heapsize * sizeof (union info));
  107. oldinfo = _heapinfo;
  108. newinfo[BLOCK(oldinfo)].busy.type = 0;
  109. newinfo[BLOCK(oldinfo)].busy.info.size
  110. = BLOCKIFY(heapsize * sizeof (union info));
  111. _heapinfo = newinfo;
  112. __free_unlocked(oldinfo);
  113. heapsize = newsize;
  114. }
  115. _heaplimit = BLOCK((char *) result + size);
  116. return result;
  117. }
  118. /* Note that morecore has to take a signed argument so
  119. that negative values can return memory to the system. */
  120. static void * __morecore(long size)
  121. {
  122. void *result;
  123. result = sbrk(size);
  124. if (result == (void *) -1)
  125. return NULL;
  126. return result;
  127. }
  128. /* Allocate memory from the heap. */
  129. void * malloc (size_t size)
  130. {
  131. void * ptr;
  132. LOCK;
  133. ptr = __malloc_unlocked(size);
  134. UNLOCK;
  135. return(ptr);
  136. }
  137. void * __malloc_unlocked (size_t size)
  138. {
  139. void *result;
  140. size_t log, block, blocks, i, lastblocks, start;
  141. struct list *next;
  142. #if defined(__MALLOC_GLIBC_COMPAT__)
  143. if (unlikely(size == 0))
  144. size++;
  145. #else
  146. /* Some programs will call malloc (0). Lets be strict and return NULL */
  147. if (unlikely(size == 0))
  148. return 0;
  149. #endif
  150. /* Check if they are doing something dumb like malloc(-1) */
  151. if (unlikely(((unsigned long)size > (unsigned long)(sizeof (struct list)*-2))))
  152. goto oom;
  153. if (unlikely(size < sizeof (struct list)))
  154. size = sizeof (struct list);
  155. if (!initialized && !initialize()) {
  156. goto oom;
  157. }
  158. /* Determine the allocation policy based on the request size. */
  159. if (size <= BLOCKSIZE / 2) {
  160. /* Small allocation to receive a fragment of a block. Determine
  161. the logarithm to base two of the fragment size. */
  162. --size;
  163. for (log = 1; (size >>= 1) != 0; ++log)
  164. ;
  165. /* Look in the fragment lists for a free fragment of the
  166. desired size. */
  167. if ((next = _fraghead[log].next) != 0) {
  168. /* There are free fragments of this size. Pop a fragment
  169. out of the fragment list and return it. Update the block's
  170. nfree and first counters. */
  171. result = next;
  172. next->prev->next = next->next;
  173. if (next->next)
  174. next->next->prev = next->prev;
  175. block = BLOCK(result);
  176. if (--_heapinfo[block].busy.info.frag.nfree)
  177. _heapinfo[block].busy.info.frag.first
  178. = (unsigned int) ((char *) next->next - (char *) NULL)
  179. % BLOCKSIZE >> log;
  180. } else {
  181. /* No free fragments of the desired size, so get a new block
  182. and break it into fragments, returning the first. */
  183. result = __malloc_unlocked(BLOCKSIZE);
  184. if (!result) {
  185. goto oom;
  186. }
  187. ++_fragblocks[log];
  188. /* Link all fragments but the first into the free list. */
  189. next = (struct list *) ((char *) result + (1 << log));
  190. next->next = 0;
  191. next->prev = &_fraghead[log];
  192. _fraghead[log].next = next;
  193. for (i = 2; i < BLOCKSIZE >> log; ++i) {
  194. next = (struct list *) ((char *) result + (i << log));
  195. next->next = _fraghead[log].next;
  196. next->prev = &_fraghead[log];
  197. next->prev->next = next;
  198. next->next->prev = next;
  199. }
  200. /* Initialize the nfree and first counters for this block. */
  201. block = BLOCK(result);
  202. _heapinfo[block].busy.type = log;
  203. _heapinfo[block].busy.info.frag.nfree = i - 1;
  204. _heapinfo[block].busy.info.frag.first = i - 1;
  205. }
  206. } else {
  207. /* Large allocation to receive one or more blocks. Search
  208. the free list in a circle starting at the last place visited.
  209. If we loop completely around without finding a large enough
  210. space we will have to get more memory from the system. */
  211. blocks = BLOCKIFY(size);
  212. start = block = _heapindex;
  213. while (_heapinfo[block].free.size < blocks) {
  214. block = _heapinfo[block].free.next;
  215. if (block == start) {
  216. /* Need to get more from the system. Check to see if
  217. the new core will be contiguous with the final free
  218. block; if so we don't need to get as much. */
  219. block = _heapinfo[0].free.prev;
  220. lastblocks = _heapinfo[block].free.size;
  221. if (_heaplimit && block + lastblocks == _heaplimit
  222. && __morecore(0) == ADDRESS(block + lastblocks)
  223. && morecore((blocks - lastblocks) * BLOCKSIZE)) {
  224. /* Note that morecore() can change the location of
  225. the final block if it moves the info table and the
  226. old one gets coalesced into the final block. */
  227. block = _heapinfo[0].free.prev;
  228. _heapinfo[block].free.size += blocks - lastblocks;
  229. continue;
  230. }
  231. result = morecore(blocks * BLOCKSIZE);
  232. if (!result) {
  233. goto oom;
  234. }
  235. block = BLOCK(result);
  236. _heapinfo[block].busy.type = 0;
  237. _heapinfo[block].busy.info.size = blocks;
  238. return result;
  239. }
  240. }
  241. /* At this point we have found a suitable free list entry.
  242. Figure out how to remove what we need from the list. */
  243. result = ADDRESS(block);
  244. if (_heapinfo[block].free.size > blocks) {
  245. /* The block we found has a bit left over, so relink the
  246. tail end back into the free list. */
  247. _heapinfo[block + blocks].free.size
  248. = _heapinfo[block].free.size - blocks;
  249. _heapinfo[block + blocks].free.next
  250. = _heapinfo[block].free.next;
  251. _heapinfo[block + blocks].free.prev
  252. = _heapinfo[block].free.prev;
  253. _heapinfo[_heapinfo[block].free.prev].free.next
  254. = _heapinfo[_heapinfo[block].free.next].free.prev
  255. = _heapindex = block + blocks;
  256. } else {
  257. /* The block exactly matches our requirements, so
  258. just remove it from the list. */
  259. _heapinfo[_heapinfo[block].free.next].free.prev
  260. = _heapinfo[block].free.prev;
  261. _heapinfo[_heapinfo[block].free.prev].free.next
  262. = _heapindex = _heapinfo[block].free.next;
  263. }
  264. _heapinfo[block].busy.type = 0;
  265. _heapinfo[block].busy.info.size = blocks;
  266. }
  267. return result;
  268. oom:
  269. __set_errno(ENOMEM);
  270. return NULL;
  271. }
  272. /* Return memory to the heap. */
  273. void free(void *ptr)
  274. {
  275. struct alignlist *l;
  276. if (ptr == NULL)
  277. return;
  278. LOCK;
  279. for (l = _aligned_blocks; l != NULL; l = l->next) {
  280. if (l->aligned == ptr) {
  281. /* Mark the block as free */
  282. l->aligned = NULL;
  283. ptr = l->exact;
  284. break;
  285. }
  286. }
  287. __free_unlocked(ptr);
  288. UNLOCK;
  289. }
  290. void __free_unlocked(void *ptr)
  291. {
  292. int block, blocks, i, type;
  293. struct list *prev, *next;
  294. if (ptr == NULL)
  295. return;
  296. block = BLOCK(ptr);
  297. switch (type = _heapinfo[block].busy.type) {
  298. case 0:
  299. /* Find the free cluster previous to this one in the free list.
  300. Start searching at the last block referenced; this may benefit
  301. programs with locality of allocation. */
  302. i = _heapindex;
  303. if (i > block)
  304. while (i > block)
  305. i = _heapinfo[i].free.prev;
  306. else {
  307. do
  308. i = _heapinfo[i].free.next;
  309. while (i > 0 && i < block);
  310. i = _heapinfo[i].free.prev;
  311. }
  312. /* Determine how to link this block into the free list. */
  313. if (block == i + _heapinfo[i].free.size) {
  314. /* Coalesce this block with its predecessor. */
  315. _heapinfo[i].free.size += _heapinfo[block].busy.info.size;
  316. block = i;
  317. } else {
  318. /* Really link this block back into the free list. */
  319. _heapinfo[block].free.size = _heapinfo[block].busy.info.size;
  320. _heapinfo[block].free.next = _heapinfo[i].free.next;
  321. _heapinfo[block].free.prev = i;
  322. _heapinfo[i].free.next = block;
  323. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  324. }
  325. /* Now that the block is linked in, see if we can coalesce it
  326. with its successor (by deleting its successor from the list
  327. and adding in its size). */
  328. if (block + _heapinfo[block].free.size == _heapinfo[block].free.next) {
  329. _heapinfo[block].free.size
  330. += _heapinfo[_heapinfo[block].free.next].free.size;
  331. _heapinfo[block].free.next
  332. = _heapinfo[_heapinfo[block].free.next].free.next;
  333. _heapinfo[_heapinfo[block].free.next].free.prev = block;
  334. }
  335. /* Now see if we can return stuff to the system. */
  336. blocks = _heapinfo[block].free.size;
  337. if (blocks >= FINAL_FREE_BLOCKS && block + blocks == _heaplimit
  338. && __morecore(0) == ADDRESS(block + blocks)) {
  339. _heaplimit -= blocks;
  340. __morecore(-blocks * BLOCKSIZE);
  341. _heapinfo[_heapinfo[block].free.prev].free.next
  342. = _heapinfo[block].free.next;
  343. _heapinfo[_heapinfo[block].free.next].free.prev
  344. = _heapinfo[block].free.prev;
  345. block = _heapinfo[block].free.prev;
  346. }
  347. /* Set the next search to begin at this block. */
  348. _heapindex = block;
  349. break;
  350. default:
  351. /* Get the address of the first free fragment in this block. */
  352. prev = (struct list *) ((char *) ADDRESS(block)
  353. + (_heapinfo[block].busy.info.frag.first
  354. << type));
  355. if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1
  356. && _fragblocks[type] > 1) {
  357. /* If all fragments of this block are free, remove them
  358. from the fragment list and free the whole block. */
  359. --_fragblocks[type];
  360. for (next = prev, i = 1; i < BLOCKSIZE >> type; ++i)
  361. next = next->next;
  362. prev->prev->next = next;
  363. if (next)
  364. next->prev = prev->prev;
  365. _heapinfo[block].busy.type = 0;
  366. _heapinfo[block].busy.info.size = 1;
  367. __free_unlocked(ADDRESS(block));
  368. } else if (_heapinfo[block].busy.info.frag.nfree) {
  369. /* If some fragments of this block are free, link this fragment
  370. into the fragment list after the first free fragment of
  371. this block. */
  372. next = ptr;
  373. next->next = prev->next;
  374. next->prev = prev;
  375. prev->next = next;
  376. if (next->next)
  377. next->next->prev = next;
  378. ++_heapinfo[block].busy.info.frag.nfree;
  379. } else {
  380. /* No fragments of this block are free, so link this fragment
  381. into the fragment list and announce that it is the first
  382. free fragment of this block. */
  383. prev = (struct list *) ptr;
  384. _heapinfo[block].busy.info.frag.nfree = 1;
  385. _heapinfo[block].busy.info.frag.first
  386. = (unsigned int) ((char *) ptr - (char *) NULL) % BLOCKSIZE
  387. >> type;
  388. prev->next = _fraghead[type].next;
  389. prev->prev = &_fraghead[type];
  390. prev->prev->next = prev;
  391. if (prev->next)
  392. prev->next->prev = prev;
  393. }
  394. break;
  395. }
  396. }