malloc.c
上传用户:super_houu
上传日期:2008-09-21
资源大小:4099k
文件大小:8k
- #include "Config.h" // Global Configuration - do not remove!
- #include "KerneluITRONRTOS.h"
- #include <stdlib.h>
- #include <embedded.h>
- #include "IncludeSysDefs.h"
- #include "ServicesInclude_heap.h"
- #ifdef HEAP_STATISTIC
- #define HEAP_MAX_BLOCKS 85
- void* heap_block_ptrs[HEAP_MAX_BLOCKS]={0};
- unsigned int heap_blocks_size[HEAP_MAX_BLOCKS]={0};
- int heap_current_block_index=0;
- int heap_malloc_fail_num=0;
- int heap_current_malloc_size=0;
- int heap_max_malloc_size=0;
- int heap_current_malloc_count=0;
- int heap_max_malloc_count=0;
- int heap_extra_free_done=0;
- int heap_size_over=0;
- int heap_no_free_space=0;
- int max_nast_free=0;
- int max_nast_malloc=0;
- int stat_max_block_size=0;
- #endif
- #pragma codeseg _TEXT "CODE"
- extern void * I49malloc (size_t __size, int pool_id);
- extern void I49free(void near *__block, int pool_id);
- extern void *I49realloc(void near *__block, size_t __size, int pool_id);
- //#define HEAP_DEBUG 1
- #ifdef D_SUPPORT_MEM_POOL
- ///////////////////////////////////////////////////////////////////////////
- // Function name : MEM_Allocate
- // Purpose : Allocate memory block from pool.
- // Input Parameters : pool_id - The index of memory pool to allcate from.
- // size - The size of requested block in size.
- // Return type : Pointer of allocated memory block.
- // Reutrn NULL if allocation failed.
- ///////////////////////////////////////////////////////////////////////////
- void *MEM_Allocate(int pool_id, size_t size)
- {
- #ifdef HEAP_DEBUG
- void * pMem = I49malloc (size, pool_id);
- tr_printf(("nMEM_Allocate(%d, %u) %p", pool_id, size, pMem));
- return pMem;
- #else
- return I49malloc (size, pool_id);
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////
- // Function name : MEM_Free
- // Purpose : Free memory block.
- // Input Parameters : pool_id - The index of memory pool that allocated from.
- // __block - Pointer of memory block to be freed.
- // Return type : NONE
- ///////////////////////////////////////////////////////////////////////////
- void MEM_Free(int pool_id, void *__block)
- {
- I49free ((void __near *)__block, pool_id);
- #ifdef HEAP_DEBUG
- tr_printf (("MEM_Free( %d, %p)n",pool_id, __block));
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////
- // Function name : MEM_PoolConstruct
- // Purpose : Construct memory pool.
- // Input Parameters : pool_id - The index of memory pool to be constructed.
- // __ptr - Start address of memory pool.
- // __size - Size of memory pool in byte.
- // Return type : NONE
- // Description : Initialized pool structure.
- ///////////////////////////////////////////////////////////////////////////
- void MEM_PoolConstruct(int pool_id, unsigned char *__ptr, int __size)
- {
- asm pushf
- asm cli
-
- _mempool[pool_id].head = (void __near *)__ptr;
- _mempool[pool_id].first = 0;
- _mempool[pool_id].last = 0;
- _mempool[pool_id].rover = 0;
- _mempool[pool_id].brklvl = (void __near *)__ptr;
- _mempool[pool_id].heaplimit = (void __near *)(__ptr + __size);
- asm popf
- #ifdef HEAP_DEBUG
- tr_printf (("MEM_PoolConstruct( %d, %p, %d)n",pool_id, __ptr, __size));
- #endif
- }
- ///////////////////////////////////////////////////////////////////////////
- // Function name : MEM_PoolDestruct
- // Purpose : Destruct memory pool.
- // Input Parameters : pool_id - The index of memory pool to be destructed.
- // Return type : TRUE if success, FALSE otherwise
- // Description : Check if all memory blocks are freed, otherwise fail.
- ///////////////////////////////////////////////////////////////////////////
- BOOL MEM_PoolDestruct(int pool_id)
- {
- asm pushf
- asm cli
-
- if (_mempool[pool_id].head != _mempool[pool_id].brklvl)
- {
- asm popf
- #ifdef HEAP_DEBUG
- tr_printf (("Fail - MEM_PoolDestruct( %d ), pool not empty.n",pool_id));
- #endif
- return FALSE; // Not all memory is freed.
- }
- _mempool[pool_id].head = 0;
- asm popf
- #ifdef HEAP_DEBUG
- tr_printf (("MEM_PoolDestruct( %d )n",pool_id));
- #endif
- return TRUE;
- }
- ///////////////////////////////////////////////////////////////////////////
- // Function name : MEM_PoolResize
- // Purpose : Change the size of memory pool.
- // Input Parameters : pool_id - The index of memory pool to be destructed.
- // : __size - The new size of memory pool
- // Return type : TRUE if success, FALSE otherwise
- // Description : If the new size is smaller than old size, check if it violates with allcated blocks
- ///////////////////////////////////////////////////////////////////////////
- BOOL MEM_PoolResize(int pool_id, int __size)
- {
- asm pushf
- asm cli
-
- if (_mempool[pool_id].brklvl > (_mempool[pool_id].head + __size))
- {
- asm popf
- #ifdef HEAP_DEBUG
- tr_printf (("Fail - MEM_PoolResize( %d, %d ), can't shrink.n",pool_id, __size));
- #endif
- return FALSE; //Can't shrink pool because some memory not freed.
- }
-
- _mempool[pool_id].heaplimit = _mempool[pool_id].head + __size;
- asm popf
- #ifdef HEAP_DEBUG
- tr_printf (("MEM_PoolResize( %d, %d )n",pool_id, __size));
- #endif
- return TRUE;
- }
- #endif //D_SUPPORT_MEM_POOL
- void *malloc(size_t __size)
- {
- #ifdef HEAP_DEBUG
- void * pMem = I49malloc (__size, DEFAULT_HEAP);
- tr_printf (("malloc %p, 0x%04Xn",pMem,__size));
- return pMem;
- #elif defined(HEAP_STATISTIC)
- static int curr_nast=0;
- void* heap_temp_malloc=I49malloc (__size, DEFAULT_HEAP);
-
- STATUS_REG SRkeep=InterruptDisable();
- curr_nast++;
- if ( curr_nast > max_nast_malloc)
- max_nast_malloc=curr_nast;
-
- if( heap_temp_malloc != NULL)
- {
- int malloc_iter;
- int flag=0;
-
- if( __size > stat_max_block_size )
- stat_max_block_size=__size;
- heap_current_malloc_size+=__size;
- if(heap_current_malloc_size>heap_max_malloc_size)
- heap_max_malloc_size=heap_current_malloc_size;
- for(malloc_iter=0;malloc_iter<HEAP_MAX_BLOCKS;malloc_iter++)
- {
- if(heap_block_ptrs[malloc_iter]==NULL)
- {
- flag=1;
- heap_blocks_size[malloc_iter]=__size;
- heap_block_ptrs[malloc_iter]=heap_temp_malloc;
- break;
- }
- }
- heap_current_malloc_count++;
- if( (heap_current_malloc_count %10)==0 )
- heap_current_malloc_count=heap_current_malloc_count;
- if(heap_current_malloc_count>heap_max_malloc_count)
- heap_max_malloc_count=heap_current_malloc_count;
- if (flag ==0 )
- {
- heap_no_free_space++;
- tr_printf(("nmalloc fail [0]. "));
- asm int 3;
- }
- } else {
- heap_malloc_fail_num++;
- tr_printf(("nmalloc fail [1]. "));
- asm int 3;
- }
- curr_nast--;
- set_SR(SRkeep);
- tr_printf (("malloc %p, 0x%04Xn",heap_temp_malloc,__size));
- return heap_temp_malloc;
- #else //HEAP_STATISTIC
- return I49malloc (__size, DEFAULT_HEAP);
- #endif
- }
- void free(void *__block)
- {
- #ifdef HEAP_DEBUG
- I49free ((void __near *)__block, DEFAULT_HEAP);
- tr_printf (("free %pn",__block));
- #elif defined(HEAP_STATISTIC)
- STATUS_REG SRkeep=InterruptDisable();
- int free_iter;
- int flag=0;
- static int curr_nast=0;
- curr_nast++;
- heap_current_malloc_count--;
- if ( curr_nast > max_nast_free)
- max_nast_free=curr_nast;
- for(free_iter=0;free_iter<HEAP_MAX_BLOCKS;free_iter++)
- {
- if(heap_block_ptrs[free_iter]==__block)
- {
- heap_current_malloc_size-=heap_blocks_size[free_iter];
- heap_block_ptrs[free_iter]=NULL;
- flag=1;
- break;
- }
- }
-
- if(flag== 0) {
- heap_extra_free_done++;
- tr_printf(("nfree fail [0]. "));
- asm int 3;
- }
- curr_nast--;
- set_SR(SRkeep);
- I49free ((void __near *)__block, DEFAULT_HEAP);
- tr_printf (("free %pn",__block));
- return;
- #else //HEAP_STATISTIC
- I49free ((void __near *)__block, DEFAULT_HEAP);
- #endif
- }
- void *realloc(void *__block, size_t __size)
- {
- #ifdef HEAP_DEBUG
- void * pMem = I49realloc ((void __near *)__block,__size, DEFAULT_HEAP);
- tr_printf (("malloc %p, 0x%04Xn",pMem,__size));
- return pMem;
- #else
- return I49realloc ((void __near *)__block,__size, DEFAULT_HEAP);
- #endif
- }
- extern void _I49InitHeapConstants(void);
- #pragma startup _I49InitHeapConstants 1