about summary refs log tree commit diff
path: root/absl/container/inlined_vector.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-01-10T19·46-0800
committerDerek Mauro <dmauro@google.com>2018-01-10T20·36-0500
commitc742b72354a84958b6a061755249822eeef87d06 (patch)
treebf7c3480e1b7396124625c49ccc103bb7bdc6008 /absl/container/inlined_vector.h
parent98bff8b2bc53aa271c0dd0dff1b33730b4df8481 (diff)
Changes imported from Abseil "staging" branch:
  - f679f7de2957ac4dca0a862d04f1165d2f503525 Merge GitHub PR #78: Fix typo in thread_identity.h by Derek Mauro <dmauro@google.com>
  - 369cbefc9ebb8503e3c25b1516c856dab3bed7ac Minor refactor of operator-(uint128). by Alex Strelnikov <strel@google.com>
  - fba0f8c33b051d90936ad0fcaa4bea83f554bf8d Merge GitHub PR #75: Fix typo in per_thread_tls.h by Derek Mauro <dmauro@google.com>
  - 76d5d25a54ab93c1ea3bc74b5a28ba335b0f2bab Implement InlinedVector::shrink_to_fit() method. by Abseil Team <absl-team@google.com>

GitOrigin-RevId: f679f7de2957ac4dca0a862d04f1165d2f503525
Change-Id: I03b39fdbd70c00a455d98d949d413dd7c8019578
Diffstat (limited to 'absl/container/inlined_vector.h')
-rw-r--r--absl/container/inlined_vector.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h
index 4bf9c18adec4..feba87b58217 100644
--- a/absl/container/inlined_vector.h
+++ b/absl/container/inlined_vector.h
@@ -573,6 +573,42 @@ class InlinedVector {
     }
   }
 
+  // InlinedVector::shrink_to_fit()
+  //
+  // Reduces memory usage by freeing unused memory.
+  // After this call `capacity()` will be equal to `max(N, size())`.
+  //
+  // If `size() <= N` and the elements are currently stored on the heap, they
+  // will be moved to the inlined storage and the heap memory deallocated.
+  // If `size() > N` and `size() < capacity()` the elements will be moved to
+  // a reallocated storage on heap.
+  void shrink_to_fit() {
+    const auto s = size();
+    if (!allocated() || s == capacity()) {
+      // There's nothing to deallocate.
+      return;
+    }
+
+    if (s <= N) {
+      // Move the elements to the inlined storage.
+      // We have to do this using a temporary, because inlined_storage and
+      // allocation_storage are in a union field.
+      auto temp = std::move(*this);
+      assign(std::make_move_iterator(temp.begin()),
+             std::make_move_iterator(temp.end()));
+      return;
+    }
+
+    // Reallocate storage and move elements.
+    // We can't simply use the same approach as above, because assign() would
+    // call into reserve() internally and reserve larger capacity than we need.
+    Allocation new_allocation(allocator(), s);
+    UninitializedCopy(std::make_move_iterator(allocated_space()),
+                      std::make_move_iterator(allocated_space() + s),
+                      new_allocation.buffer());
+    ResetAllocation(new_allocation, s);
+  }
+
   // InlinedVector::swap()
   //
   // Swaps the contents of this inlined vector with the contents of `other`.