|
|
@ -1,3 +1,6 @@ |
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project |
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later |
|
|
|
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project |
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project |
|
|
// SPDX-License-Identifier: GPL-2.0-or-later |
|
|
// SPDX-License-Identifier: GPL-2.0-or-later |
|
|
|
|
|
|
|
|
@ -16,8 +19,9 @@ namespace AudioCore { |
|
|
*/ |
|
|
*/ |
|
|
class WorkbufferAllocator { |
|
|
class WorkbufferAllocator { |
|
|
public: |
|
|
public: |
|
|
explicit WorkbufferAllocator(std::span<u8> buffer_, u64 size_) |
|
|
|
|
|
: buffer{reinterpret_cast<u64>(buffer_.data())}, size{size_} {} |
|
|
|
|
|
|
|
|
explicit WorkbufferAllocator(std::span<u8> buffer_) |
|
|
|
|
|
: buffer{buffer_} |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Allocate the given count of T elements, aligned to alignment. |
|
|
* Allocate the given count of T elements, aligned to alignment. |
|
|
@ -29,36 +33,31 @@ public: |
|
|
template <typename T> |
|
|
template <typename T> |
|
|
std::span<T> Allocate(u64 count, u64 alignment) { |
|
|
std::span<T> Allocate(u64 count, u64 alignment) { |
|
|
u64 out{0}; |
|
|
u64 out{0}; |
|
|
u64 byte_size{count * sizeof(T)}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u64 byte_size = count * sizeof(T); |
|
|
if (byte_size > 0) { |
|
|
if (byte_size > 0) { |
|
|
auto current{buffer + offset}; |
|
|
|
|
|
|
|
|
auto current{uintptr_t(buffer.data()) + offset}; |
|
|
auto aligned_buffer{Common::AlignUp(current, alignment)}; |
|
|
auto aligned_buffer{Common::AlignUp(current, alignment)}; |
|
|
if (aligned_buffer + byte_size <= buffer + size) { |
|
|
|
|
|
|
|
|
if (aligned_buffer + byte_size <= uintptr_t(buffer.data()) + buffer.size()) { |
|
|
out = aligned_buffer; |
|
|
out = aligned_buffer; |
|
|
offset = byte_size - buffer + aligned_buffer; |
|
|
|
|
|
|
|
|
offset = byte_size - uintptr_t(buffer.data()) + aligned_buffer; |
|
|
} else { |
|
|
} else { |
|
|
LOG_ERROR( |
|
|
LOG_ERROR( |
|
|
Service_Audio, |
|
|
Service_Audio, |
|
|
"Allocated buffer was too small to hold new alloc.\nAllocator size={:08X}, " |
|
|
"Allocated buffer was too small to hold new alloc.\nAllocator size={:08X}, " |
|
|
"offset={:08X}.\nAttempting to allocate {:08X} with alignment={:02X}", |
|
|
"offset={:08X}.\nAttempting to allocate {:08X} with alignment={:02X}", |
|
|
size, offset, byte_size, alignment); |
|
|
|
|
|
|
|
|
buffer.size(), offset, byte_size, alignment); |
|
|
count = 0; |
|
|
count = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return std::span<T>(reinterpret_cast<T*>(out), count); |
|
|
return std::span<T>(reinterpret_cast<T*>(out), count); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Align the current offset to the given alignment. |
|
|
|
|
|
* |
|
|
|
|
|
* @param alignment - The required starting alignment. |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
/// @brief Align the current offset to the given alignment. |
|
|
|
|
|
/// @param alignment - The required starting alignment. |
|
|
void Align(u64 alignment) { |
|
|
void Align(u64 alignment) { |
|
|
auto current{buffer + offset}; |
|
|
|
|
|
|
|
|
auto current{uintptr_t(buffer.data()) + offset}; |
|
|
auto aligned_buffer{Common::AlignUp(current, alignment)}; |
|
|
auto aligned_buffer{Common::AlignUp(current, alignment)}; |
|
|
offset = 0 - buffer + aligned_buffer; |
|
|
|
|
|
|
|
|
offset = 0 - uintptr_t(buffer.data()) + aligned_buffer; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -76,7 +75,7 @@ public: |
|
|
* @return The size of the current buffer. |
|
|
* @return The size of the current buffer. |
|
|
*/ |
|
|
*/ |
|
|
u64 GetSize() const { |
|
|
u64 GetSize() const { |
|
|
return size; |
|
|
|
|
|
|
|
|
return buffer.size(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -85,14 +84,11 @@ public: |
|
|
* @return The remaining size left in the buffer. |
|
|
* @return The remaining size left in the buffer. |
|
|
*/ |
|
|
*/ |
|
|
u64 GetRemainingSize() const { |
|
|
u64 GetRemainingSize() const { |
|
|
return size - offset; |
|
|
|
|
|
|
|
|
return buffer.size() - offset; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
|
/// The buffer into which we are allocating. |
|
|
|
|
|
u64 buffer; |
|
|
|
|
|
/// Size of the buffer we're allocating to. |
|
|
|
|
|
u64 size; |
|
|
|
|
|
|
|
|
const std::span<u8> buffer; |
|
|
/// Current offset into the buffer, an error will be thrown if it exceeds size. |
|
|
/// Current offset into the buffer, an error will be thrown if it exceeds size. |
|
|
u64 offset{}; |
|
|
u64 offset{}; |
|
|
}; |
|
|
}; |
|
|
|