-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80079de
commit 19fab74
Showing
25 changed files
with
529 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,56 @@ | ||
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 | ||
) | ||
endif() | ||
|
||
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 | ||
) | ||
|
||
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}") | ||
if(TARGET libc.src.__support.alloc.${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() | ||
endif() |
This file contains 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 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 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())); | ||
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 *block_allocator = &default_arena_allocator; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains 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 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 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 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 |
This file contains 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,44 @@ | ||
//===-- A generic base 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_BASE_H | ||
#define LLVM_LIBC_SRC___SUPPORT_ALLOC_BASE_H | ||
|
||
#include "hdr/types/size_t.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
class BaseAllocator { | ||
public: | ||
using AllocFunc = void *(BaseAllocator *self, size_t, size_t); | ||
using ExpandFunc = void *(BaseAllocator *self, void *, size_t, size_t); | ||
using FreeFunc = bool(BaseAllocator *self, void *); | ||
|
||
private: | ||
// Implementation specific functions | ||
AllocFunc *impl_alloc; | ||
ExpandFunc *impl_expand; | ||
FreeFunc *impl_free; | ||
|
||
public: | ||
constexpr BaseAllocator(AllocFunc *ia, ExpandFunc *ie, FreeFunc *ifr, | ||
size_t default_alignment) | ||
: impl_alloc(ia), impl_expand(ie), impl_free(ifr), | ||
default_alignment(default_alignment) {} | ||
|
||
size_t default_alignment; | ||
|
||
void *alloc(size_t alignment, size_t size); | ||
void *expand(void *ptr, size_t alignment, size_t size); | ||
bool free(void *ptr); | ||
}; | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_ALLOC_BASE_H |
This file contains 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 @@ | ||
add_object_library( | ||
page | ||
SRCS | ||
page.cpp | ||
HDRS | ||
../page.h | ||
DEPENDS | ||
libc.hdr.types.size_t | ||
libc.include.llvm-libc-macros.stdlib_macros | ||
libc.include.llvm-libc-macros.stdint_macros | ||
libc.src.__support.alloc.base | ||
libc.src.sys.mman.mmap | ||
libc.src.sys.mman.mremap | ||
libc.src.sys.mman.munmap | ||
libc.src.unistd.getpagesize | ||
) |
Oops, something went wrong.