Skip to content

Commit 379cc44

Browse files
authored
Revert "[libc] Breakup freelist_malloc into separate files" (#119749)
Reverts #98784 which broke libc builders.
1 parent 88bcf72 commit 379cc44

File tree

15 files changed

+110
-173
lines changed

15 files changed

+110
-173
lines changed

libc/config/baremetal/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ 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
187188
libc.src.stdlib.labs
188189
libc.src.stdlib.ldiv
189190
libc.src.stdlib.llabs

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ 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
187188
libc.src.stdlib.labs
188189
libc.src.stdlib.ldiv
189190
libc.src.stdlib.llabs

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ 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
183184
libc.src.stdlib.labs
184185
libc.src.stdlib.ldiv
185186
libc.src.stdlib.llabs

libc/src/__support/CMakeLists.txt

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

51-
add_object_library(
51+
add_header_library(
5252
freelist_heap
53-
SRCS
54-
freelist_heap.cpp
5553
HDRS
5654
freelist_heap.h
57-
COMPILE_OPTIONS
58-
-DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
5955
DEPENDS
6056
.block
61-
.freelist
6257
.freestore
63-
.freetrie
6458
libc.src.__support.CPP.cstddef
6559
libc.src.__support.CPP.array
6660
libc.src.__support.CPP.optional

libc/src/__support/freelist_heap.cpp

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

libc/src/stdlib/CMakeLists.txt

Lines changed: 28 additions & 10 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_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
326+
if(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_BAREMETAL AND 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,8 +389,32 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND 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+
392416
add_entrypoint_external(
393-
malloc
417+
free
394418
)
395419
add_entrypoint_external(
396420
calloc
@@ -401,12 +425,6 @@ if(NOT LIBC_TARGET_OS_IS_BAREMETAL AND NOT LIBC_TARGET_OS_IS_GPU)
401425
add_entrypoint_external(
402426
aligned_alloc
403427
)
404-
add_entrypoint_external(
405-
free
406-
)
407-
add_entrypoint_external(
408-
mallopt
409-
)
410428
endif()
411429
endif()
412430

@@ -495,7 +513,7 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
495513
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
496514
endif()
497515

498-
if(LIBC_TARGET_OS_IS_BAREMETAL OR LIBC_TARGET_OS_IS_GPU)
516+
if(LIBC_TARGET_OS_IS_GPU)
499517
add_entrypoint_object(
500518
malloc
501519
ALIAS

libc/src/stdlib/baremetal/CMakeLists.txt

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,3 @@ 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/baremetal/calloc.cpp

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

libc/src/stdlib/baremetal/free.cpp

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

libc/src/stdlib/baremetal/malloc.cpp

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

libc/src/stdlib/baremetal/realloc.cpp

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

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

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

9-
#include "src/stdlib/aligned_alloc.h"
109
#include "src/__support/freelist_heap.h"
1110
#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"
1216

1317
#include <stddef.h>
1418

1519
namespace LIBC_NAMESPACE_DECL {
1620

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+
1738
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
1839
return freelist_heap->aligned_allocate(alignment, size);
1940
}

libc/test/src/__support/CMakeLists.txt

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

libc/test/src/__support/freelist_heap_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
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"
1612
#include "src/string/memcmp.h"
1713
#include "src/string/memcpy.h"
1814
#include "test/UnitTest/Test.h"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===-- Unittests 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/stdlib/aligned_alloc.h"
11+
#include "src/stdlib/calloc.h"
12+
#include "src/stdlib/free.h"
13+
#include "src/stdlib/malloc.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
using LIBC_NAMESPACE::Block;
17+
using LIBC_NAMESPACE::freelist_heap;
18+
using LIBC_NAMESPACE::FreeListHeap;
19+
using LIBC_NAMESPACE::FreeListHeapBuffer;
20+
21+
TEST(LlvmLibcFreeListMalloc, Malloc) {
22+
constexpr size_t kAllocSize = 256;
23+
constexpr size_t kCallocNum = 4;
24+
constexpr size_t kCallocSize = 64;
25+
26+
void *ptr1 = LIBC_NAMESPACE::malloc(kAllocSize);
27+
auto *block = Block::from_usable_space(ptr1);
28+
EXPECT_GE(block->inner_size(), kAllocSize);
29+
30+
LIBC_NAMESPACE::free(ptr1);
31+
ASSERT_NE(block->next(), static_cast<Block *>(nullptr));
32+
ASSERT_EQ(block->next()->next(), static_cast<Block *>(nullptr));
33+
size_t heap_size = block->inner_size();
34+
35+
void *ptr2 = LIBC_NAMESPACE::calloc(kCallocNum, kCallocSize);
36+
ASSERT_EQ(ptr2, ptr1);
37+
EXPECT_GE(block->inner_size(), kCallocNum * kCallocSize);
38+
39+
for (size_t i = 0; i < kCallocNum * kCallocSize; ++i)
40+
EXPECT_EQ(reinterpret_cast<uint8_t *>(ptr2)[i], uint8_t(0));
41+
42+
LIBC_NAMESPACE::free(ptr2);
43+
EXPECT_EQ(block->inner_size(), heap_size);
44+
45+
constexpr size_t ALIGN = kAllocSize;
46+
void *ptr3 = LIBC_NAMESPACE::aligned_alloc(ALIGN, kAllocSize);
47+
EXPECT_NE(ptr3, static_cast<void *>(nullptr));
48+
EXPECT_EQ(reinterpret_cast<uintptr_t>(ptr3) % ALIGN, size_t(0));
49+
auto *aligned_block = reinterpret_cast<Block *>(ptr3);
50+
EXPECT_GE(aligned_block->inner_size(), kAllocSize);
51+
52+
LIBC_NAMESPACE::free(ptr3);
53+
EXPECT_EQ(block->inner_size(), heap_size);
54+
}

0 commit comments

Comments
 (0)