-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.h
69 lines (54 loc) · 1.97 KB
/
backend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <cstdint>
#if !(defined(ARDUINO) || defined(TEENSY4_1))
#include <array>
#include <mutex>
#endif
#include <set>
#include <string>
#if defined(ESP32)
#define N_BACKEND_LOCKS 4
#define LOCK_SPREADER 3
#else
#define N_BACKEND_LOCKS 131
#define LOCK_SPREADER 31
#endif
struct backend_stats_t {
uint64_t bytes_read;
uint64_t n_reads;
uint64_t bytes_written;
uint64_t n_writes;
uint64_t n_syncs;
uint64_t n_trims;
uint32_t io_wait; // total, in uS
uint32_t io_wait_ticks; // updated by maintenance_thread
};
class backend
{
protected:
const std::string identifier;
backend_stats_t bs;
uint64_t ts_last_acces { 0 };
#if !(defined(ARDUINO) || defined(TEENSY4_1) || defined(RP2040W))
std::array<std::mutex, N_BACKEND_LOCKS> locks;
#endif
std::set<size_t> lock_range (const uint64_t block_nr, const uint32_t block_n);
void unlock_range(const std::set<size_t> & locked_locks);
public:
backend(const std::string & identifier);
virtual ~backend();
virtual bool begin() = 0;
virtual std::string get_serial() const = 0;
virtual uint64_t get_size_in_blocks() const = 0;
virtual uint64_t get_block_size() const = 0;
// mainly for thin provisioning
virtual uint8_t get_free_space_percentage();
virtual std::pair<uint64_t, uint32_t> get_idle_state();
virtual bool sync() = 0;
void get_and_reset_stats(backend_stats_t *const target);
enum cmpwrite_result_t { CWR_OK, CWR_MISMATCH, CWR_READ_ERROR, CWR_WRITE_ERROR };
virtual bool write (const uint64_t block_nr, const uint32_t n_blocks, const uint8_t *const data) = 0;
virtual bool trim (const uint64_t block_nr, const uint32_t n_blocks ) = 0;
virtual bool read (const uint64_t block_nr, const uint32_t n_blocks, uint8_t *const data) = 0;
virtual backend::cmpwrite_result_t cmpwrite(const uint64_t block_nr, const uint32_t n_blocks, const uint8_t *const data_write, const uint8_t *const data_compare) = 0;
};