Encode flatbuffers directly into the encoder when logging

We were running out of memory when running for many hours.  Initial
debugging looked like it was a heap fragmentation issue.  Tracking the
allocated memory using the malloc hooks wasn't showing any growth of
memory.  The heap was growing though.

Instead of allocating a FlatBufferBuilder/DetachedBuffer for each
message to be logged, we can instead have the BufferEncoder provide
memory to write to, and have it only alloate that buffer space once, and
allocate it to the maximum size that a writer might see.

Change-Id: I046bd2422aea368867b0c63cee7d04c6033fe724
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/logging/lzma_encoder_test.cc b/aos/events/logging/lzma_encoder_test.cc
index 1b8c895..1e814ff 100644
--- a/aos/events/logging/lzma_encoder_test.cc
+++ b/aos/events/logging/lzma_encoder_test.cc
@@ -11,9 +11,10 @@
 
 INSTANTIATE_TEST_SUITE_P(
     MtLzma, BufferEncoderTest,
-    ::testing::Combine(::testing::Values([]() {
+    ::testing::Combine(::testing::Values([](size_t max_message_size) {
                          FLAGS_lzma_threads = 3;
-                         return std::make_unique<LzmaEncoder>(2, 4096);
+                         return std::make_unique<LzmaEncoder>(max_message_size,
+                                                              2, 4096);
                        }),
                        ::testing::Values([](std::string_view filename) {
                          return std::make_unique<LzmaDecoder>(filename);
@@ -22,9 +23,10 @@
 
 INSTANTIATE_TEST_SUITE_P(
     MtLzmaThreaded, BufferEncoderTest,
-    ::testing::Combine(::testing::Values([]() {
+    ::testing::Combine(::testing::Values([](size_t max_message_size) {
                          FLAGS_lzma_threads = 3;
-                         return std::make_unique<LzmaEncoder>(5, 4096);
+                         return std::make_unique<LzmaEncoder>(max_message_size,
+                                                              5, 4096);
                        }),
                        ::testing::Values([](std::string_view filename) {
                          return std::make_unique<ThreadedLzmaDecoder>(filename);
@@ -33,9 +35,10 @@
 
 INSTANTIATE_TEST_SUITE_P(
     Lzma, BufferEncoderTest,
-    ::testing::Combine(::testing::Values([]() {
+    ::testing::Combine(::testing::Values([](size_t max_message_size) {
                          FLAGS_lzma_threads = 1;
-                         return std::make_unique<LzmaEncoder>(2, 4096);
+                         return std::make_unique<LzmaEncoder>(max_message_size,
+                                                              2, 4096);
                        }),
                        ::testing::Values([](std::string_view filename) {
                          return std::make_unique<LzmaDecoder>(filename);
@@ -44,9 +47,10 @@
 
 INSTANTIATE_TEST_SUITE_P(
     LzmaThreaded, BufferEncoderTest,
-    ::testing::Combine(::testing::Values([]() {
+    ::testing::Combine(::testing::Values([](size_t max_message_size) {
                          FLAGS_lzma_threads = 1;
-                         return std::make_unique<LzmaEncoder>(5, 4096);
+                         return std::make_unique<LzmaEncoder>(max_message_size,
+                                                              5, 4096);
                        }),
                        ::testing::Values([](std::string_view filename) {
                          return std::make_unique<ThreadedLzmaDecoder>(filename);
@@ -63,7 +67,8 @@
   std::vector<std::vector<uint8_t>> encoded_buffers;
   {
     const int encode_chunks = quantity_distribution(*random_number_generator());
-    const auto encoder = std::make_unique<LzmaEncoder>(2);
+    const auto encoder = std::make_unique<LzmaEncoder>(
+        BufferEncoderBaseTest::kMaxMessageSize, 2);
     encoded_buffers = CreateAndEncode(encode_chunks, encoder.get());
     encoder->Finish();