libstdc++: mt_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#ifndef _MT_ALLOCATOR_H

30#define _MT_ALLOCATOR_H 1

31

33

34#include <new>

39#if __cplusplus >= 201103L

41#endif

42

43namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)

44{

45_GLIBCXX_BEGIN_NAMESPACE_VERSION

46

47

48 typedef void (*__destroy_handler)(void*);

49

50

52 {

53

54

55 typedef unsigned short int _Binmap_type;

56 typedef std::size_t size_t;

57

58

59

60 struct _Tune

61 {

62

63 enum { _S_align = 8 };

64 enum { _S_max_bytes = 128 };

65 enum { _S_min_bin = 8 };

66 enum { _S_chunk_size = 4096 - 4 * sizeof(void*) };

67 enum { _S_max_threads = 4096 };

68 enum { _S_freelist_headroom = 10 };

69

70

71

72

73 size_t _M_align;

74

75

76

77

78

79

80 size_t _M_max_bytes;

81

82

83

84

85 size_t _M_min_bin;

86

87

88

89

90

91

92

93 size_t _M_chunk_size;

94

95

96

97

98

99

100

101 size_t _M_max_threads;

102

103

104

105

106

107

108

109 size_t _M_freelist_headroom;

110

111

112 bool _M_force_new;

113

114 explicit

115 _Tune()

116 : _M_align(_S_align), _M_max_bytes(_S_max_bytes), _M_min_bin(_S_min_bin),

117 _M_chunk_size(_S_chunk_size), _M_max_threads(_S_max_threads),

118 _M_freelist_headroom(_S_freelist_headroom),

119 _M_force_new(std::getenv("GLIBCXX_FORCE_NEW") ? true : false)

120 { }

121

122 explicit

123 _Tune(size_t __align, size_t __maxb, size_t __minbin, size_t __chunk,

124 size_t __maxthreads, size_t __headroom, bool __force)

125 : _M_align(__align), _M_max_bytes(__maxb), _M_min_bin(__minbin),

126 _M_chunk_size(__chunk), _M_max_threads(__maxthreads),

127 _M_freelist_headroom(__headroom), _M_force_new(__force)

128 { }

129 };

130

131 struct _Block_address

132 {

133 void* _M_initial;

134 _Block_address* _M_next;

135 };

136

137 const _Tune&

138 _M_get_options() const

139 { return _M_options; }

140

141 void

142 _M_set_options(_Tune __t)

143 {

144 if (!_M_init)

145 _M_options = __t;

146 }

147

148 bool

149 _M_check_threshold(size_t __bytes)

150 { return __bytes > _M_options._M_max_bytes || _M_options._M_force_new; }

151

152 size_t

153 _M_get_binmap(size_t __bytes)

154 { return _M_binmap[__bytes]; }

155

156 size_t

157 _M_get_align()

158 { return _M_options._M_align; }

159

160 explicit

162 : _M_options(_Tune()), _M_binmap(0), _M_init(false) { }

163

164 explicit

166 : _M_options(__options), _M_binmap(0), _M_init(false) { }

167

168 private:

169 explicit

171

174

175 protected:

176

177 _Tune _M_options;

178

179 _Binmap_type* _M_binmap;

180

181

182

183

184 bool _M_init;

185 };

186

187

188

189

190

191

192 template<bool _Thread>

194

195

196 template<>

198 {

199 public:

200 union _Block_record

201 {

202

203 _Block_record* _M_next;

204 };

205

206 struct _Bin_record

207 {

208

209 _Block_record** _M_first;

210

211

212 _Block_address* _M_address;

213 };

214

215 void

216 _M_initialize_once()

217 {

218 if (__builtin_expect(_M_init == false, false))

219 _M_initialize();

220 }

221

222 void

223 _M_destroy() throw();

224

225 char*

226 _M_reserve_block(size_t __bytes, const size_t __thread_id);

227

228 void

229 _M_reclaim_block(char* __p, size_t __bytes) throw ();

230

231 size_t

232 _M_get_thread_id() { return 0; }

233

234 const _Bin_record&

235 _M_get_bin(size_t __which)

236 { return _M_bin[__which]; }

237

238 void

239 _M_adjust_freelist(const _Bin_record&, _Block_record*, size_t)

240 { }

241

243 : _M_bin(0), _M_bin_size(1) { }

244

245 explicit __pool(const __pool_base::_Tune& __tune)

246 : __pool_base(__tune), _M_bin(0), _M_bin_size(1) { }

247

248 private:

249

250

251

252 _Bin_record* _M_bin;

253

254

255 size_t _M_bin_size;

256

257 void

258 _M_initialize();

259 };

260

261#ifdef __GTHREADS

262

263 template<>

265 {

266 public:

267

268

269

270

271

272

273

274

275

276 struct _Thread_record

277 {

278

279 _Thread_record* _M_next;

280

281

282 size_t _M_id;

283 };

284

285 union _Block_record

286 {

287

288 _Block_record* _M_next;

289

290

291 size_t _M_thread_id;

292 };

293

294 struct _Bin_record

295 {

296

297

298

299 _Block_record** _M_first;

300

301

302 _Block_address* _M_address;

303

304

305

306

307

308

309

310

311

312

313 size_t* _M_free;

314 size_t* _M_used;

315

316

317

318

319 __gthread_mutex_t* _M_mutex;

320 };

321

322

323 void

324 _M_initialize(__destroy_handler);

325

326 void

327 _M_initialize_once()

328 {

329 if (__builtin_expect(_M_init == false, false))

330 _M_initialize();

331 }

332

333 void

334 _M_destroy() throw();

335

336 char*

337 _M_reserve_block(size_t __bytes, const size_t __thread_id);

338

339 void

340 _M_reclaim_block(char* __p, size_t __bytes) throw ();

341

342 const _Bin_record&

343 _M_get_bin(size_t __which)

344 { return _M_bin[__which]; }

345

346 void

347 _M_adjust_freelist(const _Bin_record& __bin, _Block_record* __block,

348 size_t __thread_id)

349 {

350 if (__gthread_active_p())

351 {

352 __block->_M_thread_id = __thread_id;

353 --__bin._M_free[__thread_id];

354 ++__bin._M_used[__thread_id];

355 }

356 }

357

358

359 void

360 _M_destroy_thread_key(void*) throw ();

361

362 size_t

363 _M_get_thread_id();

364

366 : _M_bin(0), _M_bin_size(1), _M_thread_freelist(0)

367 { }

368

369 explicit __pool(const __pool_base::_Tune& __tune)

370 : __pool_base(__tune), _M_bin(0), _M_bin_size(1),

371 _M_thread_freelist(0)

372 { }

373

374 private:

375

376

377

378 _Bin_record* _M_bin;

379

380

381 size_t _M_bin_size;

382

383 _Thread_record* _M_thread_freelist;

384 void* _M_thread_freelist_initial;

385

386 void

387 _M_initialize();

388 };

389#endif

390

391 template<template <bool> class _PoolTp, bool _Thread>

392 struct __common_pool

393 {

394 typedef _PoolTp<_Thread> pool_type;

395

396 static pool_type&

397 _S_get_pool()

398 {

399 static pool_type _S_pool;

400 return _S_pool;

401 }

402 };

403

404 template<template <bool> class _PoolTp, bool _Thread>

405 struct __common_pool_base;

406

407 template<template <bool> class _PoolTp>

408 struct __common_pool_base<_PoolTp, false>

409 : public __common_pool<_PoolTp, false>

410 {

411 using __common_pool<_PoolTp, false>::_S_get_pool;

412

413 static void

414 _S_initialize_once()

415 {

416 static bool __init;

417 if (__builtin_expect(__init == false, false))

418 {

419 _S_get_pool()._M_initialize_once();

420 __init = true;

421 }

422 }

423 };

424

425#ifdef __GTHREADS

426 template<template <bool> class _PoolTp>

427 struct __common_pool_base<_PoolTp, true>

428 : public __common_pool<_PoolTp, true>

429 {

430 using __common_pool<_PoolTp, true>::_S_get_pool;

431

432 static void

433 _S_initialize()

434 { _S_get_pool()._M_initialize_once(); }

435

436 static void

437 _S_initialize_once()

438 {

439 static bool __init;

440 if (__builtin_expect(__init == false, false))

441 {

442 if (__gthread_active_p())

443 {

444

445 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;

446 __gthread_once(&__once, _S_initialize);

447 }

448

449

450

451

452 _S_get_pool()._M_initialize_once();

453 __init = true;

454 }

455 }

456 };

457#endif

458

459

460 template<template <bool> class _PoolTp, bool _Thread>

462 {

463 template<typename _Tp1, template <bool> class _PoolTp1 = _PoolTp,

464 bool _Thread1 = _Thread>

465 struct _M_rebind

467

468 using __common_pool_base<_PoolTp, _Thread>::_S_get_pool;

469 using __common_pool_base<_PoolTp, _Thread>::_S_initialize_once;

470 };

471

472

473 template<typename _Tp, template <bool> class _PoolTp, bool _Thread>

474 struct __per_type_pool

475 {

476 typedef _Tp value_type;

477 typedef _PoolTp<_Thread> pool_type;

478

479 static pool_type&

480 _S_get_pool()

481 {

482 using std::size_t;

483

484 typedef typename pool_type::_Block_record _Block_record;

485 const static size_t __a = (__alignof__(_Tp) >= sizeof(_Block_record)

486 ? __alignof__(_Tp) : sizeof(_Block_record));

487

488 typedef typename __pool_base::_Tune _Tune;

489 static _Tune _S_tune(__a, sizeof(_Tp) * 64,

490 sizeof(_Tp) * 2 >= __a ? sizeof(_Tp) * 2 : __a,

491 sizeof(_Tp) * size_t(_Tune::_S_chunk_size),

492 _Tune::_S_max_threads,

493 _Tune::_S_freelist_headroom,

494 std::getenv("GLIBCXX_FORCE_NEW") ? true : false);

495 static pool_type _S_pool(_S_tune);

496 return _S_pool;

497 }

498 };

499

500 template<typename _Tp, template <bool> class _PoolTp, bool _Thread>

501 struct __per_type_pool_base;

502

503 template<typename _Tp, template <bool> class _PoolTp>

504 struct __per_type_pool_base<_Tp, _PoolTp, false>

505 : public __per_type_pool<_Tp, _PoolTp, false>

506 {

507 using __per_type_pool<_Tp, _PoolTp, false>::_S_get_pool;

508

509 static void

510 _S_initialize_once()

511 {

512 static bool __init;

513 if (__builtin_expect(__init == false, false))

514 {

515 _S_get_pool()._M_initialize_once();

516 __init = true;

517 }

518 }

519 };

520

521 #ifdef __GTHREADS

522 template<typename _Tp, template <bool> class _PoolTp>

523 struct __per_type_pool_base<_Tp, _PoolTp, true>

524 : public __per_type_pool<_Tp, _PoolTp, true>

525 {

526 using __per_type_pool<_Tp, _PoolTp, true>::_S_get_pool;

527

528 static void

529 _S_initialize()

530 { _S_get_pool()._M_initialize_once(); }

531

532 static void

533 _S_initialize_once()

534 {

535 static bool __init;

536 if (__builtin_expect(__init == false, false))

537 {

538 if (__gthread_active_p())

539 {

540

541 static __gthread_once_t __once = __GTHREAD_ONCE_INIT;

542 __gthread_once(&__once, _S_initialize);

543 }

544

545

546

547

548 _S_get_pool()._M_initialize_once();

549 __init = true;

550 }

551 }

552 };

553#endif

554

555

556 template<typename _Tp, template <bool> class _PoolTp, bool _Thread>

558 : public __per_type_pool_base<_Tp, _PoolTp, _Thread>

559 {

560 template<typename _Tp1, template <bool> class _PoolTp1 = _PoolTp,

561 bool _Thread1 = _Thread>

562 struct _M_rebind

564

565 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_get_pool;

566 using __per_type_pool_base<_Tp, _PoolTp, _Thread>::_S_initialize_once;

567 };

568

569

570

571 template<typename _Tp>

573 {

574 public:

575 typedef std::size_t size_type;

576 typedef std::ptrdiff_t difference_type;

577 typedef _Tp* pointer;

578 typedef const _Tp* const_pointer;

579 typedef _Tp& reference;

580 typedef const _Tp& const_reference;

581 typedef _Tp value_type;

582

583#if __cplusplus >= 201103L

584

585

586 typedef std::true_type propagate_on_container_move_assignment;

587#endif

588

589 pointer

590 address(reference __x) const _GLIBCXX_NOEXCEPT

592

593 const_pointer

594 address(const_reference __x) const _GLIBCXX_NOEXCEPT

596

597 size_type

598 max_size() const _GLIBCXX_USE_NOEXCEPT

599 { return size_type(-1) / sizeof(_Tp); }

600

601#if __cplusplus >= 201103L

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

603 void

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

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

606

607 template<typename _Up>

608 void

609 destroy(_Up* __p) { __p->~_Up(); }

610#else

611

612

613 void

614 construct(pointer __p, const _Tp& __val)

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

616

617 void

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

619#endif

620 };

621

622#ifdef __GTHREADS

623#define __thread_default true

624#else

625#define __thread_default false

626#endif

627

628

629

630

631

632

633

634

635

636

637

638

639 template<typename _Tp,

642 {

643 public:

644 typedef std::size_t size_type;

645 typedef std::ptrdiff_t difference_type;

646 typedef _Tp* pointer;

647 typedef const _Tp* const_pointer;

648 typedef _Tp& reference;

649 typedef const _Tp& const_reference;

650 typedef _Tp value_type;

651 typedef _Poolp __policy_type;

652 typedef typename _Poolp::pool_type __pool_type;

653

654 template<typename _Tp1, typename _Poolp1 = _Poolp>

655 struct rebind

656 {

657 typedef typename _Poolp1::template _M_rebind<_Tp1>::other pol_type;

659 };

660

661 __mt_alloc() _GLIBCXX_USE_NOEXCEPT { }

662

664

665 template<typename _Tp1, typename _Poolp1>

667

669

670 _GLIBCXX_NODISCARD pointer

671 allocate(size_type __n, const void* = 0);

672

673 void

674 deallocate(pointer __p, size_type __n);

675

676 const __pool_base::_Tune

677 _M_get_options()

678 {

679

680 return __policy_type::_S_get_pool()._M_get_options();

681 }

682

683 void

684 _M_set_options(__pool_base::_Tune __t)

685 { __policy_type::_S_get_pool()._M_set_options(__t); }

686 };

687

688 template<typename _Tp, typename _Poolp>

689 _GLIBCXX_NODISCARD typename __mt_alloc<_Tp, _Poolp>::pointer

691 allocate(size_type __n, const void*)

692 {

693 if (__n > this->max_size())

694 std::__throw_bad_alloc();

695

696#if __cpp_aligned_new && __cplusplus >= 201103L

697

698 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)

699 {

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

701 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al));

