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 ... 
随机推荐
- github & markdown & collapse & table
			github & markdown collapse & table https://github.com/Microsoft/TypeScript/issues/30034 GitH ... 
- ADO.NET工具类(二)
			using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ... 
- 自定义 ASP.NET Identity Data Model with EF
			One of the first issues you will likely encounter when getting started with ASP.NET Identity centers ... 
- Nginx Epoll事件模型优劣
			L30-31 Epoll 性能优势主要源于它不用遍历 假设有100万个链接 其它事件可能都需要遍历所有链接,而Epoll只要遍历活跃的链接,这样大大提升了效率 
- 提高网络灵活性和效率的组网方式—SD-WAN
			导读 最初,大多数企业只是简单地将软件覆盖添加到广域网连接上.但是,随着时间的推移,由于SD-WAN架构的易配置功能,企业将开始采用SD-WAN更复杂的功能. 广域网一般用于连接多个业务地点,如总部和 ... 
- java 转义字符"\u0010" "\010" "\2"等
			java转义字符 \xxx 八进制转义符 \uxxxx 十六进制转义符 像"\010","\u0010"这种字符 ... 
- [Codeforces741D]Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths——dsu on tree
			题目链接: Codeforces741D 题目大意:给出一棵树,根为$1$,每条边有一个$a-v$的小写字母,求每个点子树中的一条最长的简单路径使得这条路径上的边上的字母重排后是一个回文串. 显然如果 ... 
- HDU4864 Task(算竞进阶习题)
			贪心 比较巧妙的贪心..先把所有机器和任务按时间是第一关键字,等级为第二关键字排序. 然后用机器去匹配每一个任务. 排序之后,在时间上满足当前任务的机器,必定也在时间上满足后面的机器,所以我们每次把时 ... 
- 【Gym - 100812G 】Short Path (SPFA)
			BUPT2017 wintertraining(15) #7B 题意 n个点m条无向有权边(2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5),每个点标记了0或1,求所有1中,最近的两个1的下标及 ... 
- 【BZOJ3613】[HEOI2014]南园满地堆轻絮(贪心)
			[BZOJ3613][HEOI2014]南园满地堆轻絮(贪心) 题面 BZOJ 洛谷 题解 考虑二分的做法,每次二分一个答案,那么就会让所有的值尽可能的减少,那么\(O(n)\)扫一遍就好了. 考虑如 ... 
