-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc] add basic arena allocator #121173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RossComputerGuy
wants to merge
2
commits into
llvm:main
Choose a base branch
from
RossComputerGuy:feat/libc-alloc1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+720
−11
Open
[libc] add basic arena allocator #121173
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,3 +370,5 @@ add_subdirectory(HashTable) | |
add_subdirectory(fixed_point) | ||
|
||
add_subdirectory(time) | ||
|
||
add_subdirectory(alloc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
add_object_library( | ||
base | ||
SRCS | ||
base.cpp | ||
HDRS | ||
base.h | ||
DEPENDS | ||
libc.hdr.types.size_t | ||
libc.src.__support.macros.config | ||
) | ||
|
||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
add_subdirectory(${LIBC_TARGET_OS}) | ||
endif() | ||
|
||
if(TARGET libc.src.__support.alloc.${LIBC_TARGET_OS}.page) | ||
add_object_library( | ||
page | ||
ALIAS | ||
DEPENDS | ||
.${LIBC_TARGET_OS}.page | ||
) | ||
|
||
add_object_library( | ||
arena | ||
SRCS | ||
arena.cpp | ||
HDRS | ||
arena.h | ||
COMPILE_OPTIONS | ||
-DLIBC_PAGE_SIZE=${LIBC_CONF_PAGE_SIZE} | ||
DEPENDS | ||
.base | ||
.page | ||
libc.src.string.memmove | ||
libc.src.unistd.getpagesize | ||
) | ||
endif() | ||
|
||
if(NOT ${LIBC_CONF_ALLOC_TYPE} MATCHES "LIBC_ALLOC_TYPE_SCUDO" AND NOT ${LIBC_CONF_ALLOC_TYPE} MATCHES "LIBC_ALLOC_TYPE_EXTERN") | ||
string(TOLOWER ${LIBC_CONF_ALLOC_TYPE} LIBC_CONF_ALLOC_TYPE_NAME) | ||
string(REPLACE "libc_alloc_type_" "" LIBC_CONF_ALLOC_TYPE_NAME "${LIBC_CONF_ALLOC_TYPE_NAME}") | ||
add_object_library( | ||
alloc | ||
SRCS | ||
alloc.cpp | ||
HDRS | ||
alloc.h | ||
COMPILE_OPTIONS | ||
-DLIBC_CONF_ALLOC_TYPE=${LIBC_CONF_ALLOC_TYPE_NAME} | ||
DEPENDS | ||
.${LIBC_CONF_ALLOC_TYPE_NAME} | ||
) | ||
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <src/__support/alloc/alloc.h> | ||
#include <src/__support/alloc/arena.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
#define CONCAT(a, b) a##b | ||
#define EXPAND_AND_CONCAT(a, b) CONCAT(a, b) | ||
|
||
#define ALLOCATOR EXPAND_AND_CONCAT(LIBC_CONF_ALLOC_TYPE, _allocator) | ||
|
||
BaseAllocator *allocator = reinterpret_cast<BaseAllocator *>(&ALLOCATOR); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- libc-wide allocator -------------------------------------*- C++ -*-===// | ||
// | ||
// 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_ALLOC_ALLOC_H | ||
#define LLVM_LIBC_SRC___SUPPORT_ALLOC_ALLOC_H | ||
|
||
#include "src/__support/alloc/base.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
// The primary allocator to use | ||
extern BaseAllocator *allocator; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "src/__support/alloc/arena.h" | ||
#include "src/__support/alloc/page.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/page_size.h" | ||
#include "src/__support/memory_size.h" | ||
#include "src/string/memmove.h" | ||
#include "src/unistd/getpagesize.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
void *arena_allocate(BaseAllocator *base, size_t alignment, size_t size) { | ||
ArenaAllocator *self = reinterpret_cast<ArenaAllocator *>(base); | ||
|
||
if (self->buffer == nullptr) { | ||
self->buffer = reinterpret_cast<uint8_t *>(page_allocate(1)); | ||
self->buffer_size = self->get_page_size(); | ||
} | ||
|
||
uintptr_t curr_ptr = (uintptr_t)self->buffer + (uintptr_t)self->curr_offset; | ||
uintptr_t offset = internal::align_forward<uintptr_t>(curr_ptr, alignment); | ||
offset -= (uintptr_t)self->buffer; | ||
|
||
if (offset + size > self->buffer_size) { | ||
self->buffer = reinterpret_cast<uint8_t *>( | ||
page_expand(self->buffer, self->buffer_size / self->get_page_size())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, woops. I'll fix this when I can. |
||
self->buffer_size += self->get_page_size(); | ||
} | ||
|
||
if (offset + size <= self->buffer_size) { | ||
void *ptr = &self->buffer[offset]; | ||
self->prev_offset = offset; | ||
self->curr_offset = offset + size; | ||
return ptr; | ||
} | ||
return nullptr; | ||
} | ||
|
||
void *arena_expand(BaseAllocator *base, void *ptr, size_t alignment, | ||
size_t size) { | ||
ArenaAllocator *self = reinterpret_cast<ArenaAllocator *>(base); | ||
|
||
if (self->buffer + self->prev_offset == ptr) { | ||
self->curr_offset = self->prev_offset + size; | ||
return ptr; | ||
} else { | ||
void *new_mem = arena_allocate(base, alignment, size); | ||
memmove(new_mem, ptr, size); | ||
return new_mem; | ||
} | ||
return nullptr; | ||
} | ||
|
||
bool arena_free(BaseAllocator *base, void *ptr) { | ||
(void)base; | ||
(void)ptr; | ||
return true; | ||
} | ||
|
||
size_t ArenaAllocator::get_page_size() { | ||
if (page_size == LIBC_PAGE_SIZE_SYSTEM) { | ||
page_size = getpagesize(); | ||
} | ||
return page_size; | ||
} | ||
|
||
static ArenaAllocator default_arena_allocator(LIBC_PAGE_SIZE, | ||
2 * sizeof(void *)); | ||
BaseAllocator *arena_allocator = &default_arena_allocator; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===-- An arena allocator using pages. -------------------------*- C++ -*-===// | ||
// | ||
// 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_ALLOC_ARENA_H | ||
#define LLVM_LIBC_SRC___SUPPORT_ALLOC_ARENA_H | ||
|
||
#include "hdr/types/size_t.h" | ||
#include "src/__support/alloc/base.h" | ||
#include <stdint.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
void *arena_allocate(BaseAllocator *base, size_t alignment, size_t size); | ||
void *arena_expand(BaseAllocator *base, void *ptr, size_t alignment, | ||
size_t size); | ||
bool arena_free(BaseAllocator *base, void *ptr); | ||
|
||
class ArenaAllocator : public BaseAllocator { | ||
public: | ||
uint8_t *buffer; | ||
size_t buffer_size; | ||
size_t prev_offset; | ||
size_t curr_offset; | ||
|
||
private: | ||
size_t page_size; | ||
|
||
public: | ||
constexpr ArenaAllocator(size_t page_size, size_t default_alignment) | ||
: BaseAllocator(arena_allocate, arena_expand, arena_free, | ||
default_alignment), | ||
buffer(nullptr), buffer_size(0), prev_offset(0), curr_offset(0), | ||
page_size(page_size) {} | ||
|
||
size_t get_page_size(); | ||
}; | ||
|
||
extern BaseAllocator *arena_allocator; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_object_library( | ||
page | ||
SRCS | ||
page.cpp | ||
HDRS | ||
../page.h | ||
DEPENDS | ||
libc.src.__support.alloc.base | ||
libc.src.sys.mman.mmap | ||
libc.src.sys.mman.mremap | ||
libc.src.sys.mman.munmap | ||
libc.src.unistd.getpagesize | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "src/__support/alloc/page.h" | ||
#include "src/__suport/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
extern "C" void *__llvm_libc_page_allocate(size_t n_pages); | ||
extern "C" void *__llvm_libc_page_expand(void *ptr, size_t n_pages); | ||
extern "C" bool __llvm_libc_page_free(void *ptr, size_t n_pages); | ||
|
||
void *page_allocate(size_t n_pages) { | ||
return __llvm_libc_page_allocate(n_pages); | ||
} | ||
|
||
void *page_expand(void *ptr, size_t n_pages) { | ||
return __llvm_libc_page_expand(ptr, n_pages); | ||
} | ||
|
||
bool page_free(void *ptr, size_t n_pages) { | ||
return __llvm_libc_page_free(ptr, n_pages); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "src/__support/alloc/base.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
void *BaseAllocator::alloc(size_t alignment, size_t size) { | ||
return impl_alloc(this, alignment, size); | ||
} | ||
|
||
void *BaseAllocator::expand(void *ptr, size_t alignment, size_t size) { | ||
return impl_expand(this, ptr, alignment, size); | ||
} | ||
|
||
bool BaseAllocator::free(void *ptr) { return impl_free(this, ptr); } | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this interact with the existing embedded allocator present in the libc? Immediately it's unusual that it isn't present in a list of allocators.
The allocator doesn't currently work for Linux, but that's something I'm planning to fix in the near future. It's just for lack of a
__morecore
abstraction hooked up to e.g.sbrk
, plus the plumbing for that sycall.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with existing allocators being implemented. I could hook them up to support the option and general interface.