702 }

703#endif

704

705 __policy_type::_S_initialize_once();

706

707

708

709 __pool_type& __pool = __policy_type::_S_get_pool();

710 const size_type __bytes = __n * sizeof(_Tp);

711 if (__pool._M_check_threshold(__bytes))

712 {

713 void* __ret = ::operator new(__bytes);

714 return static_cast<_Tp*>(__ret);

715 }

716

717

718 const size_type __which = __pool._M_get_binmap(__bytes);

719 const size_type __thread_id = __pool._M_get_thread_id();

720

721

722

723 char* __c;

724 typedef typename __pool_type::_Bin_record _Bin_record;

725 const _Bin_record& __bin = __pool._M_get_bin(__which);

726 if (__bin._M_first[__thread_id])

727 {

728

729 typedef typename __pool_type::_Block_record _Block_record;

730 _Block_record* __block = __bin._M_first[__thread_id];

731 __bin._M_first[__thread_id] = __block->_M_next;

732

733 __pool._M_adjust_freelist(__bin, __block, __thread_id);

734 __c = reinterpret_cast<char*>(__block) + __pool._M_get_align();

735 }

736 else

737 {

738

739 __c = __pool._M_reserve_block(__bytes, __thread_id);

740 }

741 return static_cast<_Tp*>(static_cast<void*>(__c));

