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> {};