并行机制,无需考虑多核与锁......
#pragma once
#pragma pack(1)
typedef struct _FAST_MEMORY_LIST {
union {
struct {
unsigned short uCpuNumber;
unsigned short uName;
};
unsigned int uTags;
};
unsigned short uFlag;
unsigned short uSize;
void * pAddress;
_FAST_MEMORY_LIST * pNext;
//_FAST_MEMORY_LIST * pReve;
} FAST_MEMORY_LIST, *PFAST_MEMORY_LIST;
#pragma pack()
enum _FAST_MEMORY_FLAG_
{
moff,
mon
};
#define FAST_GET_ADDRESS(v) ((PFAST_MEMORY_LIST)(v)->pAddress)
#define FAST_GET_SIZE(v) ((PFAST_MEMORY_LIST)(v)->uSize)
static PFAST_MEMORY_LIST g_phMemList = 0;
//可初始化
static PFAST_MEMORY_LIST __cdecl fast_malidx(void)
{
register size_t size;
register unsigned short i = 0;
SYSTEM_INFO sutSystemInfo;
if (0 == g_phMemList)
{
::GetSystemInfo(&sutSystemInfo);
//内核下请调用KeQueryActiveProcessorCount函数;
size = sizeof(FAST_MEMORY_LIST) * sutSystemInfo.dwNumberOfProcessors;
//内核下请调用ExAllocatePoolWithTag函数;
g_phMemList = (PFAST_MEMORY_LIST)malloc(size);
if (0 == g_phMemList) {
return 0;
}
for (; i < sutSystemInfo.dwNumberOfProcessors; ++i)
{
g_phMemList[i].uCpuNumber = i;
g_phMemList[i].uName = 'Al';
g_phMemList[i].uFlag = moff;
g_phMemList[i].uSize = 0;
g_phMemList[i].pAddress = 0;
g_phMemList[i].pNext = 0;
}
}
//内核下请调用KeGetCurrentProcessorNumber函数;
return (&g_phMemList[::GetCurrentProcessorNumber()]);
}
static void * __cdecl fast_malloc(size_t s)
{
register PFAST_MEMORY_LIST pMemoryList = fast_malidx();
register PFAST_MEMORY_LIST pLastMemoryList = 0;
while (pMemoryList != 0)
{
if (pMemoryList->uFlag != mon && pMemoryList->uSize >= s)
{
pMemoryList->uFlag = mon;
return (pMemoryList);
}
else if (0 == pMemoryList->uSize)
{
pMemoryList->pAddress = malloc(s);
if (0 == pMemoryList->pAddress) {
return 0;
}
pMemoryList->uSize = (unsigned short)s;
pMemoryList->uFlag = mon;
return (pMemoryList);
}
pLastMemoryList = pMemoryList;
pMemoryList = pMemoryList->pNext;
}
if (0 != pLastMemoryList)
{
pMemoryList = (PFAST_MEMORY_LIST)malloc(sizeof(FAST_MEMORY_LIST));
if (0 == pMemoryList) {
return 0;
}
pMemoryList->pAddress = malloc(s);
if (0 == pMemoryList->pAddress) {
return 0;
}
pMemoryList->uCpuNumber = (unsigned short)::GetCurrentProcessorNumber();
pMemoryList->uName = 'Al';
pMemoryList->uSize = (unsigned short)s;
pMemoryList->uFlag = mon;
pMemoryList->pNext = 0;
pLastMemoryList->pNext = pMemoryList;
}
return (pMemoryList);
}
static bool __cdecl fast_free(void * p)
{
register PFAST_MEMORY_LIST pMemoryList = (PFAST_MEMORY_LIST)p;
if (pMemoryList != 0 && pMemoryList->uFlag == mon)
{
pMemoryList->uFlag = moff;
return true;
}
return false;
}
static bool __cdecl fast_delmac(void)
{
SYSTEM_INFO sutSystemInfo;
register unsigned short i = 0;
register PFAST_MEMORY_LIST pMemoryList;
register PFAST_MEMORY_LIST pLastMemoryList;
if (0 == g_phMemList) {
return false;
}
::GetSystemInfo(&sutSystemInfo);
//内核下请调用ExFreePoolWithTag函数;
for ( ; i < sutSystemInfo.dwNumberOfProcessors; ++i)
{
pMemoryList = &g_phMemList[i];
pLastMemoryList = 0;
while (pMemoryList != 0)
{
if (pMemoryList->pAddress != 0) {
free(pMemoryList->pAddress);
}
if (pLastMemoryList != 0) {
pLastMemoryList = pMemoryList->pNext;
free(pMemoryList);
}
else {
pLastMemoryList = pMemoryList->pNext;
}
pMemoryList = pLastMemoryList;
}
}
free(g_phMemList);
g_phMemList = 0;
return true;
}