Repository navigation
bufferqueue: Use size_t for the memory usage counter - #344
Conversation
BufferQueue::mMemoryUsage is a std::atomic<std::uint64_t>. On 32-bit targets without native 64-bit atomic instructions, such as riscv32, its operations become __atomic_*_8 libcalls, so every program linking the static libbit7z needs libatomic, and the tests fail to link: ld.lld: error: undefined symbol: __atomic_fetch_add_8 >>> referenced by bufferqueue.cpp:21 (src/internal/bufferqueue.cpp:21) ld.lld: error: undefined symbol: __atomic_fetch_sub_8 ld.lld: error: undefined symbol: __atomic_load_8 The counter is the total size of the buffers currently held in the queue, i.e. a sum of std::vector::size() values of in-memory objects, so it always fits in a std::size_t. Use that type, which keeps the atomic lock-free on every target and removes the libatomic dependency. Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
|
Thanks for the fix and the detailed write-up!
Agreed, I'd rather not make every consumer link libatomic.
Yeah, The fact it is I'll likely drop the
Thanks for testing it on the actual target: CI doesn't cover riscv32. I'm not planning a 4.1.1 just for it, unless you need it, so it'll ship with the next release. |
|
Some CI tests fail for reasons outside of this PR, so I'm merging it. Thank you again! |
BufferQueue::mMemoryUsageis astd::atomic<std::uint64_t>. On 32-bit targets without native 64-bit atomic instructions (e.g. riscv32), its operations are lowered to__atomic_*_8libcalls provided by libatomic. bit7z does not link libatomic, so every program linking the static library fails, e.g. the tests when cross-compiling for riscv32 with clang:The counter is the total size of the buffers currently held in the queue, i.e. a sum of
std::vector::size()values of in-memory objects, so it always fits in astd::size_t. This changes its type tostd::atomic<std::size_t>, which is lock-free on every target and avoids the libatomic dependency, rather than adding libatomic to the link of every consumer. All accesses to the counter already happen with the queue mutex held, so behaviour is unchanged.Tested by cross-building bit7z 4.1.0 with the tests enabled in OpenEmbedded for riscv32 (musl, clang, lld): the library and the test executables link, with no
__atomic_*references left.