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
|
||||
|
||||
Reference in New Issue
Block a user