| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef ABSL_MEMORY_MEMORY_H_ |
| 24 |
#define ABSL_MEMORY_MEMORY_H_ |
| 25 |
|
| 26 |
#include <cstddef> |
| 27 |
#include <limits> |
| 28 |
#include <memory> |
| 29 |
#include <new> |
| 30 |
#include <type_traits> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
#include "absl/base/macros.h" |
| 34 |
#include "absl/meta/type_traits.h" |
| 35 |
|
| 36 |
namespace absl { |
| 37 |
ABSL_NAMESPACE_BEGIN |
| 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 |
template <typename T> |
| 72 |
std::unique_ptr<T> WrapUnique(T* ptr) { |
| 73 |
static_assert(!std::is_array<T>::value, "array types are unsupported"); |
| 74 |
static_assert(std::is_object<T>::value, "non-object types are unsupported"); |
| 75 |
return std::unique_ptr<T>(ptr); |
| 76 |
} |
| 77 |
|
| 78 |
namespace memory_internal { |
| 79 |
|
| 80 |
|
| 81 |
template <typename T> |
| 82 |
struct MakeUniqueResult { |
| 83 |
using scalar = std::unique_ptr<T>; |
| 84 |
}; |
| 85 |
template <typename T> |
| 86 |
struct MakeUniqueResult<T[]> { |
| 87 |
using array = std::unique_ptr<T[]>; |
| 88 |
}; |
| 89 |
template <typename T, size_t N> |
| 90 |
struct MakeUniqueResult<T[N]> { |
| 91 |
using invalid = void; |
| 92 |
}; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#if (__cplusplus > 201103L || defined(_MSC_VER)) && \ |
| 101 |
!(defined(__GLIBCXX__) && !defined(__cpp_lib_make_unique)) |
| 102 |
using std::make_unique; |
| 103 |
#else |
| 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 |
template <typename T, typename... Args> |
| 168 |
typename memory_internal::MakeUniqueResult<T>::scalar make_unique( |
| 169 |
Args&&... args) { |
| 170 |
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
template <typename T> |
| 178 |
typename memory_internal::MakeUniqueResult<T>::array make_unique(size_t n) { |
| 179 |
return std::unique_ptr<T>(new typename absl::remove_extent_t<T>[n]()); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
template <typename T, typename... Args> |
| 185 |
typename memory_internal::MakeUniqueResult<T>::invalid make_unique( |
| 186 |
Args&&... ) = delete; |
| 187 |
#endif |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
template <typename T> |
| 197 |
auto RawPtr(T&& ptr) -> decltype(std::addressof(*ptr)) { |
| 198 |
|
| 199 |
return (ptr != nullptr) ? std::addressof(*ptr) : nullptr; |
| 200 |
} |
| 201 |
inline std::nullptr_t RawPtr(std::nullptr_t) { return nullptr; } |
| 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 |
template <typename T, typename D> |
| 227 |
std::shared_ptr<T> ShareUniquePtr(std::unique_ptr<T, D>&& ptr) { |
| 228 |
return ptr ? std::shared_ptr<T>(std::move(ptr)) : std::shared_ptr<T>(); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
template <typename T> |
| 247 |
std::weak_ptr<T> WeakenPtr(const std::shared_ptr<T>& ptr) { |
| 248 |
return std::weak_ptr<T>(ptr); |
| 249 |
} |
| 250 |
|
| 251 |
namespace memory_internal { |
| 252 |
|
| 253 |
|
| 254 |
template <template <typename> class Extract, typename Obj, typename Default, |
| 255 |
typename> |
| 256 |
struct ExtractOr { |
| 257 |
using type = Default; |
| 258 |
}; |
| 259 |
|
| 260 |
template <template <typename> class Extract, typename Obj, typename Default> |
| 261 |
struct ExtractOr<Extract, Obj, Default, void_t<Extract<Obj>>> { |
| 262 |
using type = Extract<Obj>; |
| 263 |
}; |
| 264 |
|
| 265 |
template <template <typename> class Extract, typename Obj, typename Default> |
| 266 |
using ExtractOrT = typename ExtractOr<Extract, Obj, Default, void>::type; |
| 267 |
|
| 268 |
|
| 269 |
template <typename T> |
| 270 |
using GetPointer = typename T::pointer; |
| 271 |
|
| 272 |
template <typename T> |
| 273 |
using GetConstPointer = typename T::const_pointer; |
| 274 |
|
| 275 |
template <typename T> |
| 276 |
using GetVoidPointer = typename T::void_pointer; |
| 277 |
|
| 278 |
template <typename T> |
| 279 |
using GetConstVoidPointer = typename T::const_void_pointer; |
| 280 |
|
| 281 |
template <typename T> |
| 282 |
using GetDifferenceType = typename T::difference_type; |
| 283 |
|
| 284 |
template <typename T> |
| 285 |
using GetSizeType = typename T::size_type; |
| 286 |
|
| 287 |
template <typename T> |
| 288 |
using GetPropagateOnContainerCopyAssignment = |
| 289 |
typename T::propagate_on_container_copy_assignment; |
| 290 |
|
| 291 |
template <typename T> |
| 292 |
using GetPropagateOnContainerMoveAssignment = |
| 293 |
typename T::propagate_on_container_move_assignment; |
| 294 |
|
| 295 |
template <typename T> |
| 296 |
using GetPropagateOnContainerSwap = typename T::propagate_on_container_swap; |
| 297 |
|
| 298 |
template <typename T> |
| 299 |
using GetIsAlwaysEqual = typename T::is_always_equal; |
| 300 |
|
| 301 |
template <typename T> |
| 302 |
struct GetFirstArg; |
| 303 |
|
| 304 |
template <template <typename...> class Class, typename T, typename... Args> |
| 305 |
struct GetFirstArg<Class<T, Args...>> { |
| 306 |
using type = T; |
| 307 |
}; |
| 308 |
|
| 309 |
template <typename Ptr, typename = void> |
| 310 |
struct ElementType { |
| 311 |
using type = typename GetFirstArg<Ptr>::type; |
| 312 |
}; |
| 313 |
|
| 314 |
template <typename T> |
| 315 |
struct ElementType<T, void_t<typename T::element_type>> { |
| 316 |
using type = typename T::element_type; |
| 317 |
}; |
| 318 |
|
| 319 |
template <typename T, typename U> |
| 320 |
struct RebindFirstArg; |
| 321 |
|
| 322 |
template <template <typename...> class Class, typename T, typename... Args, |
| 323 |
typename U> |
| 324 |
struct RebindFirstArg<Class<T, Args...>, U> { |
| 325 |
using type = Class<U, Args...>; |
| 326 |
}; |
| 327 |
|
| 328 |
template <typename T, typename U, typename = void> |
| 329 |
struct RebindPtr { |
| 330 |
using type = typename RebindFirstArg<T, U>::type; |
| 331 |
}; |
| 332 |
|
| 333 |
template <typename T, typename U> |
| 334 |
struct RebindPtr<T, U, void_t<typename T::template rebind<U>>> { |
| 335 |
using type = typename T::template rebind<U>; |
| 336 |
}; |
| 337 |
|
| 338 |
template <typename T, typename U> |
| 339 |
constexpr bool HasRebindAlloc(...) { |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
template <typename T, typename U> |
| 344 |
constexpr bool HasRebindAlloc(typename T::template rebind<U>::other*) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
template <typename T, typename U, bool = HasRebindAlloc<T, U>(nullptr)> |
| 349 |
struct RebindAlloc { |
| 350 |
using type = typename RebindFirstArg<T, U>::type; |
| 351 |
}; |
| 352 |
|
| 353 |
template <typename T, typename U> |
| 354 |
struct RebindAlloc<T, U, true> { |
| 355 |
using type = typename T::template rebind<U>::other; |
| 356 |
}; |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
template <typename Ptr> |
| 372 |
struct pointer_traits { |
| 373 |
using pointer = Ptr; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
using element_type = typename memory_internal::ElementType<Ptr>::type; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
using difference_type = |
| 383 |
memory_internal::ExtractOrT<memory_internal::GetDifferenceType, Ptr, |
| 384 |
std::ptrdiff_t>; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
template <typename U> |
| 390 |
using rebind = typename memory_internal::RebindPtr<Ptr, U>::type; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
static pointer pointer_to(element_type& r) { |
| 395 |
return Ptr::pointer_to(r); |
| 396 |
} |
| 397 |
}; |
| 398 |
|
| 399 |
|
| 400 |
template <typename T> |
| 401 |
struct pointer_traits<T*> { |
| 402 |
using pointer = T*; |
| 403 |
using element_type = T; |
| 404 |
using difference_type = std::ptrdiff_t; |
| 405 |
|
| 406 |
template <typename U> |
| 407 |
using rebind = U*; |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
static pointer pointer_to( |
| 412 |
element_type& r) noexcept { |
| 413 |
return std::addressof(r); |
| 414 |
} |
| 415 |
}; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
#if __cplusplus >= 201703L |
| 424 |
using std::allocator_traits; |
| 425 |
#else |
| 426 |
template <typename Alloc> |
| 427 |
struct allocator_traits { |
| 428 |
using allocator_type = Alloc; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
using value_type = typename Alloc::value_type; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
using pointer = memory_internal::ExtractOrT<memory_internal::GetPointer, |
| 437 |
Alloc, value_type*>; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
using const_pointer = |
| 443 |
memory_internal::ExtractOrT<memory_internal::GetConstPointer, Alloc, |
| 444 |
typename absl::pointer_traits<pointer>:: |
| 445 |
template rebind<const value_type>>; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
using void_pointer = memory_internal::ExtractOrT< |
| 451 |
memory_internal::GetVoidPointer, Alloc, |
| 452 |
typename absl::pointer_traits<pointer>::template rebind<void>>; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
using const_void_pointer = memory_internal::ExtractOrT< |
| 458 |
memory_internal::GetConstVoidPointer, Alloc, |
| 459 |
typename absl::pointer_traits<pointer>::template rebind<const void>>; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
using difference_type = memory_internal::ExtractOrT< |
| 465 |
memory_internal::GetDifferenceType, Alloc, |
| 466 |
typename absl::pointer_traits<pointer>::difference_type>; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
using size_type = memory_internal::ExtractOrT< |
| 472 |
memory_internal::GetSizeType, Alloc, |
| 473 |
typename std::make_unsigned<difference_type>::type>; |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
using propagate_on_container_copy_assignment = memory_internal::ExtractOrT< |
| 479 |
memory_internal::GetPropagateOnContainerCopyAssignment, Alloc, |
| 480 |
std::false_type>; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
using propagate_on_container_move_assignment = memory_internal::ExtractOrT< |
| 486 |
memory_internal::GetPropagateOnContainerMoveAssignment, Alloc, |
| 487 |
std::false_type>; |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
using propagate_on_container_swap = |
| 492 |
memory_internal::ExtractOrT<memory_internal::GetPropagateOnContainerSwap, |
| 493 |
Alloc, std::false_type>; |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
using is_always_equal = |
| 498 |
memory_internal::ExtractOrT<memory_internal::GetIsAlwaysEqual, Alloc, |
| 499 |
typename std::is_empty<Alloc>::type>; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
template <typename T> |
| 505 |
using rebind_alloc = typename memory_internal::RebindAlloc<Alloc, T>::type; |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
template <typename T> |
| 510 |
using rebind_traits = absl::allocator_traits<rebind_alloc<T>>; |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static pointer allocate(Alloc& a, |
| 515 |
size_type n) { |
| 516 |
return a.allocate(n); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static pointer allocate(Alloc& a, size_type n, |
| 523 |
const_void_pointer hint) { |
| 524 |
return allocate_impl(0, a, n, hint); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
static void deallocate(Alloc& a, pointer p, |
| 530 |
size_type n) { |
| 531 |
a.deallocate(p, n); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
template <typename T, typename... Args> |
| 539 |
static void construct(Alloc& a, T* p, |
| 540 |
Args&&... args) { |
| 541 |
construct_impl(0, a, p, std::forward<Args>(args)...); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
template <typename T> |
| 547 |
static void destroy(Alloc& a, T* p) { |
| 548 |
destroy_impl(0, a, p); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static size_type max_size(const Alloc& a) { return max_size_impl(0, a); } |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static Alloc select_on_container_copy_construction(const Alloc& a) { |
| 560 |
return select_on_container_copy_construction_impl(0, a); |
| 561 |
} |
| 562 |
|
| 563 |
private: |
| 564 |
template <typename A> |
| 565 |
static auto allocate_impl(int, A& a, |
| 566 |
size_type n, const_void_pointer hint) |
| 567 |
-> decltype(a.allocate(n, hint)) { |
| 568 |
return a.allocate(n, hint); |
| 569 |
} |
| 570 |
static pointer allocate_impl(char, Alloc& a, |
| 571 |
size_type n, const_void_pointer) { |
| 572 |
return a.allocate(n); |
| 573 |
} |
| 574 |
|
| 575 |
template <typename A, typename... Args> |
| 576 |
static auto construct_impl(int, A& a, |
| 577 |
Args&&... args) |
| 578 |
-> decltype(a.construct(std::forward<Args>(args)...)) { |
| 579 |
a.construct(std::forward<Args>(args)...); |
| 580 |
} |
| 581 |
|
| 582 |
template <typename T, typename... Args> |
| 583 |
static void construct_impl(char, Alloc&, T* p, Args&&... args) { |
| 584 |
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...); |
| 585 |
} |
| 586 |
|
| 587 |
template <typename A, typename T> |
| 588 |
static auto destroy_impl(int, A& a, |
| 589 |
T* p) -> decltype(a.destroy(p)) { |
| 590 |
a.destroy(p); |
| 591 |
} |
| 592 |
template <typename T> |
| 593 |
static void destroy_impl(char, Alloc&, T* p) { |
| 594 |
p->~T(); |
| 595 |
} |
| 596 |
|
| 597 |
template <typename A> |
| 598 |
static auto max_size_impl(int, const A& a) -> decltype(a.max_size()) { |
| 599 |
return a.max_size(); |
| 600 |
} |
| 601 |
static size_type max_size_impl(char, const Alloc&) { |
| 602 |
return (std::numeric_limits<size_type>::max)() / sizeof(value_type); |
| 603 |
} |
| 604 |
|
| 605 |
template <typename A> |
| 606 |
static auto select_on_container_copy_construction_impl(int, const A& a) |
| 607 |
-> decltype(a.select_on_container_copy_construction()) { |
| 608 |
return a.select_on_container_copy_construction(); |
| 609 |
} |
| 610 |
static Alloc select_on_container_copy_construction_impl(char, |
| 611 |
const Alloc& a) { |
| 612 |
return a; |
| 613 |
} |
| 614 |
}; |
| 615 |
#endif |
| 616 |
|
| 617 |
namespace memory_internal { |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
template <typename Alloc> |
| 622 |
using GetIsNothrow = typename Alloc::is_nothrow; |
| 623 |
|
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
template <typename Alloc> |
| 646 |
struct allocator_is_nothrow |
| 647 |
: memory_internal::ExtractOrT<memory_internal::GetIsNothrow, Alloc, |
| 648 |
std::false_type> {}; |
| 649 |
|
| 650 |
#if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW |
| 651 |
template <typename T> |
| 652 |
struct allocator_is_nothrow<std::allocator<T>> : std::true_type {}; |
| 653 |
struct default_allocator_is_nothrow : std::true_type {}; |
| 654 |
#else |
| 655 |
struct default_allocator_is_nothrow : std::false_type {}; |
| 656 |
#endif |
| 657 |
|
| 658 |
namespace memory_internal { |
| 659 |
template <typename Allocator, typename Iterator, typename... Args> |
| 660 |
void ConstructRange(Allocator& alloc, Iterator first, Iterator last, |
| 661 |
const Args&... args) { |
| 662 |
for (Iterator cur = first; cur != last; ++cur) { |
| 663 |
ABSL_INTERNAL_TRY { |
| 664 |
std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur), |
| 665 |
args...); |
| 666 |
} |
| 667 |
ABSL_INTERNAL_CATCH_ANY { |
| 668 |
while (cur != first) { |
| 669 |
--cur; |
| 670 |
std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur)); |
| 671 |
} |
| 672 |
ABSL_INTERNAL_RETHROW; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
template <typename Allocator, typename Iterator, typename InputIterator> |
| 678 |
void CopyRange(Allocator& alloc, Iterator destination, InputIterator first, |
| 679 |
InputIterator last) { |
| 680 |
for (Iterator cur = destination; first != last; |
| 681 |
static_cast<void>(++cur), static_cast<void>(++first)) { |
| 682 |
ABSL_INTERNAL_TRY { |
| 683 |
std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur), |
| 684 |
*first); |
| 685 |
} |
| 686 |
ABSL_INTERNAL_CATCH_ANY { |
| 687 |
while (cur != destination) { |
| 688 |
--cur; |
| 689 |
std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur)); |
| 690 |
} |
| 691 |
ABSL_INTERNAL_RETHROW; |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
ABSL_NAMESPACE_END |
| 697 |
} |
| 698 |
|
| 699 |
#endif |