malloc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. malloc - heap manager based on heavy use of virtual memory management.
  3. Copyright (C) 1998 Valery Shchedrin
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the 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; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA
  16. Public Functions:
  17. void *malloc(size_t size);
  18. Allocates `size` bytes
  19. returns NULL if no free memory available
  20. void *calloc(size_t unit, size_t quantity);
  21. Allocates `quantity*unit` zeroed bytes via internal malloc call
  22. void *realloc(void *ptr, size_t size);
  23. Reallocates already allocated block `ptr`, if `ptr` is not valid block
  24. then it works as malloc. NULL is returned if no free memory available
  25. void *_realloc_no_move(void *ptr, size_t size);
  26. Reallocates already allocated block `ptr`, if `ptr` is not valid block
  27. or if reallocation can't be done with shrinking/expanding already
  28. allocated block NULL is returned
  29. void free(void *ptr);
  30. Frees already allocated block, if `ptr` is incorrect one nothing will
  31. happen.
  32. */
  33. /*
  34. * Manuel Novoa III Jan 2001
  35. *
  36. * Modified to decrease object sizes.
  37. * Broke into independent object files.
  38. * Converted INIT_BLOCK() and FREE_MEM_DEL_BLOCK() from macros to functions.
  39. */
  40. #define _POSIX_SOURCE
  41. #define _XOPEN_SOURCE
  42. #include <features.h>
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #include <limits.h>
  46. #include <sys/time.h>
  47. #include <asm/page.h>
  48. #include <unistd.h>
  49. #include <sys/mman.h>
  50. #include <string.h>
  51. #include "malloc.h"
  52. #include <stdio.h>
  53. #define M_DOTRIMMING 1
  54. #define M_MULTITHREADED 0
  55. #define VALLOC_MSTART ((void*)0x1c000000)
  56. #define LARGE_MSTART ((void*)0x19000000)
  57. #define HUNK_MSTART ((void*)0x18000000)
  58. #define HUNK_MSIZE M_PAGESIZE
  59. #define HUNK_ID 0x99171713
  60. /* alignment of allocations > HUNK_THRESHOLD */
  61. #define MALLOC_ALIGN 4
  62. /* allocations < HUNK_THRESHOLD will not be aligned */
  63. #define HUNK_THRESHOLD 4
  64. /*up to HUNK_MAXSIZE blocks will be joined together to decrease memory waste*/
  65. #define HUNK_MAXSIZE 128
  66. /* returns value not less than size, aligned to MALLOC_ALIGN */
  67. #define ALIGN(size) (((size)+(MALLOC_ALIGN)-1)&(~((MALLOC_ALIGN)-1)))
  68. /* aligns s or p to page boundaries */
  69. #define PAGE_ALIGN(s) (((s)+M_PAGESIZE-1)&(~(M_PAGESIZE-1)))
  70. #define PAGE_ALIGNP(p) ((char*)PAGE_ALIGN((unsigned)(p)))
  71. #define PAGE_DOWNALIGNP(p) ((char*)(((unsigned)(p))&(~(M_PAGESIZE-1))))
  72. /* returns v * 2 for your machine (speed-up) */
  73. #define MUL2(v) ((v)*2)
  74. /* does v *= 8 for your machine (speed-up) */
  75. #define EMUL8(v) v*=8
  76. /* does v/8 for your machind (speed-up) */
  77. #define DIV8(v) ((v)/8)
  78. #if M_MULTITHREADED
  79. #error This version does not support threads
  80. #else
  81. typedef int mutex_t;
  82. #define mutex_lock(x)
  83. #define mutex_unlock(x)
  84. #define mutex_init(x)
  85. #define MUTEX_INITIALIZER 0
  86. //static mutex_t malloc_lock = MUTEX_INITIALIZER;
  87. #endif
  88. extern int __malloc_initialized;
  89. #ifdef L__malloc_init
  90. int __malloc_initialized = -1;
  91. /* -1 == uninitialized, 0 == initializing, 1 == initialized */
  92. #endif
  93. #ifndef MAP_FAILED
  94. #define MAP_FAILED ((void*)-1)
  95. #endif
  96. #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
  97. #define MAP_ANON MAP_ANONYMOUS
  98. #endif
  99. #ifndef NULL
  100. #define NULL ((void*)0)
  101. #endif
  102. /* guess pagesize */
  103. #define M_PAGESIZE getpagesize()
  104. /* HUNK MANAGER */
  105. typedef struct Hunk_s Hunk_t;
  106. struct Hunk_s { /* Hunked block - 8 byte overhead */
  107. int id; /* unique id */
  108. unsigned int total:12, used:12, size:8;
  109. Hunk_t *next; /* next free in __free_h */
  110. };
  111. #define usagemap(h) (((unsigned char *)(h))+sizeof(Hunk_t))
  112. #define hunk_ptr(h) (((char*)(h))+sizeof(Hunk_t)+ALIGN(DIV8(h->total+7)))
  113. #define hunk(h) ((Hunk_t*)(h))
  114. extern Hunk_t *__free_h[HUNK_MAXSIZE + 1];
  115. extern int __total_h[HUNK_MAXSIZE + 1];
  116. #ifdef L__malloc_init
  117. Hunk_t *__free_h[HUNK_MAXSIZE + 1]; /* free hash */
  118. int __total_h[HUNK_MAXSIZE + 1]; /* Hunk_t's `total` member */
  119. #endif
  120. extern void *__hunk_alloc(int size);
  121. #ifdef L_malloc
  122. /* __hunk_alloc allocates <= HUNK_MAXSIZE blocks */
  123. void *__hunk_alloc(int size)
  124. {
  125. Hunk_t *p;
  126. unsigned long *cpl;
  127. int i, c;
  128. // if (size >= HUNK_THRESHOLD)
  129. size = ALIGN(size);
  130. /* Look for already allocated hunkblocks */
  131. if ((p = __free_h[size]) == NULL) {
  132. if (
  133. (p =
  134. (Hunk_t *) mmap(HUNK_MSTART, HUNK_MSIZE,
  135. PROT_READ | PROT_WRITE,
  136. #ifdef __UCLIBC_HAS_MMU__
  137. MAP_PRIVATE | MAP_ANONYMOUS
  138. #else
  139. MAP_SHARED | MAP_ANONYMOUS
  140. #endif
  141. , 0, 0)) == (Hunk_t *) MAP_FAILED)
  142. // {
  143. // printf("hunk_alloc failed: %d, %d\n", size, errno);
  144. return NULL;
  145. // }
  146. memset(p, 0, HUNK_MSIZE);
  147. p->id = HUNK_ID;
  148. p->total = __total_h[size];
  149. /* p->used = 0; */
  150. p->size = size;
  151. /* p->next = (Hunk_t*)NULL; */
  152. /* memset(usagemap(p), 0, bound); */
  153. __free_h[size] = p;
  154. }
  155. /* Locate free point in usagemap */
  156. /* First find a word where not all the bits are set */
  157. for (cpl = (unsigned long *) usagemap(p); *cpl == 0xFFFFFFFF; cpl++);
  158. /* Remember the byte position of that word */
  159. i = ((unsigned char *) cpl) - usagemap(p);
  160. /* Now find find a free bit in the word using binary search */
  161. if (*(unsigned short *) cpl != 0xFFFF) {
  162. if (*(unsigned char *) cpl == 0xFF) {
  163. c = *(((unsigned char *) cpl) + 1);
  164. i++;
  165. }
  166. else
  167. {
  168. c = *(unsigned char *) cpl;
  169. }
  170. } else {
  171. i += 2;
  172. c = *(((unsigned char *) cpl) + 2);
  173. if (c == 0xFF) {
  174. c = *(((unsigned char *) cpl) + 3);
  175. i++;
  176. }
  177. }
  178. /*
  179. * Multiply i by 8 for the bit position
  180. * Further down, we divide by 8 again to find the byte position
  181. */
  182. EMUL8(i);
  183. /* If bottom nibble is set, shift down the top nibble */
  184. if ((c & 0xF) == 0xF) {
  185. c >>= 4;
  186. i += 4;
  187. }
  188. /* If bottom 2 bits are set, shift down the top two */
  189. if ((c & 0x3) == 0x3) {
  190. c >>= 2;
  191. i += 2;
  192. }
  193. /* Check which one of the two bits is set */
  194. if (c & 1)
  195. i++;
  196. usagemap(p)[DIV8(i)] |= (1 << (i & 7)); /* set bit */
  197. /* Increment counter and update hashes */
  198. if (++p->used == p->total) {
  199. __free_h[p->size] = p->next;
  200. p->next = NULL;
  201. }
  202. // fprintf(stderr, "hunk_alloc: i=%d, p->size=%d, p=%p\n", i, p->size, p);
  203. return hunk_ptr(p) + i * p->size;
  204. }
  205. #endif /* L_malloc */
  206. extern void __hunk_free(char *ptr);
  207. #ifdef L__free_support
  208. /* __hunk_free frees blocks allocated by __hunk_alloc */
  209. void __hunk_free(char *ptr)
  210. {
  211. unsigned char *up;
  212. int i, v;
  213. Hunk_t *h;
  214. if (!ptr)
  215. return;
  216. h = (Hunk_t *) PAGE_DOWNALIGNP(ptr);
  217. /* Validate `ptr` */
  218. if (h->id != HUNK_ID)
  219. return;
  220. v = ptr - hunk_ptr(h);
  221. i = v / h->size;
  222. if (v % h->size != 0 || i < 0 || i >= h->total)
  223. return;
  224. /* Update `usagemap` */
  225. up = &(usagemap(h)[DIV8(i)]);
  226. i = 1 << (i & 7);
  227. if (!(*up & i))
  228. return;
  229. *up ^= i;
  230. /* Update hunk counters */
  231. if (h->used == h->total) {
  232. if (--h->used) { /* insert into __free_h */
  233. h->next = __free_h[h->size];
  234. __free_h[h->size] = h;
  235. } /* else - it will be unmapped */
  236. } else {
  237. if (!--h->used) { /* delete from __free_h - will be __bl_freed */
  238. Hunk_t *p, *pp;
  239. for (p = __free_h[h->size], pp = NULL; p != h;
  240. pp = p, p = p->next);
  241. if (!pp)
  242. __free_h[h->size] = p->next;
  243. else
  244. pp->next = p->next;
  245. }
  246. }
  247. /* Unmap empty Hunk_t */
  248. if (!h->used)
  249. munmap((void *) h, HUNK_MSIZE);
  250. }
  251. #endif /* L__free_support */
  252. /* BLOCK MANAGER */
  253. typedef struct Block_s Block_t;
  254. struct Block_s { /* 32-bytes long control structure (if 4-byte aligned) */
  255. char *ptr; /* pointer to related data */
  256. Block_t *next; /* next in free_mem list */
  257. Block_t *l_free_mem, *r_free_mem; /* left & right subtrees of <free_mem> */
  258. Block_t *l_ptrs, *r_ptrs; /* left & right subtrees of <ptrs> */
  259. size_t size; /* size - divided by align */
  260. /* packed 4-byte attributes */
  261. /* { */
  262. signed char bal_free_mem:8; /* balance of <free_mem> subtree */
  263. signed char bal_ptrs:8; /* balance of <ptrs> subtree */
  264. unsigned int used:1; /* used/free state of the block */
  265. unsigned int broken:1; /* 1 if previous block can't be merged with it */
  266. /* } */
  267. };
  268. extern Block_t *__bl_last; /* last mmapped block */
  269. #ifdef L__malloc_init
  270. Block_t *__bl_last; /* last mmapped block */
  271. #endif
  272. #define bl_get() __hunk_alloc(sizeof(Block_t))
  273. #define bl_rel(p) __hunk_free((char*)p)
  274. extern Block_t *__Avl_Block_tfree_mem_tree;
  275. extern Block_t *__free_mem_ins(Block_t * data);
  276. extern void __free_mem_del(Block_t * data);
  277. extern void __free_mem_replace(Block_t * data);
  278. extern Block_t *__Avl_Block_tptrs_tree;
  279. extern Block_t *__ptrs_ins(Block_t * data);
  280. extern void __ptrs_del(Block_t * data);
  281. extern void __bl_uncommit(Block_t * b);
  282. extern void __bl_free(Block_t * b);
  283. /* like C++ templates ;-) */
  284. #include "avlmacro.h"
  285. #define FREE_MEM_COMPARE(i,a,b) \
  286. { \
  287. if ( (a)->size < (b)->size ) { \
  288. i = -1; \
  289. } else if ( (a)->size > (b)->size ) { \
  290. i = 1; \
  291. } else { \
  292. i = 0; \
  293. } \
  294. }
  295. #define PTRS_COMPARE(i,a,b) \
  296. { \
  297. if ( (a)->ptr < (b)->ptr ) { \
  298. i = -1; \
  299. } else if ( (a)->ptr > (b)->ptr ) { \
  300. i = 1; \
  301. } else { \
  302. i = 0; \
  303. } \
  304. }
  305. #ifdef L__avl_support
  306. Avl_Tree(free_mem, Block_t, free_mem, FREE_MEM_COMPARE)
  307. Avl_Tree_no_replace(ptrs, Block_t, ptrs, PTRS_COMPARE)
  308. #endif
  309. #define free_mem_root Avl_Root(Block_t, free_mem)
  310. #define ptrs_root Avl_Root(Block_t, ptrs)
  311. /* pp is freed block */
  312. #define FREE_MEM_DEL_BLOCK(pp,p) {p = __free_mem_del_block(pp,p);}
  313. extern Block_t *__free_mem_del_block(Block_t * pp, Block_t * p);
  314. #ifdef L_malloc
  315. Block_t *__free_mem_del_block(Block_t * pp, Block_t * p)
  316. {
  317. for (p = free_mem_root;;)
  318. if (p->size > pp->size)
  319. p = p->l_free_mem;
  320. else if (p->size < pp->size)
  321. p = p->r_free_mem;
  322. else
  323. break;
  324. if (p == pp) {
  325. if (pp->next)
  326. __free_mem_replace(pp->next);
  327. else
  328. __free_mem_del(pp);
  329. } else {
  330. for (; p->next != pp; p = p->next);
  331. p->next = pp->next;
  332. }
  333. return p;
  334. }
  335. #endif /* L_malloc */
  336. #define FREE_MEM_INS_BLOCK(pp) \
  337. { \
  338. if ((p = __free_mem_ins(pp)) != NULL)\
  339. {\
  340. pp->next = p->next;\
  341. p->next = pp;\
  342. }\
  343. else pp->next = NULL; \
  344. }
  345. /* `b` is current block, `pp` is next block */
  346. #define COMBINE_BLOCKS(b,pp) \
  347. {\
  348. __ptrs_del(pp); \
  349. b->size += pp->size; \
  350. if (pp == __bl_last) __bl_last = b; \
  351. bl_rel(pp); \
  352. }
  353. /* initializes new block b */
  354. #define INIT_BLOCK(b, pppp, sz) { p = __init_block(b, pppp, sz); }
  355. extern Block_t *__init_block(Block_t * b, char *pppp, size_t sz);
  356. #ifdef L_malloc
  357. Block_t *__init_block(Block_t * b, char *pppp, size_t sz)
  358. {
  359. Block_t *p;
  360. memset(b, 0, sizeof(Block_t));
  361. b->ptr = pppp;
  362. b->size = sz;
  363. __ptrs_ins(b);
  364. FREE_MEM_INS_BLOCK(b);
  365. return p;
  366. }
  367. #endif /* L_malloc */
  368. /* `b` is current block, `sz` its new size */
  369. /* block `b` will be splitted to one busy & one free block */
  370. #define SPLIT_BLOCK(b,sz) \
  371. {\
  372. Block_t *bt; \
  373. bt = bl_get(); \
  374. INIT_BLOCK(bt, b->ptr + sz, b->size - sz); \
  375. b->size = sz; \
  376. if (__bl_last == b) __bl_last = bt; \
  377. __bl_uncommit(bt);\
  378. }
  379. /* `b` is current block, `pp` is next free block, `sz` is needed size */
  380. #define SHRINK_BLOCK(b,pp,sz) \
  381. {\
  382. FREE_MEM_DEL_BLOCK(pp,p); \
  383. pp->ptr = b->ptr + sz; \
  384. pp->size += b->size - sz; \
  385. b->size = sz; \
  386. FREE_MEM_INS_BLOCK(pp); \
  387. __bl_uncommit(pp); \
  388. }
  389. #ifdef L_malloc
  390. static Block_t *bl_mapnew(size_t size)
  391. {
  392. size_t map_size;
  393. Block_t *pp, *p;
  394. void *pt;
  395. map_size = PAGE_ALIGN(size);
  396. pt = mmap(LARGE_MSTART, map_size, PROT_READ | PROT_WRITE | PROT_EXEC,
  397. #ifdef __UCLIBC_HAS_MMU__
  398. MAP_PRIVATE | MAP_ANONYMOUS
  399. #else
  400. MAP_SHARED | MAP_ANONYMOUS
  401. #endif
  402. , 0, 0);
  403. if (pt == MAP_FAILED)
  404. return (Block_t *) NULL;
  405. __bl_last = pp = bl_get();
  406. INIT_BLOCK(pp, (char *) pt, map_size);
  407. pp->broken = 1;
  408. return pp;
  409. }
  410. void __bl_uncommit(Block_t * b)
  411. {
  412. char *u_start, *u_end;
  413. u_start = PAGE_ALIGNP(b->ptr);
  414. u_end = PAGE_DOWNALIGNP(b->ptr + b->size);
  415. if (u_end <= u_start)
  416. return;
  417. #if M_DOTRIMMING
  418. mmap(u_start, u_end - u_start, PROT_READ | PROT_WRITE | PROT_EXEC,
  419. #ifdef __UCLIBC_HAS_MMU__
  420. MAP_PRIVATE | MAP_ANONYMOUS |MAP_FIXED
  421. #else
  422. MAP_SHARED | MAP_ANONYMOUS |MAP_FIXED
  423. #endif
  424. , 0, 0);
  425. #endif
  426. }
  427. /* requested size must be aligned to ALIGNMENT */
  428. static Block_t *bl_alloc(size_t size)
  429. {
  430. Block_t *p, *pp;
  431. /* try to find needed space in existing memory */
  432. for (p = free_mem_root, pp = NULL; p;) {
  433. if (p->size > size) {
  434. pp = p;
  435. p = p->l_free_mem;
  436. } else if (p->size < size)
  437. p = p->r_free_mem;
  438. else {
  439. pp = p;
  440. break;
  441. }
  442. }
  443. if (!pp) { /* map some memory */
  444. if (!__bl_last) { /* just do initial mmap */
  445. pp = bl_mapnew(size);
  446. if (!pp)
  447. return NULL;
  448. } else if (!__bl_last->used) { /* try growing last unused */
  449. if (mremap(PAGE_DOWNALIGNP(__bl_last->ptr),
  450. PAGE_ALIGNP(__bl_last->ptr + __bl_last->size) -
  451. PAGE_DOWNALIGNP(__bl_last->ptr),
  452. PAGE_ALIGNP(__bl_last->ptr + size) -
  453. PAGE_DOWNALIGNP(__bl_last->ptr), 0) == MAP_FAILED) { /* unable to grow -- initiate new block */
  454. pp = bl_mapnew(size);
  455. if (!pp)
  456. return NULL;
  457. } else {
  458. pp = __bl_last;
  459. FREE_MEM_DEL_BLOCK(pp, p);
  460. pp->size = PAGE_ALIGNP(pp->ptr + size) - pp->ptr;
  461. FREE_MEM_INS_BLOCK(pp);
  462. }
  463. } else { /* __bl_last is used block */
  464. if (mremap(PAGE_DOWNALIGNP(__bl_last->ptr),
  465. PAGE_ALIGNP(__bl_last->ptr + __bl_last->size) -
  466. PAGE_DOWNALIGNP(__bl_last->ptr),
  467. PAGE_ALIGNP(__bl_last->ptr + __bl_last->size +
  468. size) - PAGE_DOWNALIGNP(__bl_last->ptr),
  469. 0) == MAP_FAILED) {
  470. pp = bl_mapnew(size);
  471. if (!pp)
  472. return NULL;
  473. } else {
  474. pp = bl_get();
  475. INIT_BLOCK(pp, __bl_last->ptr + __bl_last->size,
  476. PAGE_ALIGNP(__bl_last->ptr + __bl_last->size +
  477. size) - __bl_last->ptr -
  478. __bl_last->size);
  479. __bl_last = pp;
  480. }
  481. }
  482. }
  483. /* just delete this node from free_mem tree */
  484. if (pp->next)
  485. __free_mem_replace(pp->next);
  486. else
  487. __free_mem_del(pp);
  488. pp->used = 1;
  489. if (pp->size - size > MALLOC_ALIGN) { /* this block can be splitted (it is unused,not_broken) */
  490. SPLIT_BLOCK(pp, size);
  491. }
  492. return pp;
  493. }
  494. #endif /* L_malloc */
  495. #ifdef L__free_support
  496. void __bl_free(Block_t * b)
  497. {
  498. Block_t *p, *bl_next, *bl_prev;
  499. /* Look for blocks before & after `b` */
  500. for (p = ptrs_root, bl_next = NULL, bl_prev = NULL; p;) {
  501. if (p->ptr > b->ptr) {
  502. bl_next = p;
  503. p = p->l_ptrs;
  504. } else if (p->ptr < b->ptr) {
  505. bl_prev = p;
  506. p = p->r_ptrs;
  507. } else
  508. break;
  509. }
  510. if (b->l_ptrs)
  511. for (bl_prev = b->l_ptrs; bl_prev->r_ptrs;
  512. bl_prev = bl_prev->r_ptrs);
  513. if (b->r_ptrs)
  514. for (bl_next = b->r_ptrs; bl_next->l_ptrs;
  515. bl_next = bl_next->l_ptrs);
  516. if (bl_next && !bl_next->broken && !bl_next->used) {
  517. FREE_MEM_DEL_BLOCK(bl_next, p)
  518. COMBINE_BLOCKS(b, bl_next)
  519. }
  520. if (bl_prev && !b->broken && !bl_prev->used) {
  521. FREE_MEM_DEL_BLOCK(bl_prev, p)
  522. COMBINE_BLOCKS(bl_prev, b)
  523. b = bl_prev;
  524. }
  525. b->used = 0;
  526. FREE_MEM_INS_BLOCK(b)
  527. __bl_uncommit(b);
  528. }
  529. #endif /* L__free_support */
  530. extern void __malloc_init(void);
  531. #ifdef L__malloc_init
  532. void __malloc_init(void)
  533. {
  534. int i, mapsize, x, old_x, gcount;
  535. mapsize = M_PAGESIZE;
  536. __malloc_initialized = 0;
  537. __bl_last = NULL;
  538. free_mem_root = NULL;
  539. ptrs_root = NULL;
  540. mapsize -= sizeof(Hunk_t);
  541. for (i = 1; i <= HUNK_MAXSIZE; i++) {
  542. __free_h[i] = (Hunk_t *) NULL;
  543. for (x = mapsize / i, gcount = 0, old_x = 0; old_x != x;) {
  544. old_x = x;
  545. x = (mapsize - ALIGN(DIV8(old_x + 7))) / i;
  546. if (gcount > 1 && x * i + ALIGN(DIV8(x + 7)) <= mapsize)
  547. break;
  548. if (x * i + ALIGN(DIV8(x + 7)) > mapsize)
  549. gcount++;
  550. }
  551. __total_h[i] = x;
  552. }
  553. mutex_init(&malloc_lock);
  554. __malloc_initialized = 1;
  555. // fprintf(stderr, "malloc_init: hunk_t=%d\n", sizeof(Hunk_t));
  556. }
  557. #endif /* L__malloc_init */
  558. #ifdef L_malloc
  559. void *malloc(size_t size)
  560. {
  561. void *p;
  562. if (size == 0)
  563. return NULL;
  564. if (__malloc_initialized < 0)
  565. __malloc_init();
  566. if (__malloc_initialized)
  567. mutex_lock(&malloc_lock);
  568. if (size <= HUNK_MAXSIZE)
  569. p = __hunk_alloc(size);
  570. else {
  571. if ((p = bl_alloc(ALIGN(size))) != NULL)
  572. p = ((Block_t *) p)->ptr;
  573. }
  574. if (__malloc_initialized)
  575. mutex_unlock(&malloc_lock);
  576. // fprintf(stderr, "malloc returning: s=%d, p=%p\n", size, p);
  577. return p;
  578. }
  579. #endif /* L_malloc */
  580. #ifdef L_free
  581. void free(void *ptr)
  582. {
  583. Block_t *p, *best;
  584. if (__malloc_initialized < 0)
  585. return;
  586. if (__malloc_initialized)
  587. mutex_lock(&malloc_lock);
  588. for (p = ptrs_root, best = NULL; p;) {
  589. if (p->ptr > (char *) ptr)
  590. p = p->l_ptrs;
  591. else {
  592. best = p;
  593. p = p->r_ptrs;
  594. }
  595. }
  596. if (!best || !best->used || best->ptr != (char *) ptr) {
  597. __hunk_free(ptr);
  598. if (__malloc_initialized)
  599. mutex_unlock(&malloc_lock);
  600. return;
  601. }
  602. __bl_free(best);
  603. if (__malloc_initialized)
  604. mutex_unlock(&malloc_lock);
  605. }
  606. #endif /* L_free */
  607. extern void *_realloc_no_move(void *ptr, size_t size);
  608. #ifdef L__realloc_no_move
  609. void *_realloc_no_move(void *ptr, size_t size)
  610. {
  611. Block_t *p, *best, *next;
  612. if (size <= HUNK_MAXSIZE)
  613. return NULL;
  614. if (__malloc_initialized <= 0)
  615. return malloc(size);
  616. mutex_lock(&malloc_lock);
  617. /* Locate block */
  618. for (p = ptrs_root, best = NULL; p;) {
  619. if (p->ptr > (char *) ptr)
  620. p = p->l_ptrs;
  621. else {
  622. best = p;
  623. p = p->r_ptrs;
  624. }
  625. }
  626. if (!best || !best->used || best->ptr != (char *) ptr) {
  627. mutex_unlock(&malloc_lock);
  628. return NULL;
  629. }
  630. size = ALIGN(size);
  631. if (size == best->size) {
  632. mutex_unlock(&malloc_lock);
  633. return ptr;
  634. }
  635. if (best->r_ptrs) /* get block just after */
  636. for (next = best->r_ptrs; next->l_ptrs; next = next->l_ptrs);
  637. else
  638. for (p = ptrs_root, next = NULL; p;) {
  639. if (p->ptr > best->ptr) {
  640. next = p;
  641. p = p->l_ptrs;
  642. } else if (p->ptr < best->ptr)
  643. p = p->r_ptrs;
  644. else
  645. break;
  646. }
  647. if (size < best->size) { /* shrink block */
  648. if (!next || next->used || next->broken) {
  649. if (best->size - size > MALLOC_ALIGN) { /* do split */
  650. SPLIT_BLOCK(best, size);
  651. }
  652. } else { /* just move border of next block */
  653. SHRINK_BLOCK(best, next, size);
  654. }
  655. } else if (next && !next->broken && !next->used) { /* can expand */
  656. if (best->size + next->size > size + HUNK_MAXSIZE) { /* shrink next free block */
  657. SHRINK_BLOCK(best, next, size);
  658. } else if (best->size + next->size >= size) { /* combine blocks (eat next one) */
  659. FREE_MEM_DEL_BLOCK(next, p);
  660. COMBINE_BLOCKS(best, next);
  661. } else { /* not enough memory in next block */
  662. mutex_unlock(&malloc_lock);
  663. return NULL;
  664. }
  665. } else { /* no next block */
  666. mutex_unlock(&malloc_lock);
  667. return NULL;
  668. }
  669. mutex_unlock(&malloc_lock);
  670. return best->ptr;
  671. }
  672. #endif /* L__realloc_no_move */
  673. #ifdef L_realloc
  674. void *realloc(void *ptr, size_t size)
  675. {
  676. void *tmp;
  677. tmp = _realloc_no_move(ptr, size);
  678. if (!tmp) {
  679. Block_t *p, *best;
  680. mutex_lock(&malloc_lock);
  681. for (p = ptrs_root, best = NULL; p;) {
  682. if (p->ptr > (char *) ptr)
  683. p = p->l_ptrs;
  684. else {
  685. best = p;
  686. p = p->r_ptrs;
  687. }
  688. }
  689. if (!best || !best->used || best->ptr != (char *) ptr) {
  690. if (ptr) {
  691. Hunk_t *h;
  692. h = (Hunk_t *) PAGE_DOWNALIGNP(ptr);
  693. if (h->id == HUNK_ID) {
  694. mutex_unlock(&malloc_lock);
  695. if ((size >= HUNK_THRESHOLD && ALIGN(size) == h->size)
  696. || size == h->size)
  697. return ptr;
  698. if ((tmp = malloc(size)) == NULL)
  699. return NULL;
  700. mutex_lock(&malloc_lock);
  701. memcpy(tmp, ptr, ((size < h->size) ? size : h->size));
  702. __hunk_free(ptr);
  703. mutex_unlock(&malloc_lock);
  704. return tmp;
  705. }
  706. }
  707. mutex_unlock(&malloc_lock);
  708. return malloc(size);
  709. }
  710. mutex_unlock(&malloc_lock);
  711. /* copy whole block */
  712. if ((tmp = malloc(size)) == NULL)
  713. return NULL;
  714. memcpy(tmp, ptr, ((size < best->size) ? size : best->size));
  715. mutex_lock(&malloc_lock);
  716. __bl_free(best);
  717. mutex_unlock(&malloc_lock);
  718. }
  719. return tmp;
  720. }
  721. #endif /* L_realloc */
  722. #ifdef L_calloc
  723. void *calloc(size_t unit, size_t quantity)
  724. {
  725. void *p;
  726. unit *= quantity;
  727. if ((p = malloc(unit)) == NULL)
  728. return NULL;
  729. memset(p, 0, unit);
  730. return p;
  731. }
  732. #endif /* L_calloc */