- #include "avit_memory_pool.h"
- #include "avit_oc_print.h"
- static UINT32 peak_memory_use = 0;
- static UINT32 now_memory_use = 0;
- static BYTE* pool_adr = NULL;
- static MemNode* pool_link = NULL;
- static MemNode* memory_pool_first_fitting(UINT32 size);
- void memory_pool_dump()
- {
- MemNode* temp_node = pool_link;
- temp_node = pool_link;
- while(TRUE)
- {
- if( temp_node == NULL ) break;
- oc_println("[0x%08X][0x%08X][0x%08X][0x%08X][%d][%d]",
- temp_node->last,
- temp_node,
- temp_node->next,
- temp_node->addr,
- temp_node->size,
- temp_node->used
- );
- temp_node = temp_node->next ;
- }
- }
- UINT32 memory_pool_get_available_size()
- {
- return (MEMORY_POOL_SIZE-now_memory_use);
- }
- UINT32 memory_pool_get_peak_use()
- {
- return peak_memory_use;
- }
- UINT32 memory_pool_get_now_use()
- {
- return now_memory_use;
- }
- INT32 memory_pool_init()
- {
- if(!MEMORY_POOL_ENABLE) return J_SUCCESS;
- memory_pool_release();
- oc_println("memory pool initialized");
- oc_println("memory pool size is %d",MEMORY_POOL_SIZE);
- if(J_OSP_GetAvailableMemorySize() < MEMORY_POOL_SIZE )
- {
- oc_println("There is no enough memor for oc");
- return J_FAILURE;
- }
- // 分配池所要用到的内存
- pool_adr = J_OSP_AllocateMemory(MEMORY_POOL_SIZE);
- if(pool_adr == NULL) return J_FAILURE;
- // 建立池的第一个结点
- pool_link = (MemNode*)J_OSP_AllocateMemory(sizeof(MemNode));
- pool_link->addr = pool_adr;
- pool_link->used = FALSE;
- pool_link->last = NULL;
- pool_link->next = NULL;
- pool_link->size = MEMORY_POOL_SIZE;
- if(MEMORY_POOL_COUNTER_ENABLE)
- {
- peak_memory_use = 0;
- now_memory_use = 0;
- }
- return J_SUCCESS;
- }
- static MemNode* memory_pool_first_fitting(UINT32 size)
- {
- MemNode* this_node = NULL;
- MemNode* temp_node = pool_link;
- while(TRUE)
- {
- if(temp_node == NULL) break;
- if( (temp_node->size >= size) && (temp_node->used == FALSE) )
- {
- this_node = temp_node;
- break;
- }
- temp_node = temp_node->next;
- }
- return this_node;
- }
- void* memory_pool_malloc(UINT32 size)
- {
- // 目前采用首次拟合
- MemNode* this_node = NULL;
- UINT32 nee_size = size;
- if(!MEMORY_POOL_ENABLE) return J_OSP_AllocateMemory(size);
- if(size == 0)
- {
- oc_println("abnormal memeory malloc size is 0");
- return NULL;
- }
- if( (nee_size % 4) != 0 )
- nee_size += (4-(nee_size%4));
- this_node = memory_pool_first_fitting(nee_size);
- if(this_node == NULL)
- {
- oc_println("malloc memeory faild,size is %d",size);
- return NULL;
- }
- size = nee_size;
- if(this_node->size == size)
- {
- this_node->used = TRUE;
- if(MEMORY_POOL_COUNTER_ENABLE)
- {
- now_memory_use += size;
- if(now_memory_use >= peak_memory_use) peak_memory_use = now_memory_use;
- }
- return (void*)this_node->addr;
- }
- // 操作链表关系
- {
- MemNode* last_node = this_node->last;
- MemNode* new_node = (MemNode *)J_OSP_AllocateMemory(sizeof(MemNode));
- if( this_node == pool_link )
- pool_link = new_node;
- else
- last_node->next = new_node;
- // 处理 new node
- new_node->addr = this_node->addr;
- new_node->used = TRUE;
- new_node->last = last_node;
- new_node->next = this_node;
- new_node->size = size;
- // 处理 this node
- this_node->addr += size;
- this_node->last = new_node;
- this_node->size -= size;
- if(MEMORY_POOL_COUNTER_ENABLE)
- {
- now_memory_use += size;
- if(now_memory_use >= peak_memory_use) peak_memory_use = now_memory_use;
- }
- return (void*)new_node->addr;
- }
- }
- void memory_pool_free(void* pAddr)
- {
- // 找到 pAddr 所在的 memnode
- MemNode* this_node = NULL;
- MemNode* temp_node = pool_link;
- if(!MEMORY_POOL_ENABLE)
- {
- J_OSP_FreeMemory(pAddr);
- return;
- }
- while(TRUE)
- {
- if( temp_node == NULL ) break;
- if( temp_node->addr == pAddr)
- {
- this_node = temp_node;
- break;
- }
- temp_node = temp_node->next ;
- }
- if(this_node == NULL)
- {
- oc_println("abnormal memory free!!");
- return;
- }
- this_node->used = FALSE;
- if(MEMORY_POOL_COUNTER_ENABLE) now_memory_use -= this_node->size;
- // 处理链表关系
- {
- MemNode* last_node = this_node->last;
- MemNode* next_node = this_node->next;
- // 第一次合并,和前一个块(last_node)进行合并
- if(last_node != NULL)
- {
- // 与 last_node 合并
- if( last_node->used == FALSE)
- {
- last_node->size += this_node->size;
- last_node->next = next_node;
- if(next_node != NULL)
- next_node->last = last_node;
- // 释放 this_node
- J_OSP_FreeMemory(this_node);
- this_node = NULL;
- }
- }
- // 第二次合并,需要判断前面合并过没
- if(this_node == NULL)// 已经和前面合并过了
- {
- // 判断是否需要和下一个块合并
- if(next_node != NULL)
- {
- if(next_node->used == FALSE) // next node 是空闲的
- {
- // 处理 last node
- last_node->next = next_node->next;
- last_node->size += next_node->size;
- // 处理 next_node;
- if(next_node->next != NULL)
- next_node->next->last = last_node;
- // 清除next_node
- J_OSP_FreeMemory(next_node);
- next_node = NULL;
- }
- }
- }
- else // 没合并过
- {
- // 判断是否需要和下一个块合并
- if(next_node != NULL)
- {
- if(next_node->used == FALSE) // next node 是空闲的
- {
- this_node->next = next_node->next;
- this_node->size += next_node->size;
- if(next_node->next != NULL)
- next_node->next->last = this_node;
- J_OSP_FreeMemory(next_node);
- next_node = NULL;
- }
- }
- }
- }
- }
- void memory_pool_release()
- {
- if(!MEMORY_POOL_ENABLE) return;
- memory_pool_clear();
- // 清除第一个结点
- if(pool_link != NULL)
- {
- J_OSP_FreeMemory(pool_link);
- pool_link = NULL;
- }
- if( pool_adr != NULL)
- {
- J_OSP_FreeMemory(pool_adr);
- pool_adr = NULL;
- }
- if(MEMORY_POOL_COUNTER_ENABLE)
- oc_println("peak memory use %d",peak_memory_use);
- peak_memory_use = 0;
- now_memory_use = 0;
- oc_println("memory pool released.");
- }
- void memory_pool_clear()
- {
- // 释放池的管理内存,删到只剩下一个结点
- UINT32 garbage_count = 0;
- MemNode* temp = NULL;
- if(pool_link == NULL) return;
- if(pool_link->next == NULL) return;
- while(TRUE)
- {
- temp = pool_link;
- pool_link = pool_link->next;
- J_OSP_FreeMemory(temp);
- temp = NULL;
- if(MEMORY_POOL_COUNTER_ENABLE) garbage_count++;
- if(pool_link->next == NULL) break;
- }
- pool_link->addr = pool_adr;
- pool_link->used = FALSE;
- pool_link->last = NULL;
- pool_link->next = NULL;
- pool_link->size = MEMORY_POOL_SIZE;
- now_memory_use = 0;
- if(MEMORY_POOL_COUNTER_ENABLE) oc_println("memory garbage count is %d",garbage_count);
- }
嵌入式(标准C环境下)下通用的内存池的实现---C文件
最新推荐文章于 2023-10-22 10:10:42 发布