ds/address: add pointer diff function
And use it rather than open-coding subtraction of two address_cast-s.
This commit is contained in:
@@ -87,4 +87,16 @@ namespace snmalloc
|
||||
return pointer_cast<T>(bits::align_up(address_cast(p), alignment));
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the difference in pointers in units of char. base is
|
||||
* expected to point to the base of some (sub)allocation into which cursor
|
||||
* points; would-be negative answers trip an assertion in debug builds.
|
||||
*/
|
||||
inline size_t pointer_diff(void* base, void* cursor)
|
||||
{
|
||||
assert(cursor >= base);
|
||||
return static_cast<size_t>(
|
||||
static_cast<char*>(cursor) - static_cast<char*>(base));
|
||||
}
|
||||
} // namespace snmalloc
|
||||
|
||||
@@ -1193,7 +1193,7 @@ namespace snmalloc
|
||||
#ifdef CHECK_CLIENT
|
||||
if (!is_multiple_of_sizeclass(
|
||||
sizeclass_to_size(sizeclass),
|
||||
address_cast(slab) + SUPERSLAB_SIZE - address_cast(p)))
|
||||
pointer_diff(p, pointer_offset(slab, SUPERSLAB_SIZE))))
|
||||
{
|
||||
error("Not deallocating start of an object");
|
||||
}
|
||||
|
||||
@@ -125,8 +125,7 @@ namespace snmalloc
|
||||
uint16_t pointer_to_index(void* p)
|
||||
{
|
||||
// Get the offset from the slab for a memory location.
|
||||
return static_cast<uint16_t>(
|
||||
((address_cast(p) - address_cast(this))) >> 8);
|
||||
return static_cast<uint16_t>(pointer_diff(this, p) >> 8);
|
||||
}
|
||||
};
|
||||
} // namespace snmalloc
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace snmalloc
|
||||
uint16_t pointer_to_index(void* p)
|
||||
{
|
||||
// Get the offset from the slab for a memory location.
|
||||
return static_cast<uint16_t>(address_cast(p) - address_cast(this));
|
||||
return static_cast<uint16_t>(pointer_diff(this, p));
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -144,7 +144,7 @@ namespace snmalloc
|
||||
Metaslab& meta = super->get_meta(this);
|
||||
return is_multiple_of_sizeclass(
|
||||
sizeclass_to_size(meta.sizeclass),
|
||||
address_cast(this) + SLAB_SIZE - address_cast(p));
|
||||
pointer_diff(p, pointer_offset(this, SLAB_SIZE)));
|
||||
}
|
||||
|
||||
// Returns true, if it deallocation can proceed without changing any status
|
||||
|
||||
@@ -44,9 +44,9 @@ namespace snmalloc
|
||||
// Used size_t as results in better code in MSVC
|
||||
size_t slab_to_index(Slab* slab)
|
||||
{
|
||||
auto res = ((address_cast(slab) - address_cast(this)) >> SLAB_BITS);
|
||||
assert(res == (uint8_t)res);
|
||||
return res;
|
||||
auto res = (pointer_diff(this, slab) >> SLAB_BITS);
|
||||
assert(res == static_cast<uint8_t>(res));
|
||||
return static_cast<uint8_t>(res);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user