slab.c 26 KB

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