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
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef _MALLOC_ALLOCATOR_H
00035 #define _MALLOC_ALLOCATOR_H 1
00036
00037 #include <cstdlib>
00038 #include <new>
00039 #include <bits/functexcept.h>
00040
00041 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00042
00043 using std::size_t;
00044 using std::ptrdiff_t;
00045
00046
00047
00048
00049
00050
00051
00052
00053 template<typename _Tp>
00054 class malloc_allocator
00055 {
00056 public:
00057 typedef size_t size_type;
00058 typedef ptrdiff_t difference_type;
00059 typedef _Tp* pointer;
00060 typedef const _Tp* const_pointer;
00061 typedef _Tp& reference;
00062 typedef const _Tp& const_reference;
00063 typedef _Tp value_type;
00064
00065 template<typename _Tp1>
00066 struct rebind
00067 { typedef malloc_allocator<_Tp1> other; };
00068
00069 malloc_allocator() throw() { }
00070
00071 malloc_allocator(const malloc_allocator&) throw() { }
00072
00073 template<typename _Tp1>
00074 malloc_allocator(const malloc_allocator<_Tp1>&) throw() { }
00075
00076 ~malloc_allocator() throw() { }
00077
00078 pointer
00079 address(reference __x) const { return &__x; }
00080
00081 const_pointer
00082 address(const_reference __x) const { return &__x; }
00083
00084
00085
00086 pointer
00087 allocate(size_type __n, const void* = 0)
00088 {
00089 if (__builtin_expect(__n > this->max_size(), false))
00090 std::__throw_bad_alloc();
00091
00092 pointer __ret = static_cast<_Tp*>(malloc(__n * sizeof(_Tp)));
00093 if (!__ret)
00094 std::__throw_bad_alloc();
00095 return __ret;
00096 }
00097
00098
00099 void
00100 deallocate(pointer __p, size_type)
00101 { free(static_cast<void*>(__p)); }
00102
00103 size_type
00104 max_size() const throw()
00105 { return size_t(-1) / sizeof(_Tp); }
00106
00107
00108
00109 void
00110 construct(pointer __p, const _Tp& __val)
00111 { ::new(__p) value_type(__val); }
00112
00113 void
00114 destroy(pointer __p) { __p->~_Tp(); }
00115 };
00116
00117 template<typename _Tp>
00118 inline bool
00119 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00120 { return true; }
00121
00122 template<typename _Tp>
00123 inline bool
00124 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
00125 { return false; }
00126
00127 _GLIBCXX_END_NAMESPACE
00128
00129 #endif