[libc-commits] [libc] [libc][malloc] Reuse the prev_ field for allocated blocks (PR #101265)

via libc-commits libc-commits at lists.llvm.org
Tue Jul 30 17:02:52 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 8dd574236ccaa0a183278396cfec3068b832651a 20284744862f6e5425cb26deefe70651f157b0bd --extensions cpp,h -- libc/src/__support/block.h libc/src/__support/freelist_heap.h libc/test/src/__support/block_test.cpp libc/test/src/__support/freelist_heap_test.cpp libc/test/src/__support/freelist_malloc_test.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/__support/block.h b/libc/src/__support/block.h
index ecea49926e..8fcd7317b6 100644
--- a/libc/src/__support/block.h
+++ b/libc/src/__support/block.h
@@ -353,7 +353,7 @@ Block<OffsetType, kAlign>::init(ByteSpan region) {
     return {};
 
   region = result.value();
-  if (region.size() < 2*BLOCK_OVERHEAD)
+  if (region.size() < 2 * BLOCK_OVERHEAD)
     return {};
 
   if (cpp::numeric_limits<OffsetType>::max() < region.size())
@@ -404,8 +404,8 @@ Block<OffsetType, kAlign>::allocate(Block *block, size_t alignment,
                 "two blocks.");
 
     if (Block *prev = original->prev_free()) {
-      // If there is a free block before this, we can merge the current one with the
-      // newly created one.
+      // If there is a free block before this, we can merge the current one with
+      // the newly created one.
       prev->merge_next();
     } else {
       info.prev = original;
@@ -429,14 +429,14 @@ optional<Block<OffsetType, kAlign> *>
 Block<OffsetType, kAlign>::split(size_t new_inner_size) {
   if (used())
     return {};
-  // The prev_ field of the next block is always available, so there is a minimum size to
-  // a block created through splitting.
+  // The prev_ field of the next block is always available, so there is a
+  // minimum size to a block created through splitting.
   if (new_inner_size < sizeof(prev_))
     return {};
 
   size_t old_inner_size = inner_size();
-  new_inner_size = align_up(new_inner_size - sizeof(prev_), ALIGNMENT) +
-                   sizeof(prev_);
+  new_inner_size =
+      align_up(new_inner_size - sizeof(prev_), ALIGNMENT) + sizeof(prev_);
   if (old_inner_size < new_inner_size)
     return {};
 
@@ -496,8 +496,7 @@ constexpr Block<OffsetType, kAlign>::Block(size_t outer_size)
 }
 
 template <typename OffsetType, size_t kAlign>
-Block<OffsetType, kAlign> *
-Block<OffsetType, kAlign>::as_block(ByteSpan bytes) {
+Block<OffsetType, kAlign> *Block<OffsetType, kAlign>::as_block(ByteSpan bytes) {
   return ::new (bytes.data()) Block(bytes.size());
 }
 
diff --git a/libc/test/src/__support/block_test.cpp b/libc/test/src/__support/block_test.cpp
index a6625b58f0..62d7fae67b 100644
--- a/libc/test/src/__support/block_test.cpp
+++ b/libc/test/src/__support/block_test.cpp
@@ -113,7 +113,8 @@ TEST_FOR_EACH_BLOCK_TYPE(CanSplitBlock) {
   auto *block2 = *result;
 
   EXPECT_EQ(block1->inner_size(), kSplitN);
-  EXPECT_EQ(block1->outer_size(), kSplitN - prev_field_size + BlockType::BLOCK_OVERHEAD);
+  EXPECT_EQ(block1->outer_size(),
+            kSplitN - prev_field_size + BlockType::BLOCK_OVERHEAD);
 
   EXPECT_EQ(block2->outer_size(), orig_size - block1->outer_size());
   EXPECT_FALSE(block2->used());
@@ -408,7 +409,7 @@ TEST_FOR_EACH_BLOCK_TYPE(CanAllocate) {
   constexpr size_t kN = 1024 + BlockType::BLOCK_OVERHEAD;
 
   // Ensure we can allocate everything up to the block size within this block.
-  for (size_t i = 0; i < kN - 2*BlockType::BLOCK_OVERHEAD; ++i) {
+  for (size_t i = 0; i < kN - 2 * BlockType::BLOCK_OVERHEAD; ++i) {
     alignas(BlockType::ALIGNMENT) array<byte, kN> bytes{};
     auto result = BlockType::init(bytes);
     ASSERT_TRUE(result.has_value());
diff --git a/libc/test/src/__support/freelist_malloc_test.cpp b/libc/test/src/__support/freelist_malloc_test.cpp
index 35c1e41257..9cbdec89f6 100644
--- a/libc/test/src/__support/freelist_malloc_test.cpp
+++ b/libc/test/src/__support/freelist_malloc_test.cpp
@@ -29,8 +29,8 @@ TEST(LlvmLibcFreeListMalloc, Malloc) {
   EXPECT_GE(block->inner_size(), kAllocSize);
 
   LIBC_NAMESPACE::free(ptr1);
-  ASSERT_NE(block->next(), static_cast<Block*>(nullptr));
-  ASSERT_EQ(block->next()->next(), static_cast<Block*>(nullptr));
+  ASSERT_NE(block->next(), static_cast<Block *>(nullptr));
+  ASSERT_EQ(block->next()->next(), static_cast<Block *>(nullptr));
   size_t heap_size = block->inner_size();
 
   void *ptr2 = LIBC_NAMESPACE::calloc(kCallocNum, kCallocSize);
@@ -47,7 +47,7 @@ TEST(LlvmLibcFreeListMalloc, Malloc) {
   void *ptr3 = LIBC_NAMESPACE::aligned_alloc(ALIGN, kAllocSize);
   EXPECT_NE(ptr3, static_cast<void *>(nullptr));
   EXPECT_EQ(reinterpret_cast<uintptr_t>(ptr3) % ALIGN, size_t(0));
-  auto *aligned_block = reinterpret_cast<Block*>(ptr3);
+  auto *aligned_block = reinterpret_cast<Block *>(ptr3);
   EXPECT_GE(aligned_block->inner_size(), kAllocSize);
 
   LIBC_NAMESPACE::free(ptr3);

``````````

</details>


https://github.com/llvm/llvm-project/pull/101265


More information about the libc-commits mailing list