mirror of
https://github.com/halleysfifthinc/AVCLAN-Mockingboard.git
synced 2026-08-22 13:32:52 +00:00
Redesign (simplify) top-level avclan interface to use expected/nullable unique_ptr
- Peripheral::read/send now return `expected<unique_ptr<Frame>, Error>` for a unified success/error interface. - Peripheral::route returns `expected<unique_ptr<Frame>, Error>` to distinguish intent: (intentional) non-response vs unable to respond - Other functions with optional message semantics (handle,poll,react) return a unique_ptr whose ownership-state indicates response intent (i.e. send new message) Bundled necessary changes: - Switch Frame allocation from global to local (enabled via member function new/delete) - Add new header-library tl::expected to shim avr-libstdcpp Unrelated: Defensive `continue` added after `route`, to reduce Bus activity check latency Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -9,12 +9,62 @@
|
||||
|
||||
#include "frame.hpp"
|
||||
|
||||
#if defined(AVCLAN_FRAME_POOL_N)
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <new>
|
||||
|
||||
namespace {
|
||||
template <class T, std::uint8_t N>
|
||||
requires(N >= 1 && N <= std::numeric_limits<uint8_t>::max())
|
||||
class Pool {
|
||||
public:
|
||||
constexpr Pool() {
|
||||
for (uint8_t i = 0; i < N; ++i)
|
||||
ptrs_[i] = &storage_[i];
|
||||
}
|
||||
|
||||
T *acquire() {
|
||||
if (top_ == 0)
|
||||
return nullptr;
|
||||
return ptrs_[--top_];
|
||||
}
|
||||
|
||||
void release(T *ptr) {
|
||||
// Properly would need an origin check/confirmation if this was used more
|
||||
// generally
|
||||
ptrs_[top_++] = ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<T, N> storage_;
|
||||
std::array<T *, N> ptrs_;
|
||||
uint8_t top_ = N;
|
||||
};
|
||||
|
||||
Pool<avclan::Frame, AVCLAN_FRAME_POOL_N> pool;
|
||||
} // namespace
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
using Error = avclan::detail::Error;
|
||||
using enum Error::Parse;
|
||||
} // namespace
|
||||
|
||||
namespace avclan {
|
||||
|
||||
#if defined(AVCLAN_FRAME_POOL_N)
|
||||
void *Frame::operator new(std::size_t /*count*/,
|
||||
const std::nothrow_t & /*tag*/) noexcept {
|
||||
return pool.acquire();
|
||||
}
|
||||
// NOLINTNEXTLINE(misc-new-delete-overloads) false-positive
|
||||
void Frame::operator delete(void *ptr) noexcept {
|
||||
pool.release(static_cast<Frame *>(ptr));
|
||||
}
|
||||
#endif
|
||||
|
||||
void Frame::print(Frame::Print print) const {
|
||||
if (print.binary) {
|
||||
uint8_t buffer[8];
|
||||
|
||||
Reference in New Issue
Block a user