allocator必要接口:

allocator::value_type

allocator::pointer

allocator::const_pointer

allocator::reference

allocator::const_reference

allocator::size_type

allocator::difference_type

allocator::rebind

自定义allocator,书上说此空间配置其完全无法应用于SGI STL allocator,但是现在应该修改了,默认的空间配置器也是std::allocator

//2jjalloca.h

 #ifndef _2JJALLOCA_H_
#define _2JJALLOCA_H_ #include <new>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <iostream> using namespace std; namespace JJ { template<class T>
inline T* _allocate(ptrdiff_t size, T*) {
//set_new_handler(0);//不知道哪里的?
T* tmp = (T*) (::operator new((size_t) (size * sizeof(T))));
//operator new可以被重载
if (tmp == ) {
cerr << "out of memory" << endl;
exit();
}
return tmp;
} template<class T>
inline void _deallocate(T* buffer) {
::operator delete(buffer);
//operator delete可以被重载
// operator delete(buffer);
} template<class T1, class T2>
inline void _construct(T1* p, const T2& value) {
new (p) T1(value);
} template<class T>
inline void _destroy(T* ptr) {
ptr->~T();
} template<class T>
class allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type; //rebind allocator of type U
template<class U>
struct rebind {
typedef allocator<U> other;
}; //hint used for locality. ref.[Austern],p189
pointer allocate(size_type n, const void* hint = ) {
return _allocate((difference_type) n, (pointer) );
} void deallocate(pointer p, size_type n) {
_deallocate(p);
} void construct(pointer p, const T& value) {
_construct(p, value);
} void destroy(pointer p) {
_destroy(p);
} pointer address(reference x) {
return (pointer) &x;
} const_pointer const_address(const_reference x) {
return (const_pointer) &x;
} size_type max_size() const {
return size_type(UINT_MAX / sizeof(T));
} }; }//end of namespace JJ #endif /* _2JJALLOCA_H_ */

//main.cpp

 #include <iostream>
#include <vector>
#include "2jjalloca.h" using namespace std; int main(int argc, char **argv) {
int ia[] = { , , , , };
unsigned int i;
fprintf(stderr, "ia addr:%p\n", ia);
vector<int, JJ::allocator<int>> iv(ia, ia + );
for (i = ; i < iv.size(); i++) {
cout << iv[i] << ' ';
}
cout << endl;
return ;
}

运行结果也是正常的

================>现在由此引申出一个问题,operator new

 #include <iostream>
#include <new>
#include <limits.h>
#include <stddef.h> using namespace std; template<class T>
inline T* allocate(ptrdiff_t size, T *) {
set_new_handler();
T* tmp = (T*) (::operator new((size_t) (size * sizeof(T))));
if (tmp == ) {
cerr << "out of memory" << endl;
exit();
}
return tmp;
} template<class T>
inline void deallocate(T* buffer) {
::operator delete(buffer);
} template<class T>
class Allocator {
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type; pointer allocate(size_type n) {
return ::allocate((difference_type) n, (T*) );
} void deallocate(pointer p) {
::deallocate(p);
} pointer address(reference x) {
return (pointer) &x;
} const_pointer const_address(const_reference x) {
return (const_pointer) &x;
} size_type init_page_size() {
return max(size_type(), size_type( / sizeof(T)));
} size_type max_size() const {
return max(size_type(), size_type(UINT_MAX / sizeof(T)));
}
}; template<>
class Allocator<void> {
public:
typedef void* pointer;
}; int main(int argc, char **argv) { return ;
}

STL源码po析所说,SGI定义了一个有部分符合标准的allocator配置器(上面的代码),但是我看了自己本地的代码,似乎很符合标准呀。

