Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you move the COMPILE_OPTIONS part to above DEPENDS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}

)

add_header_library(
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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>

Expand All @@ -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
176 changes: 84 additions & 92 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions libc/src/stdlib/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
21 changes: 21 additions & 0 deletions libc/src/stdlib/baremetal/aligned_alloc.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions libc/src/stdlib/baremetal/calloc.cpp
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions libc/src/stdlib/baremetal/free.cpp
Original file line number Diff line number Diff line change
@@ -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
Loading