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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 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
|
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef MEMORY_UNIQUE_PTR_INCLUDED
#define MEMORY_UNIQUE_PTR_INCLUDED
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h" // my_malloc
#include "sql/memory/aligned_atomic.h" // memory::cache_line_size
#include "sql/memory/ref_ptr.h" // memory::Ref_ptr
namespace memory {
namespace traits {
/**
Tests for the existence of `allocate(size_t)` in order to disambiguate if `T`
is an allocator class.
*/
template <class T>
auto test_for_allocate(int T::*)
-> decltype(std::declval<T>().allocate(std::declval<size_t>()),
std::true_type{});
template <class>
std::false_type test_for_allocate(...);
} // namespace traits
/**
Struct that allows for checking if `T` fulfills the Allocator named
requirements.
*/
template <class T>
struct is_allocator : decltype(traits::test_for_allocate<T>(nullptr)) {};
/**
Allocator class for instrumenting allocated memory with Performance Schema
keys.
*/
template <typename T>
class PFS_allocator {
using value_type = T;
using size_type = size_t;
/**
Constructor for the class that takes the PFS key to be used.
@param key The PFS key to be used.
*/
PFS_allocator(PSI_memory_key key);
/**
Copy constructor.
@param rhs The object to copy from.
*/
template <typename U>
PFS_allocator(PFS_allocator<U> const &rhs) noexcept;
/**
Move constructor.
@param rhs The object to move from.
*/
template <typename U>
PFS_allocator(PFS_allocator<U> &&rhs) noexcept;
/**
Retrieves the PFS for `this` allocator object.
@return The PFS key.
*/
PSI_memory_key key() const;
/**
Allocate `n` bytes and return a pointer to the beginning of the allocated
memory.
@param n The size of the memory to allocate.
@return A pointer to the beginning of the allocated memory.
*/
T *allocate(std::size_t n);
/**
Deallocates the `n` bytes stored in the memory pointer `p` is pointing to.
@param p The pointer to the beginning of the memory to deallocate.
@param n The size of the memory to deallocate.
*/
void deallocate(T *p, std::size_t n) noexcept;
/**
In-place constructs an object of class `U` in the memory pointed by `p`.
@param p The pointer to the beginning of the memory to construct the object
in.
@param args The parameters to be used with the `U` constructor.
*/
template <class U, class... Args>
void construct(U *p, Args &&...args);
/**
In-place invokes the destructor for class `T` on object pointed by `p`.
@param p The object pointer to invoke the destructor on.
*/
void destroy(T *p);
/**
The maximum size available to allocate.
@return The maximum size available to allocate.
*/
size_type max_size() const;
private:
/** The PFS key to be used to allocate memory */
PSI_memory_key m_key;
};
/**
Smart pointer to hold a unique pointer to a heap allocated memory of type `T`,
constructed using a specific allocator.
Template parameters are as follows:
- `T` is the type of the pointer to allocate. It may be an array type.
- `A` the allocator to use. If none is passed, `std::nullptr_t` is passed and
regular `new` and `delete` are used to construct the memory.
*/
template <typename T, typename A = std::nullptr_t>
class Unique_ptr {
public:
using type = typename std::remove_extent<T>::type;
using pointer = type *;
using reference = type &;
/**
Default class constructor, only to be used with no specific allocator.
*/
template <
typename D = T, typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value> * = nullptr>
Unique_ptr();
/**
Class constructor, to be used with specific allocators, passing the
allocator object to be used.
@param alloc The allocator instance to be used.
*/
template <
typename D = T, typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value> * = nullptr>
Unique_ptr(A &alloc);
/**
Class constructor, to be used with specific allocators and when `T` is an
array type, passing the allocator object to be used and the size of the
array.
@param alloc The allocator instance to be used.
@param size The size of the array to allocate.
*/
template <typename D = T, typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> * = nullptr>
Unique_ptr(A &alloc, size_t size);
/**
Class constructor, to be used with no specific allocators and when `T` is an
array type, passing the allocator object to be used and the size of the
array.
@param size The size of the array to allocate.
*/
template <typename D = T, typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> * = nullptr>
Unique_ptr(size_t size);
/**
Class constructor, to be used with specific allocators and when `T` is not
an array type, passing the allocator object to be used and the parameters to
be used with `T` object constructor.
@param alloc The allocator instance to be used.
@param args The parameters to be used with `T` object constructor.
*/
template <typename... Args, typename D = T, typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> * = nullptr>
Unique_ptr(A &alloc, Args &&...args);
/**
Class constructor, to be used with no specific allocators and when `T` is
not an array type, passing the parameters to be used with `T` object
constructor.
@param args The parameters to be used with `T` object constructor.
*/
template <typename... Args, typename D = T, typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> * = nullptr>
Unique_ptr(Args &&...args);
// Deleted copy constructor
Unique_ptr(Unique_ptr<T, A> const &rhs) = delete;
/**
Move constructor.
@param rhs The object to move data from.
*/
Unique_ptr(Unique_ptr<T, A> &&rhs);
/**
Destructor for the class.
*/
virtual ~Unique_ptr();
// Deleted copy operator
Unique_ptr<T, A> &operator=(Unique_ptr<T, A> const &rhs) = delete;
/**
Move operator.
@param rhs The object to move data from.
*/
Unique_ptr<T, A> &operator=(Unique_ptr<T, A> &&rhs);
/**
Arrow operator to access the underlying object of type `T`.
@return A pointer to the underlying object of type `T`.
*/
template <typename D = T,
std::enable_if_t<!std::is_array<D>::value> * = nullptr>
pointer operator->() const;
/**
Star operator to access the underlying object of type `T`.
@return A reference to the underlying object of type `T`.
*/
reference operator*() const;
/**
Subscript operator, to access an array element when `T` is of array type.
@param index The index of the element to retrieve the value for.
@return A reference to the value stored at index.
*/
template <typename D = T,
std::enable_if_t<std::is_array<D>::value> * = nullptr>
reference operator[](size_t index) const;
/**
Casting operator to bool.
@return `true` if the underlying pointer is instantiated, `false` otherwise.
*/
operator bool() const;
/**
Releases the ownership of the underlying allocated memory and returns a
pointer to the beginning of that memory. This smart pointer will no longer
manage the underlying memory.
@return the pointer to the allocated and no longer managed memory.
*/
template <
typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value> * = nullptr>
pointer release();
/**
Releases the ownership of the underlying allocated memory and returns a
pointer to the beginning of that memory. This smart pointer will no longer
manage the underlying memory.
@return the pointer to the allocated and no longer managed memory.
*/
template <
typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value> * = nullptr>
pointer release();
/**
Returns a pointer to the underlying allocated memory.
@return A pointer to the underlying allocated memory
*/
pointer get() const;
/**
The size of the memory allocated, in bytes.
@return The size of the memory allocated, in bytes
*/
size_t size() const;
/**
Will resize the allocated memory to `new_size`. If the configure allocator
supports this operation, the allocator is used. If not, a new memory chunk
is allocated and the memory is copied.
@param new_size The new desired size for the memory.
@return The reference to `this` object, for chaining purposed.
*/
template <
typename D = T, typename B = A,
std::enable_if_t<std::is_array<D>::value &&
std::is_same<B, std::nullptr_t>::value> * = nullptr>
Unique_ptr<T, A> &reserve(size_t new_size);
/**
Will resize the allocated memory to `new_size`. If the configure allocator
supports this operation, the allocator is used. If not, a new memory chunk
is allocated and the memory is copied.
@param new_size The new desired size for the memory.
@return The reference to `this` object, for chaining purposed.
*/
template <
typename D = T, typename B = A,
std::enable_if_t<std::is_array<D>::value &&
!std::is_same<B, std::nullptr_t>::value> * = nullptr>
Unique_ptr<T, A> &reserve(size_t new_size);
/**
Returns the used allocator instance, if any.
@return The reference to the allocator object.
*/
A &allocator() const;
private:
/** The pointer to the underlying allocated memory */
alignas(std::max_align_t) pointer m_underlying{nullptr};
/** The allocator to be used to allocate memory */
memory::Ref_ptr<A> m_allocator;
/** The size of the allocated memory */
size_t m_size{0};
/**
Clears the underlying pointer and size.
*/
void reset();
/**
Deallocates the underlying allocated memory.
*/
template <typename D = T, typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> * = nullptr>
void destroy();
/**
Deallocates the underlying allocated memory.
*/
template <typename D = T, typename B = A,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> * = nullptr>
void destroy();
/**
Deallocates the underlying allocated memory.
*/
template <typename D = T, typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> * = nullptr>
void destroy();
/**
Deallocates the underlying allocated memory.
*/
template <typename D = T, typename B = A,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> * = nullptr>
void destroy();
/**
Clones the underlying memory and returns a pointer to the clone memory.
@return A pointer to the cloned underlying memory.
*/
template <typename D = T,
std::enable_if_t<std::is_array<D>::value> * = nullptr>
pointer clone() const;
/**
Clones the underlying memory and returns a pointer to the clone memory.
@return A pointer to the cloned underlying memory.
*/
template <typename D = T,
std::enable_if_t<!std::is_array<D>::value> * = nullptr>
pointer clone() const;
};
/**
In-place constructs a new unique pointer with no specific allocator and with
array type `T`.
@param size The size of the array to allocate.
@return A new instance of unique pointer.
*/
template <typename T, std::enable_if_t<std::is_array<T>::value> * = nullptr>
Unique_ptr<T, std::nullptr_t> make_unique(size_t size);
/**
In-place constructs a new unique pointer with a specific allocator and with
array type `T`.
@param alloc A reference to the allocator object to use.
@param size The size of the array to allocate.
@return A new instance of unique pointer.
*/
template <typename T, typename A,
std::enable_if_t<std::is_array<T>::value> * = nullptr>
Unique_ptr<T, A> make_unique(A &alloc, size_t size);
/**
In-place constructs a new unique pointer with a specific allocator and with
non-array type `T`.
@param alloc A reference to the allocator object to use.
@param args The parameters to be used in constructing the instance of `T`.
@return A new instance of unique pointer.
*/
template <typename T, typename A, typename... Args,
std::enable_if_t<!std::is_array<T>::value &&
memory::is_allocator<A>::value> * = nullptr>
Unique_ptr<T, A> make_unique(A &alloc, Args &&...args);
/**
In-place constructs a new unique pointer with no specific allocator and with
non-array type `T`.
@param args The parameters to be used in constructing the instance of `T`.
@return A new instance of unique pointer.
*/
template <typename T, typename... Args,
std::enable_if_t<!std::is_array<T>::value> * = nullptr>
Unique_ptr<T, std::nullptr_t> make_unique(Args &&...args);
} // namespace memory
// global scope
template <typename T, typename U>
bool operator==(const memory::PFS_allocator<T> &lhs,
const memory::PFS_allocator<U> &rhs) {
return lhs.key() == rhs.key();
}
template <typename T, typename U>
bool operator!=(const memory::PFS_allocator<T> &lhs,
const memory::PFS_allocator<U> &rhs) {
return lhs.key() != rhs.key();
}
template <typename T1, typename A1, typename T2, typename A2>
bool operator==(memory::Unique_ptr<T1, A1> const &lhs,
memory::Unique_ptr<T2, A2> const &rhs) {
return static_cast<const void *>(lhs.get()) ==
static_cast<const void *>(rhs.get());
}
template <typename T1, typename A1, typename T2, typename A2>
bool operator!=(memory::Unique_ptr<T1, A1> const &lhs,
memory::Unique_ptr<T2, A2> const &rhs) {
return !(lhs == rhs);
}
template <typename T1, typename A1>
bool operator==(memory::Unique_ptr<T1, A1> const &lhs, std::nullptr_t) {
return lhs.get() == nullptr;
}
template <typename T1, typename A1>
bool operator!=(memory::Unique_ptr<T1, A1> const &lhs, std::nullptr_t) {
return !(lhs == nullptr);
}
// global scope
template <typename T>
memory::PFS_allocator<T>::PFS_allocator(PSI_memory_key key) : m_key{key} {}
template <typename T>
template <typename U>
memory::PFS_allocator<T>::PFS_allocator(PFS_allocator<U> const &rhs) noexcept
: m_key{rhs.m_key} {}
template <typename T>
template <typename U>
memory::PFS_allocator<T>::PFS_allocator(PFS_allocator<U> &&rhs) noexcept
: m_key{rhs.m_key} {
rhs.m_key = 0;
}
template <typename T>
PSI_memory_key memory::PFS_allocator<T>::key() const {
return this->m_key;
}
template <typename T>
T *memory::PFS_allocator<T>::allocate(std::size_t n) {
if (n <= std::numeric_limits<std::size_t>::max() / sizeof(T)) {
if (auto p = static_cast<T *>(my_malloc(this->m_key, (n * sizeof(T)),
MYF(MY_WME | ME_FATALERROR))))
return p;
}
throw std::bad_alloc();
}
template <typename T>
void memory::PFS_allocator<T>::deallocate(T *p, std::size_t) noexcept {
my_free(p);
}
template <typename T>
template <class U, class... Args>
void memory::PFS_allocator<T>::construct(U *p, Args &&...args) {
assert(p != nullptr);
try {
::new ((void *)p) U(std::forward<Args>(args)...);
} catch (...) {
assert(false); // Constructor should not throw an exception.
}
}
template <typename T>
void memory::PFS_allocator<T>::destroy(T *p) {
assert(p != nullptr);
try {
p->~T();
} catch (...) {
assert(false); // Destructor should not throw an exception
}
}
template <typename T>
size_t memory::PFS_allocator<T>::max_size() const {
return std::numeric_limits<size_t>::max() / sizeof(T);
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr() : m_underlying{nullptr}, m_size{0} {}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr(A &alloc)
: m_underlying{nullptr}, m_allocator{alloc}, m_size{0} {}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr(A &alloc, size_t size)
: m_underlying{nullptr}, m_allocator{alloc}, m_size{size} {
this->m_underlying = this->m_allocator->allocate(this->m_size);
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr(size_t size)
: m_underlying{new type[size]}, m_size{size} {}
template <typename T, typename A>
template <typename... Args, typename D, typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr(A &alloc, Args &&...args)
: m_underlying{nullptr}, m_allocator{alloc}, m_size{sizeof(T)} {
this->m_underlying = this->m_allocator->allocate(this->m_size);
this->m_allocator->construct(this->m_underlying, std::forward<Args>(args)...);
}
template <typename T, typename A>
template <typename... Args, typename D, typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> *>
memory::Unique_ptr<T, A>::Unique_ptr(Args &&...args)
: m_underlying{new T{std::forward<Args>(args)...}}, m_size{sizeof(T)} {}
template <typename T, typename A>
memory::Unique_ptr<T, A>::Unique_ptr(memory::Unique_ptr<T, A> &&rhs)
: m_underlying{rhs.m_underlying},
m_allocator{rhs.m_allocator},
m_size{rhs.m_size} {
rhs.reset();
}
template <typename T, typename A>
memory::Unique_ptr<T, A>::~Unique_ptr() {
this->destroy();
}
template <typename T, typename A>
typename memory::Unique_ptr<T, A> &memory::Unique_ptr<T, A>::operator=(
memory::Unique_ptr<T, A> &&rhs) {
this->m_underlying = rhs.m_underlying;
this->m_allocator = rhs.m_allocator;
this->m_size = rhs.m_size;
rhs.reset();
return (*this);
}
template <typename T, typename A>
template <typename D, std::enable_if_t<!std::is_array<D>::value> *>
typename memory::Unique_ptr<T, A>::pointer
memory::Unique_ptr<T, A>::operator->() const {
return this->m_underlying;
}
template <typename T, typename A>
typename memory::Unique_ptr<T, A>::reference
memory::Unique_ptr<T, A>::operator*() const {
return (*this->m_underlying);
}
template <typename T, typename A>
template <typename D, std::enable_if_t<std::is_array<D>::value> *>
typename memory::Unique_ptr<T, A>::reference
memory::Unique_ptr<T, A>::operator[](size_t index) const {
return this->m_underlying[index];
}
template <typename T, typename A>
memory::Unique_ptr<T, A>::operator bool() const {
return this->m_underlying != nullptr;
}
template <typename T, typename A>
template <typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value> *>
typename memory::Unique_ptr<T, A>::pointer memory::Unique_ptr<T, A>::release() {
pointer to_return = this->m_underlying;
this->reset();
return to_return;
}
template <typename T, typename A>
template <typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value> *>
typename memory::Unique_ptr<T, A>::pointer memory::Unique_ptr<T, A>::release() {
pointer to_return = this->m_allocator->release(this->m_underlying);
if (to_return != this->m_underlying) {
to_return = this->clone();
this->destroy();
} else {
this->reset();
}
return to_return;
}
template <typename T, typename A>
typename memory::Unique_ptr<T, A>::pointer memory::Unique_ptr<T, A>::get()
const {
return this->m_underlying;
}
template <typename T, typename A>
size_t memory::Unique_ptr<T, A>::size() const {
return this->m_size;
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_array<D>::value &&
std::is_same<B, std::nullptr_t>::value> *>
typename memory::Unique_ptr<T, A> &memory::Unique_ptr<T, A>::reserve(
size_t new_size) {
pointer old_ptr = this->m_underlying;
this->m_underlying = new type[new_size];
if (this->m_size != 0) {
std::copy(old_ptr, old_ptr + std::min(this->m_size, new_size),
this->m_underlying);
}
this->m_size = new_size;
delete[] old_ptr;
return (*this);
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_array<D>::value &&
!std::is_same<B, std::nullptr_t>::value> *>
typename memory::Unique_ptr<T, A> &memory::Unique_ptr<T, A>::reserve(
size_t new_size) {
if (this->m_allocator->can_resize()) {
this->m_underlying =
this->m_allocator->resize(this->m_underlying, this->m_size, new_size);
} else {
pointer old_ptr = this->m_underlying;
this->m_underlying = this->m_allocator->allocate(new_size);
if (this->m_size != 0) {
std::copy(old_ptr, old_ptr + std::min(this->m_size, new_size),
this->m_underlying);
this->m_allocator->deallocate(old_ptr, this->m_size);
}
}
this->m_size = new_size;
return (*this);
}
template <typename T, typename A>
A &memory::Unique_ptr<T, A>::allocator() const {
return *this->m_allocator;
}
template <typename T, typename A>
void memory::Unique_ptr<T, A>::reset() {
this->m_underlying = nullptr;
this->m_size = 0;
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> *>
void memory::Unique_ptr<T, A>::destroy() {
if (this->m_underlying != nullptr) {
delete[] this->m_underlying;
this->reset();
}
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> *>
void memory::Unique_ptr<T, A>::destroy() {
if (this->m_underlying != nullptr) {
delete this->m_underlying;
this->reset();
}
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
std::is_array<D>::value> *>
void memory::Unique_ptr<T, A>::destroy() {
if (this->m_underlying != nullptr) {
this->m_allocator->deallocate(this->m_underlying, this->m_size);
this->reset();
}
}
template <typename T, typename A>
template <typename D, typename B,
std::enable_if_t<!std::is_same<B, std::nullptr_t>::value &&
!std::is_array<D>::value> *>
void memory::Unique_ptr<T, A>::destroy() {
if (this->m_underlying != nullptr) {
this->m_allocator->destroy(this->m_underlying);
this->m_allocator->deallocate(this->m_underlying, this->m_size);
this->reset();
}
}
template <typename T, typename A>
template <typename D, std::enable_if_t<std::is_array<D>::value> *>
typename memory::Unique_ptr<T, A>::pointer memory::Unique_ptr<T, A>::clone()
const {
pointer to_return = new type[this->m_size];
std::copy(this->m_underlying, this->m_underlying + this->m_size, to_return);
return to_return;
}
template <typename T, typename A>
template <typename D, std::enable_if_t<!std::is_array<D>::value> *>
typename memory::Unique_ptr<T, A>::pointer memory::Unique_ptr<T, A>::clone()
const {
pointer to_return = new type(*this->m_underlying);
return to_return;
}
#ifndef IN_DOXYGEN // Doxygen doesn't understand this construction.
template <typename T, std::enable_if_t<std::is_array<T>::value> *>
memory::Unique_ptr<T, std::nullptr_t> memory::make_unique(size_t size) {
return memory::Unique_ptr<T, std::nullptr_t>{size};
}
template <typename T, typename A, std::enable_if_t<std::is_array<T>::value> *>
memory::Unique_ptr<T, A> memory::make_unique(A &alloc, size_t size) {
return std::move(memory::Unique_ptr<T, A>{alloc, size});
}
template <typename T, typename A, typename... Args,
std::enable_if_t<!std::is_array<T>::value &&
memory::is_allocator<A>::value> *>
memory::Unique_ptr<T, A> memory::make_unique(A &alloc, Args &&...args) {
return std::move(
memory::Unique_ptr<T, A>{alloc, std::forward<Args>(args)...});
}
template <typename T, typename... Args,
std::enable_if_t<!std::is_array<T>::value> *>
memory::Unique_ptr<T, std::nullptr_t> memory::make_unique(Args &&...args) {
return memory::Unique_ptr<T, std::nullptr_t>{std::forward<Args>(args)...};
}
#endif
#endif // MEMORY_UNIQUE_PTR_INCLUDED
|