首页 技术 正文
技术 2022年11月14日
0 收藏 381 点赞 3,240 浏览 1944 个字
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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906