diff --git a/include/malloc.h b/include/malloc.h index fb39c0d..d4a0a59 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -26,7 +26,13 @@ __NODISCARD__ void *malloc(size_t) __NOEXCEPT__; __NODISCARD__ void *calloc(size_t count, size_t size) __NOEXCEPT__; __NODISCARD__ void *realloc(void *ptr, size_t size) __NOEXCEPT__; __NODISCARD__ void *aligned_alloc(size_t alignment, size_t size) __NOEXCEPT__; -void free(void *) __NOEXCEPT__; +__NODISCARD__ void *memalign(size_t alignment, size_t size) __NOEXCEPT__; +__NODISCARD__ size_t malloc_usable_size (void *ptr) __NOEXCEPT__ +__NODISCARD__ void *memalign(size_t alignment, size_t size) __NOEXCEPT__; +__NODISCARD__ void *pvalloc(size_t size) __NOEXCEPT__; +__NODISCARD__ void *valloc(size_t size) __NOEXCEPT__; +__NODISCARD__ int posix_memalign(void **memptr, size_t alignment, size_t size) __NOEXCEPT__; +__NODISCARD__ void free(void *) __NOEXCEPT__; #ifdef __cplusplus } // extern "C" diff --git a/src/malloc.cpp b/src/malloc.cpp index e65f5aa..5bd3094 100644 --- a/src/malloc.cpp +++ b/src/malloc.cpp @@ -91,4 +91,12 @@ void *aligned_alloc(std::size_t alignment, std::size_t size) noexcept { } } +std::size_t malloc_usable_size (void *ptr) noexcept{ + DEBUG_LOG("malloc_usable_size"); + if (ptr==nullptr){ + return 0; + } + return _allocator.dataSize(reinterpret_cast(ptr)); +} + } // namespace hse diff --git a/src/malloc.h b/src/malloc.h index f12b56a..d69be7f 100644 --- a/src/malloc.h +++ b/src/malloc.h @@ -10,6 +10,7 @@ namespace hse { [[nodiscard]] void* realloc(void *ptr, std::size_t size) noexcept; [[nodiscard]] void* aligned_alloc(std::size_t alignment, std::size_t size) noexcept; void free(void *) noexcept; +[[nodiscard]] std::size_t malloc_usable_size (void *ptr) noexcept; } // namespace hse diff --git a/src/malloc_std.cpp b/src/malloc_std.cpp index c9646f1..94543ab 100644 --- a/src/malloc_std.cpp +++ b/src/malloc_std.cpp @@ -1,3 +1,4 @@ +#include "system/system.h" #include "math/math.h" #include "memory/allocator.h" @@ -24,6 +25,25 @@ void *realloc(void *ptr, size_t size) noexcept { return hse::realloc(ptr, size); void *aligned_alloc(size_t alignment, size_t size) noexcept { return hse::aligned_alloc(alignment, size); } +void *memalign(size_t alignment, size_t size) noexcept { return aligned_alloc(alignment, size); } + +void *pvalloc(size_t size) noexcept { + std::size_t page_size = hse::system::PAGE_SIZE(); + return hse::aligned_alloc(page_size, hse::math::roundUp(size, page_size)); +} + +void *valloc(size_t size) noexcept { return pvalloc(size); } + +size_t malloc_usable_size (void *ptr) noexcept { return hse::malloc_usable_size(ptr);} + +int posix_memalign(void **memptr, size_t alignment, size_t size){ + void *result = aligned_alloc(alignment, size); + if(result==nullptr) // errors should be passed + return -1; + *memptr = result; + return 0; +} + } // extern "C" } // namespace std diff --git a/src/memory/allocator.cpp b/src/memory/allocator.cpp index a6a057a..4c9e22e 100644 --- a/src/memory/allocator.cpp +++ b/src/memory/allocator.cpp @@ -261,4 +261,9 @@ std::size_t Allocator::shiftToAlignData(const MemoryControlBlock *mcb, std::size return math::roundUp(data, alignment) - data; } +std::size_t Allocator::dataSize(std::uintptr_t ptr) const noexcept { + return MemoryControlBlock::fromDataPtr(ptr)->size(); +} + + } // namespace hse::memory diff --git a/src/memory/allocator.h b/src/memory/allocator.h index 2adace9..cb73110 100644 --- a/src/memory/allocator.h +++ b/src/memory/allocator.h @@ -95,6 +95,9 @@ class Allocator { // free deallocates memory pointed by given pointer void free(std::uintptr_t); + + // size returns the size in bytes of data stored in MCB holding provided pointer + std::size_t dataSize(std::uintptr_t ptr) const noexcept; }; } // namespace hse::memory diff --git a/test/src/main.cpp b/test/src/main.cpp index fdcad79..adb4634 100644 --- a/test/src/main.cpp +++ b/test/src/main.cpp @@ -38,6 +38,13 @@ TEST_CASE("malloc: array of bytes", "[malloc][free]") { hse::free(ptr); } +TEST_CASE("malloc array of bytes and check capacity", "[malloc][free][malloc_usable_size]") { + auto *ptr = reinterpret_cast(hse::malloc(BIG_NUMBER * sizeof(std::uint8_t))); + testArray(std::span{ptr, BIG_NUMBER}); + REQUIRE(hse::malloc_usable_size(ptr)>=BIG_NUMBER * sizeof(std::uint8_t)); + hse::free(ptr); +} + TEST_CASE("malloc: big object", "[malloc][free]") { struct S { std::array data;