mt_allocator.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // MT-optimized allocator -*- C++ -*-
  2. // Copyright (C) 2003-2018 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. using std::size_t;
  37. using std::ptrdiff_t;
  38. typedef void (*__destroy_handler)(void*);
  39. /// Base class for pool object.
  40. struct __pool_base
  41. {
  42. // Using short int as type for the binmap implies we are never
  43. // caching blocks larger than 32768 with this allocator.
  44. typedef unsigned short int _Binmap_type;
  45. // Variables used to configure the behavior of the allocator,
  46. // assigned and explained in detail below.
  47. struct _Tune
  48. {
  49. // Compile time constants for the default _Tune values.
  50. enum { _S_align = 8 };
  51. enum { _S_max_bytes = 128 };
  52. enum { _S_min_bin = 8 };
  53. enum { _S_chunk_size = 4096 - 4 * sizeof(void*) };
  54. enum { _S_max_threads = 4096 };
  55. enum { _S_freelist_headroom = 10 };
  56. // Alignment needed.
  57. // NB: In any case must be >= sizeof(_Block_record), that
  58. // is 4 on 32 bit machines and 8 on 64 bit machines.
  59. size_t _M_align;
  60. // Allocation requests (after round-up to power of 2) below
  61. // this value will be handled by the allocator. A raw new/
  62. // call will be used for requests larger than this value.
  63. // NB: Must be much smaller than _M_chunk_size and in any
  64. // case <= 32768.
  65. size_t _M_max_bytes;
  66. // Size in bytes of the smallest bin.
  67. // NB: Must be a power of 2 and >= _M_align (and of course
  68. // much smaller than _M_max_bytes).
  69. size_t _M_min_bin;
  70. // In order to avoid fragmenting and minimize the number of
  71. // new() calls we always request new memory using this
  72. // value. Based on previous discussions on the libstdc++
  73. // mailing list we have chosen the value below.
  74. // See http://gcc.gnu.org/ml/libstdc++/2001-07/msg00077.html
  75. // NB: At least one order of magnitude > _M_max_bytes.
  76. size_t _M_chunk_size;
  77. // The maximum number of supported threads. For
  78. // single-threaded operation, use one. Maximum values will
  79. // vary depending on details of the underlying system. (For
  80. // instance, Linux 2.4.18 reports 4070 in
  81. // /proc/sys/kernel/threads-max, while Linux 2.6.6 reports
  82. // 65534)
  83. size_t _M_max_threads;
  84. // Each time a deallocation occurs in a threaded application
  85. // we make sure that there are no more than
  86. // _M_freelist_headroom % of used memory on the freelist. If
  87. // the number of additional records is more than
  88. // _M_freelist_headroom % of the freelist, we move these
  89. // records back to the global pool.
  90. size_t _M_freelist_headroom;
  91. // Set to true forces all allocations to use new().
  92. bool _M_force_new;
  93. explicit
  94. _Tune()
  95. : _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),
  96. _M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),
  97. _M_freelist_headroom(_S_freelist_headroom),
  98. _M_force_new(std::getenv("GLIBCXX_FORCE_NEW") ? true : false)
  99. { }
  100. explicit
  101. _Tune(size_t __align, size_t __maxb, size_t __minbin, size_t __chunk,
  102. size_t __maxthreads, size_t __headroom, bool __force)
  103. : _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),
  104. _M_chunk_size(__chunk), _M_max_threads(__maxthreads),
  105. _M_freelist_headroom(__headroom), _M_force_new(__force)
  106. { }
  107. };
  108. struct _Block_address
  109. {
  110. void* _M_initial;
  111. _Block_address* _M_next;
  112. };
  113. const _Tune&
  114. _M_get_options() const
  115. { return _M_options; }
  116. void
  117. _M_set_options(_Tune __t)
  118. {
  119. if (!_M_init)
  120. _M_options = __t;
  121. }
  122. bool
  123. _M_check_threshold(size_t __bytes)
  124. { return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }
  125. size_t
  126. _M_get_binmap(size_t __bytes)
  127. { return _M_binmap[__bytes]; }
  128. size_t
  129. _M_get_align()
  130. { return _M_options._M_align; }
  131. explicit
  132. __pool_base()
  133. : _M_options(_Tune()), _M_binmap(0), _M_init(false) { }
  134. explicit
  135. __pool_base(const _Tune& __options)
  136. : _M_options(__options), _M_binmap(0), _M_init(false) { }
  137. private:
  138. explicit
  139. __pool_base(const __pool_base&);
  140. __pool_base&
  141. operator=(const __pool_base&);
  142. protected:
  143. // Configuration options.
  144. _Tune _M_options;
  145. _Binmap_type* _M_binmap;
  146. // Configuration of the pool object via _M_options can happen
  147. // after construction but before initialization. After
  148. // initialization is complete, this variable is set to true.
  149. bool _M_init;
  150. };
  151. /**
  152. * @brief Data describing the underlying memory pool, parameterized on
  153. * threading support.
  154. */
  155. template<bool _Thread>
  156. class __pool;
  157. /// Specialization for single thread.
  158. template<>
  159. class __pool<false> : public __pool_base
  160. {
  161. public:
  162. union _Block_record
  163. {
  164. // Points to the block_record of the next free block.
  165. _Block_record* _M_next;
  166. };
  167. struct _Bin_record
  168. {
  169. // An "array" of pointers to the first free block.
  170. _Block_record** _M_first;
  171. // A list of the initial addresses of all allocated blocks.
  172. _Block_address* _M_address;
  173. };
  174. void
  175. _M_initialize_once()
  176. {
  177. if (__builtin_expect(_M_init == false, false))
  178. _M_initialize();
  179. }
  180. void
  181. _M_destroy() throw();
  182. char*
  183. _M_reserve_block(size_t __bytes, const size_t __thread_id);
  184. void
  185. _M_reclaim_block(char* __p, size_t __bytes) throw ();
  186. size_t
  187. _M_get_thread_id() { return 0; }
  188. const _Bin_record&
  189. _M_get_bin(size_t __which)
  190. { return _M_bin[__which]; }
  191. void
  192. _M_adjust_freelist(const _Bin_record&, _Block_record*, size_t)
  193. { }
  194. explicit __pool()
  195. : _M_bin(0), _M_bin_size(1) { }
  196. explicit __pool(const __pool_base::_Tune& __tune)
  197. : __pool_base(__tune), _M_bin(0), _M_bin_size(1) { }
  198. private:
  199. // An "array" of bin_records each of which represents a specific
  200. // power of 2 size. Memory to this "array" is allocated in
  201. // _M_initialize().
  202. _Bin_record* _M_bin;
  203. // Actual value calculated in _M_initialize().
  204. size_t _M_bin_size;
  205. void
  206. _M_initialize();
  207. };
  208. #ifdef __GTHREADS
  209. /// Specialization for thread enabled, via gthreads.h.
  210. template<>
  211. class __pool<true> : public __pool_base
  212. {
  213. public:
  214. // Each requesting thread is assigned an id ranging from 1 to
  215. // _S_max_threads. Thread id 0 is used as a global memory pool.
  216. // In order to get constant performance on the thread assignment
  217. // routine, we keep a list of free ids. When a thread first
  218. // requests memory we remove the first record in this list and
  219. // stores the address in a __gthread_key. When initializing the
  220. // __gthread_key we specify a destructor. When this destructor
  221. // (i.e. the thread dies) is called, we return the thread id to
  222. // the front of this list.
  223. struct _Thread_record
  224. {
  225. // Points to next free thread id record. NULL if last record in list.
  226. _Thread_record* _M_next;
  227. // Thread id ranging from 1 to _S_max_threads.
  228. size_t _M_id;
  229. };
  230. union _Block_record
  231. {
  232. // Points to the block_record of the next free block.
  233. _Block_record* _M_next;
  234. // The thread id of the thread which has requested this block.
  235. size_t _M_thread_id;
  236. };
  237. struct _Bin_record
  238. {
  239. // An "array" of pointers to the first free block for each
  240. // thread id. Memory to this "array" is allocated in
  241. // _S_initialize() for _S_max_threads + global pool 0.
  242. _Block_record** _M_first;
  243. // A list of the initial addresses of all allocated blocks.
  244. _Block_address* _M_address;
  245. // An "array" of counters used to keep track of the amount of
  246. // blocks that are on the freelist/used for each thread id.
  247. // - Note that the second part of the allocated _M_used "array"
  248. // actually hosts (atomic) counters of reclaimed blocks: in
  249. // _M_reserve_block and in _M_reclaim_block those numbers are
  250. // subtracted from the first ones to obtain the actual size
  251. // of the "working set" of the given thread.
  252. // - Memory to these "arrays" is allocated in _S_initialize()
  253. // for _S_max_threads + global pool 0.
  254. size_t* _M_free;
  255. size_t* _M_used;
  256. // Each bin has its own mutex which is used to ensure data
  257. // integrity while changing "ownership" on a block. The mutex
  258. // is initialized in _S_initialize().
  259. __gthread_mutex_t* _M_mutex;
  260. };
  261. // XXX GLIBCXX_ABI Deprecated
  262. void
  263. _M_initialize(__destroy_handler);
  264. void
  265. _M_initialize_once()
  266. {
  267. if (__builtin_expect(_M_init == false, false))
  268. _M_initialize();
  269. }
  270. void
  271. _M_destroy() throw();
  272. char*
  273. _M_reserve_block(size_t __bytes, const size_t __thread_id);
  274. void
  275. _M_reclaim_block(char* __p, size_t __bytes) throw ();
  276. const _Bin_record&
  277. _M_get_bin(size_t __which)
  278. { return _M_bin[__which]; }
  279. void
  280. _M_adjust_freelist(const _Bin_record& __bin, _Block_record* __block,
  281. size_t __thread_id)
  282. {
  283. if (__gthread_active_p())
  284. {
  285. __block->_M_thread_id = __thread_id;
  286. --__bin._M_free[__thread_id];
  287. ++__bin._M_used[__thread_id];
  288. }
  289. }
  290. // XXX GLIBCXX_ABI Deprecated
  291. void
  292. _M_destroy_thread_key(void*) throw ();
  293. size_t
  294. _M_get_thread_id();
  295. explicit __pool()
  296. : _M_bin(0), _M_bin_size(1), _M_thread_freelist(0)
  297. { }
  298. explicit __pool(const __pool_base::_Tune& __tune)
  299. : __pool_base(__tune), _M_bin(0), _M_bin_size(1),
  300. _M_thread_freelist(0)
  301. { }
  302. private:
  303. // An "array" of bin_records each of which represents a specific
  304. // power of 2 size. Memory to this "array" is allocated in
  305. // _M_initialize().
  306. _Bin_record* _M_bin;
  307. // Actual value calculated in _M_initialize().
  308. size_t _M_bin_size;
  309. _Thread_record* _M_thread_freelist;
  310. void* _M_thread_freelist_initial;
  311. void
  312. _M_initialize();
  313. };
  314. #endif
  315. template<template <bool> class _PoolTp, bool _Thread>
  316. struct __common_pool
  317. {
  318. typedef _PoolTp<_Thread> pool_type;
  319. static pool_type&
  320. _S_get_pool()
  321. {
  322. static pool_type _S_pool;
  323. return _S_pool;
  324. }
  325. };
  326. template<template <bool> class _PoolTp, bool _Thread>
  327. struct __common_pool_base;
  328. template<template <bool> class _PoolTp>
  329. struct __common_pool_base<_PoolTp, false>
  330. : public __common_pool<_PoolTp, false>
  331. {
  332. using __common_pool<_PoolTp, false>::_S_get_pool;
  333. static void
  334. _S_initialize_once()
  335. {
  336. static bool __init;
  337. if (__builtin_expect(__init == false, false))
  338. {
  339. _S_get_pool()._M_initialize_once();
  340. __init = true;
  341. }
  342. }
  343. };
  344. #ifdef __GTHREADS
  345. template<template <bool> class _PoolTp>
  346. struct __common_pool_base<_PoolTp, true>
  347. : public __common_pool<_PoolTp, true>
  348. {
  349. using __common_pool<_PoolTp, true>::_S_get_pool;
  350. static void
  351. _S_initialize()
  352. { _S_get_pool()._M_initialize_once(); }
  353. static void
  354. _S_initialize_once()
  355. {
  356. static bool __init;
  357. if (__builtin_expect(__init == false, false))
  358. {
  359. if (__gthread_active_p())
  360. {
  361. // On some platforms, __gthread_once_t is an aggregate.
  362. static __gthread_once_t __once = __GTHREAD_ONCE_INIT;
  363. __gthread_once(&__once, _S_initialize);
  364. }
  365. // Double check initialization. May be necessary on some
  366. // systems for proper construction when not compiling with
  367. // thread flags.
  368. _S_get_pool()._M_initialize_once();
  369. __init = true;
  370. }
  371. }
  372. };
  373. #endif
  374. /// Policy for shared __pool objects.
  375. template<template <bool> class _PoolTp, bool _Thread>
  376. struct __common_pool_policy : public __common_pool_base<_PoolTp, _Thread>
  377. {
  378. template<typename _Tp1, template <bool> class _PoolTp1 = _PoolTp,
  379. bool _Thread1 = _Thread>
  380. struct _M_rebind
  381. { typedef __common_pool_policy<_PoolTp1, _Thread1> other; };
  382. using __common_pool_base<_PoolTp, _Thread>::_S_get_pool;
  383. using __common_pool_base<_PoolTp, _Thread>::_S_initialize_once;
  384. };
  385. template<typename _Tp, template <bool> class _PoolTp, bool _Thread>
  386. struct __per_type_pool
  387. {
  388. typedef _Tp value_type;
  389. typedef _PoolTp<_Thread> pool_type;
  390. static pool_type&
  391. _S_get_pool()
  392. {
  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 size_t size_type;
  474. typedef 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_t(-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 size_t size_type;
  534. typedef 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. 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. 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_t __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_t __which = __pool._M_get_binmap(__bytes);
  594. const size_t __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_t __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. template<typename _Tp, typename _Poolp>
  646. inline bool
  647. operator!=(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)
  648. { return false; }
  649. #undef __thread_default
  650. _GLIBCXX_END_NAMESPACE_VERSION
  651. } // namespace
  652. #endif