[libcxx-commits] [libcxx] [libc++] Refactor some code in monotonic_buffer_resource (PR #117271)

Peng Xie via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 21 18:23:23 PST 2024


https://github.com/love1angel updated https://github.com/llvm/llvm-project/pull/117271

>From 389a848940ffe4ad5fc408974164ca02a3d22158 Mon Sep 17 00:00:00 2001
From: Peng Xie <helianthus547 at gmail.com>
Date: Fri, 22 Nov 2024 09:21:07 +0800
Subject: [PATCH] [libc++] Refactor some code in monotonic_buffer_resource

1. remove unused __default_buffer_alignment
2. two __try_allocate_from_chunk are same, put it together

This patch refactor some code in monotonic_buffer_resource.
---
 .../monotonic_buffer_resource.h               |  9 +++---
 libcxx/src/memory_resource.cpp                | 32 ++++++++-----------
 2 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/libcxx/include/__memory_resource/monotonic_buffer_resource.h b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
index c5a2b556707f6a..d972f4e4d24efd 100644
--- a/libcxx/include/__memory_resource/monotonic_buffer_resource.h
+++ b/libcxx/include/__memory_resource/monotonic_buffer_resource.h
@@ -27,8 +27,8 @@ namespace pmr {
 // [mem.res.monotonic.buffer]
 
 class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resource : public memory_resource {
-  static const size_t __default_buffer_capacity  = 1024;
-  static const size_t __default_buffer_alignment = 16;
+  static constexpr size_t __default_buffer_capacity = 1024;
+  static constexpr size_t __default_growth_factor   = 2;
 
   struct __chunk_footer {
     __chunk_footer* __next_;
@@ -38,7 +38,6 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
     _LIBCPP_HIDE_FROM_ABI size_t __allocation_size() {
       return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this);
     }
-    void* __try_allocate_from_chunk(size_t, size_t);
   };
 
   struct __initial_descriptor {
@@ -48,9 +47,11 @@ class _LIBCPP_AVAILABILITY_PMR _LIBCPP_EXPORTED_FROM_ABI monotonic_buffer_resour
       char* __end_;
       size_t __size_;
     };
-    void* __try_allocate_from_chunk(size_t, size_t);
   };
 
+  template <typename Chunk>
+  _LIBCPP_HIDE_FROM_ABI void* __try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align);
+
 public:
   _LIBCPP_HIDE_FROM_ABI monotonic_buffer_resource()
       : monotonic_buffer_resource(nullptr, __default_buffer_capacity, get_default_resource()) {}
diff --git a/libcxx/src/memory_resource.cpp b/libcxx/src/memory_resource.cpp
index 0cd575e995c0ff..dd2ece0dd9fd96 100644
--- a/libcxx/src/memory_resource.cpp
+++ b/libcxx/src/memory_resource.cpp
@@ -429,23 +429,17 @@ static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
   return ptr;
 }
 
-void* monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk(size_t bytes, size_t align) {
-  if (!__cur_)
-    return nullptr;
-  void* new_ptr       = static_cast<void*>(__cur_);
-  size_t new_capacity = (__cur_ - __start_);
-  void* aligned_ptr   = align_down(align, bytes, new_ptr, new_capacity);
-  if (aligned_ptr != nullptr)
-    __cur_ = static_cast<char*>(new_ptr);
-  return aligned_ptr;
-}
-
-void* monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk(size_t bytes, size_t align) {
-  void* new_ptr       = static_cast<void*>(__cur_);
-  size_t new_capacity = (__cur_ - __start_);
+template <typename Chunk>
+void* monotonic_buffer_resource::__try_allocate_from_chunk(Chunk& self, size_t bytes, size_t align) {
+  if constexpr (is_same_v<decay<Chunk>, monotonic_buffer_resource::__initial_descriptor>) {
+    if (self.__cur_)
+      return nullptr;
+  }
+  void* new_ptr       = static_cast<void*>(self.__cur_);
+  size_t new_capacity = (self.__cur_ - self.__start_);
   void* aligned_ptr   = align_down(align, bytes, new_ptr, new_capacity);
   if (aligned_ptr != nullptr)
-    __cur_ = static_cast<char*>(new_ptr);
+    self.__cur_ = static_cast<char*>(new_ptr);
   return aligned_ptr;
 }
 
@@ -462,10 +456,10 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
     return roundup(newsize, footer_align) + footer_size;
   };
 
-  if (void* result = __initial_.__try_allocate_from_chunk(bytes, align))
+  if (void* result = __try_allocate_from_chunk(__initial_, bytes, align))
     return result;
   if (__chunks_ != nullptr) {
-    if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align))
+    if (void* result = __try_allocate_from_chunk(*__chunks_, bytes, align))
       return result;
   }
 
@@ -478,7 +472,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
   size_t previous_capacity = previous_allocation_size();
 
   if (aligned_capacity <= previous_capacity) {
-    size_t newsize   = 2 * (previous_capacity - footer_size);
+    size_t newsize   = __default_growth_factor * (previous_capacity - footer_size);
     aligned_capacity = roundup(newsize, footer_align) + footer_size;
   }
 
@@ -491,7 +485,7 @@ void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
   footer->__align_       = align;
   __chunks_              = footer;
 
-  return __chunks_->__try_allocate_from_chunk(bytes, align);
+  return __try_allocate_from_chunk(*__chunks_, bytes, align);
 }
 
 } // namespace pmr



More information about the libcxx-commits mailing list