malloc.c 20 KB

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