defalloc.h
. // Filename: defalloc.h
.
. // Comment By: 凝霜
. // E-mail: mdl2009@vip.qq.com
. // Blog: http://blog.csdn.net/mdl13412
.
. /*
8. *
9. * Copyright (c) 1994
10. * Hewlett-Packard Company
11. *
12. * Permission to use, copy, modify, distribute and sell this software
13. * and its documentation for any purpose is hereby granted without fee,
14. * provided that the above copyright notice appear in all copies and
15. * that both that copyright notice and this permission notice appear
16. * in supporting documentation. Hewlett-Packard Company makes no
17. * representations about the suitability of this software for any
18. * purpose. It is provided "as is" without express or implied warranty.
19. *
20. */
.
. // 这个文件提供原始的HP默认allocator, 仅仅是为了向后兼容
. //
. // 不要使用这个文件,除非你使用一个需要HP-style allocator的旧容器
. // SGI STL使用一个不同的allocator接口
. // SGI-style的allocator不针对对象类型进行参数化, 他使用void *指针
.
. #ifndef DEFALLOC_H
. #define DEFALLOC_H
.
. #include <new.h>
. #include <stddef.h>
. #include <stdlib.h>
. #include <limits.h>
. #include <iostream.h>
. #include <algobase.h>
.
. // 如果内存分配失败, 则直接退出程序
. 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);
. }
.
. // 标准的STL allocator接口
. template <class T>
. class allocator
. {
. public:
. // STL type_traits需要的标准定义
. 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));
. }
. 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)));
. }
. };
.
. // 仅使用void *类型的指针
. class allocator<void>
. {
. public:
. typedef void* pointer;
. };
.
. #endif

STL defalloc.h的更多相关文章

  1. STL stl_config.h

    stl_config.h . // Filename: stl_config.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // ...

  2. STL stl_alloc.h

    # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: http://blog.csdn.net/mdl13412 # # // ...

  3. STL stl_uninitialized.h

    stl_uninitialized.h // Filename: stl_uninitialized.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com ...

  4. STL stl_construct.h

    stl_construct.h // Filename: stl_construct.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog ...

  5. STL六大组件之——分配器(内存分配,好深奥的东西)

    SGI设计了双层级配置器,第一级配置器直接使用malloc()和free(),第二级配置器则视情况采用不同的策略:当配置区块超过128bytes时,视之为“足够大”,便调用第一级配置器:当配置区小于1 ...

  6. 【转】STL空间配置器

    STL空间配置器(allocator)在所有容器内部默默工作,负责空间的配置和回收.STL标准为空间配置器定义了标准接口(可见<STL源码剖析>P43).而具体实现细节则由各编译器实现版本 ...

  7. STL内存管理

    1. 概述 STL Allocator是STL的内存管理器,也是最低调的部分之一,你可能使用了3年stl,但却不知其为何物. STL标准如下介绍Allocator the STL includes s ...

  8. 侯捷STL课程及源码剖析学习2: allocator

    以STL 的运用角度而言,空间配置器是最不需要介绍的东西,它总是隐藏在一切组件(更具体地说是指容器,container)的背后,默默工作默默付出. 一.分配器测试 测试代码 #include < ...

  9. SGI STL内存配置器存在内存泄漏吗?

    阅读了SGI的源码后对STL很是膜拜,很高质量的源码,从中学到了很多.温故而知新!下文中所有STL如无特殊说明均指SGI版本实现. STL 内存配置器 STL对内存管理最核心部分我觉得是其将C++对象 ...

随机推荐

  1. diamond源码阅读-目录监控

    PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...

  2. Expression Tree上手指南 (一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  3. 微信小程序的官方文档

    虽然不知道微信小程序今后的发展情况,不过做为一名it人员的我还是去了解它. 这是他的文档路径,里面有详细的使用和申请内测号的全部流程,这里就不再过多解释了. 看后那个开发小程序的文档记得分析你感觉微信 ...

  4. vue2 本地安装

  5. 【BZOJ5037】[Jsoi2014]电信网络 最大权闭合图

    [BZOJ5037][Jsoi2014]电信网络 Description JYY创建的电信公司,垄断着整个JSOI王国的电信网络.JYY在JSOI王国里建造了很多的通信基站.目前所有的基站都是使用2G ...

  6. PhotoKit type类型

    参考链接: https://www.jianshu.com/p/42e5d2f75452/ 1.获取图像类型 enum PHAssetCollectionType : Int { case Album ...

  7. redis持久化RDB详细操作步骤

    1.xshell远程登录服务器ssh root@192.168.142.130 2.切换到redis目录 3.创建一个配置文件s2-redis.conf 4.编辑文件 vi s2-redis.conf ...

  8. 使用django开发一个博客

    环境: MAC 10.10.5  Yosemite Python 3.73 Django 代码托管 github

  9. CVPR 2018:diractNets网络,有残差网络好吗?

    我把我明天讲PPT的材料弄上来了........哈 哈哈

  10. 分布式文件存储——GlusterFS

    一.概论 1.简介 GlusterFS (Gluster File System) 是一个开源的分布式文件系统,主要由 Z RESEARCH 公司负责开发. GlusterFS 是 Scale-Out ...