From 2083b29c9b1b54f40998a9a47ac6515f8ba3a0c4 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Thu, 24 Jan 2019 09:54:23 +0000 Subject: [PATCH] Remove a raw pointer usage Metaslab can be handled as as ref, rather than a raw pointer. --- src/mem/alloc.h | 12 ++++----- src/mem/slab.h | 66 ++++++++++++++++++++++----------------------- src/mem/superslab.h | 4 +-- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/mem/alloc.h b/src/mem/alloc.h index 3191708..98dcc76 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -359,12 +359,12 @@ namespace snmalloc { RemoteAllocator* target = super->get_allocator(); Slab* slab = Slab::get(p); - Metaslab* meta = super->get_meta(slab); + Metaslab& meta = super->get_meta(slab); // Reading a remote sizeclass won't fail, since the other allocator // can't reuse the slab, as we have not yet deallocated this // pointer. - uint8_t sizeclass = meta->sizeclass; + uint8_t sizeclass = meta.sizeclass; if (super->get_allocator() == public_state()) small_dealloc(super, p, sizeclass); @@ -411,9 +411,9 @@ namespace snmalloc if (size == PMSuperslab) { Slab* slab = Slab::get(p); - Metaslab* meta = super->get_meta(slab); + Metaslab& meta = super->get_meta(slab); - uint8_t sc = meta->sizeclass; + uint8_t sc = meta.sizeclass; size_t slab_end = (size_t)slab + SLAB_SIZE - 1; return external_pointer(p, sc, slab_end); @@ -471,9 +471,9 @@ namespace snmalloc // Reading a remote sizeclass won't fail, since the other allocator // can't reuse the slab, as we have no yet deallocated this pointer. Slab* slab = Slab::get(p); - Metaslab* meta = super->get_meta(slab); + Metaslab& meta = super->get_meta(slab); - return sizeclass_to_size(meta->sizeclass); + return sizeclass_to_size(meta.sizeclass); } else if (size == PMMediumslab) { diff --git a/src/mem/slab.h b/src/mem/slab.h index ec9a000..6387dc6 100644 --- a/src/mem/slab.h +++ b/src/mem/slab.h @@ -19,7 +19,7 @@ namespace snmalloc return (Slab*)((size_t)p & SLAB_MASK); } - Metaslab* get_meta() + Metaslab& get_meta() { Superslab* super = Superslab::get(this); return super->get_meta(this); @@ -27,22 +27,22 @@ namespace snmalloc SlabLink* get_link() { - return get_meta()->get_link(this); + return get_meta().get_link(this); } template void* alloc(SlabList* sc, size_t rsize, MemoryProvider& memory_provider) { // Read the head from the metadata stored in the superslab. - Metaslab* meta = get_meta(); - uint16_t head = meta->head; + Metaslab& meta = get_meta(); + uint16_t head = meta.head; - assert(rsize == sizeclass_to_size(meta->sizeclass)); - meta->debug_slab_invariant(is_short(), this); - assert(sc->get_head() == (SlabLink*)((size_t)this + meta->link)); - assert(!meta->is_full()); + assert(rsize == sizeclass_to_size(meta.sizeclass)); + meta.debug_slab_invariant(is_short(), this); + assert(sc->get_head() == (SlabLink*)((size_t)this + meta.link)); + assert(!meta.is_full()); - meta->add_use(); + meta.add_use(); void* p; @@ -52,24 +52,24 @@ namespace snmalloc // Read the next slot from the memory that's about to be allocated. uint16_t next = *(uint16_t*)p; - meta->head = next; + meta.head = next; } else { // This slab is being bump allocated. p = (void*)((size_t)this + head - 1); - meta->head = (head + (uint16_t)rsize) & (SLAB_SIZE - 1); - if (meta->head == 1) + meta.head = (head + (uint16_t)rsize) & (SLAB_SIZE - 1); + if (meta.head == 1) { - meta->set_full(); + meta.set_full(); } } // If we're full, we're no longer the current slab for this sizeclass - if (meta->is_full()) + if (meta.is_full()) sc->pop(); - meta->debug_slab_invariant(is_short(), this); + meta.debug_slab_invariant(is_short(), this); if (zero_mem == YesZero) { @@ -87,15 +87,15 @@ namespace snmalloc inline typename Superslab::Action dealloc( SlabList* sc, Superslab* super, void* p, MemoryProvider& memory_provider) { - Metaslab* meta = super->get_meta(this); + Metaslab& meta = super->get_meta(this); - bool was_full = meta->is_full(); - meta->debug_slab_invariant(is_short(), this); - meta->sub_use(); + bool was_full = meta.is_full(); + meta.debug_slab_invariant(is_short(), this); + meta.sub_use(); #ifndef SNMALLOC_SAFE_CLIENT if (!is_multiple_of_sizeclass( - sizeclass_to_size(meta->sizeclass), + sizeclass_to_size(meta.sizeclass), (uintptr_t)this + SLAB_SIZE - (uintptr_t)p)) { error("Not deallocating start of an object"); @@ -105,17 +105,17 @@ namespace snmalloc if (was_full) { // We are not on the sizeclass list. - if (!meta->is_unused()) + if (!meta.is_unused()) { // Update the head and the sizeclass link. uint16_t index = pointer_to_index(p); - meta->head = index; - assert(meta->valid_head(is_short())); - meta->link = index; + meta.head = index; + assert(meta.valid_head(is_short())); + meta.link = index; // Push on the list of slabs for this sizeclass. - sc->insert(meta->get_link(this)); - meta->debug_slab_invariant(is_short(), this); + sc->insert(meta.get_link(this)); + meta.debug_slab_invariant(is_short(), this); } else { @@ -126,10 +126,10 @@ namespace snmalloc return super->dealloc_slab(this, memory_provider); } } - else if (meta->is_unused()) + else if (meta.is_unused()) { // Remove from the sizeclass list and dealloc on the superslab. - sc->remove(meta->get_link(this)); + sc->remove(meta.get_link(this)); if (is_short()) return super->dealloc_short_slab(memory_provider); @@ -139,20 +139,20 @@ namespace snmalloc else { #ifndef NDEBUG - sc->debug_check_contains(meta->get_link(this)); + sc->debug_check_contains(meta.get_link(this)); #endif // Update the head and the next pointer in the free list. - uint16_t head = meta->head; + uint16_t head = meta.head; uint16_t current = pointer_to_index(p); // Set the head to the memory being deallocated. - meta->head = current; - assert(meta->valid_head(is_short())); + meta.head = current; + assert(meta.valid_head(is_short())); // Set the next pointer to the previous head. *(uint16_t*)p = head; - meta->debug_slab_invariant(is_short(), this); + meta.debug_slab_invariant(is_short(), this); } return Superslab::NoSlabReturn; } diff --git a/src/mem/superslab.h b/src/mem/superslab.h index 3f702a7..53855ac 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -152,9 +152,9 @@ namespace snmalloc } } - Metaslab* get_meta(Slab* slab) + Metaslab& get_meta(Slab* slab) { - return &meta[slab_to_index(slab)]; + return meta[slab_to_index(slab)]; } template