diff options
| author | dzzxzz <[email protected]> | 2012-03-17 06:43:49 +0000 |
|---|---|---|
| committer | dzzxzz <[email protected]> | 2012-03-17 06:43:49 +0000 |
| commit | 12b5e56f46e4b2b489a088c2ccfb6410c10686ea (patch) | |
| tree | 7cafe9a126189efd19cf014e46c95f87bbb33774 /src/slab.c | |
| parent | 3b06be1cde860cdc4cdc97f901e1794329591041 (diff) | |
unify the coding style of kernel
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1998 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'src/slab.c')
| -rw-r--r-- | src/slab.c | 85 |
1 files changed, 61 insertions, 24 deletions
diff --git a/src/slab.c b/src/slab.c index 51cc7c3c9d..04cfa3c985 100644 --- a/src/slab.c +++ b/src/slab.c @@ -1,7 +1,7 @@ /* * File : slab.c * This file is part of RT-Thread RTOS - * COPYRIGHT (C) 2008 - 2011, RT-Thread Development Team + * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at @@ -71,6 +71,7 @@ static void (*rt_free_hook)(void *ptr); /** * @addtogroup Hook */ + /*@{*/ /** @@ -235,7 +236,8 @@ void *rt_page_alloc(rt_size_t npages) struct rt_page_head *b, *n; struct rt_page_head **prev; - if(npages == 0) return RT_NULL; + if(npages == 0) + return RT_NULL; /* lock heap */ rt_sem_take(&heap_sem, RT_WAITING_FOREVER); @@ -304,7 +306,8 @@ void rt_page_free(void *addr, rt_size_t npages) goto _return; } - if (b > n + npages) break; + if (b > n + npages) + break; } n->page = npages; @@ -335,7 +338,6 @@ static void rt_page_init(void *addr, rt_size_t npages) * * @param begin_addr the beginning address of system page * @param end_addr the end address of system page - * */ void rt_system_heap_init(void *begin_addr, void *end_addr) { @@ -351,6 +353,7 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) { rt_kprintf("rt_system_heap_init, wrong address[0x%x - 0x%x]\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); + return; } @@ -372,7 +375,8 @@ void rt_system_heap_init(void *begin_addr, void *end_addr) zone_size <<= 1; zone_limit = zone_size / 4; - if (zone_limit > ZALLOC_ZONE_LIMIT) zone_limit = ZALLOC_ZONE_LIMIT; + if (zone_limit > ZALLOC_ZONE_LIMIT) + zone_limit = ZALLOC_ZONE_LIMIT; zone_page_cnt = zone_size / RT_MM_PAGE_SIZE; @@ -399,11 +403,13 @@ rt_inline int zoneindex(rt_uint32_t *bytes) if (n < 128) { *bytes = n = (n + 7) & ~7; + return(n / 8 - 1); /* 8 byte chunks, 16 zones */ } if (n < 256) { *bytes = n = (n + 15) & ~15; + return(n / 16 + 7); } if (n < 8192) @@ -411,33 +417,40 @@ rt_inline int zoneindex(rt_uint32_t *bytes) if (n < 512) { *bytes = n = (n + 31) & ~31; + return(n / 32 + 15); } if (n < 1024) { *bytes = n = (n + 63) & ~63; + return(n / 64 + 23); } if (n < 2048) { *bytes = n = (n + 127) & ~127; + return(n / 128 + 31); } if (n < 4096) { *bytes = n = (n + 255) & ~255; + return(n / 256 + 39); } *bytes = n = (n + 511) & ~511; + return(n / 512 + 47); } if (n < 16384) { *bytes = n = (n + 1023) & ~1023; + return(n / 1024 + 55); } rt_kprintf("Unexpected byte count %d", n); + return 0; } @@ -457,7 +470,6 @@ rt_inline int zoneindex(rt_uint32_t *bytes) * @param size the size of memory to be allocated * * @return the allocated memory - * */ void *rt_malloc(rt_size_t size) { @@ -467,10 +479,12 @@ void *rt_malloc(rt_size_t size) struct memusage *kup; /* zero size, return RT_NULL */ - if (size == 0) return RT_NULL; + if (size == 0) + return RT_NULL; #ifdef RT_USING_MODULE - if (rt_module_self() != RT_NULL) return rt_module_malloc(size); + if (rt_module_self() != RT_NULL) + return rt_module_malloc(size); #endif /* @@ -482,7 +496,8 @@ void *rt_malloc(rt_size_t size) size = RT_ALIGN(size, RT_MM_PAGE_SIZE); chunk = rt_page_alloc(size >> RT_MM_PAGE_BITS); - if (chunk == RT_NULL) return RT_NULL; + if (chunk == RT_NULL) + return RT_NULL; /* set kup */ kup = btokup(chunk); @@ -499,7 +514,8 @@ void *rt_malloc(rt_size_t size) #ifdef RT_MEM_STATS used_mem += size; - if (used_mem > max_mem) max_mem = used_mem; + if (used_mem > max_mem) + max_mem = used_mem; #endif goto done; } @@ -553,7 +569,8 @@ void *rt_malloc(rt_size_t size) #ifdef RT_MEM_STATS used_mem += z->z_chunksize; - if (used_mem > max_mem) max_mem = used_mem; + if (used_mem > max_mem) + max_mem = used_mem; #endif goto done; @@ -583,7 +600,8 @@ void *rt_malloc(rt_size_t size) /* allocate a zone from page */ z = rt_page_alloc(zone_size / RT_MM_PAGE_SIZE); - if (z == RT_NULL) goto fail; + if (z == RT_NULL) + goto fail; /* lock heap */ rt_sem_take(&heap_sem, RT_WAITING_FOREVER); @@ -631,7 +649,8 @@ void *rt_malloc(rt_size_t size) #ifdef RT_MEM_STATS used_mem += z->z_chunksize; - if (used_mem > max_mem) max_mem = used_mem; + if (used_mem > max_mem) + max_mem = used_mem; #endif } @@ -644,6 +663,7 @@ done: fail: rt_sem_release(&heap_sem); + return RT_NULL; } @@ -661,15 +681,18 @@ void *rt_realloc(void *ptr, rt_size_t size) slab_zone *z; struct memusage *kup; - if (ptr == RT_NULL) return rt_malloc(size); + if (ptr == RT_NULL) + return rt_malloc(size); if (size == 0) { rt_free(ptr); + return RT_NULL; } #ifdef RT_USING_MODULE - if (rt_module_self() != RT_NULL) return rt_module_realloc(ptr, size); + if (rt_module_self() != RT_NULL) + return rt_module_realloc(ptr, size); #endif /* @@ -682,7 +705,8 @@ void *rt_realloc(void *ptr, rt_size_t size) rt_size_t osize; osize = kup->size << RT_MM_PAGE_BITS; - if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL; + if ((nptr = rt_malloc(size)) == RT_NULL) + return RT_NULL; rt_memcpy(nptr, ptr, size > osize ? osize : size); rt_free(ptr); @@ -694,14 +718,16 @@ void *rt_realloc(void *ptr, rt_size_t size) RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC); zoneindex(&size); - if (z->z_chunksize == size) return(ptr); /* same chunk */ + if (z->z_chunksize == size) + return(ptr); /* same chunk */ /* * Allocate memory for the new request size. Note that zoneindex has * already adjusted the request size to the appropriate chunk size, which * should optimize our bcopy(). Then copy and return the new pointer. */ - if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL; + if ((nptr = rt_malloc(size)) == RT_NULL) + return RT_NULL; rt_memcpy(nptr, ptr, size > z->z_chunksize ? z->z_chunksize : size); rt_free(ptr); @@ -732,7 +758,8 @@ void *rt_calloc(rt_size_t count, rt_size_t size) p = rt_malloc(count * size); /* zero the memory */ - if (p) rt_memset(p, 0, count * size); + if (p) + rt_memset(p, 0, count * size); return p; } @@ -750,7 +777,8 @@ void rt_free(void *ptr) struct memusage *kup; /* free a RT_NULL pointer */ - if (ptr == RT_NULL) return ; + if (ptr == RT_NULL) + return ; RT_OBJECT_HOOK_CALL(rt_free_hook, (ptr)); @@ -758,6 +786,7 @@ void rt_free(void *ptr) if(rt_module_self() != RT_NULL) { rt_module_free(rt_module_self(), ptr); + return; } #endif @@ -796,6 +825,7 @@ void rt_free(void *ptr) /* free this page */ rt_page_free(ptr, size); + return; } @@ -837,7 +867,8 @@ void rt_free(void *ptr) RT_DEBUG_LOG(RT_DEBUG_SLAB, ("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex)); /* remove zone from zone array list */ - for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next); + for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next) + ; *pz = z->z_next; /* reset zone */ @@ -871,6 +902,7 @@ void rt_free(void *ptr) /* release pages */ rt_page_free(z, zone_size / RT_MM_PAGE_SIZE); + return; } } @@ -881,9 +913,14 @@ void rt_free(void *ptr) #ifdef RT_MEM_STATS void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used) { - if (total != RT_NULL) *total = heap_end - heap_start; - if (used != RT_NULL) *used = used_mem; - if (max_used != RT_NULL) *max_used = max_mem; + if (total != RT_NULL) + *total = heap_end - heap_start; + + if (used != RT_NULL) + *used = used_mem; + + if (max_used != RT_NULL) + *max_used = max_mem; } #ifdef RT_USING_FINSH |
