malloc.c 19 KB

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