Skip to content

Commit

Permalink
Merge pull request #59 from no92/slab-get-size
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennisbonke authored Apr 8, 2023
2 parents b3ac959 + ff1c89d commit cd20c20
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions include/frg/slab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class slab_pool {
void *realloc(void *pointer, size_t new_length);
void free(void *pointer);
void deallocate(void *pointer, size_t size);
size_t get_size(void *pointer);

size_t numUsedPages() {
return _usedPages;
Expand Down Expand Up @@ -608,6 +609,26 @@ void slab_pool<Policy, Mutex>::deallocate(void *p, size_t size) {
_verify_integrity();
}

template<typename Policy, typename Mutex>
size_t slab_pool<Policy, Mutex>::get_size(void *p) {
if(enable_checking)
_verify_integrity();

if(!p)
return 0;

auto address = reinterpret_cast<uintptr_t>(p);
auto sup = reinterpret_cast<frame *>((address - 1) & ~(sb_size - 1));

if(sup->type == frame_type::slab) {
auto slb = static_cast<slab_frame *>(sup);
return bucket_to_size(slb->index);
}

FRG_ASSERT(sup->type == frame_type::large);
return sup->length;
}


template<typename Policy, typename Mutex>
auto slab_pool<Policy, Mutex>::_construct_slab(int index)
Expand Down Expand Up @@ -803,6 +824,10 @@ class slab_allocator {
return pool_->realloc(pointer, new_size);
}

size_t get_size(void *pointer) {
return pool_->get_size(pointer);
}

private:
slab_pool<Policy, Mutex> *pool_;
};
Expand Down

0 comments on commit cd20c20

Please sign in to comment.