From patchwork Tue Jan 6 16:57:38 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 25636 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2974BBDCBF for ; Tue, 6 Jan 2026 16:58:14 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 58A0B61FC1; Tue, 6 Jan 2026 17:58:05 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KoNuWvF4"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 77E3A61FC1 for ; Tue, 6 Jan 2026 17:57:59 +0100 (CET) Received: from pb-laptop.local (185.221.143.114.nat.pool.zt.hu [185.221.143.114]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 697A51733; Tue, 6 Jan 2026 17:57:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1767718658; bh=7ADgsOd5Kh8W+29vzIwsn64OFI56wj3oNkbV31uALAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KoNuWvF4YeRFMqEpxIrsV5QuXWkEunvkaTXBXVf0vRvbMA7ONQtI+cfHbsizYitRo sBaya+YfCkV2/miXujvMCnJqVP9jo1Ifwc18kks/d1Zhe1NmpUYS7dRp/49TRPQHsC 2kqHz9UrVLHXFuypzD2D8B/5dpqyXIjpetxYh4gA= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , Kieran Bingham Subject: [PATCH v4 06/22] libcamera: base: Add alignment utility functions Date: Tue, 6 Jan 2026 17:57:38 +0100 Message-ID: <20260106165754.1759831-7-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260106165754.1759831-1-barnabas.pocze@ideasonboard.com> References: <20260106165754.1759831-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a couple internal functions to check alignment, and to align integers, pointers up to a given alignment. Signed-off-by: Barnabás Pőcze Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- changes in v3: * documentation --- include/libcamera/base/internal/align.h | 135 ++++++++++++++++++++++++ include/libcamera/base/meson.build | 1 + 2 files changed, 136 insertions(+) create mode 100644 include/libcamera/base/internal/align.h diff --git a/include/libcamera/base/internal/align.h b/include/libcamera/base/internal/align.h new file mode 100644 index 000000000..d8ee4e369 --- /dev/null +++ b/include/libcamera/base/internal/align.h @@ -0,0 +1,135 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2025, Ideas on Board Oy + * + * Alignment utilities + */ + +#pragma once + +#include +#include +#include + +#include + +/** + * \internal + * \file align.h + * \brief Utilities for handling alignment + */ + +namespace libcamera::internal::align { + +/** + * \internal + * \brief Check if pointer is aligned + * \param[in] p pointer to check + * \param[in] alignment desired alignment + * \return true if \a p is at least \a alignment aligned, false otherwise + */ +inline bool is(const void *p, std::uintptr_t alignment) +{ + assert(alignment > 0); + + return reinterpret_cast(p) % alignment == 0; +} + +/** + * \internal + * \brief Align number up + * \param[in] x number to check + * \param[in] alignment desired alignment + * \return true if \a p is at least \a alignment aligned, false otherwise + */ +template +constexpr T up(T x, cxx20::type_identity_t alignment) +{ + static_assert(std::is_unsigned_v); + assert(alignment > 0); + + const auto padding = (alignment - (x % alignment)) % alignment; + assert(x + padding >= x); + + return x + padding; +} + +/** + * \internal + * \brief Align pointer up + * \param[in] p pointer to align + * \param[in] alignment desired alignment + * \return \a p up-aligned to \a alignment + */ +template +auto *up(T *p, std::uintptr_t alignment) +{ + using U = std::conditional_t< + std::is_const_v, + const std::byte, + std::byte + >; + + return reinterpret_cast(up(reinterpret_cast(p), alignment)); +} + +/** + * \internal + * \brief Align pointer up + * \param[in] size required number of bytes + * \param[in] alignment required alignment + * \param[in] ptr base pointer + * \param[in] avail number of available bytes + * + * This function checks if the storage starting at \a ptr and continuing for \a avail + * bytes (if \a avail is nullptr, then no size checking is done) can accommodate \a size + * bytes of data aligned to \a alignment. If so, then a pointer to the start is the returned + * and \a ptr is adjusted to point right after the section of \a size bytes. If present, + * \a avail is also adjusted. + * + * Similar to std::align(). + * + * \return the appropriately aligned pointer, or nullptr if there is not enough space + */ +template +T *up(std::size_t size, std::size_t alignment, T *&ptr, std::size_t *avail = nullptr) +{ + assert(alignment > 0); + + auto p = reinterpret_cast(ptr); + const auto padding = (alignment - (p % alignment)) % alignment; + + if (avail) { + if (size > *avail || padding > *avail - size) + return nullptr; + + *avail -= size + padding; + } + + p += padding; + ptr = reinterpret_cast(p + size); + + return reinterpret_cast(p); +} + +/** + * \internal + * \brief Align pointer up + * \tparam U desired type + * \param[in] ptr base pointer + * \param[in] avail number of available bytes + * + * A convenience wrapper around libcamera::internal::align::up(std::size_t, std::size_t, T *&, std::size_t *) + * that takes the size and alignment information from the type \a U. + * + * \sa libcamera::internal::align::up(std::size_t, std::size_t, T *&, std::size_t *) + */ +template +U *up(T *&ptr, std::size_t *avail = nullptr) +{ + return reinterpret_cast, const U, U> *>( + up(sizeof(U), alignof(U), ptr, avail) + ); +} + +} /* namespace libcamera::internal::align */ diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index bb4ed556b..305dfa0dd 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -34,6 +34,7 @@ libcamera_base_private_headers = files([ libcamera_base_internal_headers = files([ 'internal/cxx20.h', + 'internal/align.h', ]) libcamera_base_headers = [