STL学习笔记:空间配置器allocator的更多相关文章

  1. C++ STL学习之 空间配置器(allocator)

    众所周知,一般情况下,一个程序包括数据结构和相应的算法,而数据结构作为存储数据的组织形式,与内存空间有着密切的联系. 在C++ STL中,空间配置器便是用来实现内存空间(一般是内存,也可以是硬盘等空间 ...

  2. stl源码剖析 详细学习笔记 空间配置器

    //---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...

  3. C++ 空间配置器(allocator)

    C++ 空间配置器(allocator) 在STL中,Memory Allocator 处于最底层的位置,为一切的 Container 提供存储服务,是一切其他组件的基石.对于一般使用 STL 的用户 ...

  4. STL源码剖析——空间配置器Allocator#1 构造与析构

    以STL的运用角度而言,空间配置器是最不需要介绍的东西,因为它扮演的是幕后的角色,隐藏在一切容器的背后默默工作.但以STL的实现角度而言,最应该首先介绍的就是空间配置器,因为这是这是容器展开一切运作的 ...

  5. 《STL源码剖析》chapter2空间配置器allocator

    为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质.是的,你可以写一个allocator,直接向硬盘取空间.sgi stl提供的配置器,配置的对 ...

  6. STL源码剖析 — 空间配置器(allocator)

    前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配 ...

  7. STL源码剖析——空间配置器Allocator#2 一/二级空间配置器

    上节学习了内存配置后的对象构造行为和内存释放前的对象析构行为,在这一节来学习内存的配置与释放. C++的内存配置基本操作是::operator new(),而释放基本操作是::operator del ...

  8. STL之空间配置器allocator

    摘要 C++STL的空间配置器将内存的分配.释放,对象的构造.析构都分开执行,内存分配由alloc::allocate()负责,内存的释放由alloc::deallocate()负责:对象的构造由:: ...

  9. STL源码剖析——空间配置器Allocator#3 自由链表与内存池

    上节在学习第二级配置器时了解了第二级配置器通过内存池与自由链表来处理小区块内存的申请.但只是对其概念进行点到为止的认识,并未深入探究.这节就来学习一下自由链表的填充和内存池的内存分配机制. refil ...

随机推荐

  1. 莫烦sklearn学习自修第七天【交叉验证】

    1. 什么是交叉验证 所谓交叉验证指的是将样本分为两组,一组为训练样本,一组为测试样本:对于哪些数据分为训练样本,哪些数据分为测试样本,进行多次拆分,每次将整个样本进行不同的拆分,对这些不同的拆分每个 ...

  2. 错误模块名称: KERNELBASE.dll错误

    今天在部署一个C/S程序的时候出了bug,日志都没有记载:本地调试当然是没问题的,所以不是代码问题,百度之发现KERNELBASE.dll这个文章说的比较靠谱,仔细研究了自己的配置文件后,果然是配置文 ...

  3. H5(仅仅是个地址)

    http://www.w3school.com.cn/html5/html_5_intro.asp (▼ヘ▼#)   怕你不看,我特地给你记个地址,应该不能再故意不看了吧   (▼ヘ▼#)

  4. kubernetes ceph-rbd挂载步骤 类型PersistentVolume

    k8s集群每一台上面都要安装客户端: ceph-deploy  install  k8s的ip地址 创建一个k8s操作用户: ceph auth add client.k8s mon 'allow r ...

  5. MD5进行解密操作

    package com.dyy.test; import java.security.MessageDigest; public class TestMD5Util { /*** * MD5加码 生成 ...

  6. 洛谷P1582 倒水题解

    题目 分析 这个题并不难,只是需要仔细思考我们首先可以很轻松的把这个题给疏通一下题意. 1:首先我们最后每个瓶子中装的水一定是一个$2^x$,因为每次都是$2$倍的加,这个应该很好理解. 2:我们要明 ...

  7. Matplotlib学习---用matplotlib画散点图,气泡图(scatter plot, bubble chart)

    Matplotlib里有两种画散点图的方法,一种是用ax.plot画,一种是用ax.scatter画. 一. 用ax.plot画 ax.plot(x,y,marker="o",co ...

  8. Going Home POJ - 2195(费用流)

    就是一个简单题 四个月前a的一道题,今天又看到了,再a一遍吧. 好吧 我想多了 用了bfs求最短路  其实不用的 因为没有障碍物 #include <iostream> #include ...

  9. AtCoder 瞎做

    目录 ARC 058 E - 和風いろはちゃん / Iroha and Haiku 题意 题解 技巧 代码 ARC 059 F - バイナリハック / Unhappy Hacking 题意 题解 技巧 ...

  10. python实用脚本集

    iScript 是Github上 PeterDing 大神写的一个脚本集,由多数的 python 脚本和少数GM脚本组成. 含有以下几个脚本: xiami.py - 下载或播放高品质虾米音乐(xiam ...