From a6161e7d0710a3b923027d56fdf818891c992f8d Mon Sep 17 00:00:00 2001 From: lizzie Date: Mon, 7 Sep 2026 23:30:42 +0000 Subject: [PATCH] 2026-09-07 23:30:42 Signed-off-by: lizzie --- src/audio_core/common/workbuffer_allocator.h | 40 +++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/audio_core/common/workbuffer_allocator.h b/src/audio_core/common/workbuffer_allocator.h index 1f4e05d0d9..fb89f97fea 100644 --- a/src/audio_core/common/workbuffer_allocator.h +++ b/src/audio_core/common/workbuffer_allocator.h @@ -1,6 +1,3 @@ -// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project -// SPDX-License-Identifier: GPL-3.0-or-later - // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -19,9 +16,8 @@ namespace AudioCore { */ class WorkbufferAllocator { public: - explicit WorkbufferAllocator(std::span buffer_) - : buffer{buffer_} - {} + explicit WorkbufferAllocator(std::span buffer_, u64 size_) + : buffer{reinterpret_cast(buffer_.data())}, size{size_} {} /** * Allocate the given count of T elements, aligned to alignment. @@ -33,31 +29,36 @@ public: template std::span Allocate(u64 count, u64 alignment) { u64 out{0}; - u64 byte_size = count * sizeof(T); + u64 byte_size{count * sizeof(T)}; + if (byte_size > 0) { - auto current{uintptr_t(buffer.data()) + offset}; + auto current{buffer + offset}; auto aligned_buffer{Common::AlignUp(current, alignment)}; - if (aligned_buffer + byte_size <= uintptr_t(buffer.data()) + buffer.size()) { + if (aligned_buffer + byte_size <= buffer + size) { out = aligned_buffer; - offset = byte_size - uintptr_t(buffer.data()) + aligned_buffer; + offset = byte_size - buffer + aligned_buffer; } else { LOG_ERROR( Service_Audio, "Allocated buffer was too small to hold new alloc.\nAllocator size={:08X}, " "offset={:08X}.\nAttempting to allocate {:08X} with alignment={:02X}", - buffer.size(), offset, byte_size, alignment); + size, offset, byte_size, alignment); count = 0; } } + return std::span(reinterpret_cast(out), count); } - /// @brief Align the current offset to the given alignment. - /// @param alignment - The required starting alignment. + /** + * Align the current offset to the given alignment. + * + * @param alignment - The required starting alignment. + */ void Align(u64 alignment) { - auto current{uintptr_t(buffer.data()) + offset}; + auto current{buffer + offset}; auto aligned_buffer{Common::AlignUp(current, alignment)}; - offset = 0 - uintptr_t(buffer.data()) + aligned_buffer; + offset = 0 - buffer + aligned_buffer; } /** @@ -75,7 +76,7 @@ public: * @return The size of the current buffer. */ u64 GetSize() const { - return buffer.size(); + return size; } /** @@ -84,11 +85,14 @@ public: * @return The remaining size left in the buffer. */ u64 GetRemainingSize() const { - return buffer.size() - offset; + return size - offset; } private: - const std::span buffer; + /// The buffer into which we are allocating. + u64 buffer; + /// Size of the buffer we're allocating to. + u64 size; /// Current offset into the buffer, an error will be thrown if it exceeds size. u64 offset{}; };