ds/address: add pointer diff function

And use it rather than open-coding subtraction of two address_cast-s.
This commit is contained in:
Nathaniel Filardo
2019-05-12 20:01:54 +01:00
parent eb2d8890de
commit 83c467eb92
5 changed files with 19 additions and 8 deletions

View File

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