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. python 常用数据结构

    #coding=utf- #元组,不可变序列(,) a=(,,,) print(a) a=tuple([,,,])#第二种定义方式 print(a) print(a[]) print(a[:]) #可 ...

  2. 大海教你学手游2015CocosLua第一季_02场景跳转和用户触摸

    : 课程地址:http://ke.qq.com/cgi-bin/courseDetail?course_id=78017 cocos2d-x直播课交流群: 461039382(公布每节课视频和资料) ...

  3. (转)linux设备驱动之USB数据传输分析 二

    3.2:控制传输过程1:root hub的控制传输在前面看到,对于root hub的情况,流程会转入rh_urb_enqueue().代码如下:static int rh_urb_enqueue (s ...

  4. 【BZOJ4375】Selling Tickets 随机化

    [BZOJ4375]Selling Tickets Description 厨师在一次晚宴上准备了n道丰盛的菜肴,来自世界各地的m位顾客想要购买宴会的门票.每一位顾客都有两道特别喜爱的菜,而只要吃到了 ...

  5. dubbo开发中使用到的一些服务配置方式

    通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例. 一.一个接口有多个实现时可以使用group来区分 1.服务提供者配置 <?xml  ...

  6. 关于logback

    1 logback是一个日志框架 2 logback的构成 LogBack被分为3个组件,logback-core, logback-classic 和 logback-access. 其中logba ...

  7. 【python】-- 函数、无参/有参参数、全局变量/局部变量

    函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函 ...

  8. 《linux 内核全然剖析》 fork.c 代码分析笔记

    fork.c 代码分析笔记 verifiy_area long last_pid=0; //全局变量,用来记录眼下最大的pid数值 void verify_area(void * addr,int s ...

  9. ansible copy文件比较慢, 使用Synchronize模块

    Ansible中的同步模块(rsync) Synchronize模块 1 2 3 4 5 6 7   This is a wrapper around rsync. Of course you cou ...

  10. (转)jquery $.proxy的使用

    在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: 1 //正常的this使用 2 $('#myElement').click(function() { ...