1// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// -----------------------------------------------------------------------------
16// File: memory.h
17// -----------------------------------------------------------------------------
18//
19// This header file contains utility functions for managing the creation and
20// conversion of smart pointers. This file is an extension to the C++
21// standard <memory> library header file.
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
36namespace absl {
37
38// -----------------------------------------------------------------------------
39// Function Template: WrapUnique()
40// -----------------------------------------------------------------------------
41//
42// Adopts ownership from a raw pointer and transfers it to the returned
43// `std::unique_ptr`, whose type is deduced. Because of this deduction, *do not*
44// specify the template type `T` when calling `WrapUnique`.
45//
46// Example:
47// X* NewX(int, int);
48// auto x = WrapUnique(NewX(1, 2)); // 'x' is std::unique_ptr<X>.
49//
50// Do not call WrapUnique with an explicit type, as in
51// `WrapUnique<X>(NewX(1, 2))`. The purpose of WrapUnique is to automatically
52// deduce the pointer type. If you wish to make the type explicit, just use
53// `std::unique_ptr` directly.
54//
55// auto x = std::unique_ptr<X>(NewX(1, 2));
56// - or -
57// std::unique_ptr<X> x(NewX(1, 2));
58//
59// While `absl::WrapUnique` is useful for capturing the output of a raw
60// pointer factory, prefer 'absl::make_unique<T>(args...)' over
61// 'absl::WrapUnique(new T(args...))'.
62//
63// auto x = WrapUnique(new X(1, 2)); // works, but nonideal.
64// auto x = make_unique<X>(1, 2); // safer, standard, avoids raw 'new'.
65//
66// Note that `absl::WrapUnique(p)` is valid only if `delete p` is a valid
67// expression. In particular, `absl::WrapUnique()` cannot wrap pointers to
68// arrays, functions or void, and it must not be used to capture pointers
69// obtained from array-new expressions (even though that would compile!).
70template <typename T>
71std::unique_ptr<T> WrapUnique(T* ptr) {
72 static_assert(!std::is_array<T>::value, "array types are unsupported");
73 static_assert(std::is_object<T>::value, "non-object types are unsupported");
74 return std::unique_ptr<T>(ptr);
75}
76
77namespace memory_internal {
78
79// Traits to select proper overload and return type for `absl::make_unique<>`.
80template <typename T>
81struct MakeUniqueResult {
82 using scalar = std::unique_ptr<T>;
83};
84template <typename T>
85struct MakeUniqueResult<T[]> {
86 using array = std::unique_ptr<T[]>;
87};
88template <typename T, size_t N>
89struct MakeUniqueResult<T[N]> {
90 using invalid = void;
91};
92
93} // namespace memory_internal
94
95// gcc 4.8 has __cplusplus at 201301 but doesn't define make_unique. Other
96// supported compilers either just define __cplusplus as 201103 but have
97// make_unique (msvc), or have make_unique whenever __cplusplus > 201103 (clang)
98#if (__cplusplus > 201103L || defined(_MSC_VER)) && \
99 !(defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8)
100using std::make_unique;
101#else
102// -----------------------------------------------------------------------------
103// Function Template: make_unique<T>()
104// -----------------------------------------------------------------------------
105//
106// Creates a `std::unique_ptr<>`, while avoiding issues creating temporaries
107// during the construction process. `absl::make_unique<>` also avoids redundant
108// type declarations, by avoiding the need to explicitly use the `new` operator.
109//
110// This implementation of `absl::make_unique<>` is designed for C++11 code and
111// will be replaced in C++14 by the equivalent `std::make_unique<>` abstraction.
112// `absl::make_unique<>` is designed to be 100% compatible with
113// `std::make_unique<>` so that the eventual migration will involve a simple
114// rename operation.
115//
116// For more background on why `std::unique_ptr<T>(new T(a,b))` is problematic,
117// see Herb Sutter's explanation on
118// (Exception-Safe Function Calls)[https://herbsutter.com/gotw/_102/].
119// (In general, reviewers should treat `new T(a,b)` with scrutiny.)
120//
121// Example usage:
122//
123// auto p = make_unique<X>(args...); // 'p' is a std::unique_ptr<X>
124// auto pa = make_unique<X[]>(5); // 'pa' is a std::unique_ptr<X[]>
125//
126// Three overloads of `absl::make_unique` are required:
127//
128// - For non-array T:
129//
130// Allocates a T with `new T(std::forward<Args> args...)`,
131// forwarding all `args` to T's constructor.
132// Returns a `std::unique_ptr<T>` owning that object.
133//
134// - For an array of unknown bounds T[]:
135//
136// `absl::make_unique<>` will allocate an array T of type U[] with
137// `new U[n]()` and return a `std::unique_ptr<U[]>` owning that array.
138//
139// Note that 'U[n]()' is different from 'U[n]', and elements will be
140// value-initialized. Note as well that `std::unique_ptr` will perform its
141// own destruction of the array elements upon leaving scope, even though
142// the array [] does not have a default destructor.
143//
144// NOTE: an array of unknown bounds T[] may still be (and often will be)
145// initialized to have a size, and will still use this overload. E.g:
146//
147// auto my_array = absl::make_unique<int[]>(10);
148//
149// - For an array of known bounds T[N]:
150//
151// `absl::make_unique<>` is deleted (like with `std::make_unique<>`) as
152// this overload is not useful.
153//
154// NOTE: an array of known bounds T[N] is not considered a useful
155// construction, and may cause undefined behavior in templates. E.g:
156//
157// auto my_array = absl::make_unique<int[10]>();
158//
159// In those cases, of course, you can still use the overload above and
160// simply initialize it to its desired size:
161//
162// auto my_array = absl::make_unique<int[]>(10);
163
164// `absl::make_unique` overload for non-array types.
165template <typename T, typename... Args>
166typename memory_internal::MakeUniqueResult<T>::scalar make_unique(
167 Args&&... args) {
168 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
169}
170
171// `absl::make_unique` overload for an array T[] of unknown bounds.
172// The array allocation needs to use the `new T[size]` form and cannot take
173// element constructor arguments. The `std::unique_ptr` will manage destructing
174// these array elements.
175template <typename T>
176typename memory_internal::MakeUniqueResult<T>::array make_unique(size_t n) {
177 return std::unique_ptr<T>(new typename absl::remove_extent_t<T>[n]());
178}
179
180// `absl::make_unique` overload for an array T[N] of known bounds.
181// This construction will be rejected.
182template <typename T, typename... Args>
183typename memory_internal::MakeUniqueResult<T>::invalid make_unique(
184 Args&&... /* args */) = delete;
185#endif
186
187// -----------------------------------------------------------------------------
188// Function Template: RawPtr()
189// -----------------------------------------------------------------------------
190//
191// Extracts the raw pointer from a pointer-like value `ptr`. `absl::RawPtr` is
192// useful within templates that need to handle a complement of raw pointers,
193// `std::nullptr_t`, and smart pointers.
194template <typename T>
195auto RawPtr(T&& ptr) -> decltype(std::addressof(*ptr)) {
196 // ptr is a forwarding reference to support Ts with non-const operators.
197 return (ptr != nullptr) ? std::addressof(*ptr) : nullptr;
198}
199inline std::nullptr_t RawPtr(std::nullptr_t) { return nullptr; }
200
201// -----------------------------------------------------------------------------
202// Function Template: ShareUniquePtr()
203// -----------------------------------------------------------------------------
204//
205// Adopts a `std::unique_ptr` rvalue and returns a `std::shared_ptr` of deduced
206// type. Ownership (if any) of the held value is transferred to the returned
207// shared pointer.
208//
209// Example:
210//
211// auto up = absl::make_unique<int>(10);
212// auto sp = absl::ShareUniquePtr(std::move(up)); // shared_ptr<int>
213// CHECK_EQ(*sp, 10);
214// CHECK(up == nullptr);
215//
216// Note that this conversion is correct even when T is an array type, and more
217// generally it works for *any* deleter of the `unique_ptr` (single-object
218// deleter, array deleter, or any custom deleter), since the deleter is adopted
219// by the shared pointer as well. The deleter is copied (unless it is a
220// reference).
221//
222// Implements the resolution of [LWG 2415](http://wg21.link/lwg2415), by which a
223// null shared pointer does not attempt to call the deleter.
224template <typename T, typename D>
225std::shared_ptr<T> ShareUniquePtr(std::unique_ptr<T, D>&& ptr) {
226 return ptr ? std::shared_ptr<T>(std::move(ptr)) : std::shared_ptr<T>();
227}
228
229// -----------------------------------------------------------------------------
230// Function Template: WeakenPtr()
231// -----------------------------------------------------------------------------
232//
233// Creates a weak pointer associated with a given shared pointer. The returned
234// value is a `std::weak_ptr` of deduced type.
235//
236// Example:
237//
238// auto sp = std::make_shared<int>(10);
239// auto wp = absl::WeakenPtr(sp);
240// CHECK_EQ(sp.get(), wp.lock().get());
241// sp.reset();
242// CHECK(wp.lock() == nullptr);
243//
244template <typename T>
245std::weak_ptr<T> WeakenPtr(const std::shared_ptr<T>& ptr) {
246 return std::weak_ptr<T>(ptr);
247}
248
249namespace memory_internal {
250
251// ExtractOr<E, O, D>::type evaluates to E<O> if possible. Otherwise, D.
252template <template <typename> class Extract, typename Obj, typename Default,
253 typename>
254struct ExtractOr {
255 using type = Default;
256};
257
258template <template <typename> class Extract, typename Obj, typename Default>
259struct ExtractOr<Extract, Obj, Default, void_t<Extract<Obj>>> {
260 using type = Extract<Obj>;
261};
262
263template <template <typename> class Extract, typename Obj, typename Default>
264using ExtractOrT = typename ExtractOr<Extract, Obj, Default, void>::type;
265
266// Extractors for the features of allocators.
267template <typename T>
268using GetPointer = typename T::pointer;
269
270template <typename T>
271using GetConstPointer = typename T::const_pointer;
272
273template <typename T>
274using GetVoidPointer = typename T::void_pointer;
275
276template <typename T>
277using GetConstVoidPointer = typename T::const_void_pointer;
278
279template <typename T>
280using GetDifferenceType = typename T::difference_type;
281
282template <typename T>
283using GetSizeType = typename T::size_type;
284
285template <typename T>
286using GetPropagateOnContainerCopyAssignment =
287 typename T::propagate_on_container_copy_assignment;
288
289template <typename T>
290using GetPropagateOnContainerMoveAssignment =
291 typename T::propagate_on_container_move_assignment;
292
293template <typename T>
294using GetPropagateOnContainerSwap = typename T::propagate_on_container_swap;
295
296template <typename T>
297using GetIsAlwaysEqual = typename T::is_always_equal;
298
299template <typename T>
300struct GetFirstArg;
301
302template <template <typename...> class Class, typename T, typename... Args>
303struct GetFirstArg<Class<T, Args...>> {
304 using type = T;
305};
306
307template <typename Ptr, typename = void>
308struct ElementType {
309 using type = typename GetFirstArg<Ptr>::type;
310};
311
312template <typename T>
313struct ElementType<T, void_t<typename T::element_type>> {
314 using type = typename T::element_type;
315};
316
317template <typename T, typename U>
318struct RebindFirstArg;
319
320template <template <typename...> class Class, typename T, typename... Args,
321 typename U>
322struct RebindFirstArg<Class<T, Args...>, U> {
323 using type = Class<U, Args...>;
324};
325
326template <typename T, typename U, typename = void>
327struct RebindPtr {
328 using type = typename RebindFirstArg<T, U>::type;
329};
330
331template <typename T, typename U>
332struct RebindPtr<T, U, void_t<typename T::template rebind<U>>> {
333 using type = typename T::template rebind<U>;
334};
335
336template <typename T, typename U>
337constexpr bool HasRebindAlloc(...) {
338 return false;
339}
340
341template <typename T, typename U>
342constexpr bool HasRebindAlloc(typename T::template rebind<U>::other*) {
343 return true;
344}
345
346template <typename T, typename U, bool = HasRebindAlloc<T, U>(nullptr)>
347struct RebindAlloc {
348 using type = typename RebindFirstArg<T, U>::type;
349};
350
351template <typename T, typename U>
352struct RebindAlloc<T, U, true> {
353 using type = typename T::template rebind<U>::other;
354};
355
356} // namespace memory_internal
357
358// -----------------------------------------------------------------------------
359// Class Template: pointer_traits
360// -----------------------------------------------------------------------------
361//
362// An implementation of C++11's std::pointer_traits.
363//
364// Provided for portability on toolchains that have a working C++11 compiler,
365// but the standard library is lacking in C++11 support. For example, some
366// version of the Android NDK.
367//
368
369template <typename Ptr>
370struct pointer_traits {
371 using pointer = Ptr;
372
373 // element_type:
374 // Ptr::element_type if present. Otherwise T if Ptr is a template
375 // instantiation Template<T, Args...>
376 using element_type = typename memory_internal::ElementType<Ptr>::type;
377
378 // difference_type:
379 // Ptr::difference_type if present, otherwise std::ptrdiff_t
380 using difference_type =
381 memory_internal::ExtractOrT<memory_internal::GetDifferenceType, Ptr,
382 std::ptrdiff_t>;
383
384 // rebind:
385 // Ptr::rebind<U> if exists, otherwise Template<U, Args...> if Ptr is a
386 // template instantiation Template<T, Args...>
387 template <typename U>
388 using rebind = typename memory_internal::RebindPtr<Ptr, U>::type;
389
390 // pointer_to:
391 // Calls Ptr::pointer_to(r)
392 static pointer pointer_to(element_type& r) { // NOLINT(runtime/references)
393 return Ptr::pointer_to(r);
394 }
395};
396
397// Specialization for T*.
398template <typename T>
399struct pointer_traits<T*> {
400 using pointer = T*;
401 using element_type = T;
402 using difference_type = std::ptrdiff_t;
403
404 template <typename U>
405 using rebind = U*;
406
407 // pointer_to:
408 // Calls std::addressof(r)
409 static pointer pointer_to(
410 element_type& r) noexcept { // NOLINT(runtime/references)
411 return std::addressof(r);
412 }
413};
414
415// -----------------------------------------------------------------------------
416// Class Template: allocator_traits
417// -----------------------------------------------------------------------------
418//
419// A C++11 compatible implementation of C++17's std::allocator_traits.
420//
421template <typename Alloc>
422struct allocator_traits {
423 using allocator_type = Alloc;
424
425 // value_type:
426 // Alloc::value_type
427 using value_type = typename Alloc::value_type;
428
429 // pointer:
430 // Alloc::pointer if present, otherwise value_type*
431 using pointer = memory_internal::ExtractOrT<memory_internal::GetPointer,
432 Alloc, value_type*>;
433
434 // const_pointer:
435 // Alloc::const_pointer if present, otherwise
436 // absl::pointer_traits<pointer>::rebind<const value_type>
437 using const_pointer =
438 memory_internal::ExtractOrT<memory_internal::GetConstPointer, Alloc,
439 typename absl::pointer_traits<pointer>::
440 template rebind<const value_type>>;
441
442 // void_pointer:
443 // Alloc::void_pointer if present, otherwise
444 // absl::pointer_traits<pointer>::rebind<void>
445 using void_pointer = memory_internal::ExtractOrT<
446 memory_internal::GetVoidPointer, Alloc,
447 typename absl::pointer_traits<pointer>::template rebind<void>>;
448
449 // const_void_pointer:
450 // Alloc::const_void_pointer if present, otherwise
451 // absl::pointer_traits<pointer>::rebind<const void>
452 using const_void_pointer = memory_internal::ExtractOrT<
453 memory_internal::GetConstVoidPointer, Alloc,
454 typename absl::pointer_traits<pointer>::template rebind<const void>>;
455
456 // difference_type:
457 // Alloc::difference_type if present, otherwise
458 // absl::pointer_traits<pointer>::difference_type
459 using difference_type = memory_internal::ExtractOrT<
460 memory_internal::GetDifferenceType, Alloc,
461 typename absl::pointer_traits<pointer>::difference_type>;
462
463 // size_type:
464 // Alloc::size_type if present, otherwise
465 // std::make_unsigned<difference_type>::type
466 using size_type = memory_internal::ExtractOrT<
467 memory_internal::GetSizeType, Alloc,
468 typename std::make_unsigned<difference_type>::type>;
469
470 // propagate_on_container_copy_assignment:
471 // Alloc::propagate_on_container_copy_assignment if present, otherwise
472 // std::false_type
473 using propagate_on_container_copy_assignment = memory_internal::ExtractOrT<
474 memory_internal::GetPropagateOnContainerCopyAssignment, Alloc,
475 std::false_type>;
476
477 // propagate_on_container_move_assignment:
478 // Alloc::propagate_on_container_move_assignment if present, otherwise
479 // std::false_type
480 using propagate_on_container_move_assignment = memory_internal::ExtractOrT<
481 memory_internal::GetPropagateOnContainerMoveAssignment, Alloc,
482 std::false_type>;
483
484 // propagate_on_container_swap:
485 // Alloc::propagate_on_container_swap if present, otherwise std::false_type
486 using propagate_on_container_swap =
487 memory_internal::ExtractOrT<memory_internal::GetPropagateOnContainerSwap,
488 Alloc, std::false_type>;
489
490 // is_always_equal:
491 // Alloc::is_always_equal if present, otherwise std::is_empty<Alloc>::type
492 using is_always_equal =
493 memory_internal::ExtractOrT<memory_internal::GetIsAlwaysEqual, Alloc,
494 typename std::is_empty<Alloc>::type>;
495
496 // rebind_alloc:
497 // Alloc::rebind<T>::other if present, otherwise Alloc<T, Args> if this Alloc
498 // is Alloc<U, Args>
499 template <typename T>
500 using rebind_alloc = typename memory_internal::RebindAlloc<Alloc, T>::type;
501
502 // rebind_traits:
503 // absl::allocator_traits<rebind_alloc<T>>
504 template <typename T>
505 using rebind_traits = absl::allocator_traits<rebind_alloc<T>>;
506
507 // allocate(Alloc& a, size_type n):
508 // Calls a.allocate(n)
509 static pointer allocate(Alloc& a, // NOLINT(runtime/references)
510 size_type n) {
511 return a.allocate(n);
512 }
513
514 // allocate(Alloc& a, size_type n, const_void_pointer hint):
515 // Calls a.allocate(n, hint) if possible.
516 // If not possible, calls a.allocate(n)
517 static pointer allocate(Alloc& a, size_type n, // NOLINT(runtime/references)
518 const_void_pointer hint) {
519 return allocate_impl(0, a, n, hint);
520 }
521
522 // deallocate(Alloc& a, pointer p, size_type n):
523 // Calls a.deallocate(p, n)
524 static void deallocate(Alloc& a, pointer p, // NOLINT(runtime/references)
525 size_type n) {
526 a.deallocate(p, n);
527 }
528
529 // construct(Alloc& a, T* p, Args&&... args):
530 // Calls a.construct(p, std::forward<Args>(args)...) if possible.
531 // If not possible, calls
532 // ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...)
533 template <typename T, typename... Args>
534 static void construct(Alloc& a, T* p, // NOLINT(runtime/references)
535 Args&&... args) {
536 construct_impl(0, a, p, std::forward<Args>(args)...);
537 }
538
539 // destroy(Alloc& a, T* p):
540 // Calls a.destroy(p) if possible. If not possible, calls p->~T().
541 template <typename T>
542 static void destroy(Alloc& a, T* p) { // NOLINT(runtime/references)
543 destroy_impl(0, a, p);
544 }
545
546 // max_size(const Alloc& a):
547 // Returns a.max_size() if possible. If not possible, returns
548 // std::numeric_limits<size_type>::max() / sizeof(value_type)
549 static size_type max_size(const Alloc& a) { return max_size_impl(0, a); }
550
551 // select_on_container_copy_construction(const Alloc& a):
552 // Returns a.select_on_container_copy_construction() if possible.
553 // If not possible, returns a.
554 static Alloc select_on_container_copy_construction(const Alloc& a) {
555 return select_on_container_copy_construction_impl(0, a);
556 }
557
558 private:
559 template <typename A>
560 static auto allocate_impl(int, A& a, // NOLINT(runtime/references)
561 size_type n, const_void_pointer hint)
562 -> decltype(a.allocate(n, hint)) {
563 return a.allocate(n, hint);
564 }
565 static pointer allocate_impl(char, Alloc& a, // NOLINT(runtime/references)
566 size_type n, const_void_pointer) {
567 return a.allocate(n);
568 }
569
570 template <typename A, typename... Args>
571 static auto construct_impl(int, A& a, // NOLINT(runtime/references)
572 Args&&... args)
573 -> decltype(a.construct(std::forward<Args>(args)...)) {
574 a.construct(std::forward<Args>(args)...);
575 }
576
577 template <typename T, typename... Args>
578 static void construct_impl(char, Alloc&, T* p, Args&&... args) {
579 ::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
580 }
581
582 template <typename A, typename T>
583 static auto destroy_impl(int, A& a, // NOLINT(runtime/references)
584 T* p) -> decltype(a.destroy(p)) {
585 a.destroy(p);
586 }
587 template <typename T>
588 static void destroy_impl(char, Alloc&, T* p) {
589 p->~T();
590 }
591
592 template <typename A>
593 static auto max_size_impl(int, const A& a) -> decltype(a.max_size()) {
594 return a.max_size();
595 }
596 static size_type max_size_impl(char, const Alloc&) {
597 return (std::numeric_limits<size_type>::max)() / sizeof(value_type);
598 }
599
600 template <typename A>
601 static auto select_on_container_copy_construction_impl(int, const A& a)
602 -> decltype(a.select_on_container_copy_construction()) {
603 return a.select_on_container_copy_construction();
604 }
605 static Alloc select_on_container_copy_construction_impl(char,
606 const Alloc& a) {
607 return a;
608 }
609};
610
611namespace memory_internal {
612
613// This template alias transforms Alloc::is_nothrow into a metafunction with
614// Alloc as a parameter so it can be used with ExtractOrT<>.
615template <typename Alloc>
616using GetIsNothrow = typename Alloc::is_nothrow;
617
618} // namespace memory_internal
619
620// ABSL_ALLOCATOR_NOTHROW is a build time configuration macro for user to
621// specify whether the default allocation function can throw or never throws.
622// If the allocation function never throws, user should define it to a non-zero
623// value (e.g. via `-DABSL_ALLOCATOR_NOTHROW`).
624// If the allocation function can throw, user should leave it undefined or
625// define it to zero.
626//
627// allocator_is_nothrow<Alloc> is a traits class that derives from
628// Alloc::is_nothrow if present, otherwise std::false_type. It's specialized
629// for Alloc = std::allocator<T> for any type T according to the state of
630// ABSL_ALLOCATOR_NOTHROW.
631//
632// default_allocator_is_nothrow is a class that derives from std::true_type
633// when the default allocator (global operator new) never throws, and
634// std::false_type when it can throw. It is a convenience shorthand for writing
635// allocator_is_nothrow<std::allocator<T>> (T can be any type).
636// NOTE: allocator_is_nothrow<std::allocator<T>> is guaranteed to derive from
637// the same type for all T, because users should specialize neither
638// allocator_is_nothrow nor std::allocator.
639template <typename Alloc>
640struct allocator_is_nothrow
641 : memory_internal::ExtractOrT<memory_internal::GetIsNothrow, Alloc,
642 std::false_type> {};
643
644#if defined(ABSL_ALLOCATOR_NOTHROW) && ABSL_ALLOCATOR_NOTHROW
645template <typename T>
646struct allocator_is_nothrow<std::allocator<T>> : std::true_type {};
647struct default_allocator_is_nothrow : std::true_type {};
648#else
649struct default_allocator_is_nothrow : std::false_type {};
650#endif
651
652namespace memory_internal {
653template <typename Allocator, typename Iterator, typename... Args>
654void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
655 const Args&... args) {
656 for (Iterator cur = first; cur != last; ++cur) {
657 ABSL_INTERNAL_TRY {
658 std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur),
659 args...);
660 }
661 ABSL_INTERNAL_CATCH_ANY {
662 while (cur != first) {
663 --cur;
664 std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
665 }
666 ABSL_INTERNAL_RETHROW;
667 }
668 }
669}
670
671template <typename Allocator, typename Iterator, typename InputIterator>
672void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
673 InputIterator last) {
674 for (Iterator cur = destination; first != last;
675 static_cast<void>(++cur), static_cast<void>(++first)) {
676 ABSL_INTERNAL_TRY {
677 std::allocator_traits<Allocator>::construct(alloc, std::addressof(*cur),
678 *first);
679 }
680 ABSL_INTERNAL_CATCH_ANY {
681 while (cur != destination) {
682 --cur;
683 std::allocator_traits<Allocator>::destroy(alloc, std::addressof(*cur));
684 }
685 ABSL_INTERNAL_RETHROW;
686 }
687 }
688}
689} // namespace memory_internal
690} // namespace absl
691
692#endif // ABSL_MEMORY_MEMORY_H_
693