Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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 @@ -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
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 @@ -178,7 +178,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
9 changes: 8 additions & 1 deletion libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,27 @@ add_header_library(
.freetrie
)

add_header_library(
add_object_library(
freelist_heap
SRCS
freelist_heap.cpp
HDRS
freelist_heap.h
DEPENDS
.block
.freelist
.freestore
.freetrie
libc.src.__support.CPP.cstddef
libc.src.__support.CPP.array
libc.src.__support.CPP.optional
libc.src.__support.CPP.span
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
19 changes: 19 additions & 0 deletions libc/src/__support/freelist_heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- 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
38 changes: 10 additions & 28 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ add_entrypoint_object(
.rand_util
)

if(NOT LIBC_TARGET_OS_IS_GPU)
if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
if(LLVM_LIBC_INCLUDE_SCUDO)
set(SCUDO_DEPS "")

Expand All @@ -349,7 +349,7 @@ if(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}
Expand Down Expand Up @@ -389,32 +389,8 @@ if(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(
free
malloc
)
add_entrypoint_external(
calloc
Expand All @@ -425,6 +401,12 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
add_entrypoint_external(
aligned_alloc
)
add_entrypoint_external(
free
)
add_entrypoint_external(
mallopt
)
endif()
endif()

Expand Down Expand Up @@ -513,7 +495,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_GPU)
if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
add_entrypoint_object(
malloc
ALIAS
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
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,14 @@
//
//===----------------------------------------------------------------------===//

#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);
}
Expand Down
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/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
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/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
21 changes: 21 additions & 0 deletions libc/src/stdlib/baremetal/malloc.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/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
21 changes: 21 additions & 0 deletions libc/src/stdlib/baremetal/realloc.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/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
Loading