Skip to content

Commit

Permalink
options/internal: Implement get_size in the debug allocator
Browse files Browse the repository at this point in the history
Also document the option to enable it in README.
  • Loading branch information
qookei authored and Dennisbonke committed Sep 20, 2024
1 parent cdd196f commit 78cebda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following custom meson options are accepted, in addition to the [built-in op
- `build_tests`: Build the test suite (see below).
- `disable_x_option`: Disable `x` component of mlibc functionality. See `meson_options.txt` for a full list of possible values for `x`. This may be used to e.g disable POSIX and glibc extensions.
- `linux_kernel_headers`: Allows for directing mlibc to installed linux headers. [These can be obtained easily](https://docs.kernel.org/kbuild/headers_install.html), placed in a directory and this option set to the corresponding path. This is required if the linux option is enabled, i.e. when the linux option is not disabled.
- `debug_allocator`: Replace the normal allocator with a debug allocator (see `mlibc/options/internal/generic/allocator.cpp` for implementation details).
- `use_freestnd_hdrs`: Use freestnd-c{,xx}-hdrs instead of looking for compiler headers. Useful if not using a compiler with the correct target triple.

The type of library to be built (static, shared, or both) is controlled by meson's `default_library` option. Passing `-Ddefault_library=static` effectively disables the dynamic linker.
Expand Down
13 changes: 13 additions & 0 deletions options/internal/generic/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ void *MemoryAllocator::reallocate(void *ptr, size_t size) {
return newArea;
}

size_t MemoryAllocator::get_size(void *ptr) {
if constexpr (logAllocations)
mlibc::infoLogger() << "MemoryAllocator::get_size(" << ptr << ")" << frg::endlog;

uintptr_t page_addr = reinterpret_cast<uintptr_t>(ptr) & ~size_t{pageSize - 1};
AllocatorMeta *meta = reinterpret_cast<AllocatorMeta *>(page_addr - pageSize);

if (meta->magic != allocatorMagic)
mlibc::panicLogger() << "Invalid allocator metadata magic in MemoryAllocator::get_size" << frg::endlog;

return meta->allocatedSize;
}

MemoryAllocator &getAllocator() {
// use frg::eternal to prevent a call to __cxa_atexit().
// this is necessary because __cxa_atexit() call this function.
Expand Down
1 change: 1 addition & 0 deletions options/internal/include/mlibc/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct MemoryAllocator {
void free(void *ptr);
void deallocate(void *ptr, size_t size);
void *reallocate(void *ptr, size_t size);
size_t get_size(void *ptr);
};

MemoryAllocator &getAllocator();
Expand Down

0 comments on commit 78cebda

Please sign in to comment.