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