// license:GPL-2.0+ // copyright-holders:Couriersud /* * palloc.h * */ #ifndef PALLOC_H_ #define PALLOC_H_ #include "pconfig.h" #include "pstring.h" #include "ptypes.h" #include #include #include #include #include #include #if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER) #include #endif namespace plib { //============================================================ // Standard arena_deleter //============================================================ template struct arena_deleter { //using arena_storage_type = P *; using arena_storage_type = typename std::conditional::type; template typename std::enable_if::type getref(X *x) { return *x;} template typename std::enable_if::type::is_stateless, X&>::type getref(X &x, Y y = nullptr) { unused_var(y); return x; } constexpr arena_deleter(arena_storage_type a = arena_storage_type()) noexcept : m_a(a) { } template::value>::type> arena_deleter(const arena_deleter &rhs) noexcept : m_a(rhs.m_a) { } void operator()(T *p) //const { /* call destructor */ p->~T(); getref(m_a).deallocate(p); } //private: arena_storage_type m_a; }; //============================================================ // owned_ptr: smart pointer with ownership information //============================================================ template class owned_ptr { public: using pointer = SC *; using element_type = SC; using deleter_type = D; owned_ptr() : m_ptr(nullptr), m_deleter(), m_is_owned(true) { } template friend class owned_ptr; owned_ptr(pointer p, bool owned) noexcept : m_ptr(p), m_deleter(), m_is_owned(owned) { } owned_ptr(pointer p, bool owned, D deleter) noexcept : m_ptr(p), m_deleter(deleter), m_is_owned(owned) { } owned_ptr(const owned_ptr &r) = delete; owned_ptr & operator =(owned_ptr &r) = delete; template owned_ptr & operator =(owned_ptr &&r) { if (m_is_owned && (m_ptr != nullptr)) //delete m_ptr; m_deleter(m_ptr); m_is_owned = r.m_is_owned; m_ptr = r.m_ptr; m_deleter = r.m_deleter; r.m_is_owned = false; r.m_ptr = nullptr; return *this; } owned_ptr(owned_ptr &&r) noexcept : m_ptr(r.m_ptr) , m_deleter(r.m_deleter) , m_is_owned(r.m_is_owned) { r.m_is_owned = false; r.m_ptr = nullptr; } owned_ptr &operator=(owned_ptr &&r) noexcept { if (m_is_owned && (m_ptr != nullptr)) //delete m_ptr; m_deleter(m_ptr); m_is_owned = r.m_is_owned; m_ptr = r.m_ptr; m_deleter = std::move(r.m_deleter); r.m_is_owned = false; r.m_ptr = nullptr; return *this; } template owned_ptr(owned_ptr &&r) noexcept : m_ptr(static_cast(r.get())) , m_deleter(r.m_deleter) , m_is_owned(r.is_owned()) { r.release(); } ~owned_ptr() noexcept { if (m_is_owned && (m_ptr != nullptr)) { //delete m_ptr; m_deleter(m_ptr); } m_is_owned = false; m_ptr = nullptr; } /** * \brief Return @c true if the stored pointer is not null. */ explicit operator bool() const noexcept { return m_ptr != nullptr; } pointer release() { pointer tmp = m_ptr; m_is_owned = false; m_ptr = nullptr; return tmp; } bool is_owned() const { return m_is_owned; } pointer operator ->() const noexcept { return m_ptr; } typename std::add_lvalue_reference::type operator *() const noexcept { return *m_ptr; } pointer get() const noexcept { return m_ptr; } deleter_type& get_deleter() noexcept { return m_deleter; } const deleter_type& get_deleter() const noexcept { return m_deleter; } private: pointer m_ptr; D m_deleter; bool m_is_owned; }; //============================================================ // Arena allocator for use with containers //============================================================ template class arena_allocator { public: using value_type = T; static constexpr const std::size_t align_size = ALIGN; using arena_type = ARENA; static_assert(align_size >= alignof(T) && (align_size % alignof(T)) == 0, "ALIGN must be greater than alignof(T) and a multiple"); arena_allocator() noexcept : m_a(arena_type::instance()) { } ~arena_allocator() noexcept = default; arena_allocator(const arena_allocator &rhs) noexcept = default; arena_allocator& operator=(const arena_allocator&) noexcept = delete; arena_allocator(arena_allocator&&) noexcept = default; arena_allocator& operator=(arena_allocator&&) = delete; arena_allocator(arena_type & a) noexcept : m_a(a) { } template arena_allocator(const arena_allocator& rhs) noexcept : m_a(rhs.m_a) { } template struct rebind { using other = arena_allocator; }; T* allocate(std::size_t n) { return reinterpret_cast(m_a.allocate(ALIGN, sizeof(T) * n)); } void deallocate(T* p, std::size_t n) noexcept { unused_var(n); m_a.deallocate(p); } template friend bool operator==(const arena_allocator& lhs, const arena_allocator& rhs) noexcept; template friend class arena_allocator; private: arena_type &m_a; }; template inline bool operator==(const arena_allocator& lhs, const arena_allocator& rhs) noexcept { return A1 == A2 && rhs.m_a == lhs.m_a; } template inline bool operator!=(const arena_allocator& lhs, const arena_allocator& rhs) noexcept { return !(lhs == rhs); } //============================================================ // Memory allocation //============================================================ struct aligned_arena { static constexpr const bool is_stateless = true; template using allocator_type = arena_allocator; template using owned_pool_ptr = plib::owned_ptr>; static inline aligned_arena &instance() { static aligned_arena s_arena; return s_arena; } static inline void *allocate( size_t alignment, size_t size ) { #if (USE_ALIGNED_ALLOCATION) #if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER) return _aligned_malloc(size, alignment); #elif defined(__APPLE__) void* p; if (::posix_memalign(&p, alignment, size) != 0) { p = nullptr; } return p; #else return aligned_alloc(alignment, size); #endif #else unused_var(alignment); return ::operator new(size); #endif } static inline void deallocate( void *ptr ) { #if (USE_ALIGNED_ALLOCATION) // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) free(ptr); #else ::operator delete(ptr); #endif } #if 0 template owned_pool_ptr make_poolptr(Args&&... args) { auto *mem = allocate(alignof(T), sizeof(T)); return owned_pool_ptr(new (mem) T(std::forward(args)...), true, arena_deleter(*this)); } #endif template owned_pool_ptr make_poolptr(Args&&... args) { auto *mem = allocate(alignof(T), sizeof(T)); try { auto *mema = new (mem) T(std::forward(args)...); return owned_pool_ptr(mema, true, arena_deleter(*this)); } catch (std::exception &e) { deallocate(mem); throw; } } }; template /*inline */ C14CONSTEXPR T *assume_aligned_ptr(T *p) noexcept { static_assert(ALIGN >= alignof(T), "Alignment must be greater or equal to alignof(T)"); static_assert(is_pow2(ALIGN), "Alignment must be a power of 2"); //auto t = reinterpret_cast(p); //if (t & (ALIGN-1)) // printf("alignment error!"); #if (USE_ALIGNED_HINTS) return reinterpret_cast(__builtin_assume_aligned(p, ALIGN)); #else return p; #endif } template constexpr const T *assume_aligned_ptr(const T *p) noexcept { static_assert(ALIGN >= alignof(T), "Alignment must be greater or equal to alignof(T)"); static_assert(is_pow2(ALIGN), "Alignment must be a power of 2"); #if (USE_ALIGNED_HINTS) return reinterpret_cast(__builtin_assume_aligned(p, ALIGN)); #else return p; #endif } // FIXME: remove template inline T *pnew(Args&&... args) { auto *p = aligned_arena::allocate(alignof(T), sizeof(T)); return new(p) T(std::forward(args)...); } template inline void pdelete(T *ptr) { ptr->~T(); aligned_arena::deallocate(ptr); } template using unique_ptr = std::unique_ptr>; template plib::unique_ptr make_unique(Args&&... args) { return plib::unique_ptr(pnew(std::forward(args)...)); } #if 0 template static owned_ptr make_owned(Args&&... args) { return owned_ptr(pnew(std::forward(args)...), true); } #endif template using aligned_allocator = aligned_arena::allocator_type; //============================================================ // traits to determine alignment size and stride size // from types supporting alignment //============================================================ PDEFINE_HAS_MEMBER(has_align, align_size); template struct align_traits { static constexpr const std::size_t align_size = alignof(std::max_align_t); static constexpr const std::size_t value_size = sizeof(typename T::value_type); static constexpr const std::size_t stride_size = lcm(align_size, value_size) / value_size; }; template struct align_traits::value, void>::type> { static constexpr const std::size_t align_size = T::align_size; static constexpr const std::size_t value_size = sizeof(typename T::value_type); static constexpr const std::size_t stride_size = lcm(align_size, value_size) / value_size; }; //============================================================ // Aligned vector //============================================================ // FIXME: needs a separate file template class aligned_vector : public std::vector> { public: using base = std::vector>; using reference = typename base::reference; using const_reference = typename base::const_reference; using pointer = typename base::pointer; using const_pointer = typename base::const_pointer; using size_type = typename base::size_type; using base::base; C14CONSTEXPR reference operator[](size_type i) noexcept { return assume_aligned_ptr(&(base::operator[](0)))[i]; } constexpr const_reference operator[](size_type i) const noexcept { return assume_aligned_ptr(&(base::operator[](0)))[i]; } pointer data() noexcept { return assume_aligned_ptr(base::data()); } const_pointer data() const noexcept { return assume_aligned_ptr(base::data()); } }; } // namespace plib #endif /* PALLOC_H_ */ 10'>210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857