From 4bafca9be7ebfa88a54757a8319b6fb54ba51681 Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Mon, 29 Apr 2019 11:33:07 +0100 Subject: [PATCH] [NFC] Automatic fixes from clang-tidy. --- src/ds/bits.h | 38 ++++++++++++++++++++------------------ src/mem/alloc.h | 36 ++++++++++++++++++------------------ src/mem/allocstats.h | 2 +- src/mem/largealloc.h | 7 ++++--- src/mem/mediumslab.h | 9 +++++---- src/mem/metaslab.h | 8 ++++---- src/mem/pool.h | 8 ++++---- src/mem/sizeclass.h | 15 ++++++++------- src/mem/sizeclasstable.h | 17 +++++++++-------- src/mem/slab.h | 16 ++++++++-------- src/mem/superslab.h | 25 ++++++++++--------------- src/mem/threadalloc.h | 2 +- 12 files changed, 92 insertions(+), 91 deletions(-) diff --git a/src/ds/bits.h b/src/ds/bits.h index 172be51..3978025 100644 --- a/src/ds/bits.h +++ b/src/ds/bits.h @@ -164,20 +164,22 @@ namespace snmalloc return BITS - index - 1; # endif #else - return (size_t)__builtin_clzl(x); + return static_cast(__builtin_clzl(x)); #endif } inline constexpr size_t rotr_const(size_t x, size_t n) { size_t nn = n & (BITS - 1); - return (x >> nn) | (x << (((size_t) - (int)nn) & (BITS - 1))); + return (x >> nn) | + (x << ((static_cast(-static_cast(nn))) & (BITS - 1))); } inline constexpr size_t rotl_const(size_t x, size_t n) { size_t nn = n & (BITS - 1); - return (x << nn) | (x >> (((size_t) - (int)nn) & (BITS - 1))); + return (x << nn) | + (x >> ((static_cast(-static_cast(nn))) & (BITS - 1))); } inline size_t rotr(size_t x, size_t n) @@ -212,7 +214,7 @@ namespace snmalloc for (int i = BITS - 1; i >= 0; i--) { - size_t mask = (size_t)1 << i; + size_t mask = static_cast(1) << i; if ((x & mask) == mask) return n; @@ -232,7 +234,7 @@ namespace snmalloc return _tzcnt_u32((uint32_t)x); # endif #else - return (size_t)__builtin_ctzl(x); + return static_cast(__builtin_ctzl(x)); #endif } @@ -242,7 +244,7 @@ namespace snmalloc for (size_t i = 0; i < BITS; i++) { - size_t mask = (size_t)1 << i; + size_t mask = static_cast(1) << i; if ((x & mask) == mask) return n; @@ -283,7 +285,7 @@ namespace snmalloc if (x <= 2) return x; - return (size_t)1 << (BITS - clz(x - 1)); + return static_cast(1) << (BITS - clz(x - 1)); } inline size_t next_pow2_bits(size_t x) @@ -298,7 +300,7 @@ namespace snmalloc if (x <= 2) return x; - return (size_t)1 << (BITS - clz_const(x - 1)); + return static_cast(1) << (BITS - clz_const(x - 1)); } constexpr size_t next_pow2_bits_const(size_t x) @@ -405,8 +407,9 @@ namespace snmalloc template static size_t to_exp_mant(size_t value) { - size_t LEADING_BIT = ((size_t)1 << (MANTISSA_BITS + LOW_BITS)) >> 1; - size_t MANTISSA_MASK = ((size_t)1 << MANTISSA_BITS) - 1; + size_t LEADING_BIT = + (static_cast(1) << (MANTISSA_BITS + LOW_BITS)) >> 1; + size_t MANTISSA_MASK = (static_cast(1) << MANTISSA_BITS) - 1; value = value - 1; @@ -421,8 +424,9 @@ namespace snmalloc template constexpr static size_t to_exp_mant_const(size_t value) { - size_t LEADING_BIT = ((size_t)1 << (MANTISSA_BITS + LOW_BITS)) >> 1; - size_t MANTISSA_MASK = ((size_t)1 << MANTISSA_BITS) - 1; + size_t LEADING_BIT = + (static_cast(1) << (MANTISSA_BITS + LOW_BITS)) >> 1; + size_t MANTISSA_MASK = (static_cast(1) << MANTISSA_BITS) - 1; value = value - 1; @@ -440,18 +444,16 @@ namespace snmalloc if (MANTISSA_BITS > 0) { m_e = m_e + 1; - size_t MANTISSA_MASK = ((size_t)1 << MANTISSA_BITS) - 1; + size_t MANTISSA_MASK = (static_cast(1) << MANTISSA_BITS) - 1; size_t m = m_e & MANTISSA_MASK; size_t e = m_e >> MANTISSA_BITS; size_t b = e == 0 ? 0 : 1; size_t shifted_e = e - b; - size_t extended_m = (m + ((size_t)b << MANTISSA_BITS)); + size_t extended_m = (m + (b << MANTISSA_BITS)); return extended_m << (shifted_e + LOW_BITS); } - else - { - return (size_t)1 << (m_e + LOW_BITS); - } + + return static_cast(1) << (m_e + LOW_BITS); } /** diff --git a/src/mem/alloc.h b/src/mem/alloc.h index b59e4c9..fda1a5d 100644 --- a/src/mem/alloc.h +++ b/src/mem/alloc.h @@ -152,14 +152,14 @@ namespace snmalloc */ void set_slab(Superslab* slab) { - set(slab, (size_t)PMSuperslab); + set(slab, static_cast(PMSuperslab)); } /** * Add a pagemap entry indicating that a medium slab has been allocated. */ void set_slab(Mediumslab* slab) { - set(slab, (size_t)PMMediumslab); + set(slab, static_cast(PMMediumslab)); } /** * Remove an entry from the pagemap corresponding to a superslab. @@ -167,7 +167,7 @@ namespace snmalloc void clear_slab(Superslab* slab) { assert(get(slab) == PMSuperslab); - set(slab, (size_t)PMNotOurs); + set(slab, static_cast(PMNotOurs)); } /** * Remove an entry corresponding to a medium slab. @@ -175,7 +175,7 @@ namespace snmalloc void clear_slab(Mediumslab* slab) { assert(get(slab) == PMMediumslab); - set(slab, (size_t)PMNotOurs); + set(slab, static_cast(PMNotOurs)); } /** * Update the pagemap to reflect a large allocation, of `size` bytes from @@ -184,17 +184,18 @@ namespace snmalloc void set_large_size(void* p, size_t size) { size_t size_bits = bits::next_pow2_bits(size); - set(p, (uint8_t)size_bits); + set(p, static_cast(size_bits)); // Set redirect slide uintptr_t ss = (uintptr_t)p + SUPERSLAB_SIZE; for (size_t i = 0; i < size_bits - SUPERSLAB_BITS; i++) { size_t run = 1ULL << i; PagemapProvider::pagemap().set_range( - ss, (uint8_t)(64 + i + SUPERSLAB_BITS), run); + ss, static_cast(64 + i + SUPERSLAB_BITS), run); ss = ss + SUPERSLAB_SIZE * run; } - PagemapProvider::pagemap().set((uintptr_t)p, (uint8_t)size_bits); + PagemapProvider::pagemap().set( + (uintptr_t)p, static_cast(size_bits)); } /** * Update the pagemap to remove a large allocation, of `size` bytes from @@ -326,15 +327,14 @@ namespace snmalloc size_t rsize = sizeclass_to_size(sizeclass); return small_alloc(sizeclass, rsize); } - else if (sizeclass < NUM_SIZECLASSES) + if (sizeclass < NUM_SIZECLASSES) { size_t rsize = sizeclass_to_size(sizeclass); return medium_alloc(sizeclass, rsize, size); } - else - { - return large_alloc(size); - } + + return large_alloc(size); + #endif } @@ -453,7 +453,7 @@ namespace snmalloc remote_dealloc(target, p, sizeclass); return; } - else if (size == PMMediumslab) + if (size == PMMediumslab) { Mediumslab* slab = Mediumslab::get(p); RemoteAllocator* target = slab->get_allocator(); @@ -499,7 +499,7 @@ namespace snmalloc return external_pointer(p, sc, slab_end); } - else if (size == PMMediumslab) + if (size == PMMediumslab) { Mediumslab* slab = Mediumslab::get(p); @@ -625,7 +625,7 @@ namespace snmalloc { this->size += sizeclass_to_size(sizeclass); - Remote* r = (Remote*)p; + Remote* r = static_cast(p); r->set_target_id(target_id); assert(r->target_id() == target_id); @@ -762,7 +762,7 @@ namespace snmalloc remote_alloc = r; } - if (id() >= (alloc_id_t)-1) + if (id() >= static_cast(-1)) error("Id should not be -1"); init_message_queue(); @@ -1209,7 +1209,7 @@ namespace snmalloc MEASURE_TIME(large_dealloc, 4, 16); size_t size_bits = bits::next_pow2_bits(size); - size_t rsize = (size_t)1 << size_bits; + size_t rsize = static_cast(1) << size_bits; assert(rsize >= SUPERSLAB_SIZE); size_t large_class = size_bits - SUPERSLAB_BITS; @@ -1222,7 +1222,7 @@ namespace snmalloc (void*)((size_t)p + OS_PAGE_SIZE), rsize - OS_PAGE_SIZE); // Initialise in order to set the correct SlabKind. - Largeslab* slab = (Largeslab*)p; + Largeslab* slab = static_cast(p); slab->init(); large_allocator.dealloc(slab, large_class); } diff --git a/src/mem/allocstats.h b/src/mem/allocstats.h index a2ec29f..d54476c 100644 --- a/src/mem/allocstats.h +++ b/src/mem/allocstats.h @@ -116,7 +116,7 @@ namespace snmalloc static constexpr size_t BUCKETS = 1 << BUCKETS_BITS; static constexpr size_t TOTAL_BUCKETS = bits::to_exp_mant_const( - ((size_t)1 << (bits::ADDRESS_BITS - 1))); + (static_cast(1) << (bits::ADDRESS_BITS - 1))); Stats sizeclass[N]; Stats large[LARGE_N]; diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index 790ee00..a26ad3f 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -98,7 +98,8 @@ namespace snmalloc { break; } - size_t rsize = ((size_t)1 << SUPERSLAB_BITS) << large_class; + size_t rsize = (static_cast(1) << SUPERSLAB_BITS) + << large_class; size_t decommit_size = rsize - OS_PAGE_SIZE; // Grab all of the chunks of this size class. auto* slab = large_stack[large_class].pop_all(); @@ -220,7 +221,7 @@ namespace snmalloc uintptr_t p0 = (uintptr_t)p; uintptr_t start = bits::align_up(p0, align); - if (start > (uintptr_t)p0) + if (start > p0) { uintptr_t end = bits::align_down(p0 + request, align); *size = end - start; @@ -320,7 +321,7 @@ namespace snmalloc template void* alloc(size_t large_class, size_t size) { - size_t rsize = ((size_t)1 << SUPERSLAB_BITS) << large_class; + size_t rsize = (static_cast(1) << SUPERSLAB_BITS) << large_class; if (size == 0) size = rsize; diff --git a/src/mem/mediumslab.h b/src/mem/mediumslab.h index 64f7ed8..6e332fc 100644 --- a/src/mem/mediumslab.h +++ b/src/mem/mediumslab.h @@ -57,11 +57,12 @@ namespace snmalloc if ((kind != Medium) || (sizeclass != sc)) { sizeclass = sc; - uint16_t ssize = (uint16_t)(rsize >> 8); + uint16_t ssize = static_cast(rsize >> 8); kind = Medium; free = medium_slab_free(sc); for (uint16_t i = free; i > 0; i--) - stack[free - i] = (uint16_t)((SUPERSLAB_SIZE >> 8) - (i * ssize)); + stack[free - i] = + static_cast((SUPERSLAB_SIZE >> 8) - (i * ssize)); } else { @@ -80,7 +81,7 @@ namespace snmalloc assert(!full()); uint16_t index = stack[head++]; - void* p = (void*)((size_t)this + ((size_t)index << 8)); + void* p = (void*)((size_t)this + (static_cast(index) << 8)); free--; assert(bits::is_aligned_block(p, OS_PAGE_SIZE)); @@ -124,7 +125,7 @@ namespace snmalloc uint16_t pointer_to_index(void* p) { // Get the offset from the slab for a memory location. - return (uint16_t)(((size_t)p - (size_t)this) >> 8); + return static_cast(((size_t)p - (size_t)this) >> 8); } }; } diff --git a/src/mem/metaslab.h b/src/mem/metaslab.h index 924f34f..3211bd6 100644 --- a/src/mem/metaslab.h +++ b/src/mem/metaslab.h @@ -26,7 +26,7 @@ namespace snmalloc "Need to be able to pack a SlabLink into any free small alloc"); static constexpr uint16_t SLABLINK_INDEX = - (uint16_t)(SLAB_SIZE - sizeof(SlabLink)); + static_cast(SLAB_SIZE - sizeof(SlabLink)); // The Metaslab represent the status of a single slab. // This can be either a short or a standard slab. @@ -90,7 +90,7 @@ namespace snmalloc void set_full() { assert(head == 1); - head = (uint16_t)~0; + head = static_cast(~0); } SlabLink* get_link(Slab* slab) @@ -104,8 +104,8 @@ namespace snmalloc size_t offset = get_slab_offset(sizeclass, is_short); size_t head_start = - remove_cache_friendly_offset(head & ~(size_t)1, sizeclass); - size_t slab_start = offset & ~(size_t)1; + remove_cache_friendly_offset(head & ~static_cast(1), sizeclass); + size_t slab_start = offset & ~static_cast(1); return ((head_start - slab_start) % size) == 0; } diff --git a/src/mem/pool.h b/src/mem/pool.h index f49214b..b365790 100644 --- a/src/mem/pool.h +++ b/src/mem/pool.h @@ -76,8 +76,8 @@ namespace snmalloc // Returns a linked list of all objects in the stack, emptying the stack. if (p == nullptr) return stack.pop_all(); - else - return p->next; + + return p->next; } void restore(T* first, T* last) @@ -91,8 +91,8 @@ namespace snmalloc { if (p == nullptr) return list; - else - return p->list_next; + + return p->list_next; } }; } diff --git a/src/mem/sizeclass.h b/src/mem/sizeclass.h index 95f4be6..27c1669 100644 --- a/src/mem/sizeclass.h +++ b/src/mem/sizeclass.h @@ -15,7 +15,8 @@ namespace snmalloc // Don't use sizeclasses that are not a multiple of the alignment. // For example, 24 byte allocations can be // problematic for some data due to alignment issues. - return (uint8_t)bits::to_exp_mant(size); + return static_cast( + bits::to_exp_mant(size)); } constexpr static inline uint8_t size_to_sizeclass_const(size_t size) @@ -23,18 +24,18 @@ namespace snmalloc // Don't use sizeclasses that are not a multiple of the alignment. // For example, 24 byte allocations can be // problematic for some data due to alignment issues. - return (uint8_t)bits::to_exp_mant_const( - size); + return static_cast( + bits::to_exp_mant_const(size)); } constexpr static inline size_t large_sizeclass_to_size(uint8_t large_class) { - return (size_t)1 << (large_class + SUPERSLAB_BITS); + return static_cast(1) << (large_class + SUPERSLAB_BITS); } // Small classes range from [MIN, SLAB], i.e. inclusive. static constexpr size_t NUM_SMALL_CLASSES = - size_to_sizeclass_const((size_t)1 << SLAB_BITS) + 1; + size_to_sizeclass_const(static_cast(1) << SLAB_BITS) + 1; static constexpr size_t NUM_SIZECLASSES = size_to_sizeclass_const(SUPERSLAB_SIZE); @@ -89,7 +90,7 @@ namespace snmalloc else // Use 32-bit division as considerably faster than 64-bit, and // everything fits into 32bits here. - return (uint32_t)(offset / rsize) * rsize; + return static_cast(offset / rsize) * rsize; } inline static bool is_multiple_of_sizeclass(size_t rsize, size_t offset) @@ -137,7 +138,7 @@ namespace snmalloc else // Use 32-bit division as considerably faster than 64-bit, and // everything fits into 32bits here. - return (uint32_t)(offset % rsize) == 0; + return static_cast(offset % rsize) == 0; } #ifdef CACHE_FRIENDLY_OFFSET diff --git a/src/mem/sizeclasstable.h b/src/mem/sizeclasstable.h index fb3310a..9794d4d 100644 --- a/src/mem/sizeclasstable.h +++ b/src/mem/sizeclasstable.h @@ -30,7 +30,8 @@ namespace snmalloc bits::from_exp_mant(sizeclass); size_t alignment = bits::min( - (size_t)1 << bits::ctz_const(size[sizeclass]), OS_PAGE_SIZE); + static_cast(1) << bits::ctz_const(size[sizeclass]), + OS_PAGE_SIZE); cache_friendly_mask[sizeclass] = (alignment - 1); inverse_cache_friendly_mask[sizeclass] = ~(alignment - 1); } @@ -41,15 +42,15 @@ namespace snmalloc for (uint8_t i = 0; i < NUM_SMALL_CLASSES; i++) { short_bump_ptr_start[i] = - (uint16_t)(1 + (short_slab_size % size[i]) + header_size); - bump_ptr_start[i] = (uint16_t)(1 + (SLAB_SIZE % size[i])); - count_per_slab[i] = (uint16_t)(SLAB_SIZE / size[i]); + static_cast(1 + (short_slab_size % size[i]) + header_size); + bump_ptr_start[i] = static_cast(1 + (SLAB_SIZE % size[i])); + count_per_slab[i] = static_cast(SLAB_SIZE / size[i]); } for (uint8_t i = NUM_SMALL_CLASSES; i < NUM_SIZECLASSES; i++) { - medium_slab_slots[i - NUM_SMALL_CLASSES] = - (uint16_t)((SUPERSLAB_SIZE - Mediumslab::header_size()) / size[i]); + medium_slab_slots[i - NUM_SMALL_CLASSES] = static_cast( + (SUPERSLAB_SIZE - Mediumslab::header_size()) / size[i]); } } }; @@ -60,8 +61,8 @@ namespace snmalloc { if (is_short) return sizeclass_metadata.short_bump_ptr_start[sc]; - else - return sizeclass_metadata.bump_ptr_start[sc]; + + return sizeclass_metadata.bump_ptr_start[sc]; } constexpr static inline size_t sizeclass_to_size(uint8_t sizeclass) diff --git a/src/mem/slab.h b/src/mem/slab.h index c359c80..fad79c5 100644 --- a/src/mem/slab.h +++ b/src/mem/slab.h @@ -10,7 +10,7 @@ namespace snmalloc uint16_t pointer_to_index(void* p) { // Get the offset from the slab for a memory location. - return (uint16_t)((size_t)p - (size_t)this); + return static_cast((size_t)p - (size_t)this); } public: @@ -51,7 +51,7 @@ namespace snmalloc void* node = (void*)((size_t)this + head); // Read the next slot from the memory that's about to be allocated. - uint16_t next = *(uint16_t*)node; + uint16_t next = *static_cast(node); meta.head = next; p = remove_cache_friendly_offset(node, meta.sizeclass); @@ -60,7 +60,7 @@ namespace snmalloc { // This slab is being bump allocated. p = (void*)((size_t)this + head - 1); - meta.head = (head + (uint16_t)rsize) & (SLAB_SIZE - 1); + meta.head = (head + static_cast(rsize)) & (SLAB_SIZE - 1); if (meta.head == 1) { meta.set_full(); @@ -123,8 +123,8 @@ namespace snmalloc // Dealloc on the superslab. if (is_short()) return super->dealloc_short_slab(memory_provider); - else - return super->dealloc_slab(this, memory_provider); + + return super->dealloc_slab(this, memory_provider); } } else if (meta.is_unused()) @@ -134,8 +134,8 @@ namespace snmalloc if (is_short()) return super->dealloc_short_slab(memory_provider); - else - return super->dealloc_slab(this, memory_provider); + + return super->dealloc_slab(this, memory_provider); } else { @@ -152,7 +152,7 @@ namespace snmalloc assert(meta.valid_head(is_short())); // Set the next pointer to the previous head. - *(uint16_t*)p = head; + *static_cast(p) = head; meta.debug_slab_invariant(is_short(), this); } return Superslab::NoSlabReturn; diff --git a/src/mem/superslab.h b/src/mem/superslab.h index 29b49c8..fab78ba 100644 --- a/src/mem/superslab.h +++ b/src/mem/superslab.h @@ -136,22 +136,16 @@ namespace snmalloc { return Available; } - else - { - return Empty; - } + + return Empty; } - else + + if (!is_full()) { - if (!is_full()) - { - return OnlyShortSlabAvailable; - } - else - { - return Full; - } + return OnlyShortSlabAvailable; } + + return Full; } Metaslab& get_meta(Slab* slab) @@ -183,7 +177,8 @@ namespace snmalloc Slab* alloc_slab(uint8_t sizeclass, MemoryProvider& memory_provider) { uint8_t h = head; - Slab* slab = (Slab*)((size_t)this + ((size_t)h << SLAB_BITS)); + Slab* slab = + (Slab*)((size_t)this + (static_cast(h) << SLAB_BITS)); uint8_t n = meta[h].next; @@ -207,7 +202,7 @@ namespace snmalloc Action dealloc_slab(Slab* slab, MemoryProvider& memory_provider) { // This is not the short slab. - uint8_t index = (uint8_t)slab_to_index(slab); + uint8_t index = static_cast(slab_to_index(slab)); uint8_t n = head - index - 1; meta[index].sizeclass = 0; diff --git a/src/mem/threadalloc.h b/src/mem/threadalloc.h index 597c68e..5cd45dd 100644 --- a/src/mem/threadalloc.h +++ b/src/mem/threadalloc.h @@ -157,7 +157,7 @@ namespace snmalloc # endif thread_alloc_release(void* p) { - Alloc** pp = (Alloc**)p; + Alloc** pp = static_cast(p); current_alloc_pool()->release(*pp); *pp = nullptr; }