Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions src/malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uintptr_t>(ptr));
}

} // namespace hse
1 change: 1 addition & 0 deletions src/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions src/malloc_std.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "system/system.h"
#include "math/math.h"
#include "memory/allocator.h"

Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions src/memory/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/memory/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint8_t *>(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<std::uint8_t, BIG_NUMBER> data;
Expand Down