首页 / C++ / C++空间配置器 Allocator
C++空间配置器 Allocator
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++空间配置器 Allocator,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3071字,纯文字阅读大概需要5分钟。
内容图文
![C++空间配置器 Allocator](/upload/InfoBanner/zyjiaocheng/611/c3c1b30c927e455bb5a1b68e88854184.jpg)
东阳的学习笔记
空间配置器代表一种特定的内存模型
,并提供一种抽象概念,以便将对内存的申请最终转化为对内存的直接调用。
应用程序开发者如何使用配置器
就应用程序员来说,只需要传入一个 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_ */
内容总结
以上是互联网集市为您收集整理的C++空间配置器 Allocator全部内容,希望文章能够帮你解决C++空间配置器 Allocator所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。