Move AlignedReallocator to AOS and deduplicate

It is starting to get duplicated, let's just move it somewhere common
and remove all the duplicates that have showed up so far.

Change-Id: Ib3164e58561dbfb47779f902e44e838ea639c496
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/containers/resizeable_buffer.h b/aos/containers/resizeable_buffer.h
index 2f6ff32..9f9967f 100644
--- a/aos/containers/resizeable_buffer.h
+++ b/aos/containers/resizeable_buffer.h
@@ -118,6 +118,20 @@
   }
 };
 
+// Allocates aligned memory.
+template <size_t alignment>
+class AlignedReallocator {
+ public:
+  static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
+    void *new_memory = std::aligned_alloc(alignment, new_capacity);
+    if (old) {
+      memcpy(new_memory, old, old_size);
+      free(old);
+    }
+    return new_memory;
+  }
+};
+
 // A resizable buffer which uses realloc when it needs to grow to attempt to
 // avoid full coppies.
 class ResizeableBuffer : public AllocatorResizeableBuffer<Reallocator> {};
diff --git a/aos/events/logging/BUILD b/aos/events/logging/BUILD
index eadefa8..3b5f57a 100644
--- a/aos/events/logging/BUILD
+++ b/aos/events/logging/BUILD
@@ -1014,7 +1014,9 @@
     srcs = [
         "ssd_profiler.cc",
     ],
+    visibility = ["//visibility:public"],
     deps = [
+        ":log_backend",
         "//aos:init",
         "//aos/containers:resizeable_buffer",
         "//aos/time",
diff --git a/aos/events/logging/buffer_encoder.h b/aos/events/logging/buffer_encoder.h
index 394992c..a155bb5 100644
--- a/aos/events/logging/buffer_encoder.h
+++ b/aos/events/logging/buffer_encoder.h
@@ -15,6 +15,10 @@
  public:
   virtual ~DataEncoder() = default;
 
+  // Size of an aligned sector used to detect when the data is aligned enough to
+  // use O_DIRECT instead.
+  static constexpr size_t kSector = 512u;
+
   // Interface to copy data into a buffer.
   class Copier {
    public:
@@ -114,21 +118,7 @@
  private:
   size_t total_bytes_ = 0;
 
-  // A class which uses aligned_alloc to allocate sector aligned blocks of
-  // memory.
-  class AlignedReallocator {
-   public:
-    static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
-      void *new_memory = std::aligned_alloc(512, new_capacity);
-      if (old) {
-        memcpy(new_memory, old, old_size);
-        free(old);
-      }
-      return new_memory;
-    }
-  };
-
-  AllocatorResizeableBuffer<AlignedReallocator> input_buffer_;
+  AllocatorResizeableBuffer<aos::AlignedReallocator<kSector>> input_buffer_;
   std::vector<absl::Span<const uint8_t>> return_queue_;
 };
 
diff --git a/aos/events/logging/log_backend_test.cc b/aos/events/logging/log_backend_test.cc
index 1e95c10..0603b33 100644
--- a/aos/events/logging/log_backend_test.cc
+++ b/aos/events/logging/log_backend_test.cc
@@ -246,19 +246,10 @@
 struct FileWriteTestBase : public ::testing::Test {
   uint8_t NextRandom() { return distribution(engine); }
 
-  class AlignedReallocator {
-   public:
-    static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
-      void *new_memory = std::aligned_alloc(512, new_capacity);
-      if (old) {
-        memcpy(new_memory, old, old_size);
-        free(old);
-      }
-      return new_memory;
-    }
-  };
+  AllocatorResizeableBuffer<AlignedReallocator<aos::logger::FileHandler::kSector
 
-  AllocatorResizeableBuffer<AlignedReallocator> buffer;
+                                               >>
+      buffer;
 
   void TestRecipe(const WriteRecipe &recipe) {
     VLOG(1) << "Starting";
@@ -374,8 +365,8 @@
 
 // Test an aligned to unaligned transition to make sure everything works.
 TEST_F(FileWriteTestBase, AlignedToUnaligned) {
-  AllocatorResizeableBuffer<AlignedReallocator> aligned_buffer;
-  AllocatorResizeableBuffer<AlignedReallocator> unaligned_buffer;
+  AllocatorResizeableBuffer<AlignedReallocator<512>> aligned_buffer;
+  AllocatorResizeableBuffer<AlignedReallocator<512>> unaligned_buffer;
 
   aligned_buffer.resize(FileHandler::kSector * 4);
   std::generate(std::begin(aligned_buffer), std::end(aligned_buffer),
diff --git a/aos/events/logging/ssd_profiler.cc b/aos/events/logging/ssd_profiler.cc
index e7493ff..093e17b 100644
--- a/aos/events/logging/ssd_profiler.cc
+++ b/aos/events/logging/ssd_profiler.cc
@@ -12,6 +12,7 @@
 #include "glog/logging.h"
 
 #include "aos/containers/resizeable_buffer.h"
+#include "aos/events/logging/log_backend.h"
 #include "aos/init.h"
 #include "aos/realtime.h"
 #include "aos/time/time.h"
@@ -39,19 +40,6 @@
               "Write speed in MB/s to simulate. This is only used when "
               "--rate_limit is specified.");
 
-// Stolen from aos/events/logging/DummyEncoder
-class AlignedReallocator {
- public:
-  static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
-    void *new_memory = std::aligned_alloc(512, new_capacity);
-    if (old) {
-      memcpy(new_memory, old, old_size);
-      free(old);
-    }
-    return new_memory;
-  }
-};
-
 void trap_sig(int signum) { exit(signum); }
 
 aos::monotonic_clock::time_point start_time = aos::monotonic_clock::min_time;
@@ -82,7 +70,9 @@
     std::signal(SIGHUP, trap_sig);
     std::atexit(cleanup);
   }
-  aos::AllocatorResizeableBuffer<AlignedReallocator> data;
+  aos::AllocatorResizeableBuffer<
+      aos::AlignedReallocator<aos::logger::FileHandler::kSector>>
+      data;
 
   {
     // We want uncompressible data.  The easiest way to do this is to grab a