From bedec1a938797f75512a0541d9d8b1a0cda2c2cb Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 6 Jan 2026 00:08:50 -0500 Subject: [PATCH] xo-arena: annex padding from xo-alloc2 --- include/xo/arena/padding.hpp | 60 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 include/xo/arena/padding.hpp diff --git a/include/xo/arena/padding.hpp b/include/xo/arena/padding.hpp new file mode 100644 index 0000000..10d03b8 --- /dev/null +++ b/include/xo/arena/padding.hpp @@ -0,0 +1,60 @@ +/** @file padding.hpp + * + * @author Roland Conybeare, Dec 2025 + **/ + +#pragma once + +#include +#include + +namespace xo { + namespace mm { + + struct padding { + /** word size for alignment**/ + static constexpr std::size_t c_alloc_alignment = sizeof(std::uintptr_t); + + static inline std::size_t is_aligned(std::size_t n, + std::size_t align = c_alloc_alignment) { + return n % align == 0; + } + + /** how much to add to @p z to get a multiple of + * @ref c_alloc_alignment + **/ + static inline std::size_t alloc_padding(std::size_t z, + std::size_t align = c_alloc_alignment) + { + + /* round up to multiple of c_bpw, but map 0 -> 0 + * (table assuming c_bpw==8) + * + * z%c_bpw dz + * ------------ + * 0 0 + * 1 7 + * 2 6 + * .. .. + * 7 1 + */ + std::size_t dz = (align - (z % align)) % align; + + return dz; + } + + /** @p z rounded up to an exact multiple + * of @ref c_alloc_alignment + **/ + static inline + std::size_t with_padding(std::size_t z, + std::size_t align = c_alloc_alignment) + { + return z + alloc_padding(z, align); + } + }; + + } /*namespace mm*/ +} /*namespace xo*/ + +/* end padding.hpp */