在AMPS中,使用内存池来管理内存,具体机制见之前的文章《AMPS:内存管理(二)》,在代码中,使用了两种实现方式,第一种是数组+单链表。今天先看看这个方式下的内存池实现。
AMPS_MemoryMgt.h
[cpp]
#ifndef __HEADER_AMPS_MEMORY_MGMT_H__
#define __HEADER_AMPS_MEMORY_MGMT_H__
#include "AMPS_Defines.h"
#include "AMPS_Trace.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void*(*AMPS_InternalMallocCallback)(int r_unSize);
typedef void*(*AMPS_InternalReallocCallback)(void* r_pvData, int r_unSize);
typedef void(*AMPS_InternalFreeCallback)(void* r_pvData);
// we need at least these many buffers
// _
// 1- 8 bytes |
// 2- 16 bytes |
// 3- 32 bytes > Lower limit chunks
// 4- 64 bytes |
// 5- 128 bytes |
// _
// _
// 6- 8 Kbytes |
// 7- 16 Kbytes > upper limit chunks
// 8- 32 Kbytes |
// 9- 64 Kbytes |
// _
#define MAX_SIZE 65536 + 1
#define LN_2 .6931471805599453 //val = log(x)/log(2)
#define LN2_OF_1024 10
#define NO_OF_SMALL_MEM_POOLS 32 //1024/32
#define NO_OF_LARGE_MEM_POOLS 10
#define NO_OF_BLOCKS_OF_SMALL_MEM_POOL 1000
#define MINIMUM_SMALL_MEM_BLOCK_SIZE 32
#define NO_OF_BLOCKS_OF_LARGE_MEM_POOL 500
#define MINIMUM_LARGE_MEM_BLOCK_SIZE 2048
#define OFFSET 21 // LN2_OF_1024 + 1024/32 -1
typedef struct _node t_node;
typedef struct _MemoryPool t_MemoryPool;
typedef struct _MemContext t_MemContext;
//requested size lies in the lower range of allocated buffers
#define GET_MEM_BUFFER_INDEX(size) (size <= 1024 ? (size -1)/32 : (int)(ceil((float)(log(size)/LN_2))) + OFFSET)
#define GET_INDEX_FROM_BUFFER(dataPtr) (*((int*)(dataPtr - sizeof(int))) < NO_OF_SMALL_MEM_POOLS+NO_OF_LARGE_MEM_POOLS ? *((int*)(dataPtr - sizeof(int))) : AMPS_ERROR_FAILURE)
/*链表结点结构*/
struct _node
{
t_node* poAMPSSListNext;
t_node* poAMPSSListPrev;
};
/*池中链表结构*/
struct _MemoryPool
{
t_node* poHead;
unsigned char* puchBuffer;
unsigned int nSize;
t_MemoryPool* poMemoryPoolNext;
};
/*内存池结构*/
struct _MemContext
{
AMPS_InternalMallocCallback pfAMPS_InternalMallocCallback;
AMPS_InternalReallocCallback pfAMPS_InternalReallocCallback;
AMPS_InternalFreeCallback pfAMPS_InternalFreeCallback;
int nNoOfMallocWithoutFree;
int nNoOfMallocWithoutFree2;
AMPS_BOOL bMMEnable;
t_MemoryPool poMemoryPools[NO_OF_SMALL_MEM_POOLS+NO_OF_LARGE_MEM_POOLS];
int nMallocCounts[NO_OF_SMALL_MEM_POOLS+NO_OF_LARGE_MEM_POOLS];
};
void* MM_Init(int r_bMMEnable);
void MM_Cleanup(void);
void MM_BufferInit(t_MemoryPool* r_poMemoryPools, int r_nBufferIndex, int r_nSize, int r_nChunks);
void MM_BufferDestroy(t_MemoryPool* r_poMemoryPools);
void* AMPS_InternalMalloc(int r_nSize);
void* AMPS_InternalRealloc(void* r_pvData, int r_nSize);
void AMPS_InternalFree(void* r_pvData);
void* OS_Malloc(int r_nSize);
void* OS_Realloc(void* r_pvData, int r_nSize);
void OS_Free(void* r_pvData);
void* MM_Malloc(int r_nSize);
void* MM_Realloc(void* r_pvData, int r_unSize);
void MM_Free(void* r_pvData);
void MM_BufferNew(int r_nSize);
int MM_GetNoOfChunks(int r_nSize);
void MM_BufferShow(t_MemoryPool* r_poMemoryPools, int r_nSize);
void MM_BufferDump(unsigned char* r_puchBuff, int r_nSize);
void MM_CalculateAndDisplayMemoryStats (void);
#ifdef __cplusplus
}
#endif
#endif /* __HEADER_AMPS_MEMORY_MGMT_H__ */
#ifndef __HEADER_AMPS_
补充:软件开发 , C++ ,