Skip to content

Commit 05bc628

Browse files
committed
[libc] Breakup freelist_malloc into separate files
This better matches the structure we use for the rest of libc.
1 parent 3fa409f commit 05bc628

File tree

11 files changed

+244
-119
lines changed

11 files changed

+244
-119
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ set(TARGET_LIBC_ENTRYPOINTS
180180
libc.src.stdlib.div
181181
libc.src.stdlib.exit
182182
libc.src.stdlib.free
183-
libc.src.stdlib.freelist_malloc
184183
libc.src.stdlib.labs
185184
libc.src.stdlib.ldiv
186185
libc.src.stdlib.llabs

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ set(TARGET_LIBC_ENTRYPOINTS
176176
libc.src.stdlib.div
177177
libc.src.stdlib.exit
178178
libc.src.stdlib.free
179-
libc.src.stdlib.freelist_malloc
180179
libc.src.stdlib.labs
181180
libc.src.stdlib.ldiv
182181
libc.src.stdlib.llabs

libc/src/__support/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ add_header_library(
2626
libc.src.__support.CPP.span
2727
)
2828

29-
add_header_library(
29+
add_object_library(
3030
freelist_heap
31+
SRCS
32+
freelist_heap.cpp
3133
HDRS
3234
freelist_heap.h
3335
DEPENDS
@@ -40,6 +42,9 @@ add_header_library(
4042
libc.src.__support.libc_assert
4143
libc.src.string.memory_utils.inline_memcpy
4244
libc.src.string.memory_utils.inline_memset
45+
COMPILE_OPTIONS
46+
-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
47+
4348
)
4449

4550
add_header_library(
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Implementation for freelist_malloc --------------------------------===//
1+
//===-- Implementation for freelist_heap ----------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -8,11 +8,6 @@
88

99
#include "src/__support/freelist_heap.h"
1010
#include "src/__support/macros/config.h"
11-
#include "src/stdlib/aligned_alloc.h"
12-
#include "src/stdlib/calloc.h"
13-
#include "src/stdlib/free.h"
14-
#include "src/stdlib/malloc.h"
15-
#include "src/stdlib/realloc.h"
1611

1712
#include <stddef.h>
1813

@@ -30,22 +25,4 @@ LIBC_CONSTINIT FreeListHeapBuffer<SIZE> freelist_heap_buffer;
3025

3126
FreeListHeap<> *freelist_heap = &freelist_heap_buffer;
3227

33-
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
34-
return freelist_heap->allocate(size);
35-
}
36-
37-
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
38-
39-
LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
40-
return freelist_heap->calloc(num, size);
41-
}
42-
43-
LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
44-
return freelist_heap->realloc(ptr, size);
45-
}
46-
47-
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
48-
return freelist_heap->aligned_allocate(alignment, size);
49-
}
50-
5128
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/CMakeLists.txt

Lines changed: 84 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -318,99 +318,58 @@ add_entrypoint_object(
318318
libc.include.stdlib
319319
)
320320

321-
if(NOT LIBC_TARGET_OS_IS_GPU)
322-
if(LLVM_LIBC_INCLUDE_SCUDO)
323-
set(SCUDO_DEPS "")
324-
325-
include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
326-
327-
# scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
328-
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
329-
if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
330-
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
331-
elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
332-
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
333-
endif()
334-
335-
if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
336-
message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
337-
Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
338-
endif()
339-
340-
list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
341-
RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
342-
343-
list(APPEND SCUDO_DEPS
344-
RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
345-
RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
346-
RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
347-
)
348-
349-
add_entrypoint_external(
350-
malloc
351-
DEPENDS
352-
${SCUDO_DEPS}
353-
)
354-
add_entrypoint_external(
355-
calloc
356-
DEPENDS
357-
${SCUDO_DEPS}
358-
)
359-
add_entrypoint_external(
360-
realloc
361-
DEPENDS
362-
${SCUDO_DEPS}
363-
)
364-
add_entrypoint_external(
365-
aligned_alloc
366-
DEPENDS
367-
${SCUDO_DEPS}
368-
)
369-
add_entrypoint_external(
370-
free
371-
DEPENDS
372-
${SCUDO_DEPS}
373-
)
374-
else()
375-
# Only use freelist malloc for baremetal targets.
376-
add_entrypoint_object(
377-
freelist_malloc
378-
SRCS
379-
freelist_malloc.cpp
380-
HDRS
381-
malloc.h
382-
DEPENDS
383-
libc.src.__support.freelist_heap
384-
COMPILE_OPTIONS
385-
-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
386-
)
387-
get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
388-
if(LIBC_TARGET_OS_IS_BAREMETAL AND NOT freelist_malloc_is_skipped)
389-
add_entrypoint_object(
390-
malloc
391-
ALIAS
392-
DEPENDS
393-
.freelist_malloc
394-
)
395-
else()
396-
add_entrypoint_external(
397-
malloc
398-
)
399-
endif()
400-
401-
add_entrypoint_external(
402-
free
403-
)
404-
add_entrypoint_external(
405-
calloc
406-
)
407-
add_entrypoint_external(
408-
realloc
409-
)
410-
add_entrypoint_external(
411-
aligned_alloc
412-
)
321+
if(LLVM_LIBC_INCLUDE_SCUDO)
322+
set(SCUDO_DEPS "")
323+
324+
include(${LIBC_SOURCE_DIR}/../compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake)
325+
326+
# scudo distinguishes riscv32 and riscv64, so we need to translate the architecture
327+
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO ${LIBC_TARGET_ARCHITECTURE})
328+
if(LIBC_TARGET_ARCHITECTURE_IS_RISCV64)
329+
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv64)
330+
elseif(LIBC_TARGET_ARCHITECTURE_IS_RISCV32)
331+
set(LIBC_TARGET_ARCHITECTURE_FOR_SCUDO riscv32)
332+
endif()
333+
334+
if(NOT (LIBC_TARGET_ARCHITECTURE_FOR_SCUDO IN_LIST ALL_SCUDO_STANDALONE_SUPPORTED_ARCH))
335+
message(FATAL_ERROR "Architecture ${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO} is not supported by SCUDO.
336+
Either disable LLVM_LIBC_INCLUDE_SCUDO or change your target architecture.")
413337
endif()
338+
339+
list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
340+
RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
341+
342+
list(APPEND SCUDO_DEPS
343+
RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
344+
RTGwpAsanBacktraceLibc.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
345+
RTGwpAsanSegvHandler.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
346+
)
347+
348+
add_entrypoint_external(
349+
malloc
350+
DEPENDS
351+
${SCUDO_DEPS}
352+
)
353+
add_entrypoint_external(
354+
calloc
355+
DEPENDS
356+
${SCUDO_DEPS}
357+
)
358+
add_entrypoint_external(
359+
realloc
360+
DEPENDS
361+
${SCUDO_DEPS}
362+
)
363+
add_entrypoint_external(
364+
aligned_alloc
365+
DEPENDS
366+
${SCUDO_DEPS}
367+
)
368+
add_entrypoint_external(
369+
free
370+
DEPENDS
371+
${SCUDO_DEPS}
372+
)
414373
endif()
415374

416375
if(NOT LLVM_LIBC_FULL_BUILD)
@@ -421,6 +380,39 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
421380
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
422381
endif()
423382

383+
if(LIBC_TARGET_OS_IS_BAREMETAL)
384+
add_entrypoint_object(
385+
malloc
386+
ALIAS
387+
DEPENDS
388+
.${LIBC_TARGET_OS}.malloc
389+
)
390+
add_entrypoint_object(
391+
free
392+
ALIAS
393+
DEPENDS
394+
.${LIBC_TARGET_OS}.free
395+
)
396+
add_entrypoint_object(
397+
calloc
398+
ALIAS
399+
DEPENDS
400+
.${LIBC_TARGET_OS}.calloc
401+
)
402+
add_entrypoint_object(
403+
realloc
404+
ALIAS
405+
DEPENDS
406+
.${LIBC_TARGET_OS}.realloc
407+
)
408+
add_entrypoint_object(
409+
aligned_alloc
410+
ALIAS
411+
DEPENDS
412+
.${LIBC_TARGET_OS}.aligned_alloc
413+
)
414+
endif()
415+
424416
if(LIBC_TARGET_OS_IS_GPU)
425417
add_entrypoint_object(
426418
malloc

libc/src/stdlib/baremetal/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,53 @@ add_entrypoint_object(
55
HDRS
66
../abort.h
77
)
8+
9+
add_entrypoint_object(
10+
malloc
11+
SRCS
12+
malloc.cpp
13+
HDRS
14+
../malloc.h
15+
DEPENDS
16+
libc.src.__support.freelist_heap
17+
)
18+
19+
add_entrypoint_object(
20+
free
21+
SRCS
22+
free.cpp
23+
HDRS
24+
../free.h
25+
DEPENDS
26+
libc.src.__support.freelist_heap
27+
)
28+
29+
add_entrypoint_object(
30+
calloc
31+
SRCS
32+
calloc.cpp
33+
HDRS
34+
../calloc.h
35+
DEPENDS
36+
libc.src.__support.freelist_heap
37+
)
38+
39+
add_entrypoint_object(
40+
realloc
41+
SRCS
42+
realloc.cpp
43+
HDRS
44+
../realloc.h
45+
DEPENDS
46+
libc.src.__support.freelist_heap
47+
)
48+
49+
add_entrypoint_object(
50+
aligned_alloc
51+
SRCS
52+
aligned_alloc.cpp
53+
HDRS
54+
../aligned_alloc.h
55+
DEPENDS
56+
libc.src.__support.freelist_heap
57+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation for freelist_malloc --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/freelist_heap.h"
10+
#include "src/__support/macros/config.h"
11+
#include "src/stdlib/aligned_alloc.h"
12+
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
18+
return freelist_heap->aligned_allocate(alignment, size);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation for freelist_malloc --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/freelist_heap.h"
10+
#include "src/__support/macros/config.h"
11+
#include "src/stdlib/calloc.h"
12+
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
18+
return freelist_heap->calloc(num, size);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/baremetal/free.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation for freelist_malloc --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/freelist_heap.h"
10+
#include "src/__support/macros/config.h"
11+
#include "src/stdlib/free.h"
12+
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
18+
19+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)