[libc-commits] [libc] 379cc44 - Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
via libc-commits
libc-commits at lists.llvm.org
Thu Dec 12 13:28:34 PST 2024
Author: Petr Hosek
Date: 2024-12-12T13:28:30-08:00
New Revision: 379cc44f56e6f220422ce85d2295833f849086e0
URL: https://github.com/llvm/llvm-project/commit/379cc44f56e6f220422ce85d2295833f849086e0
DIFF: https://github.com/llvm/llvm-project/commit/379cc44f56e6f220422ce85d2295833f849086e0.diff
LOG: Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
Reverts llvm/llvm-project#98784 which broke libc builders.
Added:
libc/src/stdlib/freelist_malloc.cpp
libc/test/src/__support/freelist_malloc_test.cpp
Modified:
libc/config/baremetal/aarch64/entrypoints.txt
libc/config/baremetal/arm/entrypoints.txt
libc/config/baremetal/riscv/entrypoints.txt
libc/src/__support/CMakeLists.txt
libc/src/stdlib/CMakeLists.txt
libc/src/stdlib/baremetal/CMakeLists.txt
libc/test/src/__support/CMakeLists.txt
libc/test/src/__support/freelist_heap_test.cpp
Removed:
libc/src/__support/freelist_heap.cpp
libc/src/stdlib/baremetal/aligned_alloc.cpp
libc/src/stdlib/baremetal/calloc.cpp
libc/src/stdlib/baremetal/free.cpp
libc/src/stdlib/baremetal/malloc.cpp
libc/src/stdlib/baremetal/realloc.cpp
################################################################################
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 694cd7b1993ca2..71b49d98942916 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -184,6 +184,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.div
libc.src.stdlib.exit
libc.src.stdlib.free
+ libc.src.stdlib.freelist_malloc
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 694cd7b1993ca2..71b49d98942916 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -184,6 +184,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.div
libc.src.stdlib.exit
libc.src.stdlib.free
+ libc.src.stdlib.freelist_malloc
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 6dc5df830eb000..e84d139d09dd8e 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -180,6 +180,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.div
libc.src.stdlib.exit
libc.src.stdlib.free
+ libc.src.stdlib.freelist_malloc
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 70ed67c156d1ae..8f85740f70a06e 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -48,19 +48,13 @@ add_header_library(
.freetrie
)
-add_object_library(
+add_header_library(
freelist_heap
- SRCS
- freelist_heap.cpp
HDRS
freelist_heap.h
- COMPILE_OPTIONS
- -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
DEPENDS
.block
- .freelist
.freestore
- .freetrie
libc.src.__support.CPP.cstddef
libc.src.__support.CPP.array
libc.src.__support.CPP.optional
diff --git a/libc/src/__support/freelist_heap.cpp b/libc/src/__support/freelist_heap.cpp
deleted file mode 100644
index 4deb0e0f09e223..00000000000000
--- a/libc/src/__support/freelist_heap.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-//===-- Implementation for freelist_heap ----------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/__support/freelist_heap.h"
-#include "src/__support/macros/config.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
-FreeListHeap *freelist_heap = &freelist_heap_symbols;
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 40ba9ead9a7ae6..14d06534a6049a 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -323,7 +323,7 @@ add_entrypoint_object(
.rand_util
)
-if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
+if(NOT LIBC_TARGET_OS_IS_GPU)
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")
@@ -349,7 +349,7 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
-
+
if (COMPILER_RT_BUILD_GWP_ASAN)
list(APPEND SCUDO_DEPS
RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
@@ -389,8 +389,32 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
${SCUDO_DEPS}
)
else()
+ # Only use freelist malloc for baremetal targets.
+ add_entrypoint_object(
+ freelist_malloc
+ SRCS
+ freelist_malloc.cpp
+ HDRS
+ malloc.h
+ DEPENDS
+ libc.src.__support.freelist_heap
+ )
+ get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
+ if(LIBC_TARGET_OS_IS_BAREMETAL AND NOT freelist_malloc_is_skipped)
+ add_entrypoint_object(
+ malloc
+ ALIAS
+ DEPENDS
+ .freelist_malloc
+ )
+ else()
+ add_entrypoint_external(
+ malloc
+ )
+ endif()
+
add_entrypoint_external(
- malloc
+ free
)
add_entrypoint_external(
calloc
@@ -401,12 +425,6 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
add_entrypoint_external(
aligned_alloc
)
- add_entrypoint_external(
- free
- )
- add_entrypoint_external(
- mallopt
- )
endif()
endif()
@@ -495,7 +513,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()
-if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
+if(LIBC_TARGET_OS_IS_GPU)
add_entrypoint_object(
malloc
ALIAS
diff --git a/libc/src/stdlib/baremetal/CMakeLists.txt b/libc/src/stdlib/baremetal/CMakeLists.txt
index 67ab1979e4d104..551a83a36b20e8 100644
--- a/libc/src/stdlib/baremetal/CMakeLists.txt
+++ b/libc/src/stdlib/baremetal/CMakeLists.txt
@@ -5,53 +5,3 @@ add_entrypoint_object(
HDRS
../abort.h
)
-
-add_entrypoint_object(
- malloc
- SRCS
- malloc.cpp
- HDRS
- ../malloc.h
- DEPENDS
- libc.src.__support.freelist_heap
-)
-
-add_entrypoint_object(
- free
- SRCS
- free.cpp
- HDRS
- ../free.h
- DEPENDS
- libc.src.__support.freelist_heap
-)
-
-add_entrypoint_object(
- calloc
- SRCS
- calloc.cpp
- HDRS
- ../calloc.h
- DEPENDS
- libc.src.__support.freelist_heap
-)
-
-add_entrypoint_object(
- realloc
- SRCS
- realloc.cpp
- HDRS
- ../realloc.h
- DEPENDS
- libc.src.__support.freelist_heap
-)
-
-add_entrypoint_object(
- aligned_alloc
- SRCS
- aligned_alloc.cpp
- HDRS
- ../aligned_alloc.h
- DEPENDS
- libc.src.__support.freelist_heap
-)
diff --git a/libc/src/stdlib/baremetal/calloc.cpp b/libc/src/stdlib/baremetal/calloc.cpp
deleted file mode 100644
index 2b3b83cebc8acc..00000000000000
--- a/libc/src/stdlib/baremetal/calloc.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/stdlib/calloc.h"
-#include "src/__support/freelist_heap.h"
-#include "src/__support/macros/config.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
- return freelist_heap->calloc(num, size);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/free.cpp b/libc/src/stdlib/baremetal/free.cpp
deleted file mode 100644
index 1e25fe5f2dcfea..00000000000000
--- a/libc/src/stdlib/baremetal/free.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/stdlib/free.h"
-#include "src/__support/freelist_heap.h"
-#include "src/__support/macros/config.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/malloc.cpp b/libc/src/stdlib/baremetal/malloc.cpp
deleted file mode 100644
index a299282667fcd5..00000000000000
--- a/libc/src/stdlib/baremetal/malloc.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/stdlib/malloc.h"
-#include "src/__support/freelist_heap.h"
-#include "src/__support/macros/config.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
- return freelist_heap->allocate(size);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/realloc.cpp b/libc/src/stdlib/baremetal/realloc.cpp
deleted file mode 100644
index fb25c68ec42964..00000000000000
--- a/libc/src/stdlib/baremetal/realloc.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/stdlib/realloc.h"
-#include "src/__support/freelist_heap.h"
-#include "src/__support/macros/config.h"
-
-#include <stddef.h>
-
-namespace LIBC_NAMESPACE_DECL {
-
-LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
- return freelist_heap->realloc(ptr, size);
-}
-
-} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/aligned_alloc.cpp b/libc/src/stdlib/freelist_malloc.cpp
similarity index 53%
rename from libc/src/stdlib/baremetal/aligned_alloc.cpp
rename to libc/src/stdlib/freelist_malloc.cpp
index e9548719c3a63f..fe56fad769378a 100644
--- a/libc/src/stdlib/baremetal/aligned_alloc.cpp
+++ b/libc/src/stdlib/freelist_malloc.cpp
@@ -6,14 +6,35 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdlib/aligned_alloc.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
+#include "src/stdlib/aligned_alloc.h"
+#include "src/stdlib/calloc.h"
+#include "src/stdlib/free.h"
+#include "src/stdlib/malloc.h"
+#include "src/stdlib/realloc.h"
#include <stddef.h>
namespace LIBC_NAMESPACE_DECL {
+static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
+FreeListHeap *freelist_heap = &freelist_heap_symbols;
+
+LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
+ return freelist_heap->allocate(size);
+}
+
+LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
+
+LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
+ return freelist_heap->calloc(num, size);
+}
+
+LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
+ return freelist_heap->realloc(ptr, size);
+}
+
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
return freelist_heap->aligned_allocate(alignment, size);
}
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 59bce9b96e3964..bcc86effd9a52c 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -63,9 +63,11 @@ if(LLVM_LIBC_FULL_BUILD)
SRCS
fake_heap.s
freelist_heap_test.cpp
+ freelist_malloc_test.cpp
DEPENDS
libc.src.__support.CPP.span
libc.src.__support.freelist_heap
+ libc.src.stdlib.freelist_malloc
libc.src.string.memcmp
libc.src.string.memcpy
)
diff --git a/libc/test/src/__support/freelist_heap_test.cpp b/libc/test/src/__support/freelist_heap_test.cpp
index 07b9a09d77bba6..991c158825a888 100644
--- a/libc/test/src/__support/freelist_heap_test.cpp
+++ b/libc/test/src/__support/freelist_heap_test.cpp
@@ -9,10 +9,6 @@
#include "src/__support/CPP/span.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
-#include "src/stdlib/aligned_alloc.h"
-#include "src/stdlib/calloc.h"
-#include "src/stdlib/free.h"
-#include "src/stdlib/malloc.h"
#include "src/string/memcmp.h"
#include "src/string/memcpy.h"
#include "test/UnitTest/Test.h"
diff --git a/libc/test/src/__support/freelist_malloc_test.cpp b/libc/test/src/__support/freelist_malloc_test.cpp
new file mode 100644
index 00000000000000..793e2498304fb9
--- /dev/null
+++ b/libc/test/src/__support/freelist_malloc_test.cpp
@@ -0,0 +1,54 @@
+//===-- Unittests for freelist_malloc -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/freelist_heap.h"
+#include "src/stdlib/aligned_alloc.h"
+#include "src/stdlib/calloc.h"
+#include "src/stdlib/free.h"
+#include "src/stdlib/malloc.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::Block;
+using LIBC_NAMESPACE::freelist_heap;
+using LIBC_NAMESPACE::FreeListHeap;
+using LIBC_NAMESPACE::FreeListHeapBuffer;
+
+TEST(LlvmLibcFreeListMalloc, Malloc) {
+ constexpr size_t kAllocSize = 256;
+ constexpr size_t kCallocNum = 4;
+ constexpr size_t kCallocSize = 64;
+
+ void *ptr1 = LIBC_NAMESPACE::malloc(kAllocSize);
+ auto *block = Block::from_usable_space(ptr1);
+ 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));
+ size_t heap_size = block->inner_size();
+
+ void *ptr2 = LIBC_NAMESPACE::calloc(kCallocNum, kCallocSize);
+ ASSERT_EQ(ptr2, ptr1);
+ EXPECT_GE(block->inner_size(), kCallocNum * kCallocSize);
+
+ for (size_t i = 0; i < kCallocNum * kCallocSize; ++i)
+ EXPECT_EQ(reinterpret_cast<uint8_t *>(ptr2)[i], uint8_t(0));
+
+ LIBC_NAMESPACE::free(ptr2);
+ EXPECT_EQ(block->inner_size(), heap_size);
+
+ constexpr size_t ALIGN = kAllocSize;
+ 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);
+ EXPECT_GE(aligned_block->inner_size(), kAllocSize);
+
+ LIBC_NAMESPACE::free(ptr3);
+ EXPECT_EQ(block->inner_size(), heap_size);
+}
More information about the libc-commits
mailing list