Create a static flatbuffer FixedStackAllocator
This allows us to create builders for flatbuffers that don't have
dynamic memory requirements without mallocing.
Signed-off-by: Maxwell Henderson <mxwhenderson@gmail.com>
Change-Id: Ic4a0d6b1bdc807ba0af47b0db44197becefa349d
diff --git a/aos/flatbuffers/base.h b/aos/flatbuffers/base.h
index 87e07c5..f99dbb8 100644
--- a/aos/flatbuffers/base.h
+++ b/aos/flatbuffers/base.h
@@ -277,6 +277,19 @@
size_t allocated_size_ = 0;
};
+// Allocates and owns a fixed-size memory buffer on the stack.
+//
+// This provides a convenient Allocator for use with the aos::fbs::Builder
+// in realtime code instead of trying to use the VectorAllocator.
+template <std::size_t N>
+class FixedStackAllocator : public SpanAllocator {
+ public:
+ FixedStackAllocator() : SpanAllocator({buffer_, sizeof(buffer_)}) {}
+
+ private:
+ uint8_t buffer_[N];
+};
+
namespace internal {
std::ostream &DebugBytes(std::span<const uint8_t> span, std::ostream &os);
inline void ClearSpan(std::span<uint8_t> span) {