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 DEFALLOC_H
00027 #define DEFALLOC_H
00028
00029 #ifndef UNDER_CE
00030 #include <new.h>
00031 #include <stddef.h>
00032 #else
00033 #include <wce_defs.h>
00034 #endif
00035 #include <stdlib.h>
00036 #include <limits.h>
00037 #ifndef UNDER_CE
00038 #include <iostream.h>
00039 #endif
00040 #include <algobase.h>
00041
00042 template <class T>
00043 inline T* allocate(ptrdiff_t size, T*) {
00044 #ifndef UNDER_CE
00045 set_new_handler(0);
00046 #endif
00047 T* tmp = (T*)(::operator new((size_t)(size * sizeof(T))));
00048 if (tmp == 0) {
00049 #ifndef UNDER_CE
00050 cerr << "out of memory" << endl;
00051 exit(1);
00052 #else
00053 __THROW_BAD_ALLOC;
00054 #endif
00055 }
00056 return tmp;
00057 }
00058
00059 template <class T>
00060 inline void deallocate(T* buffer) {
00061 ::operator delete(buffer);
00062 }
00063
00064 template <class T>
00065 class allocator {
00066 public:
00067 typedef T value_type;
00068 typedef T* pointer;
00069 typedef const T* const_pointer;
00070 typedef T& reference;
00071 typedef const T& const_reference;
00072 typedef size_t size_type;
00073 typedef ptrdiff_t difference_type;
00074 pointer allocate(size_type n) {
00075 return ::allocate((difference_type)n, (pointer)0);
00076 }
00077 void deallocate(pointer p) { ::deallocate(p); }
00078 pointer address(reference x) { return (pointer)&x; }
00079 const_pointer const_address(const_reference x) {
00080 return (const_pointer)&x;
00081 }
00082 size_type init_page_size() {
00083 return max(size_type(1), size_type(4096/sizeof(T)));
00084 }
00085 size_type max_size() const {
00086 return max(size_type(1), size_type(UINT_MAX/sizeof(T)));
00087 }
00088 };
00089
00090 class allocator<void> {
00091 public:
00092 typedef void* pointer;
00093 };
00094
00095
00096
00097 #endif