www.pudn.com > HTTP代理服务器.rar > MemPool.h
#if !defined(AFX_MEMPOOL_H__8F60E9B2_1F69_45E6_A7D9_30FCD75327D0__INCLUDED_) #define AFX_MEMPOOL_H__8F60E9B2_1F69_45E6_A7D9_30FCD75327D0__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "MyProxy.h" //头文件 #include#include #include #include #define DEBUG 1 //宏定义 #define BLOCK 1024 #define MAXNUM 8 //数据结构 struct ListNode { void * ptr; struct ListNode * next; }; ListNode lstMemRoot[MAXNUM];//未分配节点链表 ListNode lstNodeBuff; //已分配节点链表 CRITICAL_SECTION cs; //临界段 //初始化内存池 void InitializeMemPool() { InitializeCriticalSection(&cs); if(DEBUG) { Log("初始化临界段"); } for(int i=0;i BLOCK * MAXNUM) { ptr = malloc(piMemLen+2); //直接分配,多分配2个字节 *(char*)ptr = MAXNUM; // return (void*)((char*)ptr+2); } iChoose = (piMemLen-1)/BLOCK; if(lstMemRoot[iChoose].next == NULL) { //直接分配内存 ptr = malloc(piMemLen+2); //多分配2个字节 *((char*)ptr) = iChoose; return (void*)((char*)ptr+2); } else { //进入临界区域 EnterCriticalSection(&cs); if(DEBUG) { Log("进入临界段"); } tmp = lstMemRoot[iChoose].next; //删除此节点 lstMemRoot[iChoose].next = tmp->next; //将此节点插入到节点链表 tmp->next = lstNodeBuff.next; lstNodeBuff.next = tmp; if(DEBUG) { Log("退出临界段"); } LeaveCriticalSection(&cs); return tmp->ptr; } return 0; } void FreeMem(void *pMemPtr) { ListNode * tmp = NULL; int iChoose = 0; //选择链表 iChoose = *((char*)pMemPtr-2); // // if(iChoose > MAXNUM-1) { free((char*)pMemPtr-2); return; } //进入临界区域 EnterCriticalSection(&cs); if(DEBUG) { Log("进入临界段"); } //如果已分配节点链表为空 if(lstNodeBuff.next == NULL) { tmp = (ListNode*)malloc(sizeof(ListNode)); tmp -> ptr = NULL; tmp -> next = NULL; } else { tmp = lstNodeBuff.next; tmp -> ptr = NULL; tmp -> next = lstNodeBuff.next -> next; lstNodeBuff.next = tmp -> next; } //保存回收的内存地址 tmp -> next = lstMemRoot[iChoose].next; tmp -> ptr = pMemPtr; lstMemRoot[iChoose].next = tmp; if(DEBUG) { Log("退出临界段"); } LeaveCriticalSection(&cs); } void CloseMemPool() { DeleteCriticalSection(&cs); if(DEBUG) { Log("销毁临界段"); } ListNode * pos = NULL; //释放链表 for(int i=0;i ptr != NULL) { free(pos->ptr); //释放内存 } pos = pos->next; } } //释放链表 pos = lstNodeBuff.next; while(pos != NULL) { if(pos->ptr != NULL) { free(pos->ptr); //释放指针 } pos = pos->next; } //恢复全局变量初始值 for(i=0;i