VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Buffer.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "Types.h"
16#include "Checksum.h"
17#include <filesystem>
18
19namespace vc64::util {
20
21namespace fs = ::std::filesystem;
22
23template <class T> struct Allocator {
24
25 static constexpr isize maxCapacity = 512 * 1024 * 1024;
26
27 T *&ptr;
28 isize size;
29
30 Allocator(T *&ptr) : ptr(ptr), size(0) { ptr = nullptr; }
31 Allocator(const Allocator&) = delete;
32 ~Allocator() { dealloc(); }
33
34 // Queries the buffer state
35 isize bytesize() const { return size * sizeof(T); }
36 bool empty() const { return size == 0; }
37 explicit operator bool() const { return !empty(); }
38
39 // Initializers
40 void alloc(isize elements);
41 void dealloc();
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);
47
48 // Resizes an existing buffer
49 void resize(isize elements);
50 void resize(isize elements, T pad);
51
52 // Overwrites elements with a default value
53 void clear(T value, isize offset, isize len);
54 void clear(T value = 0, isize offset = 0) { clear(value, offset, size - offset); }
55
56 // Imports or exports the buffer contents
57 void copy(T *buf, isize offset, isize len) const;
58 void copy(T *buf) const { copy(buf, 0, size); }
59
60 // Replaces a byte or character sequence by another one
61 void patch(const u8 *seq, const u8 *subst);
62 void patch(const char *seq, const char *subst);
63
64 // Computes a checksum of a certain kind
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; }
69};
70
71template <class T> struct Buffer : public Allocator <T> {
72
73 T *ptr = nullptr;
74
75 Buffer()
76 : Allocator<T>(ptr) { };
77 Buffer(isize bytes)
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); }
87
88 T operator [] (isize i) const { return ptr[i]; }
89 T &operator [] (isize i) { return ptr[i]; }
90};
91
92}