libstdc++: new_allocator.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30#ifndef _STD_NEW_ALLOCATOR_H

31#define _STD_NEW_ALLOCATOR_H 1

32

34#include <new>

37#if __cplusplus >= 201103L

39#endif

40

41namespace std _GLIBCXX_VISIBILITY(default)

42{

43_GLIBCXX_BEGIN_NAMESPACE_VERSION

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62 template<typename _Tp>

64 {

65 public:

66 typedef _Tp value_type;

67 typedef std::size_t size_type;

68 typedef std::ptrdiff_t difference_type;

69#if __cplusplus <= 201703L

70 typedef _Tp* pointer;

71 typedef const _Tp* const_pointer;

72 typedef _Tp& reference;

73 typedef const _Tp& const_reference;

74

75 template<typename _Tp1>

76 struct rebind

78#endif

79

80#if __cplusplus >= 201103L

81

82

83 typedef std::true_type propagate_on_container_move_assignment;

84#endif

85

86 __attribute__((__always_inline__))

87 _GLIBCXX20_CONSTEXPR

89

90 __attribute__((__always_inline__))

91 _GLIBCXX20_CONSTEXPR

93

94 template<typename _Tp1>

95 __attribute__((__always_inline__))

96 _GLIBCXX20_CONSTEXPR

98

99#if __cplusplus >= 201103L

101#endif

102

103#if __cplusplus <= 201703L

105

106 pointer

107 address(reference __x) const _GLIBCXX_NOEXCEPT

109

110 const_pointer

111 address(const_reference __x) const _GLIBCXX_NOEXCEPT

113#endif

114

115#if __has_builtin(__builtin_operator_new) >= 201802L

116# define _GLIBCXX_OPERATOR_NEW __builtin_operator_new

117# define _GLIBCXX_OPERATOR_DELETE __builtin_operator_delete

118#else

119# define _GLIBCXX_OPERATOR_NEW ::operator new

120# define _GLIBCXX_OPERATOR_DELETE ::operator delete

121#endif

122

123

124

125 _GLIBCXX_NODISCARD _Tp*

126 allocate(size_type __n, const void* = static_cast<const void*>(0))

127 {

128#if __cplusplus >= 201103L

129

130

131 static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");

132#endif

133

134 if (__builtin_expect(__n > this->_M_max_size(), false))

135 {

136

137

138 if (__n > (std::size_t(-1) / sizeof(_Tp)))

139 std::__throw_bad_array_new_length();

140 std::__throw_bad_alloc();

141 }

142

143#if __cpp_aligned_new && __cplusplus >= 201103L

144 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)

145 {

146 std::align_val_t __al = std::align_val_t(alignof(_Tp));

147 return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp),

148 __al));

149 }

150#endif

151 return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));

152 }

153

154

155 void

156 deallocate(_Tp* __p, size_type __n __attribute__ ((__unused__)))

157 {

158#if __cpp_sized_deallocation

159# define _GLIBCXX_SIZED_DEALLOC(p, n) (p), (n) * sizeof(_Tp)

160#else

161# define _GLIBCXX_SIZED_DEALLOC(p, n) (p)

162#endif

163

164#if __cpp_aligned_new && __cplusplus >= 201103L

165 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)

166 {

167 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),

168 std::align_val_t(alignof(_Tp)));

169 return;

170 }

171#endif

172 _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));

173 }

174

175#undef _GLIBCXX_SIZED_DEALLOC

176#undef _GLIBCXX_OPERATOR_DELETE

177#undef _GLIBCXX_OPERATOR_NEW

178

179#if __cplusplus <= 201703L

180 __attribute__((__always_inline__))

181 size_type

182 max_size() const _GLIBCXX_USE_NOEXCEPT

183 { return _M_max_size(); }

184

185#if __cplusplus >= 201103L

186 template<typename _Up, typename... _Args>

187 __attribute__((__always_inline__))

188 void

189 construct(_Up* __p, _Args&&... __args)

190 noexcept(__is_nothrow_new_constructible<_Up, _Args...>)

191 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

192

193 template<typename _Up>

194 __attribute__((__always_inline__))

195 void

196 destroy(_Up* __p)

198 { __p->~_Up(); }

199#else

200

201

202 __attribute__((__always_inline__))

203 void

204 construct(pointer __p, const _Tp& __val)

205 { ::new((void *)__p) _Tp(__val); }

206

207 __attribute__((__always_inline__))

208 void

209 destroy(pointer __p) { __p->~_Tp(); }

210#endif

211#endif

212

213 template<typename _Up>

214 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool

216 _GLIBCXX_NOTHROW

217 { return true; }

218

219#if __cpp_impl_three_way_comparison < 201907L

220 template<typename _Up>

221 friend __attribute__((__always_inline__)) _GLIBCXX20_CONSTEXPR bool

223 _GLIBCXX_NOTHROW

224 { return false; }

225#endif

226

227 private:

228 __attribute__((__always_inline__))

229 _GLIBCXX_CONSTEXPR size_type

230 _M_max_size() const _GLIBCXX_USE_NOEXCEPT

231 {

232#if __PTRDIFF_MAX__ < __SIZE_MAX__

233 return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);

234#else

235 return std::size_t(-1) / sizeof(_Tp);

236#endif

237 }

238 };

239

240_GLIBCXX_END_NAMESPACE_VERSION

241}

242

243#endif

__bool_constant< true > true_type

The type used as a compile-time boolean with true value.

constexpr _Tp * __addressof(_Tp &__r) noexcept

Same as C++11 std::addressof.

ISO C++ entities toplevel namespace is std.

An allocator that uses global new, as per C++03 [20.4.1].