定长内存池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大小的内存吗?为什么?

相关推荐
人间乄惊鸿客39 分钟前
c++ 右值
c++·算法
fanstuck2 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻2 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
ShineWinsu2 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
码完就睡3 小时前
数据结构——图的基础概念
数据结构
瑶池酒剑仙3 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
猿人谷5 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
地平线开发者5 小时前
地平线 × 同济无人车队:PointPillars 从浮点模型到 BPU 部署
算法·自动驾驶
iiiiii116 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习