Skip to content

Commit b1c2777

Browse files
committed
[WIP][libc] Add freelist malloc
1 parent 2e0e163 commit b1c2777

18 files changed

+2053
-18
lines changed

libc/config/baremetal/riscv/entrypoints.txt

+1
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/type_traits.h

+1
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"
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

+20
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

+13-1
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,20 @@ elseif(LIBC_TARGET_OS_IS_GPU)
379379
aligned_alloc
380380
)
381381
else()
382-
add_entrypoint_external(
382+
add_entrypoint_object(
383383
malloc
384+
SRCS
385+
freelist_malloc.cpp
386+
HDRS
387+
malloc.h
388+
DEPENDS
389+
libc.src.__support.CPP.new
390+
libc.src.__support.CPP.optional
391+
libc.src.__support.CPP.span
392+
libc.src.__support.CPP.type_traits
393+
libc.src.__support.fixedvector
394+
libc.src.string.memcpy
395+
libc.src.string.memset
384396
)
385397
add_entrypoint_external(
386398
free

0 commit comments

Comments
 (0)