00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _CPP_BACKWARD_DEFALLOC_H
00027 #define _CPP_BACKWARD_DEFALLOC_H 1
00028
00029 #include "backward_warning.h"
00030 #include "new.h"
00031 #include <stddef.h>
00032 #include <stdlib.h>
00033 #include <limits.h>
00034 #include "iostream.h"
00035 #include "algobase.h"
00036
00037
00038 template <class _Tp>
00039 inline _Tp* allocate(ptrdiff_t __size, _Tp*) {
00040 set_new_handler(0);
00041 _Tp* __tmp = (_Tp*)(::operator new((size_t)(__size * sizeof(_Tp))));
00042 if (__tmp == 0) {
00043 cerr << "out of memory" << endl;
00044 exit(1);
00045 }
00046 return __tmp;
00047 }
00048
00049
00050 template <class _Tp>
00051 inline void deallocate(_Tp* __buffer) {
00052 ::operator delete(__buffer);
00053 }
00054
00055 template <class _Tp>
00056 class allocator {
00057 public:
00058 typedef _Tp value_type;
00059 typedef _Tp* pointer;
00060 typedef const _Tp* const_pointer;
00061 typedef _Tp& reference;
00062 typedef const _Tp& const_reference;
00063 typedef size_t size_type;
00064 typedef ptrdiff_t difference_type;
00065 pointer allocate(size_type __n) {
00066 return ::allocate((difference_type)__n, (pointer)0);
00067 }
00068 void deallocate(pointer __p) { ::deallocate(__p); }
00069 pointer address(reference __x) { return (pointer)&__x; }
00070 const_pointer const_address(const_reference __x) {
00071 return (const_pointer)&__x;
00072 }
00073 size_type init_page_size() {
00074 return max(size_type(1), size_type(4096/sizeof(_Tp)));
00075 }
00076 size_type max_size() const {
00077 return max(size_type(1), size_type(UINT_MAX/sizeof(_Tp)));
00078 }
00079 };
00080
00081 class allocator<void> {
00082 public:
00083 typedef void* pointer;
00084 };
00085
00086
00087
00088 #endif