742 }

743

744 template<typename _Tp, typename _Poolp>

745 void

746 __mt_alloc<_Tp, _Poolp>::

747 deallocate(pointer __p, size_type __n)

748 {

749 if (__builtin_expect(__p != 0, true))

750 {

751#if __cpp_aligned_new && __cplusplus >= 201103L

752

753 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)

754 {

755 ::operator delete(__p, std::align_val_t(alignof(_Tp)));

756 return;

757 }

758#endif

759

760

761

762 __pool_type& __pool = __policy_type::_S_get_pool();

763 const size_type __bytes = __n * sizeof(_Tp);

764 if (__pool._M_check_threshold(__bytes))

765 ::operator delete(__p);

766 else

767 __pool._M_reclaim_block(reinterpret_cast<char*>(__p), __bytes);

768 }

769 }

770

771 template<typename _Tp, typename _Poolp>

772 inline bool

773 operator==(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)

774 { return true; }

775

776#if __cpp_impl_three_way_comparison < 201907L

777 template<typename _Tp, typename _Poolp>

778 inline bool

779 operator!=(const __mt_alloc<_Tp, _Poolp>&, const __mt_alloc<_Tp, _Poolp>&)

780 { return false; }

781#endif

782

783#undef __thread_default

784

785_GLIBCXX_END_NAMESPACE_VERSION

786}

787

788#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.

GNU extensions for public use.

Base class for pool object.

Data describing the underlying memory pool, parameterized on threading support.

Policy for shared __pool objects.

Policy for individual __pool objects.

Base class for _Tp dependent member functions.

This is a fixed size (power of 2) allocator which - when compiled with thread support - will maintain...