File indexing completed on 2025-07-11 08:06:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_CONTAINER_ALLOCATOR_HPP
0012 #define BOOST_CONTAINER_ALLOCATOR_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/container/detail/config_begin.hpp>
0023 #include <boost/container/detail/workaround.hpp>
0024 #include <boost/container/container_fwd.hpp>
0025 #include <boost/container/detail/version_type.hpp>
0026 #include <boost/container/throw_exception.hpp>
0027 #include <boost/container/detail/dlmalloc.hpp>
0028 #include <boost/container/detail/multiallocation_chain.hpp>
0029
0030 #include <boost/move/detail/force_ptr.hpp>
0031
0032 #include <cstddef>
0033 #include <cassert>
0034
0035
0036
0037 namespace boost {
0038 namespace container {
0039
0040 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0041
0042 template<unsigned Version, unsigned int AllocationDisableMask>
0043 class allocator<void, Version, AllocationDisableMask>
0044 {
0045 typedef allocator<void, Version, AllocationDisableMask> self_t;
0046 public:
0047 typedef void value_type;
0048 typedef void * pointer;
0049 typedef const void* const_pointer;
0050 typedef int & reference;
0051 typedef const int & const_reference;
0052 typedef std::size_t size_type;
0053 typedef std::ptrdiff_t difference_type;
0054 typedef boost::container::dtl::
0055 version_type<self_t, Version> version;
0056
0057 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0058 typedef boost::container::dtl::
0059 basic_multiallocation_chain<void*> multiallocation_chain;
0060 #endif
0061
0062
0063
0064 template<class T2>
0065 struct rebind
0066 {
0067 typedef allocator< T2
0068 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0069 , Version, AllocationDisableMask
0070 #endif
0071 > other;
0072 };
0073
0074
0075
0076 allocator()
0077 {}
0078
0079
0080
0081 allocator(const allocator &)
0082 {}
0083
0084
0085
0086 template<class T2>
0087 allocator(const allocator<T2, Version, AllocationDisableMask> &)
0088 {}
0089 };
0090
0091 #endif
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 template< class T
0103 , unsigned Version BOOST_CONTAINER_DOCONLY(=2)
0104 , unsigned int AllocationDisableMask BOOST_CONTAINER_DOCONLY(=0)>
0105 class allocator
0106 {
0107 typedef unsigned int allocation_type;
0108 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0109 private:
0110
0111
0112 typedef allocator<T, Version, AllocationDisableMask> self_t;
0113
0114
0115 template<class T2, unsigned int Version2, unsigned int AllocationDisableMask2>
0116 allocator& operator=(const allocator<T2, Version2, AllocationDisableMask2>&);
0117
0118 BOOST_STATIC_CONSTEXPR unsigned int ForbiddenMask =
0119 BOOST_CONTAINER_ALLOCATE_NEW | BOOST_CONTAINER_EXPAND_BWD | BOOST_CONTAINER_EXPAND_FWD ;
0120
0121
0122 BOOST_CONTAINER_STATIC_ASSERT(( (AllocationDisableMask & ForbiddenMask) != ForbiddenMask ));
0123
0124
0125 BOOST_CONTAINER_STATIC_ASSERT(( Version != 1 || (AllocationDisableMask == 0) ));
0126
0127 #endif
0128
0129 public:
0130 typedef T value_type;
0131 typedef T * pointer;
0132 typedef const T * const_pointer;
0133 typedef T & reference;
0134 typedef const T & const_reference;
0135 typedef std::size_t size_type;
0136 typedef std::ptrdiff_t difference_type;
0137
0138 typedef boost::container::dtl::
0139 version_type<self_t, Version> version;
0140
0141 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0142 typedef boost::container::dtl::
0143 basic_multiallocation_chain<void*> void_multiallocation_chain;
0144
0145 typedef boost::container::dtl::
0146 transform_multiallocation_chain
0147 <void_multiallocation_chain, T> multiallocation_chain;
0148 #endif
0149
0150
0151
0152 template<class T2>
0153 struct rebind
0154 {
0155 typedef allocator<T2, Version, AllocationDisableMask> other;
0156 };
0157
0158
0159
0160 allocator() BOOST_NOEXCEPT_OR_NOTHROW
0161 {}
0162
0163
0164
0165 allocator(const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0166 {}
0167
0168
0169
0170 template<class T2>
0171 allocator(const allocator<T2
0172 #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
0173 , Version, AllocationDisableMask
0174 #endif
0175 > &) BOOST_NOEXCEPT_OR_NOTHROW
0176 {}
0177
0178
0179
0180
0181
0182 BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocate(size_type count, const void * hint= 0)
0183 {
0184 (void)hint;
0185 if(count > size_type(-1)/(2u*sizeof(T)))
0186 boost::container::throw_bad_alloc();
0187 void *ret = dlmalloc_malloc(count*sizeof(T));
0188 if(!ret)
0189 boost::container::throw_bad_alloc();
0190 return static_cast<pointer>(ret);
0191 }
0192
0193
0194
0195 inline void deallocate(pointer ptr, size_type) BOOST_NOEXCEPT_OR_NOTHROW
0196 { dlmalloc_free(ptr); }
0197
0198
0199
0200 inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
0201 { return size_type(-1)/(2u*sizeof(T)); }
0202
0203
0204
0205 inline friend void swap(self_t &, self_t &) BOOST_NOEXCEPT_OR_NOTHROW
0206 {}
0207
0208
0209
0210 BOOST_CONTAINER_ATTRIBUTE_NODISCARD
0211 friend bool operator==(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0212 { return true; }
0213
0214
0215
0216 BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
0217 friend bool operator!=(const allocator &, const allocator &) BOOST_NOEXCEPT_OR_NOTHROW
0218 { return false; }
0219
0220
0221
0222
0223
0224 BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocation_command(allocation_type command,
0225 size_type limit_size,
0226 size_type &prefer_in_recvd_out_size,
0227 pointer &reuse)
0228 {
0229 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0230 const allocation_type mask(AllocationDisableMask);
0231 command &= ~mask;
0232 pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
0233 if(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION))
0234 boost::container::throw_bad_alloc();
0235 return ret;
0236 }
0237
0238
0239
0240
0241
0242
0243 BOOST_CONTAINER_ATTRIBUTE_NODISCARD size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
0244 {
0245 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0246 return dlmalloc_size(p);
0247 }
0248
0249
0250
0251
0252
0253 BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline pointer allocate_one()
0254 {
0255 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0256 return this->allocate(1);
0257 }
0258
0259
0260
0261
0262 inline void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
0263 {
0264 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0265 this->allocate_many(1, num_elements, chain);
0266 }
0267
0268
0269
0270
0271
0272 void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
0273 {
0274 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0275 return this->deallocate(p, 1);
0276 }
0277
0278
0279
0280 inline
0281 void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
0282 {
0283 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0284 return this->deallocate_many(chain);
0285 }
0286
0287
0288
0289
0290 void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
0291 {
0292 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0293 dlmalloc_memchain ch;
0294 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
0295 if(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
0296 boost::container::throw_bad_alloc();
0297 }
0298 chain.incorporate_after(chain.before_begin()
0299 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
0300 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
0301 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
0302
0303
0304
0305
0306
0307 }
0308
0309
0310
0311
0312 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
0313 {
0314 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0315 dlmalloc_memchain ch;
0316 BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
0317 if(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), BOOST_CONTAINER_DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch)){
0318 boost::container::throw_bad_alloc();
0319 }
0320 chain.incorporate_after(chain.before_begin()
0321 ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
0322 ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
0323 ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );
0324
0325
0326
0327
0328
0329 }
0330
0331
0332
0333
0334 void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
0335 {
0336 BOOST_CONTAINER_STATIC_ASSERT(( Version > 1 ));
0337 dlmalloc_memchain ch;
0338 void *beg(&*chain.begin()), *last(&*chain.last());
0339 size_t size(chain.size());
0340 BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
0341 dlmalloc_multidealloc(&ch);
0342
0343 }
0344
0345 private:
0346
0347 pointer priv_allocation_command
0348 (allocation_type command, std::size_t limit_size
0349 ,size_type &prefer_in_recvd_out_size
0350 ,pointer &reuse_ptr)
0351 {
0352 std::size_t const preferred_size = prefer_in_recvd_out_size;
0353 dlmalloc_command_ret_t ret = {0 , 0};
0354 if((limit_size > this->max_size()) || (preferred_size > this->max_size())){
0355 return pointer();
0356 }
0357 std::size_t l_size = limit_size*sizeof(T);
0358 std::size_t p_size = preferred_size*sizeof(T);
0359 std::size_t r_size;
0360 {
0361 void* reuse_ptr_void = reuse_ptr;
0362 ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
0363 reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
0364 }
0365 prefer_in_recvd_out_size = r_size/sizeof(T);
0366 return (pointer)ret.first;
0367 }
0368 };
0369
0370 }
0371 }
0372
0373 #include <boost/container/detail/config_end.hpp>
0374
0375 #endif
0376