Provide absl::InlinedVector specialization for N=0
Create an AOS version of the abseil InlinedVector that allows for N=0.
This provides a type that we can use to use stack-allocated memory for
the first N elements in a vector and then dynamically allocate all
subsequent memory.
Change-Id: Ia6d22a64deba1a841b188291bf79d2758ee94e99
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/containers/inlined_vector_test.cc b/aos/containers/inlined_vector_test.cc
new file mode 100644
index 0000000..23266e9
--- /dev/null
+++ b/aos/containers/inlined_vector_test.cc
@@ -0,0 +1,46 @@
+#include "aos/containers/inlined_vector.h"
+
+#include "gtest/gtest.h"
+
+#include "aos/realtime.h"
+
+DECLARE_bool(die_on_malloc);
+
+namespace aos {
+namespace testing {
+
+// Checks that we don't malloc until/unless we need to increase the size of the
+// vector.
+TEST(SizedArrayTest, NoUnnecessaryMalloc) {
+ gflags::FlagSaver flag_saver;
+ FLAGS_die_on_malloc = true;
+ RegisterMallocHook();
+ InlinedVector<int, 5> a;
+ {
+ aos::ScopedRealtime realtime;
+ a.push_back(9);
+ a.push_back(7);
+ a.push_back(1);
+ a.push_back(2);
+ a.push_back(3);
+
+ // And double-check that we can actually construct a new object at realtime.
+ InlinedVector<int, 5> b;
+ }
+ EXPECT_DEATH(
+ {
+ aos::ScopedRealtime realtime;
+ a.push_back(4);
+ },
+ "Malloced");
+}
+
+// Tests that we can create/define a vector with zero statically allocated
+// elements (the absl::InlinedVector does not allow this for some reason).
+TEST(SizedArrayTest, ZeroLengthVector) {
+ InlinedVector<int, 0> zero;
+ zero.push_back(1);
+ ASSERT_EQ(1, zero[0]);
+}
+} // namespace testing
+} // namespace aos