标准库Allocator的简易实现(二)
自己实现Allocator并不难,其实只需要改变allocate和deallocate,来实现自己的内存分配策略。
下面是一个std::allocator的模拟实现
#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP #include <stddef.h>
#include <limits> template <typename T>
class Allocator
{
public:
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; //Allocator::rebind<T2>::other
template <typename V>
struct rebind
{
typedef Allocator<V> other;
}; pointer address(reference value) const { return &value; }
const_pointer address(const_reference value) const { return &value; } Allocator() throw() { }
Allocator(const Allocator &) throw() { }
//不同类型的allcator可以相互复制
template <typename V> Allocator(const Allocator<V> &other) { }
~Allocator() throw() { } //最多可以分配的数目
size_type max_size() const throw()
{ return std::numeric_limits<size_type>::max() / sizeof(T); } //分配内存,返回该类型的指针
pointer allocate(size_type num)
{ return (pointer)(::operator new(num * sizeof(T))); } //执行构造函数,构建一个对象
void construct(pointer p, const T &value)
{ new ((void*)p) T(value); } //销毁对象
void destroy(pointer p)
{ p->~T(); } //释放内存
void deallocate(pointer p, size_type num)
{ ::operator delete((void *)p); }
}; //这两个运算符不需要friend声明
template <typename T, typename V>
bool operator==(const Allocator<T> &, const Allocator<V> &) throw()
{ return true; } template <typename T, typename V>
bool operator!=(const Allocator<T> &, const Allocator<V> &) throw()
{ return false; } #endif
这里注意rebind的实现,如果需要使用Test的分配器分配其他类型,就可以这样:
Allocator<Test>::rebind<Test2>::other alloc;
测试代码如下:
#include "Allocator.hpp"
#include <string>
#include <vector>
using namespace std; int main(int argc, char const *argv[])
{
vector<string, Allocator<string> > vec(10, "haha"); vec.push_back("foo");
vec.push_back("bar"); //Allocator<Test>::rebind<Test2>::other alloc; return 0;
}
标准库Allocator的简易实现(二)的更多相关文章
- 标准库Allocator的使用(一)
上一篇我们提到了new运算符以及它的工作步骤,其实无非是把两项工作独立出来: 1.申请原始内存 2.执行构造函数 delete也涉及了两个工作: 1.执行析构函数 2.释放原始内存 其实标准库提供了另 ...
- 标准库Allocator(三)uninitialized_fill等函数的实现
前面我们使用了uninitialized_fill,来批量初始化某一段内存. 下面提供三个函数的实现代码,这三个代码的共同点是: 1.遇到错误,抛出异常 2.出现异常时,把之前构造的对象全部销毁 所以 ...
- c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- c运行时库,c标准库,Windows系统api的关系
原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...
- (转)c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- 怎么使用C++标准库来实现二维数组
在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...
- Python3标准库(二) re模块
正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...
- python 教程 第十二章、 标准库
第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1) sys模块 import sys if len(sys.argv) ...
- python练习 - 系统基本信息获取(sys标准库)+ 二维数据表格输出(tabulate库)
系统基本信息获取 描述 获取系统的递归深度.当前执行文件路径.系统最大UNICODE编码值等3个信息,并打印输出. ...
随机推荐
- codevs 1269 匈牙利游戏——次短路(spfa)
欢迎来到匈牙利游戏!布达佩斯(匈牙利首都)的街道形成了一个弯曲的单向网络. 你被强制要求参加一个赛跑作为一个TV秀的一部分节目,比赛中你需要穿越这些街道,从s开始,到t结束. 很自然的,你想要尽快的完 ...
- symfony 关于nginx的配置问题
好久没有使用symfony,今天想重新宠幸的时候,却碰到了一个很尴尬的问题,下载安装好symfony的时候访问首页成功安装,然而写了一个其它的路由却怎么都是404. 官网看了n遍的路由配置,什么开始第 ...
- Cocoa Pods 'No such file or Directory' Error
http://stackoverflow.com/questions/27727998/cocoa-pods-no-such-file-or-directory-error 0down votefav ...
- mlock家族:锁定物理内存【转】
转自:http://blog.csdn.net/fjt19900921/article/details/8074541 锁住内存是为了防止这段内存被操作系统swap掉.并且由于此操作风险高,仅超级用户 ...
- LVDS 数据通道详解 单8 单6
1.1.1 LVDS接口分类 1.1.1.1 单路6bit LVDS 这种接口电路中,采用单路方式传输,每个基色信号采用6位数据,共18位RGB数据,因此 ...
- UVA 10940 Throwing cards away II
题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ...
- c++ poco StreamSocket tcpclient测试用例
1.代码 #include <iostream> #include "Poco/Net/Socket.h" #include "Poco/Net/Stream ...
- js生成一周内的日期+周几
(如有错敬请指点,以下是我工作中遇到并且解决的问题) 效果有两种: 两者区别是 1.第一天(今天)显示今日 2.第一天(今天)显示周几 (第一个图是在手机上显示的效果,第二个是PC网页上显示的效果) ...
- RQNOJ PID217 / [NOIP1999]拦截导弹【n^2 / LIS】
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- struts2进阶
Struts2 一.Struts的工作原理 Struts2的工作机制3.1Struts2体系结构图 Strut2的体系结构如图15所示: (图15) 3.2Struts2的工作机制 从图15可以看出, ...