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