STL-空间配置器

近来看了看《STL源码剖析》中的空间配置器,尝试着读了一下,觉得模板还是强大的,同时对于allocator的函数有了进一步的认识。

cpp 复制代码
#if 0
#include<memory>
//alloctor 的必要接口
allocator::valuetype
allocator::pointer
allocator::const_pointer
allocator::reference
allocator::const_reference
allocator::size_type
allocator::difference_type
allocator::rebind
allocator::allocactor() //default constructor
allocator::allocator(const allocator&) //copy constructor
template <class U>allocator::allocator(const allocator<U>&) //泛化的 copy constructor
allocator::~allocator//default constructor
pointer allocator::address(reference x) const//返回某个对象的地址。a.address(x) const 等同于&x
const_pointer allocator::address(const_reference x) const//返回某个const 对象的地址
pointer allocator::allocate(size_type n,const void* = 0) //分配空间,存n个对象。第二个参数是提示,实现上可能会利用它来增进区域性,可忽略
void allocator::deallocate(point p,size_type n) //归还当前分配的空间
size_type allocator::max_size() const //返回可成功分配的最大量
void allocator::construct(point p,const T& x) //等同于new
void allocator::destroy(point p) //等同于p->~T()

#endif

//设计一个简单的空间配置器 allocator
//file:steem_alloc.h  自己的头文件
#ifndef _STEEMALLOC_
#define _STEEMALLOC_

#include<new>
#include<cstddef>
#include<cstdlib>
#include<climits>
#include<iostream>

namespace steem
{
	template <class T>
	inline T* _allocate(ptrdiff_t size, T*)
	{
		set_new_handler(0);
		T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));

		if (tmp == 0)
		{
			cerr << "out of memory" << endl;
			exit(1);
		}
		return tmp;
	}

	template <class T>
	inline void _deallocate(T* buffer)
	{
		::operator delete(buffer);
	}

	template <class T1,class T2>
	inline void _construct(T1* p, const T2& value)
	{
		new(p) T1(value);
	}

	template <class T>
	inline void _destroy(T* ptr)
	{
		ptr->~T();
	}

	template <class T>
	class allocator
	{
	public:
		typedef T			value_type;
		typedef T*			pointer;
		typedef const T*	const_pointer;
		typedef T&			reference;
		typedef const T&	const_reference;
		typedef ptrdiff_t	defference_type;

		//rebind allocator of type U
		template <class U>
		struct rebind
		{
			typedef alloactor<U> other;
		};

		pointer allocate(size_type n, const void* hint = 0)
		{
			return _allocate(((difference_type)n, (pointer)0));
		}

		void deallocate(point p, size_type n) { _deallocate(p); }

		void construct(pointer p, const T& value)
		{
			_construct(p, value);
		}

		void destroy(point p) { _destroy(p); }

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

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

		size_type max_size() const
		{
			return size_type(UINT_MAX / sizeof(T));
		}
	};

}


#endif  //STEMMALLOC


#include<vector>
#include<iostream>
using namespace std;

//main 函数中
int main_t1()
{
	int ia[5] = { 0,1,2,3,4 };
	unsigned int i;

	vector<int, steem::allocator<int> > iv(ia, ia + 5);
	for (i = 0; i < iv.size(); i++)
	{
		cout << iv[i] << ' ';
	}
	cout << endl;

	return 0;
}
相关推荐
DevSecOps选型指南38 分钟前
2025软件供应链安全最佳实践︱证券DevSecOps下供应链与开源治理实践
网络·安全·web安全·开源·代码审计·软件供应链安全
int型码农1 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT1 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面1 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked931 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,1 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵2 小时前
有效的括号题解
数据结构·算法·
GIS小天2 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__2 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃2 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