Debian Codegraph @

unittest/gunit/innodb/lob/lot0plist.h

raw symbols SCIP
1 /*****************************************************************************
2
3 Copyright (c) 2016, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27 #ifndef _lot0plist_h_
28 #define _lot0plist_h_
29
30 /** The Page List */
31
32 #include "lot0types.h"
33 #include "mach0data.h"
34 #include "mtr0log.h"
35 #include "mtr0types.h"
36
37 using paddr_t = ulint;
38
39 /** The node of page list. */
40 struct plist_node_t {
41 static const ulint OFFSET_PREV = 0; /* 2 bytes */
42 static const ulint OFFSET_NEXT = 2; /* 2 bytes */
43 static const ulint SIZE = 4;
44
45 plist_node_t() : m_frame(nullptr), m_node(nullptr) {}
46 plist_node_t(byte *frame, byte *node) : m_frame(frame), m_node(node) {}
47
48 bool is_before(const plist_node_t &node) const {
49 ut_ad(!is_null());
50 ut_ad(!node.is_null());
51 return (addr() < node.addr());
52 }
53
54 void init() {
55 ut_ad(m_node != nullptr);
56 mlog_write_ulint(m_node + OFFSET_PREV, 0, MLOG_2BYTES);
57 mlog_write_ulint(m_node + OFFSET_NEXT, 0, MLOG_2BYTES);
58 }
59
60 void set_prev(paddr_t addr) {
61 ut_ad(addr < UNIV_PAGE_SIZE);
62 mlog_write_ulint(m_node + OFFSET_PREV, addr, MLOG_2BYTES);
63 }
64
65 void set_prev_node(plist_node_t &prev) { set_prev(prev.addr()); }
66
67 void set_next(paddr_t addr) {
68 ut_ad(!is_null());
69 ut_ad(addr < UNIV_PAGE_SIZE);
70 mlog_write_ulint(m_node + OFFSET_NEXT, addr, MLOG_2BYTES);
71 }
72
73 void set_next_node(const plist_node_t &next) { set_next(next.addr()); }
74
75 paddr_t get_prev() const { return (mach_read_from_2(m_node + OFFSET_PREV)); }
76
77 paddr_t get_next() const { return (mach_read_from_2(m_node + OFFSET_NEXT)); }
78
79 plist_node_t get_next_node() const {
80 paddr_t addr = get_next();
81 byte *node = nullptr;
82 if (addr != 0) {
83 node = m_frame + addr;
84 }
85 return (plist_node_t(m_frame, node));
86 }
87
88 plist_node_t get_prev_node() const {
89 paddr_t addr = get_prev();
90 byte *node = nullptr;
91 if (addr != 0) {
92 node = m_frame + addr;
93 }
94 return (plist_node_t(m_frame, node));
95 }
96
97 paddr_t addr() const { return (m_node == nullptr) ? 0 : (m_node - m_frame); }
98
99 byte *ptr() const { return (m_node); }
100
101 bool is_null() const { return (m_node == nullptr); }
102
103 std::ostream &print(std::ostream &out) const {
104 out << "[plist_node_t: next=" << get_next() << ", prev=" << get_prev()
105 << ", this=" << addr() << "]";
106 return (out);
107 }
108
109 void set_frame(byte *frame) { m_frame = frame; }
110 void set_node(byte *node) { m_node = node; }
111
112 byte *m_frame;
113 byte *m_node;
114 };
115
116 inline std::ostream &operator<<(std::ostream &out, const plist_node_t &obj) {
117 return (obj.print(out));
118 }
119
120 /** The base node of page list. */
121 struct plist_base_node_t {
122 static const ulint OFFSET_LEN = 0; /* 4 bytes */
123 static const ulint OFFSET_FIRST = 4; /* 2 bytes */
124 static const ulint OFFSET_LAST = 6; /* 2 bytes */
125
126 /** The total size (in bytes) of a page list base node. */
127 static const ulint SIZE = 8;
128
129 plist_base_node_t(byte *frame, byte *base) : m_frame(frame), m_base(base) {}
130
131 void init() {
132 mlog_write_ulint(m_base + OFFSET_LEN, 0, MLOG_4BYTES);
133 mlog_write_ulint(m_base + OFFSET_FIRST, 0, MLOG_2BYTES);
134 mlog_write_ulint(m_base + OFFSET_LAST, 0, MLOG_2BYTES);
135 }
136
137 void remove(plist_node_t &node) {
138 plist_node_t prev = node.get_prev_node();
139 plist_node_t next = node.get_next_node();
140
141 if (prev.is_null()) {
142 set_first(next.addr());
143 } else {
144 prev.set_next(next.addr());
145 }
146
147 if (next.is_null()) {
148 set_last(prev.addr());
149 } else {
150 next.set_prev(prev.addr());
151 }
152
153 node.set_next(0);
154 node.set_prev(0);
155
156 decr_len();
157 }
158
159 void push_front(plist_node_t &node) {
160 if (get_len() == 0) {
161 add_to_empty(node);
162 } else {
163 paddr_t cur_addr = node.addr();
164 paddr_t first_addr = get_first();
165 plist_node_t first_node = get_node(first_addr);
166 node.set_next(first_addr);
167 node.set_prev(0);
168 first_node.set_prev(cur_addr);
169 set_first(cur_addr);
170 incr_len();
171 }
172 }
173
174 /** Insert node2 after node1. */
175 void insert_after(plist_node_t &node1, plist_node_t &node2) {
176 if (node1.is_null()) {
177 push_back(node2);
178 } else {
179 plist_node_t node3 = node1.get_next_node();
180 node1.set_next_node(node2);
181 node2.set_next_node(node3);
182
183 if (node3.is_null()) {
184 set_last(node2.addr());
185 } else {
186 node3.set_prev_node(node2);
187 }
188
189 node2.set_prev_node(node1);
190
191 incr_len();
192 }
193 }
194
195 /** Insert node2 before node3. */
196 void insert_before(plist_node_t &node3, plist_node_t &node2) {
197 if (node3.is_null()) {
198 push_back(node2);
199 } else {
200 plist_node_t node1 = node3.get_prev_node();
201
202 if (node1.is_null()) {
203 set_first(node2.addr());
204 } else {
205 node1.set_next_node(node2);
206 }
207
208 node2.set_next_node(node3);
209 node3.set_prev_node(node2);
210 node2.set_prev_node(node1);
211
212 incr_len();
213 }
214 }
215
216 void add_to_empty(plist_node_t &node) {
217 ut_ad(get_len() == 0);
218
219 set_first(node.addr());
220 set_last(node.addr());
221 incr_len();
222 }
223
224 void push_back(plist_node_t &node) {
225 if (get_len() == 0) {
226 add_to_empty(node);
227 } else {
228 paddr_t cur_addr = node.addr();
229 paddr_t last_addr = get_last();
230 plist_node_t last_node = get_node(last_addr);
231 node.set_next(0);
232 node.set_prev_node(last_node);
233 last_node.set_next(cur_addr);
234 set_last(cur_addr);
235 incr_len();
236 }
237 }
238
239 bool empty() const { return (get_len() == 0); }
240
241 ulint get_len() const { return (mach_read_from_4(m_base + OFFSET_LEN)); }
242
243 paddr_t get_first() const {
244 return (mach_read_from_2(m_base + OFFSET_FIRST));
245 }
246
247 plist_node_t get_first_node() const {
248 plist_node_t result;
249 result.set_frame(m_frame);
250
251 if (!empty()) {
252 byte *node = m_frame + get_first();
253 result.set_node(node);
254 }
255 return (result);
256 }
257
258 paddr_t get_last() const { return (mach_read_from_2(m_base + OFFSET_LAST)); }
259
260 plist_node_t get_last_node() const {
261 plist_node_t result;
262 result.set_frame(m_frame);
263 if (!empty()) {
264 result.set_node(m_frame + get_last());
265 }
266 return (result);
267 }
268
269 void set_len(ulint len) {
270 mlog_write_ulint(m_base + OFFSET_LEN, len, MLOG_4BYTES);
271 }
272
273 void incr_len() {
274 ulint len = mach_read_from_4(m_base + OFFSET_LEN);
275 mlog_write_ulint(m_base + OFFSET_LEN, len + 1, MLOG_4BYTES);
276 }
277
278 void decr_len() {
279 ulint len = mach_read_from_4(m_base + OFFSET_LEN);
280 mlog_write_ulint(m_base + OFFSET_LEN, len - 1, MLOG_4BYTES);
281 }
282
283 void set_first(paddr_t addr) {
284 mlog_write_ulint(m_base + OFFSET_FIRST, addr, MLOG_2BYTES);
285 }
286
287 void set_last(paddr_t addr) {
288 mlog_write_ulint(m_base + OFFSET_LAST, addr, MLOG_2BYTES);
289 }
290
291 plist_node_t get_node(paddr_t addr) {
292 byte *node = m_frame + addr;
293 return (plist_node_t(m_frame, node));
294 }
295
296 paddr_t addr() const { return (m_base - m_frame); }
297
298 std::ostream &print(std::ostream &out) const {
299 out << "[plist_base_node_t: len=" << get_len() << ", first=" << get_first()
300 << ", last=" << get_last() << ", this=" << addr() << "]";
301 return (out);
302 }
303
304 std::ostream &print_list(std::ostream &out) const {
305 print(out);
306 out << std::endl;
307 for (plist_node_t cur = get_first_node(); !cur.is_null();
308 cur = cur.get_next_node()) {
309 out << cur << std::endl;
310 }
311 return (out);
312 }
313
314 byte *m_frame;
315 byte *m_base;
316 };
317
318 inline std::ostream &operator<<(std::ostream &out,
319 const plist_base_node_t &obj) {
320 return (obj.print(out));
321 }
322
323 #endif // _lot0plist_h_