⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shm_alloc.hpp

📁 一个内存池的allocator,参考stl源码的内存池实现
💻 HPP
字号:
#ifndef _SHM_ALLOC_HPP_
#define _SHM_ALLOC_HPP_

#include "memory_pool.hpp"
#include <new>
#include <cstddef>

using namespace std;

namespace happycode
{
	template<class T,int MIN_BLOCK_SIZE,int MAX_BLOCK_SIZE>
	class shm_allocator 
	{
	public:
		typedef T 	value_type;
		typedef T* 	pointer;
		typedef const T*	const_pointer;
		typedef T&	reference;
		typedef const T&	const_reference;
		typedef size_t	size_type;
		typedef ptrdiff_t	difference_type;

		template<class U>
		struct rebind {
			typedef shm_allocator<U,MIN_BLOCK_SIZE,MAX_BLOCK_SIZE> other;
		};
		
		pointer allocate(size_type n,const void *hint=0) {
			return (pointer)mempoolptr->allocate(n*sizeof(T));
		}

		void deallocate(pointer p,size_type n) {
			mempoolptr->deallocate(p,n*sizeof(T));
		}

		void construct(pointer p,const T& value) {
			new(p) T(value);//placement new
		}

		void destory(pointer p) { p->~T(); };

		pointer address(reference x) {
			return (pointer)&x;
		}

		const_pointer const_address(const_reference x) {
			return (const_pointer)&x;
		}

		size_type max_size() const { 
			return MAX_BLOCK_SIZE;
		}
	public :
		static MemoryPool<MIN_BLOCK_SIZE,MAX_BLOCK_SIZE> * mempoolptr;
	};

	template<class T,int MIN_BLOCK_SIZE,int MAX_BLOCK_SIZE>
	MemoryPool<MIN_BLOCK_SIZE,MAX_BLOCK_SIZE>* shm_allocator<T,MIN_BLOCK_SIZE,MAX_BLOCK_SIZE>::mempoolptr;
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -