Fix multicore byte_manager illegal parallel execution, fix w5500 pbuf creation failed checking.

This commit is contained in:
涂煜洋 2023-09-07 09:30:37 +08:00
parent efba3f3bf3
commit aefe92d67d
2 changed files with 260 additions and 259 deletions

View File

@ -168,6 +168,9 @@ static struct pbuf* wiz_read_receive_pbuf(struct pbuf* buf)
int32_t data_len = wiz_sock_recvfrom(0, rx_frame, RX_FRAME_SIZE, addr, &port); int32_t data_len = wiz_sock_recvfrom(0, rx_frame, RX_FRAME_SIZE, addr, &port);
if (data_len > 0 && data_len <= RX_FRAME_SIZE) { if (data_len > 0 && data_len <= RX_FRAME_SIZE) {
buf = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL); buf = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL);
if (buf == NULL) {
return NULL;
}
memcpy(buf->payload, rx_frame, data_len); memcpy(buf->payload, rx_frame, data_len);
} else { } else {
return NULL; return NULL;

View File

@ -61,6 +61,9 @@ enum SmallSizeAllocSize {
#define SMALL_SIZE_32B(ITEMSIZE) ((ITEMSIZE + SIZEOF_DYNAMICALLOCNODE_MEM) * SMALL_NUMBER_32B) /* Calculate the total size for SIZEOF_32B blocks*/ #define SMALL_SIZE_32B(ITEMSIZE) ((ITEMSIZE + SIZEOF_DYNAMICALLOCNODE_MEM) * SMALL_NUMBER_32B) /* Calculate the total size for SIZEOF_32B blocks*/
#define SMALL_SIZE_64B(ITEMSIZE) ((ITEMSIZE + SIZEOF_DYNAMICALLOCNODE_MEM) * SMALL_NUMBER_64B) /* Calculate the total size for SIZEOF_64B blocks*/ #define SMALL_SIZE_64B(ITEMSIZE) ((ITEMSIZE + SIZEOF_DYNAMICALLOCNODE_MEM) * SMALL_NUMBER_64B) /* Calculate the total size for SIZEOF_64B blocks*/
#define FREE_LIST_LOCK() DISABLE_INTERRUPT()
#define FREE_LIST_UNLOCK(lock) ENABLE_INTERRUPT(lock)
/** /**
* The structure describes an allocated memory block from dynamic buddy memory. * The structure describes an allocated memory block from dynamic buddy memory.
*/ */
@ -632,7 +635,7 @@ void *x_malloc(x_size_t size)
register x_base lock = 0; register x_base lock = 0;
/* hold lock before allocation */ /* hold lock before allocation */
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
/* alignment */ /* alignment */
size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
@ -640,26 +643,26 @@ void *x_malloc(x_size_t size)
/* parameter detection */ /* parameter detection */
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
/* parameter detection */ /* parameter detection */
if(size == 0 ){ if (size == 0) {
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
return NONE; return NONE;
} }
if((size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)){ if ((size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) {
/* alignment */ /* alignment */
size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
goto try_extmem; goto try_extmem;
} }
#else #else
/* parameter detection */ /* parameter detection */
if((size == 0) || (size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) { if ((size == 0) || (size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) {
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
return NONE; return NONE;
} }
#endif #endif
/* determine allocation operation from static segments or dynamic buddy memory */ /* determine allocation operation from static segments or dynamic buddy memory */
#ifdef KERNEL_SMALL_MEM_ALLOC #ifdef KERNEL_SMALL_MEM_ALLOC
if(size <= SIZEOF_32B) { if (size <= SIZEOF_32B) {
ret = ByteManager.static_manager[0].done->malloc(&ByteManager, SIZEOF_32B); ret = ByteManager.static_manager[0].done->malloc(&ByteManager, SIZEOF_32B);
} else if (size <= SIZEOF_64B) { } else if (size <= SIZEOF_64B) {
ret = ByteManager.static_manager[1].done->malloc(&ByteManager, SIZEOF_64B); ret = ByteManager.static_manager[1].done->malloc(&ByteManager, SIZEOF_64B);
@ -673,16 +676,15 @@ void *x_malloc(x_size_t size)
} }
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
try_extmem: try_extmem:
if(NONE == ret) { if (NONE == ret) {
for(i = 0; i < EXTSRAM_MAX_NUM; i++) { for (i = 0; i < EXTSRAM_MAX_NUM; i++) {
if(NONE != ExtByteManager[i].done) { if (NONE != ExtByteManager[i].done) {
ret = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1)); ret = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1));
if (ret){ if (ret) {
CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM));
break; break;
} }
} }
} }
} }
@ -690,7 +692,7 @@ try_extmem:
} }
/* release lock */ /* release lock */
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
return ret; return ret;
} }
@ -702,12 +704,12 @@ try_extmem:
* *
* @return pointer on success; NULL on failure * @return pointer on success; NULL on failure
*/ */
void *x_realloc(void *pointer, x_size_t size) void* x_realloc(void* pointer, x_size_t size)
{ {
x_size_t newsize = 0; x_size_t newsize = 0;
x_size_t oldsize = 0; x_size_t oldsize = 0;
void *newmem = NONE; void* newmem = NONE;
struct DynamicAllocNode *oldnode = NONE; struct DynamicAllocNode* oldnode = NONE;
/* the given pointer is NULL */ /* the given pointer is NULL */
if (pointer == NONE) if (pointer == NONE)
@ -718,17 +720,17 @@ void *x_realloc(void *pointer, x_size_t size)
x_free(pointer); x_free(pointer);
return NONE; return NONE;
} }
CHECK(ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager,pointer)); CHECK(ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager, pointer));
/* alignment and calculate the real size */ /* alignment and calculate the real size */
newsize = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); newsize = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
newsize += SIZEOF_DYNAMICALLOCNODE_MEM; newsize += SIZEOF_DYNAMICALLOCNODE_MEM;
oldnode= PTR2ALLOCNODE((char*)pointer - SIZEOF_DYNAMICALLOCNODE_MEM); oldnode = PTR2ALLOCNODE((char*)pointer - SIZEOF_DYNAMICALLOCNODE_MEM);
CHECK(ByteManager.done->JudgeAllocated(oldnode)); CHECK(ByteManager.done->JudgeAllocated(oldnode));
/* achieve the old memory size */ /* achieve the old memory size */
if(ByteManager.done->JudgeStaticOrDynamic(oldnode)) { if (ByteManager.done->JudgeStaticOrDynamic(oldnode)) {
oldsize = ((struct segment*)(long)(oldnode->size))->block_size; oldsize = ((struct segment*)(long)(oldnode->size))->block_size;
} else { } else {
oldsize = oldnode->size - SIZEOF_DYNAMICALLOCNODE_MEM; oldsize = oldnode->size - SIZEOF_DYNAMICALLOCNODE_MEM;
@ -736,12 +738,12 @@ void *x_realloc(void *pointer, x_size_t size)
/* allocate new memory */ /* allocate new memory */
newmem = x_malloc(size); newmem = x_malloc(size);
if(newmem == NONE) { if (newmem == NONE) {
return NONE; return NONE;
} }
/* copy the old memory and then release old memory pointer */ /* copy the old memory and then release old memory pointer */
memcpy((char*)newmem, (char*) pointer,size > oldsize ? oldsize : size); memcpy((char*)newmem, (char*)pointer, size > oldsize ? oldsize : size);
x_free(pointer); x_free(pointer);
return newmem; return newmem;
@ -755,12 +757,12 @@ void *x_realloc(void *pointer, x_size_t size)
* *
* @return pointer on success; NULL on failure * @return pointer on success; NULL on failure
*/ */
void *x_calloc(x_size_t count, x_size_t size) void* x_calloc(x_size_t count, x_size_t size)
{ {
void *p = NONE; void* p = NONE;
/* parameter detection */ /* parameter detection */
if(count * size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory) if (count * size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)
return NONE; return NONE;
/* calls x_malloc to allocate count * size memory */ /* calls x_malloc to allocate count * size memory */
@ -778,10 +780,10 @@ void *x_calloc(x_size_t count, x_size_t size)
* *
* @param pointer the memory to be released * @param pointer the memory to be released
*/ */
void x_free(void *pointer) void x_free(void* pointer)
{ {
x_base lock = 0; x_base lock = 0;
struct DynamicAllocNode *node = NONE; struct DynamicAllocNode* node = NONE;
/* parameter detection */ /* parameter detection */
if (pointer == NONE) { if (pointer == NONE) {
@ -789,10 +791,10 @@ void x_free(void *pointer)
} }
/* hold lock before release */ /* hold lock before release */
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
if (!ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager, pointer)) { if (!ByteManager.dynamic_buddy_manager.done->JudgeLegal(&ByteManager.dynamic_buddy_manager, pointer)) {
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
SYS_ERR("[%s] Freeing a unallocated address.\n", __func__); SYS_ERR("[%s] Freeing a unallocated address.\n", __func__);
return; return;
} }
@ -802,20 +804,20 @@ void x_free(void *pointer)
/* judge release the memory block ro static_segment or dynamic buddy memory */ /* judge release the memory block ro static_segment or dynamic buddy memory */
#ifdef KERNEL_SMALL_MEM_ALLOC #ifdef KERNEL_SMALL_MEM_ALLOC
if(node->flag & STATIC_BLOCK_MASK) { if (node->flag & STATIC_BLOCK_MASK) {
ByteManager.static_manager->done->release(pointer); ByteManager.static_manager->done->release(pointer);
} else } else
#endif #endif
{ {
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
/* judge the pointer is not malloced from extern memory*/ /* judge the pointer is not malloced from extern memory*/
if(0 == (node->flag & 0xFF0000)) { if (0 == (node->flag & 0xFF0000)) {
ByteManager.dynamic_buddy_manager.done->release(&ByteManager,pointer); ByteManager.dynamic_buddy_manager.done->release(&ByteManager, pointer);
} }
/* judge the pointer is malloced from extern memory*/ /* judge the pointer is malloced from extern memory*/
if(0 != (node->flag & 0xFF0000)) { if (0 != (node->flag & 0xFF0000)) {
ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1].dynamic_buddy_manager.done->release(&ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1],pointer); ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1].dynamic_buddy_manager.done->release(&ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1], pointer);
} }
#else #else
ByteManager.dynamic_buddy_manager.done->release(&ByteManager, pointer); ByteManager.dynamic_buddy_manager.done->release(&ByteManager, pointer);
@ -823,7 +825,7 @@ void x_free(void *pointer)
} }
/* release the lock */ /* release the lock */
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
} }
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
@ -834,7 +836,7 @@ void x_free(void *pointer)
* @param end_phy_address the end physical address for static and dynamic memory * @param end_phy_address the end physical address for static and dynamic memory
* @param extsram_idx the idx of extsram chip * @param extsram_idx the idx of extsram chip
*/ */
void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint8 extsram_idx) void ExtSramInitBoardMemory(void* start_phy_address, void* end_phy_address, uint8 extsram_idx)
{ {
register x_size_t offset = 0; register x_size_t offset = 0;
@ -842,12 +844,12 @@ void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint
NULL_PARAM_CHECK(end_phy_address); NULL_PARAM_CHECK(end_phy_address);
KDEBUG_NOT_IN_INTERRUPT; KDEBUG_NOT_IN_INTERRUPT;
struct DynamicBuddyMemory *uheap = &ExtByteManager[extsram_idx].dynamic_buddy_manager; struct DynamicBuddyMemory* uheap = &ExtByteManager[extsram_idx].dynamic_buddy_manager;
/* align begin and end addr to page */ /* align begin and end addr to page */
ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start = ALIGN_MEN_UP((x_ubase)start_phy_address, MM_PAGE_SIZE); ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start = ALIGN_MEN_UP((x_ubase)start_phy_address, MM_PAGE_SIZE);
ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end = ALIGN_MEN_DOWN((x_ubase)end_phy_address, MM_PAGE_SIZE); ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end = ALIGN_MEN_DOWN((x_ubase)end_phy_address, MM_PAGE_SIZE);
KPrintf("%s: 0x%x-0x%x extsram_idx = %d\n",__func__,ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start,ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end, extsram_idx); KPrintf("%s: 0x%x-0x%x extsram_idx = %d\n", __func__, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end, extsram_idx);
/* parameter detection */ /* parameter detection */
if (ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start >= ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end) { if (ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start >= ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end) {
@ -868,7 +870,6 @@ void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint
ExtByteManager[extsram_idx].dynamic_buddy_manager.done = &DynamicDone; ExtByteManager[extsram_idx].dynamic_buddy_manager.done = &DynamicDone;
ExtByteManager[extsram_idx].done = &NodeDone; ExtByteManager[extsram_idx].done = &NodeDone;
/* dynamic buddy memory initialization */ /* dynamic buddy memory initialization */
ExtByteManager[extsram_idx].dynamic_buddy_manager.done->init(&ExtByteManager[extsram_idx].dynamic_buddy_manager, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end - ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start); ExtByteManager[extsram_idx].dynamic_buddy_manager.done->init(&ExtByteManager[extsram_idx].dynamic_buddy_manager, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start, ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_end - ExtByteManager[extsram_idx].dynamic_buddy_manager.dynamic_buddy_start);
} }
@ -880,7 +881,7 @@ void ExtSramInitBoardMemory(void *start_phy_address, void *end_phy_address, uint
* @param start_phy_address the start physical address for static and dynamic memory * @param start_phy_address the start physical address for static and dynamic memory
* @param end_phy_address the end physical address for static and dynamic memory * @param end_phy_address the end physical address for static and dynamic memory
*/ */
void InitBoardMemory(void *start_phy_address, void *end_phy_address) void InitBoardMemory(void* start_phy_address, void* end_phy_address)
{ {
register x_size_t offset = 0; register x_size_t offset = 0;
@ -888,16 +889,16 @@ void InitBoardMemory(void *start_phy_address, void *end_phy_address)
NULL_PARAM_CHECK(end_phy_address); NULL_PARAM_CHECK(end_phy_address);
KDEBUG_NOT_IN_INTERRUPT; KDEBUG_NOT_IN_INTERRUPT;
struct DynamicBuddyMemory *mheap = &ByteManager.dynamic_buddy_manager; struct DynamicBuddyMemory* mheap = &ByteManager.dynamic_buddy_manager;
/* align begin and end addr to page */ /* align begin and end addr to page */
ByteManager.dynamic_buddy_manager.dynamic_buddy_start = ALIGN_MEN_UP((x_ubase)start_phy_address, MM_PAGE_SIZE); ByteManager.dynamic_buddy_manager.dynamic_buddy_start = ALIGN_MEN_UP((x_ubase)start_phy_address, MM_PAGE_SIZE);
ByteManager.dynamic_buddy_manager.dynamic_buddy_end = ALIGN_MEN_DOWN((x_ubase)end_phy_address, MM_PAGE_SIZE); ByteManager.dynamic_buddy_manager.dynamic_buddy_end = ALIGN_MEN_DOWN((x_ubase)end_phy_address, MM_PAGE_SIZE);
//KPrintf("%s: 0x%x-0x%x \n",__func__,ByteManager.dynamic_buddy_manager.dynamic_buddy_start,ByteManager.dynamic_buddy_manager.dynamic_buddy_end); // KPrintf("%s: 0x%x-0x%x \n",__func__,ByteManager.dynamic_buddy_manager.dynamic_buddy_start,ByteManager.dynamic_buddy_manager.dynamic_buddy_end);
/* parameter detection */ /* parameter detection */
if (ByteManager.dynamic_buddy_manager.dynamic_buddy_start >= ByteManager.dynamic_buddy_manager.dynamic_buddy_end) { if (ByteManager.dynamic_buddy_manager.dynamic_buddy_start >= ByteManager.dynamic_buddy_manager.dynamic_buddy_end) {
//KPrintf("InitBoardMemory, wrong address[0x%x - 0x%x]\n", (x_ubase)start_phy_address, (x_ubase)end_phy_address); // KPrintf("InitBoardMemory, wrong address[0x%x - 0x%x]\n", (x_ubase)start_phy_address, (x_ubase)end_phy_address);
SYS_KDEBUG_LOG(KDBG_MEM, ("InitBoardMemory, wrong address[0x%x - 0x%x]\n", (x_ubase)start_phy_address, (x_ubase)end_phy_address)); SYS_KDEBUG_LOG(KDBG_MEM, ("InitBoardMemory, wrong address[0x%x - 0x%x]\n", (x_ubase)start_phy_address, (x_ubase)end_phy_address));
return; return;
} }
@ -916,7 +917,6 @@ void InitBoardMemory(void *start_phy_address, void *end_phy_address)
ByteManager.static_manager[MM_SEGMENT_64B].done = &StaticDone; ByteManager.static_manager[MM_SEGMENT_64B].done = &StaticDone;
ByteManager.done = &NodeDone; ByteManager.done = &NodeDone;
/* dynamic buddy memory initialization */ /* dynamic buddy memory initialization */
ByteManager.dynamic_buddy_manager.done->init(&ByteManager.dynamic_buddy_manager, ByteManager.dynamic_buddy_manager.dynamic_buddy_start, ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start); ByteManager.dynamic_buddy_manager.done->init(&ByteManager.dynamic_buddy_manager, ByteManager.dynamic_buddy_manager.dynamic_buddy_start, ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start);
@ -935,20 +935,19 @@ void InitBoardMemory(void *start_phy_address, void *end_phy_address)
* *
* @return pointer on success; NULL on failure * @return pointer on success; NULL on failure
*/ */
void *x_umalloc(x_size_t size) void* x_umalloc(x_size_t size)
{ {
uint8 i = 0; uint8 i = 0;
void *ret = NONE; void* ret = NONE;
register x_base lock = 0; register x_base lock = 0;
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
/* parameter detection */ /* parameter detection */
if(size == 0 ){ if (size == 0) {
return NONE; return NONE;
} }
if((size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)){ if ((size > ByteManager.dynamic_buddy_manager.dynamic_buddy_end - ByteManager.dynamic_buddy_manager.dynamic_buddy_start - ByteManager.dynamic_buddy_manager.active_memory)) {
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
/* alignment */ /* alignment */
size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
goto try_extmem; goto try_extmem;
@ -956,24 +955,23 @@ void *x_umalloc(x_size_t size)
#else #else
/* parameter detection */ /* parameter detection */
if((size == 0) || (size > UserByteManager.dynamic_buddy_manager.dynamic_buddy_end - UserByteManager.dynamic_buddy_manager.dynamic_buddy_start - UserByteManager.dynamic_buddy_manager.active_memory)) if ((size == 0) || (size > UserByteManager.dynamic_buddy_manager.dynamic_buddy_end - UserByteManager.dynamic_buddy_manager.dynamic_buddy_start - UserByteManager.dynamic_buddy_manager.active_memory))
return NONE; return NONE;
#endif #endif
/* hold lock before allocation */ /* hold lock before allocation */
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
/* alignment */ /* alignment */
size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); size = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
ret = UserByteManager.dynamic_buddy_manager.done->malloc(&UserByteManager.dynamic_buddy_manager,size,DYNAMIC_BLOCK_NO_EXTMEM_MASK); ret = UserByteManager.dynamic_buddy_manager.done->malloc(&UserByteManager.dynamic_buddy_manager, size, DYNAMIC_BLOCK_NO_EXTMEM_MASK);
if(ret != NONE) if (ret != NONE)
CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM));
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
try_extmem: try_extmem:
if(NONE == ret) { if (NONE == ret) {
for(i = 0; i < EXTSRAM_MAX_NUM; i++) { for (i = 0; i < EXTSRAM_MAX_NUM; i++) {
if(NONE != ExtByteManager[i].done) { if (NONE != ExtByteManager[i].done) {
ret = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1)); ret = ExtByteManager[i].dynamic_buddy_manager.done->malloc(&ExtByteManager[i].dynamic_buddy_manager, size, DYNAMIC_BLOCK_EXTMEMn_MASK(i + 1));
if (ret) { if (ret) {
CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM)); CHECK(ExtByteManager[i].dynamic_buddy_manager.done->JudgeLegal(&ExtByteManager[i].dynamic_buddy_manager, ret - SIZEOF_DYNAMICALLOCNODE_MEM));
@ -984,7 +982,7 @@ try_extmem:
} }
#endif #endif
/* release lock */ /* release lock */
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
return ret; return ret;
} }
@ -996,12 +994,12 @@ try_extmem:
* *
* @return pointer on success; NULL on failure * @return pointer on success; NULL on failure
*/ */
void *x_urealloc(void *pointer, x_size_t size) void* x_urealloc(void* pointer, x_size_t size)
{ {
x_size_t newsize = 0; x_size_t newsize = 0;
x_size_t oldsize = 0; x_size_t oldsize = 0;
void *newmem = NONE; void* newmem = NONE;
struct DynamicAllocNode *oldnode = NONE; struct DynamicAllocNode* oldnode = NONE;
/* the given pointer is NULL */ /* the given pointer is NULL */
if (pointer == NONE) if (pointer == NONE)
@ -1012,17 +1010,17 @@ void *x_urealloc(void *pointer, x_size_t size)
x_ufree(pointer); x_ufree(pointer);
return NONE; return NONE;
} }
CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager,pointer)); CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager, pointer));
/* alignment and calculate the real size */ /* alignment and calculate the real size */
newsize = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE); newsize = ALIGN_MEN_UP(size, MEM_ALIGN_SIZE);
newsize += SIZEOF_DYNAMICALLOCNODE_MEM; newsize += SIZEOF_DYNAMICALLOCNODE_MEM;
oldnode= PTR2ALLOCNODE((char*)pointer - SIZEOF_DYNAMICALLOCNODE_MEM); oldnode = PTR2ALLOCNODE((char*)pointer - SIZEOF_DYNAMICALLOCNODE_MEM);
CHECK(UserByteManager.done->JudgeAllocated(oldnode)); CHECK(UserByteManager.done->JudgeAllocated(oldnode));
/* achieve the old memory size */ /* achieve the old memory size */
if(UserByteManager.done->JudgeStaticOrDynamic(oldnode)) { if (UserByteManager.done->JudgeStaticOrDynamic(oldnode)) {
oldsize = ((struct segment*)(oldnode->size))->block_size; oldsize = ((struct segment*)(oldnode->size))->block_size;
} else { } else {
oldsize = oldnode->size - SIZEOF_DYNAMICALLOCNODE_MEM; oldsize = oldnode->size - SIZEOF_DYNAMICALLOCNODE_MEM;
@ -1030,12 +1028,12 @@ void *x_urealloc(void *pointer, x_size_t size)
/* allocate new memory */ /* allocate new memory */
newmem = x_umalloc(size); newmem = x_umalloc(size);
if(newmem == NONE) { if (newmem == NONE) {
return NONE; return NONE;
} }
/* copy the old memory and then release old memory pointer */ /* copy the old memory and then release old memory pointer */
memcpy((char*)newmem, (char*) pointer,size > oldsize ? oldsize : size); memcpy((char*)newmem, (char*)pointer, size > oldsize ? oldsize : size);
x_ufree(pointer); x_ufree(pointer);
return newmem; return newmem;
@ -1049,12 +1047,12 @@ void *x_urealloc(void *pointer, x_size_t size)
* *
* @return pointer on success; NULL on failure * @return pointer on success; NULL on failure
*/ */
void *x_ucalloc(x_size_t count, x_size_t size) void* x_ucalloc(x_size_t count, x_size_t size)
{ {
void *p = NONE; void* p = NONE;
/* parameter detection */ /* parameter detection */
if(count * size > UserByteManager.dynamic_buddy_manager.dynamic_buddy_end - UserByteManager.dynamic_buddy_manager.dynamic_buddy_start - UserByteManager.dynamic_buddy_manager.active_memory) if (count * size > UserByteManager.dynamic_buddy_manager.dynamic_buddy_end - UserByteManager.dynamic_buddy_manager.dynamic_buddy_start - UserByteManager.dynamic_buddy_manager.active_memory)
return NONE; return NONE;
/* calls x_malloc to allocate count * size memory */ /* calls x_malloc to allocate count * size memory */
@ -1072,37 +1070,37 @@ void *x_ucalloc(x_size_t count, x_size_t size)
* *
* @param pointer the memory to be released * @param pointer the memory to be released
*/ */
void x_ufree(void *pointer) void x_ufree(void* pointer)
{ {
x_base lock = 0; x_base lock = 0;
struct DynamicAllocNode *node = NONE; struct DynamicAllocNode* node = NONE;
/* parameter detection */ /* parameter detection */
if (pointer == NONE) if (pointer == NONE)
return ; return;
CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager,pointer)); CHECK(UserByteManager.dynamic_buddy_manager.done->JudgeLegal(&UserByteManager.dynamic_buddy_manager, pointer));
/* hold lock before release */ /* hold lock before release */
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
node = PTR2ALLOCNODE((char*)pointer-SIZEOF_DYNAMICALLOCNODE_MEM); node = PTR2ALLOCNODE((char*)pointer - SIZEOF_DYNAMICALLOCNODE_MEM);
CHECK(UserByteManager.done->JudgeAllocated(node)); CHECK(UserByteManager.done->JudgeAllocated(node));
#ifdef MEM_EXTERN_SRAM #ifdef MEM_EXTERN_SRAM
/* judge the pointer is not malloced from extern memory*/ /* judge the pointer is not malloced from extern memory*/
if(0 == (node->flag & 0xFF0000)) { if (0 == (node->flag & 0xFF0000)) {
UserByteManager.dynamic_buddy_manager.done->release(&ByteManager,pointer); UserByteManager.dynamic_buddy_manager.done->release(&ByteManager, pointer);
} }
/* judge the pointer is malloced from extern memory*/ /* judge the pointer is malloced from extern memory*/
if(0 != (node->flag & 0xFF0000)) { if (0 != (node->flag & 0xFF0000)) {
ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1].dynamic_buddy_manager.done->release(&ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1],pointer); ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1].dynamic_buddy_manager.done->release(&ExtByteManager[((node->flag & 0xFF0000) >> 16) - 1], pointer);
} }
#else #else
UserByteManager.dynamic_buddy_manager.done->release(&UserByteManager,pointer); UserByteManager.dynamic_buddy_manager.done->release(&UserByteManager, pointer);
#endif #endif
/* release the lock */ /* release the lock */
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
} }
/** /**
@ -1213,7 +1211,7 @@ void ShowBuddy(void)
int lock = 0; int lock = 0;
struct DynamicFreeNode *debug = NONE; struct DynamicFreeNode *debug = NONE;
lock = CriticalAreaLock(); lock = FREE_LIST_LOCK();
KPrintf("\n\033[41;1mlist memory information\033[0m\n", __func__); KPrintf("\n\033[41;1mlist memory information\033[0m\n", __func__);
for(int level = 0; level < MEM_LINKNRS; level++) { for(int level = 0; level < MEM_LINKNRS; level++) {
KPrintf("%s level [%d],memory size[2^%d] \n",__func__, level,level +6); KPrintf("%s level [%d],memory size[2^%d] \n",__func__, level,level +6);
@ -1258,7 +1256,7 @@ void ShowBuddy(void)
} }
#endif #endif
CriticalAreaUnLock(lock); FREE_LIST_UNLOCK(lock);
} }
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),
ShowBuddy,ShowBuddy,list memory usage information); ShowBuddy,ShowBuddy,list memory usage information);