C++空间配置器 Allocator

东阳的学习笔记

空间配置器代表一种特定的内存模型,并提供一种抽象概念,以便将对内存的申请最终转化为对内存的直接调用。

应用程序开发者如何使用配置器

就应用程序员来说,只需要传入一个 template 参数就可以了。

程序库开发者如何使用配置器

配置器提供了一个接口,包括分配、生成、销毁和回收对象。

  • a.allocate(num) 为 num 个元素分配内存
  • b.construct§ 将 p 所指的元素初始化
  • destroy§ 销毁 p 所指的元素
  • deallocate(p, num) 回收 p 所指的 可容纳 num 个元素的内存空间

针对 未初始化之内存的一些方便好用的函数 强烈承诺:要么全部成功,要么什么都不做

  • uninitialized_fill(beg, end, val) 以 val 初始化 [beg, end)
  • uninitialized_fill_n(beg, num, val)
  • unintialized_copy(beg, end, men): 以[beg, end)的各个元素初始化 mem 开始的各个元素

原始存储期的迭代器

C++标准库程序还定义了一个 class raw_storage_iterator,可使用STL算法

使用 get_temporary_buffer() 和 return_temporary_buffer() 很难写出异常安全的程序代码,所以它们基本上已经不再被应用于程序库中

自定义配置器

完成自己的配置器轻而易举。通常你需要变化的只是 max_size(), allocate(), deallocate()。你可将将自己在内存方面的读到策略体现在这三个函数中。

/*
 * defalloc.hpp
 *
 *  Created on: 2021年1月14日
 *      Author: san
 */

#ifndef UTIL_DEFALLOC_HPP_
#define UTIL_DEFALLOC_HPP_

#include <limits>

namespace std {
template <class T>
class allocator {
public:
	//type definitions
	typedef size_t       size_type;
	typedef ptrdiff_t    difference_type;
	typedef T*           pointer;
	typedef const T*     const_pointer;
	typedef T&           reference;
	typedef const T&     const_reference;
	typedef T            value_type;

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

	// return address of values
	pointer address(reference value) const
	{
		return &value;
	}

	const_pointer address(const_reference value) const
	{
		return &value;
	}

	/* constructors and destructor
	 * - nothing to do because the allocator has no state
	 */
	allocator() throw()
	{
	}

	allocator(const allocator &) throw()
	{
	}

	template <class U>
	allocator(const allocator<U> &) throw()
	{
	}

	~allocator() throw()
	{
	}

	// return maximum number of elements that can be allocated
	size_type max_size() const throw()
	{
		// 与平台相关
		return numeric_limits<size_type>::max() / sizeof(T);
	}

	// allocate but don't initialize num elements of type T
	pointer allocate(size_type num,
			allocator<void>::const_pointer hint = 0)
	{
		// allocate memory with global new
		return (pointer) (::operator new(num*sizeof(T)));
	}

	// initialize elements of allocated storage p with value
	void construct(pointer p, const reference value)
	{
		// initialize memory with placement new
		new((void*)p)T(value);
	}

	// destroy objects by calling their destructor
	void destory(pointer p)
	{
		// destory objects by calling their destructor
		p->~T();
	}

	// deallocate storage p of deleted elements
	void deallocate(pointer p, size_type num)
	{
		// deallocate memory with global delete
		::operator delete((void*)p);
	}
};

// return all specialization of this allocator are interchangeable
template <class T1, class T2>
bool operator== (const allocator<T1>&,
		const allocator<T2> &) throw()
{
	return true;
}

template <class T1, class T2>
bool operator!= (const allocator<T1> &,
		const allocator<T2> &) throw()
{
	return false;
}

}



#endif /* UTIL_DEFALLOC_HPP_ */

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东阳z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值