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. linux不同终端的操作是如何在messages日志中区分的

    今天在定位一个问题时,查看message日志,需要知道message日志中的记录分别是哪个Xterm终端操作的.比较了半天才发现原来日志中可以通过pts来进行区分.如下所示: --12T15:::|n ...

  2. 关于IWMS中遇到的问题及解决方法

    1.生成的文章上传到外网上,但是没一会儿又变成原来的样子? 解决方案:把上传页面对应的template中的.aspx页面也要上传到外网去.

  3. SSH本地端口转发的理解

    ssh -L 3307:127.0.0.1:3306 user@ssh-server -N 其中127.0.0.1:3306是指 ssh-server要访问资源的ip和端口 而3307则是隧道的开口, ...

  4. MySQL 大数据量分页优化

    假设有一个千万量级的表,取1到10条数据: ,; ,; 这两条语句查询时间应该在毫秒级完成: ,; 你可能没想到,这条语句执行之间在5s左右: 为什么相差这么大? 可能mysql并没有你想的那么智能, ...

  5. webpack始出来

    一直想好好整理一下webpack,现在就整理吧. 总结自己的实际搭建的整理情况,我还是要先对自己说一句,以后给文件夹起名字的时候不要用一些特殊的关键字,比如我在做这个demo的时候,我用的文件夹名称叫 ...

  6. Cisco IP 电话 将它的voice mail 发送到手机

    功能一.将语音转成文字发送短信(有微软认知.百度认知.云片短信API) 功能二.直接将音频发送到微信 不废话,直接送个包 链接: https://github.com/JaviZhu/KLCN.Spe ...

  7. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  8. codeforces263B

    Squares CodeForces - 263B Vasya has found a piece of paper with a coordinate system written on it. T ...

  9. Vue 计算

    目标:字段c=字段a+字段b 方法1 直接使用Mustache(胡子表达式) <body> <div id="example" > <input v- ...

  10. 【XSY2707】snow 线段树 并查集

    题目描述 有\(n\)个人和一条长度为\(t\)的线段,每个人还有一个工作范围(是一个区间).最开始整条线段都是白的.定义每个人的工作长度是这个人的工作范围中白色部分的长度(会随着线段改变而改变).每 ...