blob: ed0d23e2a2485f329985376f326d3153e044f36d [file] [log] [blame]
Brian Silvermanf51499a2020-09-21 12:49:08 -07001#ifndef AOS_CONTAINERS_RESIZEABLE_BUFFER_H_
2#define AOS_CONTAINERS_RESIZEABLE_BUFFER_H_
3
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdlib>
Brian Silvermanf51499a2020-09-21 12:49:08 -07005#include <memory>
6
7#include "glog/logging.h"
8
9namespace aos {
10
11// Kind of like a subset of vector<uint8_t>, but with less destructor calls.
12// When building unoptimized, especially with sanitizers, the vector<uint8_t>
13// version ends up being really slow in tests.
Austin Schuhbed6eeb2023-02-04 11:42:03 -080014//
15// F is the allocator used for reallocating the memory used by the buffer.
16template <class F>
17class AllocatorResizeableBuffer {
Brian Silvermanf51499a2020-09-21 12:49:08 -070018 public:
Austin Schuhbed6eeb2023-02-04 11:42:03 -080019 AllocatorResizeableBuffer() = default;
Brian Silvermanf51499a2020-09-21 12:49:08 -070020
Austin Schuhbed6eeb2023-02-04 11:42:03 -080021 AllocatorResizeableBuffer(const AllocatorResizeableBuffer &other) { *this = other; }
22 AllocatorResizeableBuffer(AllocatorResizeableBuffer &&other) { *this = std::move(other); }
23 AllocatorResizeableBuffer &operator=(const AllocatorResizeableBuffer &other) {
Brian Silvermanf51499a2020-09-21 12:49:08 -070024 resize(other.size());
25 memcpy(storage_.get(), other.storage_.get(), size());
26 return *this;
27 }
Austin Schuhbed6eeb2023-02-04 11:42:03 -080028 AllocatorResizeableBuffer &operator=(AllocatorResizeableBuffer &&other) {
Brian Silvermanf51499a2020-09-21 12:49:08 -070029 std::swap(storage_, other.storage_);
30 std::swap(size_, other.size_);
31 std::swap(capacity_, other.capacity_);
32 return *this;
33 }
34
35 uint8_t *data() { return static_cast<uint8_t *>(storage_.get()); }
36 const uint8_t *data() const {
37 return static_cast<const uint8_t *>(storage_.get());
38 }
39
40 uint8_t *begin() { return data(); }
41 const uint8_t *begin() const { return data(); }
42 uint8_t *end() { return data() + size(); }
43 const uint8_t *end() const { return data() + size(); }
44
45 size_t size() const { return size_; }
46 size_t capacity() const { return capacity_; }
47
Austin Schuh83548e92022-10-16 18:41:37 -070048 void reserve(size_t new_size) {
49 if (new_size > capacity_) {
50 Allocate(new_size);
51 }
52 }
53
Brian Silvermanf51499a2020-09-21 12:49:08 -070054 void resize(size_t new_size) {
55 if (new_size > capacity_) {
56 Allocate(new_size);
57 }
58 size_ = new_size;
59 }
60
61 void erase_front(size_t count) {
62 if (count == 0) {
63 return;
64 }
65 CHECK_LE(count, size_);
66 memmove(static_cast<void *>(data()), static_cast<void *>(data() + count),
67 size_ - count);
68 size_ -= count;
69 }
70
71 void push_back(uint8_t x) {
72 if (size_ == capacity_) {
73 Allocate(std::max<size_t>(16, size_ * 2));
74 }
75 *end() = x;
76 ++size_;
77 CHECK_LE(size_, capacity_);
78 }
79
80 private:
81 // We need this silly function because C++ is bad, and extensions to it which
82 // we work with make it a true nightmare.
83 //
84 // (a) You can't easily write out the signature of free because it depends on
85 // whether exceptions are enabled or not. You could use decltype, but see (b).
86 //
87 // (b) You can't easily write &free because CUDA overloads it with a
88 // __device__ version. You could cast to the appropriate version, but see (a).
89 //
90 // There's probably some kind of SFINAE thing which could find the matching
91 // signature from a set of choices, and then we could just
92 // static_cast<TheSignature>(&free). However, that sounds like a nightmare,
93 // especially because it has to conditionally enable the part mentioning CUDA
94 // identifiers in the preprocessor. This little function is way simpler.
95 static void DoFree(void *p) { free(p); }
96
97 void Allocate(size_t new_capacity) {
98 void *const old = storage_.release();
Austin Schuhbed6eeb2023-02-04 11:42:03 -080099 storage_.reset(CHECK_NOTNULL(F::Realloc(old, capacity_, new_capacity)));
Brian Silvermanf51499a2020-09-21 12:49:08 -0700100 capacity_ = new_capacity;
101 }
102
103 std::unique_ptr<void, decltype(&DoFree)> storage_{nullptr, &DoFree};
104 size_t size_ = 0, capacity_ = 0;
105};
106
Austin Schuhbed6eeb2023-02-04 11:42:03 -0800107// An allocator which just uses realloc to allocate.
108class Reallocator {
109 public:
110 static void *Realloc(void *old, size_t /*old_size*/, size_t new_capacity) {
111 return realloc(old, new_capacity);
112 }
113};
114
115// A resizable buffer which uses realloc when it needs to grow to attempt to
116// avoid full coppies.
117class ResizeableBuffer : public AllocatorResizeableBuffer<Reallocator> {};
118
Brian Silvermanf51499a2020-09-21 12:49:08 -0700119} // namespace aos
120
121#endif // AOS_CONTAINERS_RESIZEABLE_BUFFER_H_