From 4b18b4de7713c5d5cf8fabd1d4ad8000f6a12cee Mon Sep 17 00:00:00 2001 From: Roman Astrakhantsev Date: Tue, 8 Dec 2020 06:21:31 +0300 Subject: [PATCH 1/2] feat: malloc_usable_size --- include/malloc.h | 1 + src/malloc.cpp | 8 ++++++++ src/malloc.h | 1 + src/malloc_std.cpp | 2 ++ src/memory/allocator.cpp | 5 +++++ src/memory/allocator.h | 3 +++ test/src/main.cpp | 7 +++++++ 7 files changed, 27 insertions(+) diff --git a/include/malloc.h b/include/malloc.h index fb39c0d..7c95005 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -26,6 +26,7 @@ __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__; +__NODISCARD__ size_t malloc_usable_size (void *ptr) __NOEXCEPT__ void free(void *) __NOEXCEPT__; #ifdef __cplusplus 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..4325b3b 100644 --- a/src/malloc_std.cpp +++ b/src/malloc_std.cpp @@ -24,6 +24,8 @@ 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); } +size_t malloc_usable_size (void *ptr) noexcept { return hse::malloc_usable_size(ptr);} + } // 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; From 5f0f289fcc08da5c4d5d5f5f6306d3bf201fb9f0 Mon Sep 17 00:00:00 2001 From: Roman Astrakhantsev Date: Tue, 8 Dec 2020 06:39:37 +0300 Subject: [PATCH 2/2] feat: memalign, posix_memalign, pvalloc, valloc --- include/malloc.h | 7 ++++++- src/malloc_std.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/malloc.h b/include/malloc.h index 7c95005..d4a0a59 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -26,8 +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__; +__NODISCARD__ void *memalign(size_t alignment, size_t size) __NOEXCEPT__; __NODISCARD__ size_t malloc_usable_size (void *ptr) __NOEXCEPT__ -void free(void *) __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_std.cpp b/src/malloc_std.cpp index 4325b3b..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,8 +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