Skip to content

Commit 7544719

Browse files
committed
[WIP][libc] Add freelist malloc
1 parent 5bec47c commit 7544719

22 files changed

+1728
-20
lines changed

clang/cmake/caches/Fuchsia-stage2.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
189189
set(RUNTIMES_${target}_SANITIZER_TEST_CXX "libc++" CACHE STRING "")
190190
set(RUNTIMES_${target}_SANITIZER_TEST_CXX_INTREE ON CACHE BOOL "")
191191
set(RUNTIMES_${target}_LLVM_TOOLS_DIR "${CMAKE_BINARY_DIR}/bin" CACHE BOOL "")
192-
set(RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind" CACHE STRING "")
192+
set(RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind;libc" CACHE STRING "")
193193

194194
# Use .build-id link.
195195
list(APPEND RUNTIME_BUILD_ID_LINK "${target}")

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ set(TARGET_LIBC_ENTRYPOINTS
170170
libc.src.stdlib.ldiv
171171
libc.src.stdlib.llabs
172172
libc.src.stdlib.lldiv
173+
libc.src.stdlib.malloc
173174
libc.src.stdlib.qsort
174175
libc.src.stdlib.rand
175176
libc.src.stdlib.srand

libc/src/__support/CPP/algorithm.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
2525
return (a < b) ? a : b;
2626
}
2727

28+
template <class InputIt, class UnaryPred>
29+
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q) {
30+
for (; first != last; ++first)
31+
if (!q(*first))
32+
return first;
33+
34+
return last;
35+
}
36+
37+
template <class InputIt, class UnaryPred>
38+
constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
39+
return find_if_not(first, last, p) == last;
40+
}
41+
2842
} // namespace cpp
2943
} // namespace LIBC_NAMESPACE
3044

libc/src/__support/CPP/cstddef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace LIBC_NAMESPACE::cpp {
1616

1717
enum class byte : unsigned char {};
1818

19+
using ::max_align_t __attribute__((__using_if_exists__));
20+
1921
template <class IntegerType>
2022
LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
2123
operator>>(byte b, IntegerType shift) noexcept {

libc/src/__support/CPP/type_traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/CPP/type_traits/add_lvalue_reference.h"
1313
#include "src/__support/CPP/type_traits/add_pointer.h"
1414
#include "src/__support/CPP/type_traits/add_rvalue_reference.h"
15+
#include "src/__support/CPP/type_traits/aligned_storage.h"
1516
#include "src/__support/CPP/type_traits/bool_constant.h"
1617
#include "src/__support/CPP/type_traits/conditional.h"
1718
#include "src/__support/CPP/type_traits/decay.h"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- aligned_storage type_traits --------------------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H
10+
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H
11+
12+
#include <stddef.h> // size_t
13+
14+
namespace LIBC_NAMESPACE::cpp {
15+
16+
template <size_t Len, size_t Align> struct aligned_storage {
17+
struct type {
18+
alignas(Align) unsigned char data[Len];
19+
};
20+
};
21+
22+
template <size_t Len, size_t Align>
23+
using aligned_storage_t = typename aligned_storage<Len, Align>::type;
24+
25+
} // namespace LIBC_NAMESPACE::cpp
26+
27+
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H

libc/src/__support/fixedvector.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ template <typename T, size_t CAPACITY> class FixedVector {
2424
public:
2525
constexpr FixedVector() = default;
2626

27+
template <typename It> FixedVector(It begin, It end) {
28+
for (; begin != end; ++begin) {
29+
push_back(*begin);
30+
}
31+
}
32+
33+
FixedVector(size_t count, const T &value) {
34+
for (size_t i = 0; i < count; ++i) {
35+
push_back(value);
36+
}
37+
}
38+
2739
bool push_back(const T &obj) {
2840
if (item_count == CAPACITY)
2941
return false;
@@ -36,6 +48,9 @@ template <typename T, size_t CAPACITY> class FixedVector {
3648

3749
T &back() { return store[item_count - 1]; }
3850

51+
T &operator[](size_t idx) { return store[idx]; }
52+
const T &operator[](size_t idx) const { return store[idx]; }
53+
3954
bool pop_back() {
4055
if (item_count == 0)
4156
return false;
@@ -44,6 +59,7 @@ template <typename T, size_t CAPACITY> class FixedVector {
4459
}
4560

4661
bool empty() const { return item_count == 0; }
62+
size_t size() const { return item_count; }
4763

4864
// Empties the store for all practical purposes.
4965
void reset() { item_count = 0; }
@@ -63,6 +79,10 @@ template <typename T, size_t CAPACITY> class FixedVector {
6379
return reverse_iterator{&store[item_count]};
6480
}
6581
LIBC_INLINE constexpr reverse_iterator rend() { return store.rend(); }
82+
83+
using iterator = typename cpp::array<T, CAPACITY>::iterator;
84+
LIBC_INLINE constexpr iterator begin() { return store.begin(); }
85+
LIBC_INLINE constexpr iterator end() { return iterator{&store[item_count]}; }
6686
};
6787

6888
} // namespace LIBC_NAMESPACE

libc/src/stdlib/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,19 @@ elseif(LIBC_TARGET_OS_IS_GPU)
369369
aligned_alloc
370370
)
371371
else()
372-
add_entrypoint_external(
372+
add_entrypoint_object(
373373
malloc
374+
SRCS
375+
freelist_malloc.cpp
376+
HDRS
377+
malloc.h
378+
DEPENDS
379+
libc.src.__support.CPP.optional
380+
libc.src.__support.CPP.span
381+
libc.src.__support.CPP.type_traits
382+
libc.src.__support.fixedvector
383+
libc.src.string.memcpy
384+
libc.src.string.memset
374385
)
375386
add_entrypoint_external(
376387
free

0 commit comments

Comments
 (0)