SGI的标准的空间适配器 allocator
来源:互联网 发布:python setdefault函数 编辑:程序博客网 时间:2024/05/11 20:46
// SGI STL 配置器的实现#include <new>#include <cstddef>#include <cstdlib>#include <limits>#include <iostream>//#include <algobase>size_t max(size_t a, size_t b)//由于没找到algobase 头文件我自己完成了这个max { if(a>b) return a; else return b;}template <typename T>inline T * allocate (ptrdiff_t size , T *){ T * tmp = (T*) (::operator new ((size_t)(size * sizeof(T))));//申请size * T大小的空间并强制转化为size_t(unsigned int)类型 再把头地址的T*指针赋予tmp if(tmp == 0) { std::cout << "out of memory" <<std::endl; exit(1); } return tmp;}template <typename T>inline void deallocate(T * p){ ::operator delete(p);}template <typename 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, (pointer)0);//没有看出吧size_t类型转化为ptrdiff_t类型的必要,第二个参数我自己会写成NULL; } void deallocate(pointer p) { dellocate(p); } pointer address (reference x) { return (pointer)&x;//返回存有X地址的指针 } const_pointer const_address(const_reference x) { return (const_pointer)&x; } size_type init_page_size() { return max(size_type(1),size_type(4096/sizeof(T))) ;//返回每个元素所占的大小? } size_type max_size() const//这里的const表示函数无法修改类的成员 { return max(size_type(1),size_type(UINT_MAX/sizeof(T))); }}; class allocator<void> //给出一个空特例? 在DEV C++ 编译环境下会出现没有丁页template的错误. { public: typedef void *pointer; } ;
这个allocator的实现其实和上次的空间适配器简易实现的思路相同,我只是练手顺便写了下来。
0 0
- SGI的标准的空间适配器 allocator
- 标准模板库的空间配置器 STL's allocator
- 标准模板库的空间配置器 STL allocator
- 【STL】SGI空间配置器 Allocator
- STL源码:空间配置器(三)内存的配置和释放、SGI的两级适配器
- SGI STL的空间配置器alloc
- SGI版的空间配置器
- SGI STL 空间配置器(allocator)源码剖析
- SGI STL 空间配置器(allocator)源码剖析
- SGI STL学习笔记(1):空间配置器(allocator)
- SGI STL空间适配器freelist设计
- CUJ:标准库:调试用的Allocator
- 标准库中allocator类的使用
- 一个简单的空间配置器: allocator
- STL源码剖析之一:空间适配器(allocator)
- 根据allocator的标准接口,编写的功能、接口不齐全的allocator
- SGI特殊的空间配置器 std::alloc
- SGI STL第二级空间配置器__default_alloc_template的chunk_alloc函数
- 漫说软件工程
- Unity Vector3.Lerp()与Vecotr3.MoveTowards()方法区别
- C语言void泛型指针
- 立体渲染
- JS input text只能输入数字且两个小数
- SGI的标准的空间适配器 allocator
- struts2 练习 经验汇总
- C++中的Sleep函数分析
- 自相关性的诊断以及修正方法r语言代码
- 生活小乐趣
- 黑马程序员——Java基础---IO流
- IK&FK
- 开始Java函数了
- 黑马程序员——Java基础---网络编程