Skip to content

Commit 7bf3137

Browse files
authoredDec 16, 2024
[libc] Breakup freelist_malloc into separate files (#119806)
This better matches the structure we use for the rest of libc.
1 parent 0a7e048 commit 7bf3137

File tree

17 files changed

+204
-119
lines changed

17 files changed

+204
-119
lines changed
 

‎libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ set(TARGET_LIBC_ENTRYPOINTS
184184
libc.src.stdlib.div
185185
libc.src.stdlib.exit
186186
libc.src.stdlib.free
187-
libc.src.stdlib.freelist_malloc
188187
libc.src.stdlib.labs
189188
libc.src.stdlib.ldiv
190189
libc.src.stdlib.llabs

‎libc/config/baremetal/arm/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ set(TARGET_LIBC_ENTRYPOINTS
184184
libc.src.stdlib.div
185185
libc.src.stdlib.exit
186186
libc.src.stdlib.free
187-
libc.src.stdlib.freelist_malloc
188187
libc.src.stdlib.labs
189188
libc.src.stdlib.ldiv
190189
libc.src.stdlib.llabs

‎libc/config/baremetal/riscv/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/fuzzing/__support/CMakeLists.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,21 @@ add_libc_fuzzer(
2424
-D__LIBC_EXPLICIT_SIMD_OPT
2525
)
2626

27-
add_libc_fuzzer(
28-
freelist_heap_fuzz
29-
SRCS
30-
freelist_heap_fuzz.cpp
31-
DEPENDS
32-
libc.src.__support.freelist_heap
33-
)
27+
if(LLVM_LIBC_FULL_BUILD)
28+
add_libc_fuzzer(
29+
freelist_heap_fuzz
30+
SRCS
31+
fake_heap.s
32+
freelist_heap_fuzz.cpp
33+
DEPENDS
34+
libc.src.__support.freelist_heap
35+
)
36+
# TODO(#119995): Remove this once sccache on Windows no longer requires
37+
# the use of -DCMAKE_MSVC_DEBUG_INFORMATION_FORMAT=Embedded.
38+
get_fq_target_name(freelist_heap_fuzz freelist_heap_fuzz_target_name)
39+
set_target_properties(
40+
${freelist_heap_fuzz_target_name}
41+
PROPERTIES
42+
MSVC_DEBUG_INFORMATION_FORMAT ""
43+
)
44+
endif()

‎libc/fuzzing/__support/fake_heap.s

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Test fake definition for heap symbols -----------------------------===//
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+
.globl _end, __llvm_libc_heap_limit
10+
11+
.bss
12+
_end:
13+
.fill 1024
14+
__llvm_libc_heap_limit:
15+

‎libc/src/__support/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,19 @@ add_header_library(
4848
.freetrie
4949
)
5050

51-
add_header_library(
51+
add_object_library(
5252
freelist_heap
53+
SRCS
54+
freelist_heap.cpp
5355
HDRS
5456
freelist_heap.h
57+
COMPILE_OPTIONS
58+
-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
5559
DEPENDS
5660
.block
61+
.freelist
5762
.freestore
63+
.freetrie
5864
libc.src.__support.CPP.cstddef
5965
libc.src.__support.CPP.array
6066
libc.src.__support.CPP.optional

‎libc/src/__support/freelist_heap.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation for freelist_heap ----------------------------------===//
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+
12+
#include <stddef.h>
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
17+
FreeListHeap *freelist_heap = &freelist_heap_symbols;
18+
19+
} // namespace LIBC_NAMESPACE_DECL

‎libc/src/stdlib/CMakeLists.txt

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ add_entrypoint_object(
323323
.rand_util
324324
)
325325

326-
if(NOT LIBC_TARGET_OS_IS_GPU)
326+
if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
327327
if(LLVM_LIBC_INCLUDE_SCUDO)
328328
set(SCUDO_DEPS "")
329329

@@ -349,7 +349,7 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
349349

350350
list(APPEND SCUDO_DEPS RTScudoStandalone.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
351351
RTScudoStandaloneCWrappers.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO})
352-
352+
353353
if (COMPILER_RT_BUILD_GWP_ASAN)
354354
list(APPEND SCUDO_DEPS
355355
RTGwpAsan.${LIBC_TARGET_ARCHITECTURE_FOR_SCUDO}
@@ -389,32 +389,8 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
389389
${SCUDO_DEPS}
390390
)
391391
else()
392-
# Only use freelist malloc for baremetal targets.
393-
add_entrypoint_object(
394-
freelist_malloc
395-
SRCS
396-
freelist_malloc.cpp
397-
HDRS
398-
malloc.h
399-
DEPENDS
400-
libc.src.__support.freelist_heap
401-
)
402-
get_target_property(freelist_malloc_is_skipped libc.src.stdlib.freelist_malloc "SKIPPED")
403-
if(LIBC_TARGET_OS_IS_BAREMETAL AND NOT freelist_malloc_is_skipped)
404-
add_entrypoint_object(
405-
malloc
406-
ALIAS
407-
DEPENDS
408-
.freelist_malloc
409-
)
410-
else()
411-
add_entrypoint_external(
412-
malloc
413-
)
414-
endif()
415-
416392
add_entrypoint_external(
417-
free
393+
malloc
418394
)
419395
add_entrypoint_external(
420396
calloc
@@ -425,6 +401,12 @@ if(NOT LIBC_TARGET_OS_IS_GPU)
425401
add_entrypoint_external(
426402
aligned_alloc
427403
)
404+
add_entrypoint_external(
405+
free
406+
)
407+
add_entrypoint_external(
408+
mallopt
409+
)
428410
endif()
429411
endif()
430412

@@ -513,7 +495,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
513495
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
514496
endif()
515497

516-
if(LIBC_TARGET_OS_IS_GPU)
498+
if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
517499
add_entrypoint_object(
518500
malloc
519501
ALIAS

‎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+
)

‎libc/src/stdlib/freelist_malloc.cpp renamed to ‎libc/src/stdlib/baremetal/aligned_alloc.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/stdlib/aligned_alloc.h"
910
#include "src/__support/freelist_heap.h"
1011
#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"
1612

1713
#include <stddef.h>
1814

1915
namespace LIBC_NAMESPACE_DECL {
2016

21-
static LIBC_CONSTINIT FreeListHeap freelist_heap_symbols;
22-
FreeListHeap *freelist_heap = &freelist_heap_symbols;
23-
24-
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
25-
return freelist_heap->allocate(size);
26-
}
27-
28-
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { return freelist_heap->free(ptr); }
29-
30-
LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
31-
return freelist_heap->calloc(num, size);
32-
}
33-
34-
LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
35-
return freelist_heap->realloc(ptr, size);
36-
}
37-
3817
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
3918
return freelist_heap->aligned_allocate(alignment, size);
4019
}

‎libc/src/stdlib/baremetal/calloc.cpp

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/stdlib/calloc.h"
10+
#include "src/__support/freelist_heap.h"
11+
#include "src/__support/macros/config.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/stdlib/free.h"
10+
#include "src/__support/freelist_heap.h"
11+
#include "src/__support/macros/config.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

‎libc/src/stdlib/baremetal/malloc.cpp

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/stdlib/malloc.h"
10+
#include "src/__support/freelist_heap.h"
11+
#include "src/__support/macros/config.h"
12+
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
18+
return freelist_heap->allocate(size);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL

‎libc/src/stdlib/baremetal/realloc.cpp

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/stdlib/realloc.h"
10+
#include "src/__support/freelist_heap.h"
11+
#include "src/__support/macros/config.h"
12+
13+
#include <stddef.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
18+
return freelist_heap->realloc(ptr, size);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL

‎libc/test/src/__support/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ if(LLVM_LIBC_FULL_BUILD)
6363
SRCS
6464
fake_heap.s
6565
freelist_heap_test.cpp
66-
freelist_malloc_test.cpp
6766
DEPENDS
6867
libc.src.__support.CPP.span
6968
libc.src.__support.freelist_heap
70-
libc.src.stdlib.freelist_malloc
7169
libc.src.string.memcmp
7270
libc.src.string.memcpy
7371
)

0 commit comments

Comments
 (0)