[libc-commits] [libc] [libc] Use best-fit binary trie to make malloc logarithmic (PR #106259)
via libc-commits
libc-commits at lists.llvm.org
Thu Oct 10 10:41:14 PDT 2024
================
@@ -0,0 +1,108 @@
+//===-- Interface for freestore ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_FREESTORE_H
+#define LLVM_LIBC_SRC___SUPPORT_FREESTORE_H
+
+#include "freetrie.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+/// A best-fit store of variously-sized free blocks. Blocks can be inserted and
+/// removed in logarithmic time.
+class FreeStore {
+public:
+ /// Sets the range of possible block sizes. This can only be called when the
+ /// trie is empty.
+ void set_range(FreeTrie::SizeRange range) { large_trie.set_range(range); }
+
+ /// Insert a free block. If the block is too small to be tracked, nothing
+ /// happens.
+ void insert(Block<> *block);
+
+ /// Remove a free block. If the block is too small to be tracked, nothing
+ /// happens.
+ void remove(Block<> *block);
+
+ /// Remove a best-fit free block that can contain the given size when
+ /// allocated. Returns nullptr if there is no such block.
+ Block<> *remove_best_fit(size_t size);
+
+private:
+ static constexpr size_t ALIGNMENT = alignof(max_align_t);
+ static constexpr size_t MIN_OUTER_SIZE =
+ align_up(Block<>::BLOCK_OVERHEAD + sizeof(FreeList::Node), ALIGNMENT);
+ static constexpr size_t MIN_LARGE_OUTER_SIZE =
+ align_up(Block<>::BLOCK_OVERHEAD + sizeof(FreeTrie::Node), ALIGNMENT);
+ static constexpr size_t NUM_SMALL_SIZES =
+ (MIN_LARGE_OUTER_SIZE - MIN_OUTER_SIZE) / ALIGNMENT;
----------------
nopsledder wrote:
Maybe nothing to do here, but I was confused as how this was anything other than 1 until I realized that `FreeTrie::Node` IS-A `FreeList::Node`.
https://github.com/llvm/llvm-project/pull/106259
More information about the libc-commits
mailing list