NuriKit v0.1.0b2
Loading...
Searching...
No Matches
dumb_buffer.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2025 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef NURI_CORE_CONTAINER_DUMB_BUFFER_H_
7#define NURI_CORE_CONTAINER_DUMB_BUFFER_H_
8
10#include <cstddef>
11#include <cstdlib>
12#include <cstring>
13#include <utility>
14
15#include <absl/log/absl_check.h>
17
18#include "nuri/meta.h"
19
20namespace nuri {
21namespace internal {
22 // NOLINTBEGIN(*-no-malloc,*-owning-memory)
23 template <class T>
24 class DumbBuffer {
25 constexpr static size_t bytes(size_t len) noexcept {
26 return len * sizeof(T);
27 }
28
29 static T *alloc(size_t len) noexcept {
30 void *buf = std::malloc(bytes(len));
31 ABSL_QCHECK(buf != nullptr);
32 return static_cast<T *>(buf);
33 }
34
35 static T *realloc(T *orig, size_t len) noexcept {
36 void *buf = std::realloc(static_cast<void *>(orig), bytes(len));
37 ABSL_QCHECK(buf != nullptr);
38 return static_cast<T *>(buf);
39 }
40
41 static void copy(T *dst, const T *src, size_t len) noexcept {
42 std::memcpy(static_cast<void *>(dst), static_cast<const void *>(src),
43 bytes(len));
44 }
45
46 public:
47 static_assert(std::is_same_v<T, remove_cvref_t<T>>,
48 "T must not have cv-qualifiers or reference");
49 static_assert(std::is_trivially_copyable_v<T>,
50 "T must be trivially copyable");
51 static_assert(alignof(T) <= alignof(std::max_align_t),
52 "T must have less than or equal alignment to all scalar "
53 "types");
54
55 DumbBuffer(size_t len) noexcept: data_(alloc(len)), len_(len) { }
56
57 DumbBuffer(const DumbBuffer &other) noexcept
58 : data_(alloc(other.len_)), len_(other.len_) {
59 copy(data_, other.data_, len_);
60 }
61
62 DumbBuffer(DumbBuffer &&other) noexcept
63 : data_(other.data_), len_(other.len_) {
64 other.data_ = nullptr;
65 other.len_ = 0;
66 }
67
68 ~DumbBuffer() noexcept { std::free(static_cast<void *>(data_)); }
69
70 DumbBuffer &operator=(const DumbBuffer &other) noexcept {
71 if (this == &other) {
72 return *this;
73 }
74
75 resize(other.len_);
76 copy(data_, other.data_, len_);
77 return *this;
78 }
79
80 DumbBuffer &operator=(DumbBuffer &&other) noexcept {
81 std::swap(data_, other.data_);
82 std::swap(len_, other.len_);
83 return *this;
84 }
85
86 T &operator[](size_t idx) noexcept { return data_[idx]; }
87
88 const T &operator[](size_t idx) const noexcept { return data_[idx]; }
89
90 T *data() noexcept { return data_; }
91
92 const T *data() const noexcept { return data_; }
93
94 size_t size() const noexcept { return len_; }
95
96 void resize(size_t len) noexcept {
97 data_ = realloc(data_, len_);
98 len_ = len;
99 }
100
101 T *begin() noexcept { return data_; }
102 T *end() noexcept { return data_ + len_; }
103
104 const T *cbegin() const noexcept { return data_; }
105 const T *cend() const noexcept { return data_ + len_; }
106 const T *begin() const noexcept { return cbegin(); }
107 const T *end() const noexcept { return cend(); }
108
109 private:
110 T *data_;
111 size_t len_;
112 };
113 // NOLINTEND(*-no-malloc,*-owning-memory)
114} // namespace internal
115} // namespace nuri
116
117#endif /* NURI_CORE_CONTAINER_DUMB_BUFFER_H_ */
Definition crdgen.h:16