[libc-commits] [libc] [libc][hermetic] use mutex-protected freelist heap for hermetic test (PR #208587)

via libc-commits libc-commits at lists.llvm.org
Thu Jul 9 16:47:12 PDT 2026


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 origin/main HEAD --extensions h,cpp -- libc/src/__support/sbrk_heap.h libc/test/UnitTest/HermeticHeap.cpp libc/test/src/__support/sbrk_heap_test.cpp libc/src/__support/freelist.h libc/src/__support/freelist_heap.h libc/src/__support/freetrie.h libc/test/UnitTest/HermeticTestUtils.cpp libc/test/src/__support/freelist_heap_test.cpp libc/test/src/__support/freetrie_test.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

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

``````````diff
diff --git a/libc/src/__support/freetrie.h b/libc/src/__support/freetrie.h
index 78e1940f0..22f0cdec0 100644
--- a/libc/src/__support/freetrie.h
+++ b/libc/src/__support/freetrie.h
@@ -130,25 +130,26 @@ LIBC_INLINE void FreeTrie::push(BlockRef block) {
 
   if (!range.contains(size)) {
     if (empty()) {
-      range = SizeRange(
-          range.min,
-          cpp::max(range.width, cpp::bit_ceil(size - range.min + 1)));
+      range =
+          SizeRange(range.min,
+                    cpp::max(range.width, cpp::bit_ceil(size - range.min + 1)));
     } else {
-    // Dynamically expand the trie upwards by doubling the range and creating a
-    // new root node using the pushed block. The previous root becomes the
-    // lower child of the new root.
-    Node *node = new (block.usable_space()) Node;
-    node->parent = nullptr;
-    node->lower = root;
-    node->upper = nullptr;
-    FreeList list;
-    list.push(node);
-    root->parent = node;
-    root = node;
-    range = SizeRange(range.min, range.width * 2);
-    LIBC_ASSERT(range.contains(size) &&
-                "pushed block size exceeds dynamic trie expansion limit (at most 2x current range when non-empty)");
-    return;
+      // Dynamically expand the trie upwards by doubling the range and creating
+      // a new root node using the pushed block. The previous root becomes the
+      // lower child of the new root.
+      Node *node = new (block.usable_space()) Node;
+      node->parent = nullptr;
+      node->lower = root;
+      node->upper = nullptr;
+      FreeList list;
+      list.push(node);
+      root->parent = node;
+      root = node;
+      range = SizeRange(range.min, range.width * 2);
+      LIBC_ASSERT(range.contains(size) &&
+                  "pushed block size exceeds dynamic trie expansion limit (at "
+                  "most 2x current range when non-empty)");
+      return;
     }
   }
 
diff --git a/libc/test/src/__support/freelist_heap_test.cpp b/libc/test/src/__support/freelist_heap_test.cpp
index cd2ce4477..dbb54de3e 100644
--- a/libc/test/src/__support/freelist_heap_test.cpp
+++ b/libc/test/src/__support/freelist_heap_test.cpp
@@ -363,7 +363,8 @@ TEST(LlvmLibcFreeListHeap, Adopt) {
   void *ptr1 = allocator.allocate(1500);
   EXPECT_NE(ptr1, static_cast<void *>(nullptr));
 
-  // Initial buffer is now almost full; allocating another 1500 bytes should fail.
+  // Initial buffer is now almost full; allocating another 1500 bytes should
+  // fail.
   void *ptr_fail = allocator.allocate(1500);
   EXPECT_EQ(ptr_fail, static_cast<void *>(nullptr));
 
@@ -392,15 +393,16 @@ TEST(LlvmLibcFreeListHeap, AdoptMMap) {
   EXPECT_NE(ptr1, static_cast<void *>(nullptr));
 
   // Allocate a second page via mmap and adopt it into the heap.
-  void *addr2 = LIBC_NAMESPACE::mmap(nullptr, PAGE_SIZE * 2,
-                                     PROT_READ | PROT_WRITE,
-                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  void *addr2 =
+      LIBC_NAMESPACE::mmap(nullptr, PAGE_SIZE * 2, PROT_READ | PROT_WRITE,
+                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   ASSERT_NE(addr2, MAP_FAILED);
   span<byte> region2(reinterpret_cast<byte *>(addr2), PAGE_SIZE * 2);
 
   EXPECT_TRUE(allocator.adopt(region2));
 
-  // Now we should be able to allocate a larger block from the newly adopted region!
+  // Now we should be able to allocate a larger block from the newly adopted
+  // region!
   void *ptr2 = allocator.allocate(PAGE_SIZE);
   EXPECT_NE(ptr2, static_cast<void *>(nullptr));
 
diff --git a/libc/test/src/__support/sbrk_heap_test.cpp b/libc/test/src/__support/sbrk_heap_test.cpp
index dbc9ca32b..4cbf534c7 100644
--- a/libc/test/src/__support/sbrk_heap_test.cpp
+++ b/libc/test/src/__support/sbrk_heap_test.cpp
@@ -27,11 +27,13 @@ TEST(LlvmLibcSbrkHeapTest, BasicAllocationAndDoubling) {
   EXPECT_NE(ptr1, static_cast<void *>(nullptr));
 
   // Allocate another 400 bytes; this exceeds the initial 512-byte heap,
-  // triggering SYS_brk growth (doubling the heap by adopting another 512 bytes).
+  // triggering SYS_brk growth (doubling the heap by adopting another 512
+  // bytes).
   void *ptr2 = heap.allocate(400);
   EXPECT_NE(ptr2, static_cast<void *>(nullptr));
 
-  // Allocate a larger block (2048 bytes), triggering multiple doublings via SYS_brk.
+  // Allocate a larger block (2048 bytes), triggering multiple doublings via
+  // SYS_brk.
   void *ptr3 = heap.allocate(2048);
   EXPECT_NE(ptr3, static_cast<void *>(nullptr));
 

``````````

</details>


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


More information about the libc-commits mailing list