Allow querying the allocator for the buffer size (#1404)

This commit is contained in:
Angelos Katharopoulos
2024-09-11 21:02:16 -07:00
committed by GitHub
parent 8b30acd7eb
commit 881f09b2e2
10 changed files with 42 additions and 13 deletions

View File

@@ -23,11 +23,22 @@ void free(Buffer buffer) {
}
Buffer CommonAllocator::malloc(size_t size, bool) {
return Buffer{std::malloc(size)};
void* ptr = std::malloc(size + sizeof(size_t));
if (ptr != nullptr) {
*static_cast<size_t*>(ptr) = size;
}
return Buffer{ptr};
}
void CommonAllocator::free(Buffer buffer) {
std::free(buffer.raw_ptr());
std::free(buffer.ptr());
}
size_t CommonAllocator::size(Buffer buffer) const {
if (buffer.ptr() == nullptr) {
return 0;
}
return *static_cast<size_t*>(buffer.ptr());
}
Buffer malloc_or_wait(size_t size) {