定长内存池ObjectPool

ObjectPool.h

cpp 复制代码
#pragma once
#include <iostream>
#include <vector>
#include <time.h>
using std::cout;
using std::endl;

#ifdef _WIN32
#include <Windows.h>
#else
//
#endif

inline static void* SystemAlloc(size_t kpage)
{
#ifdef _WIN32
	void* ptr = VirtualAlloc(0, kpage << 13, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#else
	//linux下brk mmap等
#endif
	if (ptr == nullptr)
		throw std::bad_alloc();
	return ptr;
}

template<class T>
class ObjectPool
{
private:
	char* _memory = nullptr;//指向大块内存的指针
	size_t _remainBytes = 0;//大块内存在切分过程中的剩余字节数
	void* _freeList = nullptr;//连接还回来的内存块的链表的头指针。
public:
	T* New()
	//每次 New(),从已经申请好的 128KB 大块内存里切出一小块。
	//这一小块大小是:max(sizeof(T), sizeof(void*))
	{
		T* obj = nullptr;//优先把还回来的内存块对象再次重复利用。
		if (_freeList)
		{
			void* next = *((void**)_freeList);
			obj = (T*)_freeList;
			_freeList = next;
			//取走第一块内存
		}
		else
		{
			//剩余内存不够一个对象大小时 重新开大块空间
			if (_remainBytes < sizeof(T))
			{
				_remainBytes = 128 * 1024;//128kb
				_memory = (char*)SystemAlloc(_remainBytes >> 13);
				//相当于除以2^13 
				if (_memory == nullptr)
				{
					throw std::bad_alloc();
				}
			}
			obj = (T*)_memory;
			size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
			_memory += objSize;
			_remainBytes -= objSize;

		}
		// 定位new,显式调用T的构造函数初始化
		new(obj)T;

		return obj;
	}

	void Delete(T* obj)
	{
		//显示调用析构函数清理对象
		obj->~T();
		//头插
		*((void**)obj) = _freeList;
		_freeList = obj;
	}
};

struct TreeNode
{
	int _val;
	TreeNode* _left;
	TreeNode* _right;

	TreeNode()
		:_val(0)
		, _left(nullptr)
		, _right(nullptr)
	{
	}
};
void TestObjectPool()
{
	// 申请释放的轮次
	const size_t Rounds = 5;

	// 每轮申请释放多少次
	const size_t N = 100000;

	std::vector<TreeNode*> v1;
	v1.reserve(N);

	size_t begin1 = clock();
	for (size_t j = 0; j < Rounds; j++)
	{
		for (int i = 0; i < N; i++)
		{
			v1.push_back(new TreeNode);
		}
		for (int i = 0; i < N; i++)
		{
			delete v1[i];
		}
		v1.clear();
	}
	size_t end1 = clock();

	std::vector<TreeNode*> v2;
	v2.reserve(N);
	ObjectPool<TreeNode> TNpool;
	size_t begin2 = clock();
	for (size_t j = 0; j < Rounds; j++)
	{
		for (int i = 0; i < N; i++)
		{
			v2.push_back(TNpool.New());
		}
		for (int i = 0; i < N; i++)
		{
			TNpool.Delete(v2[i]);
		}
		v2.clear();
	}
	size_t end2 = clock();

	cout << "new cost time:" << end1 - begin1 << endl;
	cout << "object pool cost time:" << end2 - begin2 << endl;
}

1.为什么不能直接void* next = _freeList;?

2.为什么_freeList设计成void*类型?

3.VirtualAlloc函数参数是什么? 为什么调之前>>13,调之后<<13,不就相当于直接传吗?

4.obj = (T*)_memory;这样就相当于从_memory拿走T大小的内存吗?为什么?

相关推荐
叼烟扛炮1 小时前
C++第八讲:string 类
开发语言·c++·算法·string
MOONICK1 小时前
bit7z压缩与解压
c++
Chase_______1 小时前
LeetCode 1493 & 3634 题解:滑动窗口双指针,从“删一个元素的全1子数组“到“最少移除使数组平衡“
算法·leetcode
努力努力再努力wz1 小时前
【Qt入门系列】第一个 Qt Widgets 程序:项目创建、UI 文件、Hello World、对象树与 qDebug 日志
java·c语言·开发语言·数据结构·c++·qt·ui
Hua-Jay2 小时前
OpenCV联合C++/Qt 学习笔记(十五)----形态学操作及应用
c++·笔记·qt·opencv·学习·计算机视觉
悲伤小伞2 小时前
LeetCode 热题 100_4-283. 移动零
算法·leetcode·职场和发展
程序员老舅2 小时前
深入底层:Linux MMU 工作原理全解
linux·服务器·网络·c++·linux内核·内存管理·linux内存
星 海2 小时前
网络芯片对IP地址最长前缀匹配算法的实现
网络·算法
凤凰院凶涛QAQ2 小时前
《C++转Java快速入手系列》抽象类和接口篇
java·开发语言·c++