malloc.c 13 KB

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