From 3815f9aa9f02648e535a90f7b604f1744ae4bd8d Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 20 Nov 2022 15:12:18 +0100 Subject: [PATCH] Introduce an offset_ptr function to avoid quirky C pointer arithmetic --- luna/Alignment.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/luna/Alignment.h b/luna/Alignment.h index e69c0aad..0721aae5 100644 --- a/luna/Alignment.h +++ b/luna/Alignment.h @@ -36,4 +36,11 @@ template constexpr T get_blocks_from_size(T value, T block_size) static_assert(get_blocks_from_size(40960, 4096) == 10); static_assert(get_blocks_from_size(194, 64) == 4); static_assert(get_blocks_from_size(2, 32) == 1); -static_assert(get_blocks_from_size(0, 256) == 0); \ No newline at end of file +static_assert(get_blocks_from_size(0, 256) == 0); + +// Offset a pointer by exactly bytes, no matter the type. Useful to avoid the quirks that come from C pointer +// arithmetic. +template constexpr T* offset_ptr(T* ptr, Offset offset) +{ + return (T*)((char*)ptr + offset); +} \ No newline at end of file