Skip to content

Commit 4e2a9e5

Browse files
authored
[libc] Breakup freelist_malloc into separate files (#98784)
This better matches the structure we use for the rest of libc.
1 parent f0f8434 commit 4e2a9e5

File tree

15 files changed

+173
-110
lines changed

15 files changed

+173
-110
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/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
)

libc/test/src/__support/freelist_heap_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "src/__support/CPP/span.h"
1010
#include "src/__support/freelist_heap.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/stdlib/aligned_alloc.h"
13+
#include "src/stdlib/calloc.h"
14+
#include "src/stdlib/free.h"
15+
#include "src/stdlib/malloc.h"
1216
#include "src/string/memcmp.h"
1317
#include "src/string/memcpy.h"
1418
#include "test/UnitTest/Test.h"

libc/test/src/__support/freelist_malloc_test.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)