NFC: pointer_offset* functions always return void*

This requires that the caller perform the cast on the output rather than the
input, which is a little closer to the truth.  Shuffle some casts into the right
position.
This commit is contained in:
Nathaniel Filardo
2021-03-02 20:45:51 +00:00
committed by Matthew Parkinson
parent f295a3f191
commit cbab7a3455
5 changed files with 14 additions and 15 deletions

View File

@@ -18,19 +18,19 @@ namespace snmalloc
/** /**
* Perform pointer arithmetic and return the adjusted pointer. * Perform pointer arithmetic and return the adjusted pointer.
*/ */
template<typename T> template<typename U = void, typename T>
inline T* pointer_offset(T* base, size_t diff) inline U* pointer_offset(T* base, size_t diff)
{ {
return reinterpret_cast<T*>(reinterpret_cast<char*>(base) + diff); return reinterpret_cast<U*>(reinterpret_cast<char*>(base) + diff);
} }
/** /**
* Perform pointer arithmetic and return the adjusted pointer. * Perform pointer arithmetic and return the adjusted pointer.
*/ */
template<typename T> template<typename U = void, typename T>
inline T* pointer_offset_signed(T* base, ptrdiff_t diff) inline U* pointer_offset_signed(T* base, ptrdiff_t diff)
{ {
return reinterpret_cast<T*>(reinterpret_cast<char*>(base) + diff); return reinterpret_cast<U*>(reinterpret_cast<char*>(base) + diff);
} }
/** /**

View File

@@ -31,7 +31,7 @@ namespace snmalloc
SNMALLOC_FAST_PATH T* get_next() SNMALLOC_FAST_PATH T* get_next()
{ {
return pointer_offset_signed(static_cast<T*>(this), to_next); return pointer_offset_signed<T>(this, to_next);
} }
}; };

View File

@@ -101,8 +101,7 @@ namespace snmalloc
void* bigger = remove_block(align_bits + 1); void* bigger = remove_block(align_bits + 1);
if (bigger != nullptr) if (bigger != nullptr)
{ {
void* left_over = auto left_over = pointer_offset(bigger, bits::one_at_bit(align_bits));
pointer_offset(bigger, bits::one_at_bit(align_bits));
ranges[align_bits][0] = left_over; ranges[align_bits][0] = left_over;
check_block(left_over, align_bits); check_block(left_over, align_bits);
} }

View File

@@ -391,7 +391,7 @@ namespace snmalloc
while (chunkmap_slab_kind >= CMLargeRangeMin) while (chunkmap_slab_kind >= CMLargeRangeMin)
{ {
// This is a large alloc redirect. // This is a large alloc redirect.
ss = pointer_offset_signed( ss = pointer_offset_signed<Superslab>(
ss, ss,
-(static_cast<ptrdiff_t>(1) -(static_cast<ptrdiff_t>(1)
<< (chunkmap_slab_kind - CMLargeRangeMin + SUPERSLAB_BITS))); << (chunkmap_slab_kind - CMLargeRangeMin + SUPERSLAB_BITS)));
@@ -402,7 +402,7 @@ namespace snmalloc
{ {
if constexpr ((location == End) || (location == OnePastEnd)) if constexpr ((location == End) || (location == OnePastEnd))
// We don't know the End, so return MAX_PTR // We don't know the End, so return MAX_PTR
return pointer_offset<void>(nullptr, UINTPTR_MAX); return pointer_offset<void, void>(nullptr, UINTPTR_MAX);
else else
// We don't know the Start, so return MIN_PTR // We don't know the Start, so return MIN_PTR
return nullptr; return nullptr;
@@ -1181,8 +1181,8 @@ namespace snmalloc
Slab* slab = alloc_slab<allow_reserve>(sizeclass); Slab* slab = alloc_slab<allow_reserve>(sizeclass);
if (slab == nullptr) if (slab == nullptr)
return nullptr; return nullptr;
bp = reinterpret_cast<SlabNext*>(pointer_offset( bp = pointer_offset<SlabNext>(
slab, get_initial_offset(sizeclass, Metaslab::is_short(slab)))); slab, get_initial_offset(sizeclass, Metaslab::is_short(slab)));
return small_alloc_build_free_list<zero_mem, allow_reserve>(sizeclass); return small_alloc_build_free_list<zero_mem, allow_reserve>(sizeclass);
} }

View File

@@ -183,8 +183,8 @@ namespace snmalloc
Slab* alloc_slab(sizeclass_t sizeclass) Slab* alloc_slab(sizeclass_t sizeclass)
{ {
uint8_t h = head; uint8_t h = head;
Slab* slab = pointer_offset( Slab* slab = reinterpret_cast<Slab*>(
reinterpret_cast<Slab*>(this), (static_cast<size_t>(h) << SLAB_BITS)); pointer_offset(this, (static_cast<size_t>(h) << SLAB_BITS)));
uint8_t n = meta[h].next; uint8_t n = meta[h].next;