常用快速内存管理池功能

          并行机制,无需考虑多核与锁......


#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;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值