STL学习笔记:空间配置器allocator
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的更多相关文章
- C++ STL学习之 空间配置器(allocator)
众所周知,一般情况下,一个程序包括数据结构和相应的算法,而数据结构作为存储数据的组织形式,与内存空间有着密切的联系. 在C++ STL中,空间配置器便是用来实现内存空间(一般是内存,也可以是硬盘等空间 ...
- stl源码剖析 详细学习笔记 空间配置器
//---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...
- C++ 空间配置器(allocator)
C++ 空间配置器(allocator) 在STL中,Memory Allocator 处于最底层的位置,为一切的 Container 提供存储服务,是一切其他组件的基石.对于一般使用 STL 的用户 ...
- STL源码剖析——空间配置器Allocator#1 构造与析构
以STL的运用角度而言,空间配置器是最不需要介绍的东西,因为它扮演的是幕后的角色,隐藏在一切容器的背后默默工作.但以STL的实现角度而言,最应该首先介绍的就是空间配置器,因为这是这是容器展开一切运作的 ...
- 《STL源码剖析》chapter2空间配置器allocator
为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质.是的,你可以写一个allocator,直接向硬盘取空间.sgi stl提供的配置器,配置的对 ...
- STL源码剖析 — 空间配置器(allocator)
前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配 ...
- STL源码剖析——空间配置器Allocator#2 一/二级空间配置器
上节学习了内存配置后的对象构造行为和内存释放前的对象析构行为,在这一节来学习内存的配置与释放. C++的内存配置基本操作是::operator new(),而释放基本操作是::operator del ...
- STL之空间配置器allocator
摘要 C++STL的空间配置器将内存的分配.释放,对象的构造.析构都分开执行,内存分配由alloc::allocate()负责,内存的释放由alloc::deallocate()负责:对象的构造由:: ...
- STL源码剖析——空间配置器Allocator#3 自由链表与内存池
上节在学习第二级配置器时了解了第二级配置器通过内存池与自由链表来处理小区块内存的申请.但只是对其概念进行点到为止的认识,并未深入探究.这节就来学习一下自由链表的填充和内存池的内存分配机制. refil ...
随机推荐
- Ansible入门与实践
一.ansible介绍 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无 ...
- python django 的环境搭建(centos)
一.安装好nginx 二.安装uwsgi yum install python-devel -y pip3 install uwsgi #测试启动django /usr/local/python3/b ...
- 莫烦keras学习自修第四天【分类问题】
1.代码实战 #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ # 导入numpy import numpy as np np.random.seed(133 ...
- python数学第四天【古典概型】
- crontab 从nano 转换为 vim
crontab默认编辑器为nano,不方便使用. 修改crontab默认编辑器为vi或者其他的编辑器. export EDITOR="/usr/bin/vim" ; cront ...
- vue环境搭建+vscode
https://blog.csdn.net/junshangshui/article/details/80376489
- jdbc工具类1.0
package cn.zhouzhou; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManag ...
- 关于构造器中的super()
1.为什么在子类的constructor里面要加一句super()? 答:如果子类用了extends的关键字继承的父类,那么子类在使用构造器的时候就要加super()语句,这是语法规范,就是这么定的. ...
- Razor Pages with ASP.NET Core 2
With ASP.NET Core 2 we get another way of building web applications. It’s one of those new things th ...
- 开始一个简单的ASP.NET Web API 2 (C#)
创建一个Web API 项目 在本教程中,你将使用ASP.NET Web API 来创建一个web API 并返回产品列表. 网页前端使用jQuery 显示结果. 选择ASP.NET Web Appl ...