[NFC] Automatic fixes from clang-tidy.

This commit is contained in:
David Chisnall
2019-04-29 11:33:07 +01:00
parent e6325e5cd2
commit 4bafca9be7
12 changed files with 92 additions and 91 deletions

View File

@@ -164,20 +164,22 @@ namespace snmalloc
return BITS - index - 1;
# endif
#else
return (size_t)__builtin_clzl(x);
return static_cast<size_t>(__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<size_t>(-static_cast<int>(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<size_t>(-static_cast<int>(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<size_t>(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<size_t>(__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<size_t>(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<size_t>(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<size_t>(1) << (BITS - clz_const(x - 1));
}
constexpr size_t next_pow2_bits_const(size_t x)
@@ -405,8 +407,9 @@ namespace snmalloc
template<size_t MANTISSA_BITS, size_t LOW_BITS = 0>
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<size_t>(1) << (MANTISSA_BITS + LOW_BITS)) >> 1;
size_t MANTISSA_MASK = (static_cast<size_t>(1) << MANTISSA_BITS) - 1;
value = value - 1;
@@ -421,8 +424,9 @@ namespace snmalloc
template<size_t MANTISSA_BITS, size_t LOW_BITS = 0>
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<size_t>(1) << (MANTISSA_BITS + LOW_BITS)) >> 1;
size_t MANTISSA_MASK = (static_cast<size_t>(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<size_t>(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<size_t>(1) << (m_e + LOW_BITS);
}
/**

View File

@@ -152,14 +152,14 @@ namespace snmalloc
*/
void set_slab(Superslab* slab)
{
set(slab, (size_t)PMSuperslab);
set(slab, static_cast<size_t>(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<size_t>(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<size_t>(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<size_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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<zero_mem, allow_reserve>(sizeclass, rsize);
}
else if (sizeclass < NUM_SIZECLASSES)
if (sizeclass < NUM_SIZECLASSES)
{
size_t rsize = sizeclass_to_size(sizeclass);
return medium_alloc<zero_mem, allow_reserve>(sizeclass, rsize, size);
}
else
{
return large_alloc<zero_mem, allow_reserve>(size);
}
return large_alloc<zero_mem, allow_reserve>(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<location>(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<Remote*>(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<alloc_id_t>(-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<size_t>(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<Largeslab*>(p);
slab->init();
large_allocator.dealloc(slab, large_class);
}

View File

@@ -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<BUCKETS_BITS>(
((size_t)1 << (bits::ADDRESS_BITS - 1)));
(static_cast<size_t>(1) << (bits::ADDRESS_BITS - 1)));
Stats sizeclass[N];
Stats large[LARGE_N];

View File

@@ -98,7 +98,8 @@ namespace snmalloc
{
break;
}
size_t rsize = ((size_t)1 << SUPERSLAB_BITS) << large_class;
size_t rsize = (static_cast<size_t>(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<ZeroMem zero_mem = NoZero, AllowReserve allow_reserve = YesReserve>
void* alloc(size_t large_class, size_t size)
{
size_t rsize = ((size_t)1 << SUPERSLAB_BITS) << large_class;
size_t rsize = (static_cast<size_t>(1) << SUPERSLAB_BITS) << large_class;
if (size == 0)
size = rsize;

View File

@@ -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<uint16_t>(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<uint16_t>((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<size_t>(index) << 8));
free--;
assert(bits::is_aligned_block<OS_PAGE_SIZE>(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<uint16_t>(((size_t)p - (size_t)this) >> 8);
}
};
}

View File

@@ -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<uint16_t>(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<uint16_t>(~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<size_t>(1), sizeclass);
size_t slab_start = offset & ~static_cast<size_t>(1);
return ((head_start - slab_start) % size) == 0;
}

View File

@@ -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;
}
};
}

View File

@@ -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<INTERMEDIATE_BITS, MIN_ALLOC_BITS>(size);
return static_cast<uint8_t>(
bits::to_exp_mant<INTERMEDIATE_BITS, MIN_ALLOC_BITS>(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<INTERMEDIATE_BITS, MIN_ALLOC_BITS>(
size);
return static_cast<uint8_t>(
bits::to_exp_mant_const<INTERMEDIATE_BITS, MIN_ALLOC_BITS>(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<size_t>(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<size_t>(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<uint32_t>(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<uint32_t>(offset % rsize) == 0;
}
#ifdef CACHE_FRIENDLY_OFFSET

View File

@@ -30,7 +30,8 @@ namespace snmalloc
bits::from_exp_mant<INTERMEDIATE_BITS, MIN_ALLOC_BITS>(sizeclass);
size_t alignment = bits::min(
(size_t)1 << bits::ctz_const(size[sizeclass]), OS_PAGE_SIZE);
static_cast<size_t>(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<uint16_t>(1 + (short_slab_size % size[i]) + header_size);
bump_ptr_start[i] = static_cast<uint16_t>(1 + (SLAB_SIZE % size[i]));
count_per_slab[i] = static_cast<uint16_t>(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<uint16_t>(
(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)

View File

@@ -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<uint16_t>((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<uint16_t*>(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<uint16_t>(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<uint16_t*>(p) = head;
meta.debug_slab_invariant(is_short(), this);
}
return Superslab::NoSlabReturn;

View File

@@ -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<size_t>(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<uint8_t>(slab_to_index(slab));
uint8_t n = head - index - 1;
meta[index].sizeclass = 0;

View File

@@ -157,7 +157,7 @@ namespace snmalloc
# endif
thread_alloc_release(void* p)
{
Alloc** pp = (Alloc**)p;
Alloc** pp = static_cast<Alloc**>(p);
current_alloc_pool()->release(*pp);
*pp = nullptr;
}