[libc-commits] [libc] [libc] Breakup freelist_malloc into separate files (PR #98784)
via libc-commits
libc-commits at lists.llvm.org
Sat Jul 13 18:43:06 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Petr Hosek (petrhosek)
<details>
<summary>Changes</summary>
This better matches the structure we use for the rest of libc.
---
Full diff: https://github.com/llvm/llvm-project/pull/98784.diff
11 Files Affected:
- (modified) libc/config/baremetal/arm/entrypoints.txt (-1)
- (modified) libc/config/baremetal/riscv/entrypoints.txt (-1)
- (modified) libc/src/__support/CMakeLists.txt (+6-1)
- (renamed) libc/src/__support/freelist_heap.cpp (+1-24)
- (modified) libc/src/stdlib/CMakeLists.txt (+84-92)
- (modified) libc/src/stdlib/baremetal/CMakeLists.txt (+50)
- (added) libc/src/stdlib/baremetal/aligned_alloc.cpp (+21)
- (added) libc/src/stdlib/baremetal/calloc.cpp (+21)
- (added) libc/src/stdlib/baremetal/free.cpp (+19)
- (added) libc/src/stdlib/baremetal/malloc.cpp (+21)
- (added) libc/src/stdlib/baremetal/realloc.cpp (+21)
``````````diff
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 90a4dab2decba..9734ed09b9039 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 e735dd157c6b2..3ce9c01e24d5e 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 d8a192f1ffa57..27ed7fb47b99c 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 cfffa0425ff66..6925d3c3c0eed 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 7b7e55db391fa..3fc385d2351cb 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 551a83a36b20e..67ab1979e4d10 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 0000000000000..4594bf32dc1b1
--- /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 0000000000000..c24caa2a180eb
--- /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 0000000000000..5be5f72491e04
--- /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 0000000000000..2d64c250ed15d
--- /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 0000000000000..f3957e94d7d16
--- /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
``````````
</details>
https://github.com/llvm/llvm-project/pull/98784
More information about the libc-commits
mailing list