自己实现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. parallel programming. this causual litery nots represents my recent progress in parallel programming in c#.It`s interesting.

    not to say extra words,let`s start the code. pasted below: using System; using System.Collections.Ge ...

  2. JavaEE中Filter实现用户登录拦截

    实现思路是编写过滤器,如果用户登录之后session中会存一个user.如果未登录就为null,就可以通过过滤器将用户重定向到登陆页面,让用户进行登陆,当然过滤器得判断用户访问的如果是登陆请求需要放行 ...

  3. 使用matlab判断男声与女声

    (转自) http://wenku.baidu.com/view/1d55480fbe1e650e52ea99a3.html %filename:manwoman.m %different man f ...

  4. 非MFC工程中使用MFC库

    目录(?)[-] 需求说明 常见问题 问题分析 参考解决方法 我的解决方案 Stdafxh的原理   需求说明 C++工程的类型有很多,从VS(或VC)可以看到常见的有:Win32 Console A ...

  5. 有关BOM(Browser Object Model)的内容

    包括: BOM概述 BOM模型 Window对象(常用属性和方法,窗口的打开,窗口的关闭,模态对话框,定时器) Navigator对象(遍历navigator对象的所有属性,Navigator 对象集 ...

  6. Centos 查看进程的几条命令

    1. ps -ef | grep java 表示查看所有进程里 CMD 是 java 的进程信息 2. ps -aux | grep java -aux 显示所有状态 3. kill -9 [PID] ...

  7. 优化html中mp4视频加载速度

    如果使用参数faststart就会在生成完上边结构之后将moov移动到mdat前面:ffmpeg –i input.flv –c copy –f mp4 –movflags faststart out ...

  8. spark streaming 异常No output streams registered, so nothing to execute

    实现spark streaming demo时,代码: public static void main (String[] args) { SparkConf conf = new SparkConf ...

  9. flask框架基本使用(3)(session与cookies)

    #转载请留言联系 flask 框架基本使用(1):https://www.cnblogs.com/chichung/p/9756935.html flask 框架基本使用(2):https://www ...

  10. JAVA线程池调优

        在JAVA中,线程可以使用定制的代码来管理,应用也可以利用线程池.在使用线程池时,有一个因素非常关键:调节线程池的大小对获得最好的性能至关重要.线程池的性能会随线程池大小这一基本选择而有所不同 ...