slab.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * File : slab.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2008-07-12 Bernard the first version
  13. */
  14. /*
  15. * KERN_SLABALLOC.C - Kernel SLAB memory allocator
  16. *
  17. * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
  18. *
  19. * This code is derived from software contributed to The DragonFly Project
  20. * by Matthew Dillon <dillon@backplane.com>
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * 3. Neither the name of The DragonFly Project nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific, prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  44. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. *
  49. */
  50. #include <rthw.h>
  51. #include <rtthread.h>
  52. /* #define RT_SLAB_DEBUG */
  53. #if defined (RT_USING_HEAP) && defined (RT_USING_SLAB)
  54. #ifdef RT_USING_HOOK
  55. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  56. static void (*rt_free_hook)(void *ptr);
  57. /**
  58. * @addtogroup Hook
  59. */
  60. /*@{*/
  61. /**
  62. * This function will set a hook function, which will be invoked when a memory
  63. * block is allocated from heap memory.
  64. *
  65. * @param hook the hook function
  66. */
  67. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  68. {
  69. rt_malloc_hook = hook;
  70. }
  71. /**
  72. * This function will set a hook function, which will be invoked when a memory
  73. * block is released to heap memory.
  74. *
  75. * @param hook the hook function
  76. */
  77. void rt_free_sethook(void (*hook)(void *ptr))
  78. {
  79. rt_free_hook = hook;
  80. }
  81. /*@}*/
  82. #endif
  83. /*
  84. * slab allocator implementation
  85. *
  86. * A slab allocator reserves a ZONE for each chunk size, then lays the
  87. * chunks out in an array within the zone. Allocation and deallocation
  88. * is nearly instantanious, and fragmentation/overhead losses are limited
  89. * to a fixed worst-case amount.
  90. *
  91. * The downside of this slab implementation is in the chunk size
  92. * multiplied by the number of zones. ~80 zones * 128K = 10MB of VM per cpu.
  93. * In a kernel implementation all this memory will be physical so
  94. * the zone size is adjusted downward on machines with less physical
  95. * memory. The upside is that overhead is bounded... this is the *worst*
  96. * case overhead.
  97. *
  98. * Slab management is done on a per-cpu basis and no locking or mutexes
  99. * are required, only a critical section. When one cpu frees memory
  100. * belonging to another cpu's slab manager an asynchronous IPI message
  101. * will be queued to execute the operation. In addition, both the
  102. * high level slab allocator and the low level zone allocator optimize
  103. * M_ZERO requests, and the slab allocator does not have to pre initialize
  104. * the linked list of chunks.
  105. *
  106. * XXX Balancing is needed between cpus. Balance will be handled through
  107. * asynchronous IPIs primarily by reassigning the z_Cpu ownership of chunks.
  108. *
  109. * XXX If we have to allocate a new zone and M_USE_RESERVE is set, use of
  110. * the new zone should be restricted to M_USE_RESERVE requests only.
  111. *
  112. * Alloc Size Chunking Number of zones
  113. * 0-127 8 16
  114. * 128-255 16 8
  115. * 256-511 32 8
  116. * 512-1023 64 8
  117. * 1024-2047 128 8
  118. * 2048-4095 256 8
  119. * 4096-8191 512 8
  120. * 8192-16383 1024 8
  121. * 16384-32767 2048 8
  122. * (if RT_MM_PAGE_SIZE is 4K the maximum zone allocation is 16383)
  123. *
  124. * Allocations >= zone_limit go directly to kmem.
  125. *
  126. * API REQUIREMENTS AND SIDE EFFECTS
  127. *
  128. * To operate as a drop-in replacement to the FreeBSD-4.x malloc() we
  129. * have remained compatible with the following API requirements:
  130. *
  131. * + small power-of-2 sized allocations are power-of-2 aligned (kern_tty)
  132. * + all power-of-2 sized allocations are power-of-2 aligned (twe)
  133. * + malloc(0) is allowed and returns non-RT_NULL (ahc driver)
  134. * + ability to allocate arbitrarily large chunks of memory
  135. */
  136. /*
  137. * Chunk structure for free elements
  138. */
  139. typedef struct slab_chunk
  140. {
  141. struct slab_chunk *c_next;
  142. } slab_chunk;
  143. /*
  144. * The IN-BAND zone header is placed at the beginning of each zone.
  145. */
  146. typedef struct slab_zone {
  147. rt_int32_t z_magic; /* magic number for sanity check */
  148. rt_int32_t z_nfree; /* total free chunks / ualloc space in zone */
  149. rt_int32_t z_nmax; /* maximum free chunks */
  150. struct slab_zone *z_next; /* zoneary[] link if z_nfree non-zero */
  151. rt_uint8_t *z_baseptr; /* pointer to start of chunk array */
  152. rt_int32_t z_uindex; /* current initial allocation index */
  153. rt_int32_t z_chunksize; /* chunk size for validation */
  154. rt_int32_t z_zoneindex; /* zone index */
  155. slab_chunk *z_freechunk; /* free chunk list */
  156. } slab_zone;
  157. #define ZALLOC_SLAB_MAGIC 0x51ab51ab
  158. #define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */
  159. #define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */
  160. #define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */
  161. #define NZONES 72 /* number of zones */
  162. #define ZONE_RELEASE_THRESH 2 /* threshold number of zones */
  163. static slab_zone *zone_array[NZONES]; /* linked list of zones NFree > 0 */
  164. static slab_zone *zone_free; /* whole zones that have become free */
  165. static int zone_free_cnt;
  166. static int zone_size;
  167. static int zone_limit;
  168. static int zone_page_cnt;
  169. #ifdef RT_MEM_STATS
  170. /* some statistical variable */
  171. static rt_uint32_t rt_mem_allocated = 0;
  172. static rt_uint32_t rt_mem_zone = 0;
  173. static rt_uint32_t rt_mem_page_allocated = 0;
  174. #endif
  175. /*
  176. * Misc constants. Note that allocations that are exact multiples of
  177. * RT_MM_PAGE_SIZE, or exceed the zone limit, fall through to the kmem module.
  178. */
  179. #define MIN_CHUNK_SIZE 8 /* in bytes */
  180. #define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1)
  181. /*
  182. * Array of descriptors that describe the contents of each page
  183. */
  184. #define PAGE_TYPE_FREE 0x00
  185. #define PAGE_TYPE_SMALL 0x01
  186. #define PAGE_TYPE_LARGE 0x02
  187. struct memusage {
  188. rt_uint32_t type:2 ; /* page type */
  189. rt_uint32_t size:30; /* pages allocated or offset from zone */
  190. };
  191. static struct memusage *memusage = RT_NULL;
  192. #define btokup(addr) (&memusage[((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS])
  193. static rt_uint32_t heap_start, heap_end;
  194. /* page allocator */
  195. struct rt_page_head
  196. {
  197. struct rt_page_head *next; /* next valid page */
  198. rt_size_t page; /* number of page */
  199. /* dummy */
  200. char dummy[RT_MM_PAGE_SIZE - (sizeof(struct rt_page_head*) + sizeof (rt_size_t))];
  201. };
  202. static struct rt_page_head *rt_page_list;
  203. static void *rt_page_alloc(rt_size_t npages)
  204. {
  205. struct rt_page_head *b, *n;
  206. struct rt_page_head **prev;
  207. RT_ASSERT(npages != 0);
  208. for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next))
  209. {
  210. if (b->page > npages)
  211. {
  212. /* splite pages */
  213. n = b + npages;
  214. n->next = b->next;
  215. n->page = b->page - npages;
  216. *prev = n;
  217. break;
  218. }
  219. if (b->page == npages)
  220. {
  221. /* this node fit, remove this node */
  222. *prev = b->next;
  223. break;
  224. }
  225. }
  226. return b;
  227. }
  228. static void rt_page_free(void *addr, rt_size_t npages)
  229. {
  230. struct rt_page_head *b, *n;
  231. struct rt_page_head **prev;
  232. RT_ASSERT(addr != RT_NULL);
  233. RT_ASSERT((rt_uint32_t)addr % RT_MM_PAGE_SIZE == 0);
  234. RT_ASSERT(npages != 0);
  235. n = (struct rt_page_head *)addr;
  236. for (prev = &rt_page_list; (b = *prev) != RT_NULL; prev = &(b->next))
  237. {
  238. RT_ASSERT(b->page > 0);
  239. RT_ASSERT(b > n || b + b->page <= n);
  240. if (b + b->page == n)
  241. {
  242. if (b + (b->page += npages) == b->next)
  243. {
  244. b->page += b->next->page;
  245. b->next = b->next->next;
  246. }
  247. return;
  248. }
  249. if (b == n + npages)
  250. {
  251. n->page = b->page + npages;
  252. n->next = b->next;
  253. *prev = n;
  254. return;
  255. }
  256. if (b > n + npages) break;
  257. }
  258. n->page = npages;
  259. n->next = b;
  260. *prev = n;
  261. }
  262. /*
  263. * Initialize the page allocator
  264. */
  265. static void rt_page_init(void* addr, rt_size_t npages)
  266. {
  267. RT_ASSERT(addr != RT_NULL);
  268. RT_ASSERT(npages != 0);
  269. rt_page_list = RT_NULL;
  270. rt_page_free(addr, npages);
  271. }
  272. /**
  273. * @ingroup SystemInit
  274. *
  275. * This function will init system heap
  276. *
  277. * @param begin_addr the beginning address of system page
  278. * @param end_addr the end address of system page
  279. *
  280. */
  281. void rt_system_heap_init(void *begin_addr, void* end_addr)
  282. {
  283. rt_uint32_t limsize, npages;
  284. /* align begin and end addr to page */
  285. heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE);
  286. heap_end = RT_ALIGN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE);
  287. limsize = heap_end - heap_start;
  288. npages = limsize / RT_MM_PAGE_SIZE;
  289. #ifdef RT_SLAB_DEBUG
  290. rt_kprintf("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n", heap_start, heap_end, limsize, npages);
  291. #endif
  292. /* init pages */
  293. rt_page_init((void*)heap_start, npages);
  294. /* calculate zone size */
  295. zone_size = ZALLOC_MIN_ZONE_SIZE;
  296. while (zone_size < ZALLOC_MAX_ZONE_SIZE && (zone_size << 1) < (limsize/1024))
  297. zone_size <<= 1;
  298. zone_limit = zone_size / 4;
  299. if (zone_limit > ZALLOC_ZONE_LIMIT) zone_limit = ZALLOC_ZONE_LIMIT;
  300. zone_page_cnt = zone_size / RT_MM_PAGE_SIZE;
  301. #ifdef RT_SLAB_DEBUG
  302. rt_kprintf("zone size 0x%x, zone page count 0x%x\n", zone_size, zone_page_cnt);
  303. #endif
  304. /* allocate memusage array */
  305. limsize = npages * sizeof(struct memusage);
  306. limsize = RT_ALIGN(limsize, RT_MM_PAGE_SIZE);
  307. memusage = rt_page_alloc(limsize/RT_MM_PAGE_SIZE);
  308. #ifdef RT_SLAB_DEBUG
  309. rt_kprintf("memusage 0x%x, size 0x%x\n", (rt_uint32_t)memusage, limsize);
  310. #endif
  311. }
  312. /*
  313. * Calculate the zone index for the allocation request size and set the
  314. * allocation request size to that particular zone's chunk size.
  315. */
  316. rt_inline int zoneindex(rt_uint32_t *bytes)
  317. {
  318. rt_uint32_t n = (rt_uint32_t)*bytes; /* unsigned for shift opt */
  319. if (n < 128)
  320. {
  321. *bytes = n = (n + 7) & ~7;
  322. return(n / 8 - 1); /* 8 byte chunks, 16 zones */
  323. }
  324. if (n < 256)
  325. {
  326. *bytes = n = (n + 15) & ~15;
  327. return(n / 16 + 7);
  328. }
  329. if (n < 8192)
  330. {
  331. if (n < 512)
  332. {
  333. *bytes = n = (n + 31) & ~31;
  334. return(n / 32 + 15);
  335. }
  336. if (n < 1024)
  337. {
  338. *bytes = n = (n + 63) & ~63;
  339. return(n / 64 + 23);
  340. }
  341. if (n < 2048)
  342. {
  343. *bytes = n = (n + 127) & ~127;
  344. return(n / 128 + 31);
  345. }
  346. if (n < 4096)
  347. {
  348. *bytes = n = (n + 255) & ~255;
  349. return(n / 256 + 39);
  350. }
  351. *bytes = n = (n + 511) & ~511;
  352. return(n / 512 + 47);
  353. }
  354. if (n < 16384)
  355. {
  356. *bytes = n = (n + 1023) & ~1023;
  357. return(n / 1024 + 55);
  358. }
  359. rt_kprintf("Unexpected byte count %d", n);
  360. return 0;
  361. }
  362. /**
  363. * @addtogroup MM
  364. */
  365. /*@{*/
  366. /**
  367. * This function will allocate a block from system heap memory.
  368. * - If the nbytes is less than zero,
  369. * or
  370. * - If there is no nbytes sized memory valid in system,
  371. * the RT_NULL is returned.
  372. *
  373. * @param size the size of memory to be allocated
  374. *
  375. * @return the allocated memory
  376. *
  377. */
  378. void *rt_malloc(rt_size_t size)
  379. {
  380. slab_zone *z;
  381. rt_int32_t zi;
  382. slab_chunk *chunk;
  383. rt_base_t interrupt_level;
  384. struct memusage *kup;
  385. /* zero size, return RT_NULL */
  386. if (size == 0) return RT_NULL;
  387. /*
  388. * Handle large allocations directly. There should not be very many of
  389. * these so performance is not a big issue.
  390. */
  391. if (size >= zone_limit)
  392. {
  393. size = RT_ALIGN(size, RT_MM_PAGE_SIZE);
  394. chunk = rt_page_alloc(size >> RT_MM_PAGE_BITS);
  395. if (chunk == RT_NULL) return RT_NULL;
  396. /* set kup */
  397. kup = btokup(chunk);
  398. kup->type = PAGE_TYPE_LARGE;
  399. kup->size = size >> RT_MM_PAGE_BITS;
  400. #ifdef RT_SLAB_DEBUG
  401. rt_kprintf("malloc a large memory 0x%x, page cnt %d, kup %d\n",
  402. size,
  403. size >> RT_MM_PAGE_BITS,
  404. ((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS);
  405. #endif
  406. /* lock interrupt */
  407. interrupt_level = rt_hw_interrupt_disable();
  408. goto done;
  409. }
  410. /*
  411. * Attempt to allocate out of an existing zone. First try the free list,
  412. * then allocate out of unallocated space. If we find a good zone move
  413. * it to the head of the list so later allocations find it quickly
  414. * (we might have thousands of zones in the list).
  415. *
  416. * Note: zoneindex() will panic of size is too large.
  417. */
  418. zi = zoneindex(&size);
  419. RT_ASSERT(zi < NZONES);
  420. #ifdef RT_SLAB_DEBUG
  421. rt_kprintf("try to malloc 0x%x on zone: %d\n", size, zi);
  422. #endif
  423. interrupt_level = rt_hw_interrupt_disable();
  424. if ((z = zone_array[zi]) != RT_NULL)
  425. {
  426. RT_ASSERT(z->z_nfree > 0);
  427. /* Remove us from the zone_array[] when we become empty */
  428. if (--z->z_nfree == 0)
  429. {
  430. zone_array[zi] = z->z_next;
  431. z->z_next = RT_NULL;
  432. }
  433. /*
  434. * No chunks are available but nfree said we had some memory, so
  435. * it must be available in the never-before-used-memory area
  436. * governed by uindex. The consequences are very serious if our zone
  437. * got corrupted so we use an explicit rt_kprintf rather then a KASSERT.
  438. */
  439. if (z->z_uindex + 1 != z->z_nmax)
  440. {
  441. z->z_uindex = z->z_uindex + 1;
  442. chunk = (slab_chunk *)(z->z_baseptr + z->z_uindex * size);
  443. }
  444. else
  445. {
  446. /* find on free chunk list */
  447. chunk = z->z_freechunk;
  448. /* remove this chunk from list */
  449. z->z_freechunk = z->z_freechunk->c_next;
  450. }
  451. goto done;
  452. }
  453. /*
  454. * If all zones are exhausted we need to allocate a new zone for this
  455. * index.
  456. *
  457. * At least one subsystem, the tty code (see CROUND) expects power-of-2
  458. * allocations to be power-of-2 aligned. We maintain compatibility by
  459. * adjusting the base offset below.
  460. */
  461. {
  462. rt_int32_t off;
  463. if ((z = zone_free) != RT_NULL)
  464. {
  465. /* remove zone from free zone list */
  466. zone_free = z->z_next;
  467. --zone_free_cnt;
  468. }
  469. else
  470. {
  471. /* allocate a zone from page */
  472. z = rt_page_alloc(zone_size / RT_MM_PAGE_SIZE);
  473. if (z == RT_NULL) goto fail;
  474. #ifdef RT_SLAB_DEBUG
  475. rt_kprintf("alloc a new zone: 0x%x\n", (rt_uint32_t)z);
  476. #endif
  477. /* set message usage */
  478. for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++)
  479. {
  480. kup->type = PAGE_TYPE_SMALL;
  481. kup->size = off;
  482. kup ++;
  483. }
  484. }
  485. /* clear to zero */
  486. rt_memset(z, 0, sizeof(slab_zone));
  487. /* offset of slab zone struct in zone */
  488. off = sizeof(slab_zone);
  489. /*
  490. * Guarentee power-of-2 alignment for power-of-2-sized chunks.
  491. * Otherwise just 8-byte align the data.
  492. */
  493. if ((size | (size - 1)) + 1 == (size << 1))
  494. off = (off + size - 1) & ~(size - 1);
  495. else
  496. off = (off + MIN_CHUNK_MASK) & ~MIN_CHUNK_MASK;
  497. z->z_magic = ZALLOC_SLAB_MAGIC;
  498. z->z_zoneindex = zi;
  499. z->z_nmax = (zone_size - off) / size;
  500. z->z_nfree = z->z_nmax - 1;
  501. z->z_baseptr = (rt_uint8_t*)z + off;
  502. z->z_uindex = 0;
  503. z->z_chunksize = size;
  504. chunk = (slab_chunk *)(z->z_baseptr + z->z_uindex * size);
  505. /* link to zone array */
  506. z->z_next = zone_array[zi];
  507. zone_array[zi] = z;
  508. }
  509. done:
  510. rt_hw_interrupt_enable(interrupt_level);
  511. #ifdef RT_USING_HOOK
  512. if (rt_malloc_hook != RT_NULL) rt_malloc_hook((char*)chunk, size);
  513. #endif
  514. return chunk;
  515. fail:
  516. rt_hw_interrupt_enable(interrupt_level);
  517. return RT_NULL;
  518. }
  519. /**
  520. * This function will change the size of previously allocated memory block.
  521. *
  522. * @param ptr the previously allocated memory block
  523. * @param size the new size of memory block
  524. *
  525. * @return the allocated memory
  526. */
  527. void *rt_realloc(void *ptr, rt_size_t size)
  528. {
  529. void *nptr;
  530. slab_zone *z;
  531. struct memusage *kup;
  532. if (ptr == RT_NULL) return rt_malloc(size);
  533. if (size == 0)
  534. {
  535. rt_free(ptr);
  536. return RT_NULL;
  537. }
  538. /*
  539. * Get the original allocation's zone. If the new request winds up
  540. * using the same chunk size we do not have to do anything.
  541. */
  542. kup = btokup((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
  543. if (kup->type == PAGE_TYPE_LARGE)
  544. {
  545. rt_size_t osize;
  546. osize = kup->size << RT_MM_PAGE_BITS;
  547. if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL;
  548. rt_memcpy(nptr, ptr, size > osize? osize : size);
  549. rt_free(ptr);
  550. return nptr;
  551. }
  552. else if (kup->type == PAGE_TYPE_SMALL)
  553. {
  554. z = (slab_zone*)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE);
  555. RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
  556. zoneindex(&size);
  557. if (z->z_chunksize == size) return(ptr); /* same chunk */
  558. /*
  559. * Allocate memory for the new request size. Note that zoneindex has
  560. * already adjusted the request size to the appropriate chunk size, which
  561. * should optimize our bcopy(). Then copy and return the new pointer.
  562. */
  563. if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL;
  564. rt_memcpy(nptr, ptr, size > z->z_chunksize? z->z_chunksize : size);
  565. rt_free(ptr);
  566. return nptr;
  567. }
  568. return RT_NULL;
  569. }
  570. /**
  571. * This function will contiguously allocate enough space for count objects
  572. * that are size bytes of memory each and returns a pointer to the allocated
  573. * memory.
  574. *
  575. * The allocated memory is filled with bytes of value zero.
  576. *
  577. * @param count number of objects to allocate
  578. * @param size size of the objects to allocate
  579. *
  580. * @return pointer to allocated memory / NULL pointer if there is an error
  581. */
  582. void *rt_calloc(rt_size_t count, rt_size_t size)
  583. {
  584. void *p;
  585. /* allocate 'count' objects of size 'size' */
  586. p = rt_malloc(count * size);
  587. /* zero the memory */
  588. if (p) rt_memset(p, 0, count * size);
  589. return p;
  590. }
  591. /**
  592. * This function will release the previously allocated memory block by rt_malloc.
  593. * The released memory block is taken back to system heap.
  594. *
  595. * @param ptr the address of memory which will be released
  596. */
  597. void rt_free(void *ptr)
  598. {
  599. slab_zone *z;
  600. slab_chunk *chunk;
  601. struct memusage *kup;
  602. rt_base_t interrupt_level;
  603. /* free a RT_NULL pointer */
  604. if (ptr == RT_NULL) return ;
  605. #ifdef RT_USING_HOOK
  606. if (rt_free_hook != RT_NULL) rt_free_hook(ptr);
  607. #endif
  608. /* get memory usage */
  609. #ifdef RT_SLAB_DEBUG
  610. rt_uint32 addr = ((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
  611. rt_kprintf("free a memory 0x%x and align to 0x%x, kup index %d\n",
  612. (rt_uint32_t)ptr,
  613. (rt_uint32_t)addr,
  614. ((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS);
  615. #endif
  616. kup = btokup((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK);
  617. /* release large allocation */
  618. if (kup->type == PAGE_TYPE_LARGE)
  619. {
  620. rt_uint32_t size;
  621. /* clear page counter */
  622. interrupt_level = rt_hw_interrupt_disable();
  623. size = kup->size;
  624. kup->size = 0;
  625. rt_hw_interrupt_enable(interrupt_level);
  626. #ifdef RT_SLAB_DEBUG
  627. rt_kprintf("free large memory block 0x%x, page count %d\n", (rt_uint32_t)ptr, size);
  628. #endif
  629. /* free this page */
  630. rt_page_free(ptr, size);
  631. return;
  632. }
  633. /* zone case. get out zone. */
  634. z = (slab_zone*)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE);
  635. RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
  636. interrupt_level = rt_hw_interrupt_disable();
  637. chunk = (slab_chunk*)ptr;
  638. chunk->c_next = z->z_freechunk;
  639. z->z_freechunk = chunk;
  640. /*
  641. * Bump the number of free chunks. If it becomes non-zero the zone
  642. * must be added back onto the appropriate list.
  643. */
  644. if (z->z_nfree++ == 0)
  645. {
  646. z->z_next = zone_array[z->z_zoneindex];
  647. zone_array[z->z_zoneindex] = z;
  648. }
  649. /*
  650. * If the zone becomes totally free, and there are other zones we
  651. * can allocate from, move this zone to the FreeZones list. Since
  652. * this code can be called from an IPI callback, do *NOT* try to mess
  653. * with kernel_map here. Hysteresis will be performed at malloc() time.
  654. */
  655. if (z->z_nfree == z->z_nmax &&
  656. (z->z_next || zone_array[z->z_zoneindex] != z))
  657. {
  658. slab_zone **pz;
  659. #ifdef RT_SLAB_DEBUG
  660. rt_kprintf("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex);
  661. #endif
  662. /* remove zone from zone array list */
  663. for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next) ;
  664. *pz = z->z_next;
  665. /* reset zone */
  666. z->z_magic = -1;
  667. /* insert to free zone list */
  668. z->z_next = zone_free;
  669. zone_free = z;
  670. ++zone_free_cnt;
  671. /* release zone to page allocator */
  672. if (zone_free_cnt > ZONE_RELEASE_THRESH)
  673. {
  674. register rt_base_t i;
  675. z = zone_free;
  676. zone_free = z->z_next;
  677. --zone_free_cnt;
  678. /* set message usage */
  679. for (i = 0, kup = btokup(z); i < zone_page_cnt; i ++)
  680. {
  681. kup->type = PAGE_TYPE_FREE;
  682. kup->size = 0;
  683. kup ++;
  684. }
  685. /* release pages */
  686. rt_page_free(z, zone_size);
  687. }
  688. }
  689. rt_hw_interrupt_enable(interrupt_level);
  690. }
  691. /*@}*/
  692. #endif