自己实现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的简易实现(二)的更多相关文章

  1. 标准库Allocator的使用(一)

    上一篇我们提到了new运算符以及它的工作步骤,其实无非是把两项工作独立出来: 1.申请原始内存 2.执行构造函数 delete也涉及了两个工作: 1.执行析构函数 2.释放原始内存 其实标准库提供了另 ...

  2. 标准库Allocator(三)uninitialized_fill等函数的实现

    前面我们使用了uninitialized_fill,来批量初始化某一段内存. 下面提供三个函数的实现代码,这三个代码的共同点是: 1.遇到错误,抛出异常 2.出现异常时,把之前构造的对象全部销毁 所以 ...

  3. c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  4. c运行时库,c标准库,Windows系统api的关系

    原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...

  5. (转)c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  6. 怎么使用C++标准库来实现二维数组

    在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...

  7. Python3标准库(二) re模块

    正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...

  8. python 教程 第十二章、 标准库

    第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1)    sys模块 import sys if len(sys.argv) ...

  9. python练习 - 系统基本信息获取(sys标准库)+ 二维数据表格输出(tabulate库)

    系统基本信息获取 描述 获取系统的递归深度.当前执行文件路径.系统最大UNICODE编码值等3个信息,并打印输出.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

随机推荐

  1. 寻找道路(NOIP2014)神奇之题。。

    原题传送门 这道题嘛.. 首先根据题目,我们要先知道哪些点能够到达终点.(反向BFS) 然后我们再求最短路的途中,必须随时判断周围的点是否被第一次BFS标记过.. 所以再来一次BFS. 数组记得清零, ...

  2. Linux 根据组来划分账号,根据部门同一账号的组,同一组下拥有同一权限

    #新机器添加 #创建部门组与账号 useradd testgroup #创建员工账号加入到部门组里 useradd -g testgroup user1 #员工在/data目录下创建的默认权限为774 ...

  3. js比对一维数组全等的算法

    //辅助方法1,返回某个值在数组中的位置 Array.prototype.indexOf = function (e) { for (var i = 0, l = this.length; i < ...

  4. Selenium2+python自动化9-CSS定位语法【转载】

    前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...

  5. python接口自动化1-发送get请求【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

  6. ros navigation stack---move_base

    大部分内容参考自: ros_by_example_hydro_volume_1.pdf 主要是讲如何让先锋机器人在空白地图上运动 上面图是navigation框架图,可以看到move_base处在核心 ...

  7. springBoot Ribbon Hystrix Dashboard

    1.引入依赖 <!-- 引入关于 hystrix Dashboard的依赖 --> <dependency> <groupId>org.springframewor ...

  8. Ubuntu 14.04LTS+Git

    Git是我们常用的代码托管工具,作为程序员,Git是必备的. 安装Git的方法很简单,官网就有写:http://git-scm.com/download/linux 根据官网的说明,用: sudo a ...

  9. react className的2种变量写法

    ES6新增的不少语法都是极好用的, 在拼接变量与字符串时,模版字符串``就是典型的用法 以下是2种写法 <div className={"bubble-box" +' '+` ...

  10. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...