|
| 1 | +//===-- Interface 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 | +#ifndef LLVM_LIBC_SRC_STDLIB_FREELIST_HEAP_H |
| 10 | +#define LLVM_LIBC_SRC_STDLIB_FREELIST_HEAP_H |
| 11 | + |
| 12 | +#include <stddef.h> |
| 13 | + |
| 14 | +#include "block.h" |
| 15 | +#include "freelist.h" |
| 16 | +#include "src/__support/CPP/optional.h" |
| 17 | +#include "src/__support/CPP/span.h" |
| 18 | +#include "src/string/memcpy.h" |
| 19 | +#include "src/string/memset.h" |
| 20 | + |
| 21 | +namespace LIBC_NAMESPACE { |
| 22 | + |
| 23 | +void MallocInit(uint8_t *heap_low_addr, uint8_t *heap_high_addr); |
| 24 | + |
| 25 | +using cpp::optional; |
| 26 | +using cpp::span; |
| 27 | + |
| 28 | +static constexpr cpp::array<size_t, 6> defaultBuckets{16, 32, 64, |
| 29 | + 128, 256, 512}; |
| 30 | + |
| 31 | +template <size_t kNumBuckets = defaultBuckets.size()> class FreeListHeap { |
| 32 | +public: |
| 33 | + using BlockType = Block<>; |
| 34 | + |
| 35 | + template <size_t> friend class FreeListHeapBuffer; |
| 36 | + |
| 37 | + struct HeapStats { |
| 38 | + size_t total_bytes; |
| 39 | + size_t bytes_allocated; |
| 40 | + size_t cumulative_allocated; |
| 41 | + size_t cumulative_freed; |
| 42 | + size_t total_allocate_calls; |
| 43 | + size_t total_free_calls; |
| 44 | + }; |
| 45 | + FreeListHeap(span<cpp::byte> region); |
| 46 | + |
| 47 | + void *Allocate(size_t size); |
| 48 | + void Free(void *ptr); |
| 49 | + void *Realloc(void *ptr, size_t size); |
| 50 | + void *Calloc(size_t num, size_t size); |
| 51 | + |
| 52 | + void LogHeapStats(); |
| 53 | + const HeapStats &heap_stats() const { return heap_stats_; } |
| 54 | + |
| 55 | +private: |
| 56 | + span<cpp::byte> BlockToSpan(BlockType *block) { |
| 57 | + return span<cpp::byte>(block->usable_space(), block->inner_size()); |
| 58 | + } |
| 59 | + |
| 60 | + void InvalidFreeCrash() { __builtin_trap(); } |
| 61 | + |
| 62 | + span<cpp::byte> region_; |
| 63 | + FreeList<kNumBuckets> freelist_; |
| 64 | + HeapStats heap_stats_; |
| 65 | +}; |
| 66 | + |
| 67 | +template <size_t kNumBuckets> |
| 68 | +FreeListHeap<kNumBuckets>::FreeListHeap(span<cpp::byte> region) |
| 69 | + : freelist_(defaultBuckets), heap_stats_() { |
| 70 | + auto result = BlockType::init(region); |
| 71 | + BlockType *block = *result; |
| 72 | + |
| 73 | + freelist_.add_chunk(BlockToSpan(block)); |
| 74 | + |
| 75 | + region_ = region; |
| 76 | + heap_stats_.total_bytes = region.size(); |
| 77 | +} |
| 78 | + |
| 79 | +template <size_t kNumBuckets> |
| 80 | +void *FreeListHeap<kNumBuckets>::Allocate(size_t size) { |
| 81 | + // Find a chunk in the freelist. Split it if needed, then return |
| 82 | + |
| 83 | + auto chunk = freelist_.find_chunk(size); |
| 84 | + |
| 85 | + if (chunk.data() == nullptr) { |
| 86 | + return nullptr; |
| 87 | + } |
| 88 | + freelist_.remove_chunk(chunk); |
| 89 | + |
| 90 | + BlockType *chunk_block = BlockType::from_usable_space(chunk.data()); |
| 91 | + |
| 92 | + // Split that chunk. If there's a leftover chunk, add it to the freelist |
| 93 | + optional<BlockType *> result = BlockType::split(chunk_block, size); |
| 94 | + if (result) { |
| 95 | + freelist_.add_chunk(BlockToSpan(*result)); |
| 96 | + } |
| 97 | + |
| 98 | + chunk_block->mark_used(); |
| 99 | + |
| 100 | + heap_stats_.bytes_allocated += size; |
| 101 | + heap_stats_.cumulative_allocated += size; |
| 102 | + heap_stats_.total_allocate_calls += 1; |
| 103 | + |
| 104 | + return chunk_block->usable_space(); |
| 105 | +} |
| 106 | + |
| 107 | +template <size_t kNumBuckets> void FreeListHeap<kNumBuckets>::Free(void *ptr) { |
| 108 | + cpp::byte *bytes = static_cast<cpp::byte *>(ptr); |
| 109 | + |
| 110 | + if (bytes < region_.data() || bytes >= region_.data() + region_.size()) { |
| 111 | + InvalidFreeCrash(); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + BlockType *chunk_block = BlockType::from_usable_space(bytes); |
| 116 | + |
| 117 | + size_t size_freed = chunk_block->inner_size(); |
| 118 | + // Ensure that the block is in-use |
| 119 | + if (!chunk_block->used()) { |
| 120 | + InvalidFreeCrash(); |
| 121 | + return; |
| 122 | + } |
| 123 | + chunk_block->mark_free(); |
| 124 | + // Can we combine with the left or right blocks? |
| 125 | + BlockType *prev = chunk_block->prev(); |
| 126 | + BlockType *next = nullptr; |
| 127 | + |
| 128 | + if (!chunk_block->last()) { |
| 129 | + next = chunk_block->next(); |
| 130 | + } |
| 131 | + |
| 132 | + if (prev != nullptr && !prev->used()) { |
| 133 | + // Remove from freelist and merge |
| 134 | + freelist_.remove_chunk(BlockToSpan(prev)); |
| 135 | + chunk_block = chunk_block->prev(); |
| 136 | + BlockType::merge_next(chunk_block); |
| 137 | + } |
| 138 | + |
| 139 | + if (next != nullptr && !next->used()) { |
| 140 | + freelist_.remove_chunk(BlockToSpan(next)); |
| 141 | + BlockType::merge_next(chunk_block); |
| 142 | + } |
| 143 | + // Add back to the freelist |
| 144 | + freelist_.add_chunk(BlockToSpan(chunk_block)); |
| 145 | + |
| 146 | + heap_stats_.bytes_allocated -= size_freed; |
| 147 | + heap_stats_.cumulative_freed += size_freed; |
| 148 | + heap_stats_.total_free_calls += 1; |
| 149 | +} |
| 150 | + |
| 151 | +// Follows constract of the C standard realloc() function |
| 152 | +// If ptr is free'd, will return nullptr. |
| 153 | +template <size_t kNumBuckets> |
| 154 | +void *FreeListHeap<kNumBuckets>::Realloc(void *ptr, size_t size) { |
| 155 | + if (size == 0) { |
| 156 | + Free(ptr); |
| 157 | + return nullptr; |
| 158 | + } |
| 159 | + |
| 160 | + // If the pointer is nullptr, allocate a new memory. |
| 161 | + if (ptr == nullptr) { |
| 162 | + return Allocate(size); |
| 163 | + } |
| 164 | + |
| 165 | + cpp::byte *bytes = static_cast<cpp::byte *>(ptr); |
| 166 | + |
| 167 | + if (bytes < region_.data() || bytes >= region_.data() + region_.size()) { |
| 168 | + return nullptr; |
| 169 | + } |
| 170 | + |
| 171 | + BlockType *chunk_block = BlockType::from_usable_space(bytes); |
| 172 | + if (!chunk_block->used()) { |
| 173 | + return nullptr; |
| 174 | + } |
| 175 | + size_t old_size = chunk_block->inner_size(); |
| 176 | + |
| 177 | + // Do nothing and return ptr if the required memory size is smaller than |
| 178 | + // the current size. |
| 179 | + if (old_size >= size) { |
| 180 | + return ptr; |
| 181 | + } |
| 182 | + |
| 183 | + void *new_ptr = Allocate(size); |
| 184 | + // Don't invalidate ptr if Allocate(size) fails to initilize the memory. |
| 185 | + if (new_ptr == nullptr) { |
| 186 | + return nullptr; |
| 187 | + } |
| 188 | + memcpy(new_ptr, ptr, old_size); |
| 189 | + |
| 190 | + Free(ptr); |
| 191 | + return new_ptr; |
| 192 | +} |
| 193 | + |
| 194 | +template <size_t kNumBuckets> |
| 195 | +void *FreeListHeap<kNumBuckets>::Calloc(size_t num, size_t size) { |
| 196 | + void *ptr = Allocate(num * size); |
| 197 | + if (ptr != nullptr) { |
| 198 | + memset(ptr, 0, num * size); |
| 199 | + } |
| 200 | + return ptr; |
| 201 | +} |
| 202 | + |
| 203 | +extern FreeListHeap<> *freelist_heap; |
| 204 | + |
| 205 | +} // namespace LIBC_NAMESPACE |
| 206 | + |
| 207 | +#endif // LLVM_LIBC_SRC_STDLIB_FREELIST_HEAP_H |
0 commit comments