当前位置:编程学习 > 网站相关 >>

AMPS:内存管理模块源码解读(二)

本节看看另一个实现方式。此方法思路如下:其内存池结构为一个存放已分配内存信息的双链表,一个表示内存池大小的变量,一个指向当前内存链表结点的指针链表,如下:
 
 
[cpp] 
/*内存池结构*/  
struct _newMMContext  
{  
    t_AMPSDList* memBuffList;      /*结点链表*/  
    int nSizeOfBuff;               /*池大小*/  
    t_AMPSSList* poCurrentListPtr; /*当前所指结点*/  
};  
 
/*内存池结构*/
struct _newMMContext
{
    t_AMPSDList* memBuffList;      /*结点链表*/
    int nSizeOfBuff;               /*池大小*/
t_AMPSSList* poCurrentListPtr; /*当前所指结点*/
};
 
其中,内存结点链表各结点结构如下:
 
 
[cpp] view plaincopyprint?
/*内存链表结点结构*/  
struct _newMM_MemoryNode  
{  
int nAllocatedBytesInBlock; /*已分配字节数*/  
char* pchCurrPtrInBlock;    /*当前指针*/  
char* memBuff;              /*内存*/  
};  
 
/*内存链表结点结构*/
struct _newMM_MemoryNode
{
int nAllocatedBytesInBlock; /*已分配字节数*/
char* pchCurrPtrInBlock;    /*当前指针*/
char* memBuff;              /*内存*/
};
在memBuff中,每分配一个内存块,当前phCurrPtrInBlock向后移动需要分配的字节数+内存描述信息区。
 
其中内存信息描述区的结构如下:
 
 
[cpp]   
/*内存链表描述结构*/  
struct _newMMBuffDescriptor  
{  
char markerBytes[4]; /*存放值DEAD,此值在分配时写,在释放时比较,如不一致,说明内存已出错。*/  
int nSizeOfBuff;     /*此块内存的大小*/  
t_AMPSSList* dlistNode;/*存放已分配的内存结点指针*/  
};  
 
/*内存链表描述结构*/
struct _newMMBuffDescriptor
{
char markerBytes[4]; /*存放值DEAD,此值在分配时写,在释放时比较,如不一致,说明内存已出错。*/
int nSizeOfBuff;     /*此块内存的大小*/
t_AMPSSList* dlistNode;/*存放已分配的内存结点指针*/
};
此结构主要用于在内存释放时判断是否有重复释放或者猜踩内存的现象,前面markerBytes为每一小块内存的开头信息,固定值"DEAD",相当于一个检验信息。
 
下面看看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 struct  _newMMContext     t_NewMMContext;  
typedef struct  _newMM_MemoryNode t_NewMM_MemoryNode;  
typedef struct  _newMMBuffDescriptor t_NewMMBuffDescriptor;  
typedef struct  _newMemSizeCmp         t_NewMemSizeCmp;  
  
  
/*内存池结构*/  
struct _newMMContext  
{  
    t_AMPSDList* memBuffList;      /*结点链表*/  
    int nSizeOfBuff;               /*池大小*/  
    t_AMPSSList* poCurrentListPtr; /*当前所指结点*/  
};  
  
/*内存链表结点结构*/  
struct _newMM_MemoryNode  
{  
int nAllocatedBytesInBlock; /*已分配字节数*/  
char* pchCurrPtrInBlock;    /*当前指针*/  
char* memBuff;              /*内存*/  
};  
  
/*内存链表描述结构*/  
struct _newMMBuffDescriptor  
{  
char markerBytes[4]; /*存放值DEAD,此值在分配时写,在释放时比较,如不一致,说明内存已出错。*/  
int nSizeOfBuff;     /*此块内存的大小*/  
t_AMPSSList* dlistNode;/*存放已分配的内存结点指针*/  
};  
  
/*内存大小比较结构*/  
struct _newMemSizeCmp  
{  
int nSizeRequired;  
int maxSize;  
};  
  
void* MM_New_Init(int nSizeOfBuff);  
int compareAvailableBytes(void* size, void* listData);  
void* MM_New_Malloc(void* mm_Object,int nSize);  
void MM_New_Free(void* mm_Object,char* buffToFree);  
  
  
  
  
#ifdef __cplusplus   
   }  
#endif   
  
#endif /* __HEADER_AMPS_MEMORY_MGMT_H__ */  
 
#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 struct  _newMMContext     t_NewMMContext;
typedef struct  _newMM_MemoryNode t_NewMM_MemoryNode;
typedef struct  _newMMBuffDescriptor t_NewMMBuffDescriptor;
typedef struct  _newMemSizeCmp         t_NewMemSizeCmp;
 
 
/*内存池结构*/
struct _newMMContext
{
    t_AMPSDList* memBuffList;      /*结点链表*/
    int nSizeOfBuff;               /*池大小*/
t_AMPSSList* poCurrentListPtr; /*当前所指结点*/
};
 
/*内存链表结点结构*/
struct _newMM_MemoryNode
{
int nAllocatedBytesInBlock; /*已分配字节数*/
char* pchCurrPtrInBlock;    /*当前指针*/
char* memBuff;              /*内存*/
};
 
/*内存链表描述结构*/
struct _newMMBuffDescriptor
{
char markerBytes[4]; /*存放值DEAD,此值在分配时写,在释放时比较,如不一致,说明内存已出错。*/
int nSizeOfBuff;     /*此块内存的大小*/
t_AMPSSList* dlistNode;/*存放已分配的内存结点指针*/
};
 
/*内存大小比较结构*/
struct _newMemSizeCmp
{
int nSizeRequired;
int maxSize;
};
 
void* MM_New_Init(int nSizeOfBuff);
int compareAvailableBytes(void* size, void* listData);
void* MM_New_Malloc(void* mm_Object,int nSize);
void MM_New_Free(void* mm_Object,char*
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,