mt_allocator.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // MT-optimized allocator -*- C++ -*-
  2. // Copyright (C) 2003-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/mt_allocator.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _MT_ALLOCATOR_H
  24. #define _MT_ALLOCATOR_H 1
  25. #include <new>
  26. #include <cstdlib>
  27. #include <bits/functexcept.h>
  28. #include <ext/atomicity.h>
  29. #include <bits/move.h>
  30. #if __cplusplus >= 201103L
  31. #include <type_traits>
  32. #endif
  33. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. typedef void (*__destroy_handler)(void*);
  37. /// Base class for pool object.
  38. struct __pool_base
  39. {
  40. // Using short int as type for the binmap implies we are never
  41. // caching blocks larger than 32768 with this allocator.
  42. typedef unsigned short int _Binmap_type;
  43. typedef std::size_t size_t;
  44. // Variables used to configure the behavior of the allocator,
  45. // assigned and explained in detail below.
  46. struct _Tune
  47. {
  48. // Compile time constants for the default _Tune values.
  49. enum { _S_align = 8 };
  50. enum { _S_max_bytes = 128 };
  51. enum { _S_min_bin = 8 };
  52. enum { _S_chunk_size = 4096 - 4 * sizeof(void*) };
  53. enum { _S_max_threads = 4096 };
  54. enum { _S_freelist_headroom = 10 };
  55. // Alignment needed.
  56. // NB: In any case must be >= sizeof(_Block_record), that
  57. // is 4 on 32 bit machines and 8 on 64 bit machines.
  58. size_t _M_align;
  59. // Allocation requests (after round-up to power of 2) below
  60. // this value will be handled by the allocator. A raw new/
  61. // call will be used for requests larger than this value.
  62. // NB: Must be much smaller than _M_chunk_size and in any
  63. // case <= 32768.
  64. size_t _M_max_bytes;
  65. // Size in bytes of the smallest bin.
  66. // NB: Must be a power of 2 and >= _M_align (and of course
  67. // much smaller than _M_max_bytes).
  68. size_t _M_min_bin;
  69. // In order to avoid fragmenting and minimize the number of
  70. // new() calls we always request new memory using this
  71. // value. Based on previous discussions on the libstdc++
  72. // mailing list we have chosen the value below.
  73. // See http://gcc.gnu.org/ml/libstdc++/2001-07/msg00077.html
  74. // NB: At least one order of magnitude > _M_max_bytes.
  75. size_t _M_chunk_size;
  76. // The maximum number of supported threads. For
  77. // single-threaded operation, use one. Maximum values will
  78. // vary depending on details of the underlying system. (For
  79. // instance, Linux 2.4.18 reports 4070 in
  80. // /proc/sys/kernel/threads-max, while Linux 2.6.6 reports
  81. // 65534)
  82. size_t _M_max_threads;
  83. // Each time a deallocation occurs in a threaded application
  84. // we make sure that there are no more than
  85. // _M_freelist_headroom % of used memory on the freelist. If
  86. // the number of additional records is more than
  87. // _M_freelist_headroom % of the freelist, we move these
  88. // records back to the global pool.
  89. size_t _M_freelist_headroom;
  90. // Set to true forces all allocations to use new().
  91. bool _M_force_new;
  92. explicit
  93. _Tune()
  94. : _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),
  95. _M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),
  96. _M_freelist_headroom(_S_freelist_headroom),
  97. _M_force_new(std::getenv("GLIBCXX_FORCE_NEW") ? true : false)
  98. { }
  99. explicit
  100. _Tune(size_t __align, size_t __maxb, size_t __minbin, size_t __chunk,
  101. size_t __maxthreads, size_t __headroom, bool __force)
  102. : _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),
  103. _M_chunk_size(__chunk), _M_max_threads(__maxthreads),
  104. _M_freelist_headroom(__headroom), _M_force_new(__force)
  105. { }
  106. };
  107. struct _Block_address
  108. {
  109. void* _M_initial;
  110. _Block_address* _M_next;
  111. };
  112. const _Tune&
  113. _M_get_options() const
  114. { return _M_options; }
  115. void
  116. _M_set_options(_Tune __t)
  117. {
  118. if (!_M_init)
  119. _M_options = __t;
  120. }
  121. bool
  122. _M_check_threshold(size_t __bytes)
  123. { return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }
  124. size_t
  125. _M_get_binmap(size_t __bytes)
  126. { return _M_binmap[__bytes]; }
  127. size_t
  128. _M_get_align()
  129. { return _M_options._M_align; }
  130. explicit
  131. __pool_base()
  132. : _M_options(_Tune()), _M_binmap(0), _M_init(false) { }
  133. explicit
  134. __pool_base(const _Tune& __options)
  135. : _M_options(__options), _M_binmap(0), _M_init(false) { }
  136. private:
  137. explicit
  138. __pool_base(const __pool_base&);
  139. __pool_base&
  140. operator=(const __pool_base&);
  141. protected:
  142. // Configuration options.
  143. _Tune _M_options;
  144. _Binmap_type* _M_binmap;
  145. // Configuration of the pool object via _M_options can happen
  146. // after construction but before initialization. After
  147. // initialization is complete, this variable is set to true.
  148. bool _M_init;
  149. };
  150. /**
  151. * @brief Data describing the underlying memory pool, parameterized on
  152. * threading support.
  153. */
  154. template<bool _Thread>
  155. class __pool;
  156. /// Specialization for single thread.
  157. template<>
  158. class __pool<false> : public __pool_base
  159. {
  160. public:
  161. union _Block_record
  162. {
  163. // Points to the block_record of the next free block.
  164. _Block_record* _M_next;
  165. };
  166. struct _Bin_record
  167. {
  168. // An "array" of pointers to the first free block.
  169. _Block_record** _M_first;
  170. // A list of the initial addresses of all allocated blocks.
  171. _Block_address* _M_address;
  172. };
  173. void
  174. _M_initialize_once()
  175. {
  176. if (__builtin_expect(_M_init == false, false))
  177. _M_initialize();
  178. }
  179. void
  180. _M_destroy() throw();
  181. char*
  182. _M_reserve_block(size_t __bytes, const size_t __thread_id);
  183. void
  184. _M_reclaim_block(char* __p, size_t __bytes) throw ();
  185. size_t
  186. _M_get_thread_id() { return 0; }
  187. const _Bin_record&
  188. _M_get_bin(size_t __which)
  189. { return _M_bin[__which]; }
  190. void
  191. _M_adjust_freelist(const _Bin_record&, _Block_record*, size_t)
  192. { }
  193. explicit __pool()
  194. : _M_bin(0), _M_bin_size(1) { }
  195. explicit __pool(const __pool_base::_Tune& __tune)
  196. : __pool_base(__tune), _M_bin(0), _M_bin_size(1) { }
  197. private:
  198. // An "array" of bin_records each of which represents a specific
  199. // power of 2 size. Memory to this "array" is allocated in
  200. // _M_initialize().
  201. _Bin_record* _M_bin;
  202. // Actual value calculated in _M_initialize().
  203. size_t _M_bin_size;
  204. void
  205. _M_initialize();
  206. };
  207. #ifdef __GTHREADS
  208. /// Specialization for thread enabled, via gthreads.h.
  209. template<>
  210. class __pool<true> : public __pool_base
  211. {
  212. public:
  213. // Each requesting thread is assigned an id ranging from 1 to
  214. // _S_max_threads. Thread id 0 is used as a global memory pool.
  215. // In order to get constant performance on the thread assignment
  216. // routine, we keep a list of free ids. When a thread first
  217. // requests memory we remove the first record in this list and
  218. // stores the address in a __gthread_key. When initializing the
  219. // __gthread_key we specify a destructor. When this destructor
  220. // (i.e. the thread dies) is called, we return the thread id to
  221. // the front of this list.
  222. struct _Thread_record
  223. {
  224. // Points to next free thread id record. NULL if last record in list.
  225. _Thread_record* _M_next;
  226. // Thread id ranging from 1 to _S_max_threads.
  227. size_t _M_id;
  228. };
  229. union _Block_record
  230. {
  231. // Points to the block_record of the next free block.
  232. _Block_record* _M_next;
  233. // The thread id of the thread which has requested this block.
  234. size_t _M_thread_id;
  235. };
  236. struct _Bin_record
  237. {
  238. // An "array" of pointers to the first free block for each
  239. // thread id. Memory to this "array" is allocated in
  240. // _S_initialize() for _S_max_threads + global pool 0.
  241. _Block_record** _M_first;
  242. // A list of the initial addresses of all allocated blocks.
  243. _Block_address* _M_address;
  244. // An "array" of counters used to keep track of the amount of
  245. // blocks that are on the freelist/used for each thread id.
  246. // - Note that the second part of the allocated _M_used "array"
  247. // actually hosts (atomic) counters of reclaimed blocks: in
  248. // _M_reserve_block and in _M_reclaim_block those numbers are
  249. // subtracted from the first ones to obtain the actual size
  250. // of the "working set" of the given thread.
  251. // - Memory to these "arrays" is allocated in _S_initialize()
  252. // for _S_max_threads + global pool 0.
  253. size_t* _M_free;
  254. size_t* _M_used;
  255. // Each bin has its own mutex which is used to ensure data
  256. // integrity while changing "ownership" on a block. The mutex
  257. // is initialized in _S_initialize().
  258. __gthread_mutex_t* _M_mutex;
  259. };
  260. // XXX GLIBCXX_ABI Deprecated
  261. void
  262. _M_initialize(__destroy_handler);
  263. void
  264. _M_initialize_once()
  265. {
  266. if (__builtin_expect(_M_init == false, false))
  267. _M_initialize();
  268. }
  269. void
  270. _M_destroy() throw();
  271. char*
  272. _M_reserve_block(size_t __bytes, const size_t __thread_id);
  273. void
  274. _M_reclaim_block(char* __p, size_t __bytes) throw ();
  275. const _Bin_record&
  276. _M_get_bin(size_t __which)
  277. { return _M_bin[__which]; }
  278. void
  279. _M_adjust_freelist(const _Bin_record& __bin, _Block_record* __block,
  280. size_t __thread_id)
  281. {
  282. if (__gthread_active_p())
  283. {
  284. __block->_M_thread_id = __thread_id;
  285. --__bin._M_free[__thread_id];
  286. ++__bin._M_used[__thread_id];
  287. }
  288. }
  289. // XXX GLIBCXX_ABI Deprecated
  290. void
  291. _M_destroy_thread_key(void*) throw ();
  292. size_t
  293. _M_get_thread_id();
  294. explicit __pool()
  295. : _M_bin(0), _M_bin_size(1), _M_thread_freelist(0)
  296. { }
  297. explicit __pool(const __pool_base::_Tune& __tune)
  298. : __pool_base(__tune), _M_bin(0), _M_bin_size(1),
  299. _M_thread_freelist(0)
  300. { }
  301. private:
  302. // An "array" of bin_records each of which represents a specific
  303. // power of 2 size. Memory to this "array" is allocated in
  304. // _M_initialize().
  305. _Bin_record* _M_bin;
  306. // Actual value calculated in _M_initialize().
  307. size_t _M_bin_size;
  308. _Thread_record* _M_thread_freelist;
  309. void* _M_thread_freelist_initial;
  310. void
  311. _M_initialize();
  312. };
  313. #endif
  314. template<template <bool> class _PoolTp, bool _Thread>
  315. struct __common_pool
  316. {
  317. typedef _PoolTp<_Thread> pool_type;
  318. static pool_type&
  319. _S_get_pool()
  320. {
  321. static pool_type _S_pool;
  322. return _S_pool;
  323. }
  324. };
  325. template<template <bool> class _PoolTp, bool _Thread>
  326. struct __common_pool_base;
  327. template<template <bool> class _PoolTp>
  328. struct __common_pool_base<_PoolTp, false>
  329. : public __common_pool<_PoolTp, false>
  330. {
  331. using __common_pool<_PoolTp, false>::_S_get_pool;
  332. static void
  333. _S_initialize_once()
  334. {
  335. static bool __init;
  336. if (__builtin_expect(__init == false, false))
  337. {
  338. _S_get_pool()._M_initialize_once();
  339. __init = true;
  340. }
  341. }
  342. };
  343. #ifdef __GTHREADS
  344. template<template <bool> class _PoolTp>
  345. struct __common_pool_base<_PoolTp, true>
  346. : public __common_pool<_PoolTp, true>
  347. {
  348. using __common_pool<_PoolTp, true>::_S_get_pool;
  349. static void
  350. _S_initialize()
  351. { _S_get_pool()._M_initialize_once(); }
  352. static void
  353. _S_initialize_once()
  354. {
  355. static bool __init;
  356. if (__builtin_expect(__init == false, false))
  357. {
  358. if (__gthread_active_p())
  359. {
  360. // On some platforms, __gthread_once_t is an aggregate.
  361. static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
  362. __gthread_once(&__once, _S_initialize);
  363. }
  364. // Double check initialization. May be necessary on some
  365. // systems for proper construction when not compiling with
  366. // thread flags.
  367. _S_get_pool()._M_initialize_once();
  368. __init = true;
  369. }
  370. }
  371. };
  372. #endif
  373. /// Policy for shared __pool objects.
  374. template<template <bool> class _PoolTp, bool _Thread>
  375. struct __common_pool_policy : public __common_pool_base<_PoolTp, _Thread>
  376. {
  377. template<typename _Tp1, template <bool> class _PoolTp1 = _PoolTp,
  378. bool _Thread1 = _Thread>
  379. struct _M_rebind
  380. { typedef __common_pool_policy<_PoolTp1, _Thread1> other; };
  381. using __common_pool_base<_PoolTp, _Thread>::_S_get_pool;
  382. using __common_pool_base<_PoolTp, _Thread>::_S_initialize_once;
  383. };
  384. template<typename _Tp, template <bool> class _PoolTp, bool _Thread>
  385. struct __per_type_pool
  386. {
  387. typedef _Tp value_type;
  388. typedef _PoolTp<_Thread> pool_type;
  389. static pool_type&
  390. _S_get_pool()
  391. {
  392. using std::size_t;
  393. // Sane defaults for the _PoolTp.
  394. typedef typename pool_type::_Block_record _Block_record;
  395. const static size_t __a = (__alignof__(_Tp) >= sizeof(_Block_record)
  396. ? __alignof__(_Tp) : sizeof(_Block_record));
  397. typedef typename __pool_base::_Tune _Tune;
  398. static _Tune _S_tune(__a, sizeof(_Tp) * 64,
  399. sizeof(_Tp) * 2 >= __a ? sizeof(_Tp) * 2 : __a,
  400. sizeof(_Tp) * size_t(_Tune::_S_chunk_size),
  401. _Tune::_S_max_threads,
  402. _Tune::_S_freelist_headroom,
  403. std::getenv("GLIBCXX_FORCE_NEW") ? true : false);
  404. static pool_type _S_pool(_S_tune);
  405. return _S_pool;
  406. }
  407. };
  408. template<typename _Tp, template <bool> class _PoolTp, bool _Thread>
  409. struct __per_type_pool_base;
  410. template<typename _Tp, template <bool> class _PoolTp>
  411. struct __per_type_pool_base<_Tp, _PoolTp, false>
  412. : public __per_type_pool<_Tp, _PoolTp, false>
  413. {
  414. using __per_type_pool<_Tp, _PoolTp, false>::_S_get_pool;
  415. static void
  416. _S_initialize_once()
  417. {
  418. static bool __init;
  419. if (__builtin_expect(__init == false, false))
  420. {
  421. _S_get_pool()._M_initialize_once();
  422. __init = true;
  423. }
  424. }
  425. };
  426. #ifdef __GTHREADS
  427. template<typename _Tp, template <bool> class _PoolTp>
  428. struct __per_type_pool_base<_Tp, _PoolTp, true>
  429. : public __per_type_pool<_Tp, _PoolTp, true>
  430. {
  431. using __per_type_pool<_Tp, _PoolTp, true>::_S_get_pool;
  432. static void
  433. _S_initialize()
  434. { _S_get_pool()._M_initialize_once(); }
  435. static void
  436. _S_initialize_once()
  437. {
  438. static bool __init;
  439. if (__builtin_expect(__init == false, false))
  440. {
  441. if (__gthread_active_p())
  442. {
  443. // On some platforms, __gthread_once_t is an aggregate.
  444. static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
  445. __gthread_once(&__once, _S_initialize);
  446. }
  447. // Double check initialization. May be necessary on some
  448. // systems for proper construction when not compiling with
  449. // thread flags.
  450. _S_get_pool()._M_initialize_once();
  451. __init = true;
  452. }
  453. }
  454. };
  455. #endif
  456. /// Policy for individual __pool objects.
  457. template<typename _Tp, template <bool> class _PoolTp, bool _Thread>
  458. struct __per_type_pool_policy
  459. : public __per_type_pool_base<_Tp, _PoolTp, _Thread>
  460. {
  461. template<typename _Tp1, template <bool> class _PoolTp1 = _PoolTp,
  462. bool _Thread1 = _Thread>
  463. struct _M_rebind
  464. { typedef __per_type_pool_policy<_Tp1, _PoolTp1, _Thread1> other; };
  465. using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_get_pool;
  466. using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_initialize_once;
  467. };
  468. /// Base class for _Tp dependent member functions.
  469. template<typename _Tp>
  470. class __mt_alloc_base
  471. {
  472. public:
  473. typedef std::size_t size_type;
  474. typedef std::ptrdiff_t difference_type;
  475. typedef _Tp* pointer;
  476. typedef const _Tp* const_pointer;
  477. typedef _Tp& reference;
  478. typedef const _Tp& const_reference;
  479. typedef _Tp value_type;
  480. #if __cplusplus >= 201103L
  481. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  482. // 2103. propagate_on_container_move_assignment
  483. typedef std::true_type propagate_on_container_move_assignment;
  484. #endif
  485. pointer
  486. address(reference __x) const _GLIBCXX_NOEXCEPT
  487. { return std::__addressof(__x); }
  488. const_pointer
  489. address(const_reference __x) const _GLIBCXX_NOEXCEPT
  490. { return std::__addressof(__x); }
  491. size_type
  492. max_size() const _GLIBCXX_USE_NOEXCEPT
  493. { return size_type(-1) / sizeof(_Tp); }
  494. #if __cplusplus >= 201103L
  495. template<typename _Up, typename... _Args>
  496. void
  497. construct(_Up* __p, _Args&&... __args)
  498. { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
  499. template<typename _Up>
  500. void
  501. destroy(_Up* __p) { __p->~_Up(); }
  502. #else
  503. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  504. // 402. wrong new expression in [some_] allocator::construct
  505. void
  506. construct(pointer __p, const _Tp& __val)
  507. { ::new((void *)__p) _Tp(__val); }
  508. void
  509. destroy(pointer __p) { __p->~_Tp(); }
  510. #endif
  511. };
  512. #ifdef __GTHREADS
  513. #define __thread_default true
  514. #else
  515. #define __thread_default false
  516. #endif
  517. /**
  518. * @brief This is a fixed size (power of 2) allocator which - when
  519. * compiled with thread support - will maintain one freelist per
  520. * size per thread plus a @a global one. Steps are taken to limit
  521. * the per thread freelist sizes (by returning excess back to
  522. * the @a global list).
  523. * @ingroup allocators
  524. *
  525. * Further details:
  526. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/mt_allocator.html
  527. */
  528. template<typename _Tp,
  529. typename _Poolp = __common_pool_policy<__pool, __thread_default> >
  530. class __mt_alloc : public __mt_alloc_base<_Tp>
  531. {
  532. public:
  533. typedef std::size_t size_type;
  534. typedef std::ptrdiff_t difference_type;
  535. typedef _Tp* pointer;
  536. typedef const _Tp* const_pointer;
  537. typedef _Tp& reference;
  538. typedef const _Tp& const_reference;
  539. typedef _Tp value_type;
  540. typedef _Poolp __policy_type;
  541. typedef typename _Poolp::pool_type __pool_type;
  542. template<typename _Tp1, typename _Poolp1 = _Poolp>
  543. struct rebind
  544. {
  545. typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;
  546. typedef __mt_alloc<_Tp1, pol_type> other;
  547. };
  548. __mt_alloc() _GLIBCXX_USE_NOEXCEPT { }
  549. __mt_alloc(const __mt_alloc&) _GLIBCXX_USE_NOEXCEPT { }
  550. template<typename _Tp1, typename _Poolp1>
  551. __mt_alloc(const __mt_alloc<_Tp1, _Poolp1>&) _GLIBCXX_USE_NOEXCEPT { }
  552. ~__mt_alloc() _GLIBCXX_USE_NOEXCEPT { }
  553. _GLIBCXX_NODISCARD pointer
  554. allocate(size_type __n, const void* = 0);
  555. void
  556. deallocate(pointer __p, size_type __n);
  557. const __pool_base::_Tune
  558. _M_get_options()
  559. {
  560. // Return a copy, not a reference, for external consumption.
  561. return __policy_type::_S_get_pool()._M_get_options();
  562. }
  563. void
  564. _M_set_options(__pool_base::_Tune __t)
  565. { __policy_type::_S_get_pool()._M_set_options(__t); }
  566. };
  567. template<typename _Tp, typename _Poolp>
  568. _GLIBCXX_NODISCARD typename __mt_alloc<_Tp, _Poolp>::pointer
  569. __mt_alloc<_Tp, _Poolp>::
  570. allocate(size_type __n, const void*)
  571. {
  572. if (__n > this->max_size())
  573. std::__throw_bad_alloc();
  574. #if __cpp_aligned_new
  575. // Types with extended alignment are handled by operator new/delete.
  576. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  577. {
  578. std::align_val_t __al = std::align_val_t(alignof(_Tp));
  579. return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));
  580. }
  581. #endif
  582. __policy_type::_S_initialize_once();
  583. // Requests larger than _M_max_bytes are handled by operator
  584. // new/delete directly.
  585. __pool_type& __pool = __policy_type::_S_get_pool();
  586. const size_type __bytes = __n * sizeof(_Tp);
  587. if (__pool._M_check_threshold(__bytes))
  588. {
  589. void* __ret = ::operator new(__bytes);
  590. return static_cast<_Tp*>(__ret);
  591. }
  592. // Round up to power of 2 and figure out which bin to use.
  593. const size_type __which = __pool._M_get_binmap(__bytes);
  594. const size_type __thread_id = __pool._M_get_thread_id();
  595. // Find out if we have blocks on our freelist. If so, go ahead
  596. // and use them directly without having to lock anything.
  597. char* __c;
  598. typedef typename __pool_type::_Bin_record _Bin_record;
  599. const _Bin_record& __bin = __pool._M_get_bin(__which);
  600. if (__bin._M_first[__thread_id])
  601. {
  602. // Already reserved.
  603. typedef typename __pool_type::_Block_record _Block_record;
  604. _Block_record* __block = __bin._M_first[__thread_id];
  605. __bin._M_first[__thread_id] = __block->_M_next;
  606. __pool._M_adjust_freelist(__bin, __block, __thread_id);
  607. __c = reinterpret_cast<char*>(__block) + __pool._M_get_align();
  608. }
  609. else
  610. {
  611. // Null, reserve.
  612. __c = __pool._M_reserve_block(__bytes, __thread_id);
  613. }
  614. return static_cast<_Tp*>(static_cast<void*>(__c));
  615. }
  616. template<typename _Tp, typename _Poolp>
  617. void
  618. __mt_alloc<_Tp, _Poolp>::
  619. deallocate(pointer __p, size_type __n)
  620. {
  621. if (__builtin_expect(__p != 0, true))
  622. {
  623. #if __cpp_aligned_new
  624. // Types with extended alignment are handled by operator new/delete.
  625. if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
  626. {
  627. ::operator delete(__p, std::align_val_t(alignof(_Tp)));
  628. return;
  629. }
  630. #endif
  631. // Requests larger than _M_max_bytes are handled by
  632. // operators new/delete directly.
  633. __pool_type& __pool = __policy_type::_S_get_pool();
  634. const size_type __bytes = __n * sizeof(_Tp);
  635. if (__pool._M_check_threshold(__bytes))
  636. ::operator delete(__p);
  637. else
  638. __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);
  639. }
  640. }
  641. template<typename _Tp, typename _Poolp>
  642. inline bool
  643. operator==(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)
  644. { return true; }
  645. #if __cpp_impl_three_way_comparison < 201907L
  646. template<typename _Tp, typename _Poolp>
  647. inline bool
  648. operator!=(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)
  649. { return false; }
  650. #endif
  651. #undef __thread_default
  652. _GLIBCXX_END_NAMESPACE_VERSION
  653. } // namespace
  654. #endif