21namespace fs = ::std::filesystem;
23template <
class T>
struct Allocator {
25 static constexpr isize maxCapacity = 512 * 1024 * 1024;
30 Allocator(T *&ptr) : ptr(ptr), size(0) { ptr =
nullptr; }
31 Allocator(
const Allocator&) =
delete;
32 ~Allocator() { dealloc(); }
35 isize bytesize()
const {
return size *
sizeof(T); }
36 bool empty()
const {
return size == 0; }
37 explicit operator bool()
const {
return !empty(); }
40 void alloc(isize elements);
42 void init(isize elements, T value = 0);
43 void init(
const T *buf, isize elements);
44 void init(
const Allocator<T> &other);
45 void init(
const fs::path &path);
46 void init(
const fs::path &path,
const string &name);
49 void resize(isize elements);
50 void resize(isize elements, T pad);
53 void clear(T value, isize offset, isize len);
54 void clear(T value = 0, isize offset = 0) { clear(value, offset, size - offset); }
57 void copy(T *buf, isize offset, isize len)
const;
58 void copy(T *buf)
const { copy(buf, 0, size); }
61 void patch(
const u8 *seq,
const u8 *subst);
62 void patch(
const char *seq,
const char *subst);
65 u32 fnv32()
const {
return ptr ? util::fnv32((u8 *)ptr, bytesize()) : 0; }
66 u64 fnv64()
const {
return ptr ? util::fnv64((u8 *)ptr, bytesize()) : 0; }
67 u16 crc16()
const {
return ptr ? util::crc16((u8 *)ptr, bytesize()) : 0; }
68 u32 crc32()
const {
return ptr ? util::crc32((u8 *)ptr, bytesize()) : 0; }
71template <
class T>
struct Buffer :
public Allocator <T> {
76 : Allocator<T>(ptr) { };
78 : Allocator<T>(ptr) { this->init(bytes); }
79 Buffer(isize bytes, T value)
80 : Allocator<T>(ptr) { this->init(bytes, value); }
81 Buffer(
const T *buf, isize len)
82 : Allocator<T>(ptr) { this->init(buf, len); }
83 Buffer(
const fs::path &path)
84 : Allocator<T>(ptr) { this->init(path); }
85 Buffer(
const fs::path &path,
const string &name)
86 : Allocator<T>(ptr) { this->init(path, name); }
88 T operator [] (isize i)
const {
return ptr[i]; }
89 T &operator [] (isize i) {
return ptr[i]; }