[libc-commits] [libc] [libc] Breakup freelist_malloc into separate files (PR #98784)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Wed Dec 11 10:34:05 PST 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/98784
>From 05bc628e75761bcdbafbb97bee29a5fb120e85bf Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 13 Jul 2024 18:41:25 -0700
Subject: [PATCH 1/5] [libc] Breakup freelist_malloc into separate files
This better matches the structure we use for the rest of libc.
---
libc/config/baremetal/arm/entrypoints.txt | 1 -
libc/config/baremetal/riscv/entrypoints.txt | 1 -
libc/src/__support/CMakeLists.txt | 7 +-
.../freelist_heap.cpp} | 25 +--
libc/src/stdlib/CMakeLists.txt | 176 +++++++++---------
libc/src/stdlib/baremetal/CMakeLists.txt | 50 +++++
libc/src/stdlib/baremetal/aligned_alloc.cpp | 21 +++
libc/src/stdlib/baremetal/calloc.cpp | 21 +++
libc/src/stdlib/baremetal/free.cpp | 19 ++
libc/src/stdlib/baremetal/malloc.cpp | 21 +++
libc/src/stdlib/baremetal/realloc.cpp | 21 +++
11 files changed, 244 insertions(+), 119 deletions(-)
rename libc/src/{stdlib/freelist_malloc.cpp => __support/freelist_heap.cpp} (53%)
create mode 100644 libc/src/stdlib/baremetal/aligned_alloc.cpp
create mode 100644 libc/src/stdlib/baremetal/calloc.cpp
create mode 100644 libc/src/stdlib/baremetal/free.cpp
create mode 100644 libc/src/stdlib/baremetal/malloc.cpp
create mode 100644 libc/src/stdlib/baremetal/realloc.cpp
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 90a4dab2decba9..9734ed09b9039b 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -180,7 +180,6 @@ 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 e735dd157c6b2e..3ce9c01e24d5ea 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -176,7 +176,6 @@ 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 d8a192f1ffa570..27ed7fb47b99c4 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -26,8 +26,10 @@ add_header_library(
libc.src.__support.CPP.span
)
-add_header_library(
+add_object_library(
freelist_heap
+ SRCS
+ freelist_heap.cpp
HDRS
freelist_heap.h
DEPENDS
@@ -40,6 +42,9 @@ add_header_library(
libc.src.__support.libc_assert
libc.src.string.memory_utils.inline_memcpy
libc.src.string.memory_utils.inline_memset
+ COMPILE_OPTIONS
+ -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
+
)
add_header_library(
diff --git a/libc/src/stdlib/freelist_malloc.cpp b/libc/src/__support/freelist_heap.cpp
similarity index 53%
rename from libc/src/stdlib/freelist_malloc.cpp
rename to libc/src/__support/freelist_heap.cpp
index cfffa0425ff66d..6925d3c3c0eedc 100644
--- a/libc/src/stdlib/freelist_malloc.cpp
+++ b/libc/src/__support/freelist_heap.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation for freelist_malloc --------------------------------===//
+//===-- 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.
@@ -8,11 +8,6 @@
#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>
@@ -30,22 +25,4 @@ LIBC_CONSTINIT FreeListHeapBuffer<SIZE> freelist_heap_buffer;
FreeListHeap<> *freelist_heap = &freelist_heap_buffer;
-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);
-}
-
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 7b7e55db391fae..3fc385d2351cbc 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -318,99 +318,58 @@ add_entrypoint_object(
libc.include.stdlib
)
-if(NOT LIBC_TARGET_OS_IS_GPU)
- if(LLVM_LIBC_INCLUDE_SCUDO)
- set(SCUDO_DEPS "")
-
- include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
-
- # scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
- if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
- elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
- endif()
-
- if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
- message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
- Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
- endif()
-
- list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
-
- list(APPEND SCUDO_DEPS
- RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- )
-
- add_entrypoint_external(
- malloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- calloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- realloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- aligned_alloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- free
- DEPENDS
- ${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
- COMPILE_OPTIONS
- -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
- )
- 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(
- free
- )
- add_entrypoint_external(
- calloc
- )
- add_entrypoint_external(
- realloc
- )
- add_entrypoint_external(
- aligned_alloc
- )
+if(LLVM_LIBC_INCLUDE_SCUDO)
+ set(SCUDO_DEPS "")
+
+ include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
+
+ # scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
+ if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
+ elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
+ endif()
+
+ if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
+ message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
+ Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
endif()
+
+ list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
+
+ list(APPEND SCUDO_DEPS
+ RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ )
+
+ add_entrypoint_external(
+ malloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ calloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ realloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ aligned_alloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ free
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
endif()
if(NOT LLVM_LIBC_FULL_BUILD)
@@ -421,6 +380,39 @@ 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)
+ add_entrypoint_object(
+ malloc
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.malloc
+ )
+ add_entrypoint_object(
+ free
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.free
+ )
+ add_entrypoint_object(
+ calloc
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.calloc
+ )
+ add_entrypoint_object(
+ realloc
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.realloc
+ )
+ add_entrypoint_object(
+ aligned_alloc
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.aligned_alloc
+ )
+endif()
+
if(LIBC_TARGET_OS_IS_GPU)
add_entrypoint_object(
malloc
diff --git a/libc/src/stdlib/baremetal/CMakeLists.txt b/libc/src/stdlib/baremetal/CMakeLists.txt
index 551a83a36b20e8..67ab1979e4d104 100644
--- a/libc/src/stdlib/baremetal/CMakeLists.txt
+++ b/libc/src/stdlib/baremetal/CMakeLists.txt
@@ -5,3 +5,53 @@ 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/aligned_alloc.cpp b/libc/src/stdlib/baremetal/aligned_alloc.cpp
new file mode 100644
index 00000000000000..4594bf32dc1b15
--- /dev/null
+++ b/libc/src/stdlib/baremetal/aligned_alloc.cpp
@@ -0,0 +1,21 @@
+//===-- 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/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/aligned_alloc.h"
+
+#include <stddef.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
+ return freelist_heap->aligned_allocate(alignment, size);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/baremetal/calloc.cpp b/libc/src/stdlib/baremetal/calloc.cpp
new file mode 100644
index 00000000000000..c24caa2a180eb0
--- /dev/null
+++ b/libc/src/stdlib/baremetal/calloc.cpp
@@ -0,0 +1,21 @@
+//===-- 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/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/calloc.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
new file mode 100644
index 00000000000000..5be5f72491e044
--- /dev/null
+++ b/libc/src/stdlib/baremetal/free.cpp
@@ -0,0 +1,19 @@
+//===-- 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/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/free.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
new file mode 100644
index 00000000000000..2d64c250ed15dc
--- /dev/null
+++ b/libc/src/stdlib/baremetal/malloc.cpp
@@ -0,0 +1,21 @@
+//===-- 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/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/malloc.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
new file mode 100644
index 00000000000000..f3957e94d7d162
--- /dev/null
+++ b/libc/src/stdlib/baremetal/realloc.cpp
@@ -0,0 +1,21 @@
+//===-- 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/__support/freelist_heap.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/realloc.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
>From ad04ace0fa9c3614d4ff9edba01f50f3de118b12 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 10 Dec 2024 15:01:11 -0800
Subject: [PATCH 2/5] Fixes
---
libc/src/__support/CMakeLists.txt | 2 +
libc/src/stdlib/CMakeLists.txt | 54 ++++++++-------------
libc/src/stdlib/baremetal/aligned_alloc.cpp | 2 +-
libc/src/stdlib/baremetal/calloc.cpp | 2 +-
libc/src/stdlib/baremetal/free.cpp | 2 +-
libc/src/stdlib/baremetal/malloc.cpp | 2 +-
libc/src/stdlib/baremetal/realloc.cpp | 2 +-
7 files changed, 27 insertions(+), 39 deletions(-)
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b3e5c9672364e2..11653ae9039977 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -56,7 +56,9 @@ add_object_library(
freelist_heap.h
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/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 81382c6d487c20..ad4dd636c8250c 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -387,6 +387,25 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
DEPENDS
${SCUDO_DEPS}
)
+elseif(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
+ add_entrypoint_external(
+ malloc
+ )
+ add_entrypoint_external(
+ calloc
+ )
+ add_entrypoint_external(
+ realloc
+ )
+ add_entrypoint_external(
+ aligned_alloc
+ )
+ add_entrypoint_external(
+ free
+ )
+ add_entrypoint_external(
+ mallopt
+ )
endif()
if(NOT LLVM_LIBC_FULL_BUILD)
@@ -474,40 +493,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)
- add_entrypoint_object(
- malloc
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.malloc
- )
- add_entrypoint_object(
- free
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.free
- )
- add_entrypoint_object(
- calloc
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.calloc
- )
- add_entrypoint_object(
- realloc
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.realloc
- )
- add_entrypoint_object(
- aligned_alloc
- ALIAS
- DEPENDS
- .${LIBC_TARGET_OS}.aligned_alloc
- )
-endif()
-
-if(LIBC_TARGET_OS_IS_GPU)
+if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
add_entrypoint_object(
malloc
ALIAS
diff --git a/libc/src/stdlib/baremetal/aligned_alloc.cpp b/libc/src/stdlib/baremetal/aligned_alloc.cpp
index 4594bf32dc1b15..e9548719c3a63f 100644
--- a/libc/src/stdlib/baremetal/aligned_alloc.cpp
+++ b/libc/src/stdlib/baremetal/aligned_alloc.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#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 <stddef.h>
diff --git a/libc/src/stdlib/baremetal/calloc.cpp b/libc/src/stdlib/baremetal/calloc.cpp
index c24caa2a180eb0..2b3b83cebc8acc 100644
--- a/libc/src/stdlib/baremetal/calloc.cpp
+++ b/libc/src/stdlib/baremetal/calloc.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "src/stdlib/calloc.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
-#include "src/stdlib/calloc.h"
#include <stddef.h>
diff --git a/libc/src/stdlib/baremetal/free.cpp b/libc/src/stdlib/baremetal/free.cpp
index 5be5f72491e044..1e25fe5f2dcfea 100644
--- a/libc/src/stdlib/baremetal/free.cpp
+++ b/libc/src/stdlib/baremetal/free.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "src/stdlib/free.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
-#include "src/stdlib/free.h"
#include <stddef.h>
diff --git a/libc/src/stdlib/baremetal/malloc.cpp b/libc/src/stdlib/baremetal/malloc.cpp
index 2d64c250ed15dc..a299282667fcd5 100644
--- a/libc/src/stdlib/baremetal/malloc.cpp
+++ b/libc/src/stdlib/baremetal/malloc.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "src/stdlib/malloc.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
-#include "src/stdlib/malloc.h"
#include <stddef.h>
diff --git a/libc/src/stdlib/baremetal/realloc.cpp b/libc/src/stdlib/baremetal/realloc.cpp
index f3957e94d7d162..fb25c68ec42964 100644
--- a/libc/src/stdlib/baremetal/realloc.cpp
+++ b/libc/src/stdlib/baremetal/realloc.cpp
@@ -6,9 +6,9 @@
//
//===----------------------------------------------------------------------===//
+#include "src/stdlib/realloc.h"
#include "src/__support/freelist_heap.h"
#include "src/__support/macros/config.h"
-#include "src/stdlib/realloc.h"
#include <stddef.h>
>From af14cc497c4739f387f618fbce37aa3395a9be89 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 10 Dec 2024 15:08:52 -0800
Subject: [PATCH 3/5] Fixes
---
libc/src/stdlib/CMakeLists.txt | 164 +++++++++++++++++----------------
1 file changed, 83 insertions(+), 81 deletions(-)
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index ad4dd636c8250c..40ba9ead9a7ae6 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -323,89 +323,91 @@ add_entrypoint_object(
.rand_util
)
-if(LLVM_LIBC_INCLUDE_SCUDO)
- set(SCUDO_DEPS "")
-
- include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
-
- # scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
- # set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
- if (LIBC_TARGET_OS_IS_DARWIN AND (LIBC_TARGET_ARCHITECTURE STREQUAL "arm"))
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO arm64)
+if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
+ if(LLVM_LIBC_INCLUDE_SCUDO)
+ set(SCUDO_DEPS "")
+
+ include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
+
+ # scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
+ # set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
+ if (LIBC_TARGET_OS_IS_DARWIN AND (LIBC_TARGET_ARCHITECTURE STREQUAL "arm"))
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO arm64)
+ else()
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
+ endif()
+ if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
+ elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
+ set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
+ endif()
+
+ if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
+ message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
+ Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
+ endif()
+
+ 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}
+ RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
+ )
+ endif()
+
+ add_entrypoint_external(
+ malloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ calloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ realloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ aligned_alloc
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ free
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
+ add_entrypoint_external(
+ mallopt
+ DEPENDS
+ ${SCUDO_DEPS}
+ )
else()
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
- endif()
- if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
- elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
- set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
- endif()
-
- if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
- message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
- Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
+ add_entrypoint_external(
+ malloc
+ )
+ add_entrypoint_external(
+ calloc
+ )
+ add_entrypoint_external(
+ realloc
+ )
+ add_entrypoint_external(
+ aligned_alloc
+ )
+ add_entrypoint_external(
+ free
+ )
+ add_entrypoint_external(
+ mallopt
+ )
endif()
-
- 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}
- RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
- )
- endif()
-
- add_entrypoint_external(
- malloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- calloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- realloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- aligned_alloc
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- free
- DEPENDS
- ${SCUDO_DEPS}
- )
- add_entrypoint_external(
- mallopt
- DEPENDS
- ${SCUDO_DEPS}
- )
-elseif(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
- add_entrypoint_external(
- malloc
- )
- add_entrypoint_external(
- calloc
- )
- add_entrypoint_external(
- realloc
- )
- add_entrypoint_external(
- aligned_alloc
- )
- add_entrypoint_external(
- free
- )
- add_entrypoint_external(
- mallopt
- )
endif()
if(NOT LLVM_LIBC_FULL_BUILD)
>From 343fba40d3320aef673e3224869fad100b2c7993 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 11 Dec 2024 09:50:22 -0800
Subject: [PATCH 4/5] Fixes
---
libc/config/baremetal/aarch64/entrypoints.txt | 1 -
libc/src/__support/CMakeLists.txt | 5 +-
libc/test/src/__support/CMakeLists.txt | 6 ++-
.../test/src/__support/freelist_heap_test.cpp | 35 ++++++++++++
.../src/__support/freelist_malloc_test.cpp | 54 -------------------
5 files changed, 41 insertions(+), 60 deletions(-)
delete mode 100644 libc/test/src/__support/freelist_malloc_test.cpp
diff --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 9027717acb4dae..0fbeec337ae3dc 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -182,7 +182,6 @@ 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 11653ae9039977..70ed67c156d1ae 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -54,6 +54,8 @@ add_object_library(
freelist_heap.cpp
HDRS
freelist_heap.h
+ COMPILE_OPTIONS
+ -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
DEPENDS
.block
.freelist
@@ -66,9 +68,6 @@ add_object_library(
libc.src.__support.libc_assert
libc.src.string.memory_utils.inline_memcpy
libc.src.string.memory_utils.inline_memset
- COMPILE_OPTIONS
- -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
-
)
add_header_library(
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index bcc86effd9a52c..836ef37a0fbae6 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -63,11 +63,13 @@ 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.stdlib.aligned_alloc
+ libc.src.stdlib.calloc
+ libc.src.stdlib.free
+ libc.src.stdlib.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 991c158825a888..60013c20088cf8 100644
--- a/libc/test/src/__support/freelist_heap_test.cpp
+++ b/libc/test/src/__support/freelist_heap_test.cpp
@@ -288,3 +288,38 @@ TEST_FOR_EACH_ALLOCATOR(InvalidAlignedAllocAlignment, 2048) {
ptr = allocator.aligned_allocate(0, 8);
EXPECT_EQ(ptr, static_cast<void *>(nullptr));
}
+
+TEST(LlvmLibcFreeListHeap, 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);
+}
diff --git a/libc/test/src/__support/freelist_malloc_test.cpp b/libc/test/src/__support/freelist_malloc_test.cpp
deleted file mode 100644
index 793e2498304fb9..00000000000000
--- a/libc/test/src/__support/freelist_malloc_test.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-//===-- 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);
-}
>From fca6247f0ad6f40a0b2f44d30ea130d3eb382d0f Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 11 Dec 2024 10:33:35 -0800
Subject: [PATCH 5/5] Missing includes
---
libc/test/src/__support/freelist_heap_test.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/libc/test/src/__support/freelist_heap_test.cpp b/libc/test/src/__support/freelist_heap_test.cpp
index 60013c20088cf8..d4aa9d40126929 100644
--- a/libc/test/src/__support/freelist_heap_test.cpp
+++ b/libc/test/src/__support/freelist_heap_test.cpp
@@ -9,6 +9,10 @@
#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"
More information about the libc-commits
mailing list