forked from ShawnZhong/MadFS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx_block.h
93 lines (80 loc) · 3.03 KB
/
tx_block.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "alloc/alloc.h"
#include "block/tx.h"
#include "cursor/tx_entry.h"
#include "idx.h"
#include "mem_table.h"
#include "utils/timer.h"
namespace madfs::dram {
struct TxBlockCursor {
LogicalBlockIdx idx;
union {
pmem::TxBlock* block;
pmem::MetaBlock* meta;
void* addr;
};
explicit TxBlockCursor(pmem::MetaBlock* meta) : idx(), meta(meta) {}
explicit TxBlockCursor(TxCursor cursor)
: idx(cursor.idx.block_idx), block(cursor.block) {}
TxBlockCursor(LogicalBlockIdx idx, MemTable* mem_table)
: idx(idx),
addr(idx == LogicalBlockIdx::max() ? nullptr
: mem_table->lidx_to_addr_rw(idx)) {}
static TxBlockCursor max() { return {LogicalBlockIdx::max(), nullptr}; }
/**
* Advance to the first entry of the next tx block
* @param mem_table used to find the memory address of the next block
* @return true on success; false when reaches the end
*/
bool advance_to_next_block(MemTable* mem_table) {
assert(addr != nullptr);
LogicalBlockIdx next_idx =
idx == 0 ? meta->get_next_tx_block() : block->get_next_tx_block();
if (next_idx == 0) return false;
idx = next_idx;
addr = mem_table->lidx_to_addr_rw(idx);
return true;
}
/**
* Advance to the next orphan tx block
* @param mem_table used to find the memory address of the next block
* @return true on success; false when reaches the end
*/
bool advance_to_next_orphan(MemTable* mem_table) {
assert(addr != nullptr);
LogicalBlockIdx next_idx = idx == 0 ? meta->get_next_orphan_block()
: block->get_next_orphan_block();
if (next_idx == 0) return false;
idx = next_idx;
addr = mem_table->lidx_to_addr_rw(idx);
return true;
}
void set_next_orphan_block(LogicalBlockIdx block_idx) const {
assert(addr != nullptr);
return idx == 0 ? meta->set_next_orphan_block(block_idx)
: block->set_next_orphan_block(block_idx);
}
[[nodiscard]] LogicalBlockIdx get_next_orphan_block() const {
assert(addr != nullptr);
return idx == 0 ? meta->get_next_orphan_block()
: block->get_next_orphan_block();
}
/**
* NOTE: the ordering of two tx_blocks denotes "at some points, one block is
* ahead of the other block on the linked list," and this is only determined
* by their tx_seq. two tx blocks may never on the same linked list, which
* makes such comparison meaningless. this function would return true/false
* anyway, but it is the caller's responsibility to ensure such ordering is
* meaningful.
*/
friend bool operator<(const TxBlockCursor& lhs, const TxBlockCursor& rhs) {
if (lhs.idx == rhs.idx) return false;
if (lhs.idx == LogicalBlockIdx::max()) return false;
if (rhs.idx == LogicalBlockIdx::max()) return true;
if (lhs.idx == 0) return true;
if (rhs.idx == 0) return false;
return lhs.block->get_tx_seq() < rhs.block->get_tx_seq();
}
};
static_assert(sizeof(TxBlockCursor) == 16);
} // namespace madfs::dram