1 /*****************************************************************************
2 
3 Copyright (c) 2016, 2017, Oracle and/or its affiliates. All Rights Reserved.
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 also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 #ifndef _fut0lst_h_
27 #define _fut0lst_h_
28 
29 #include "fil0fil.h"
30 #include "lot0types.h"
31 #include "mach0data.h"
32 #include "mtr0log.h"
33 #include "mtr0types.h"
34 #include "ut0byte.h"
35 #include "ut0dbg.h"
36 
37 /* We define the field offsets of a node for the list */
38 #define FLST_PREV                                   \
39   0 /* 6-byte address of the previous list element; \
40     the page part of address is FIL_NULL, if no     \
41     previous element */
42 #define FLST_NEXT                              \
43   FIL_ADDR_SIZE /* 6-byte address of the next  \
44         list element; the page part of address \
45         is FIL_NULL, if no next element */
46 
47 /* We define the field offsets of a base node for the list */
48 #define FLST_LEN 0 /* 32-bit list length field */
49 #define FLST_FIRST                         \
50   4 /* 6-byte address of the first element \
51     of the list; undefined if empty list */
52 #define FLST_LAST                              \
53   (4 + FIL_ADDR_SIZE) /* 6-byte address of the \
54           last element of the list; undefined  \
55           if empty list */
56 
57 /* The physical size of a list base node in bytes */
58 #define FLST_BASE_NODE_SIZE (4 + 2 * FIL_ADDR_SIZE)
59 
60 /* The physical size of a list node in bytes */
61 #define FLST_NODE_SIZE (2 * FIL_ADDR_SIZE)
62 
63 typedef byte flst_base_node_t;
64 typedef byte flst_node_t;
65 
66 void flst_init(flst_base_node_t *base);
67 void flst_add_last(flst_base_node_t *base, flst_node_t *node);
68 
flst_get_len(const flst_base_node_t * base)69 inline ulint flst_get_len(const flst_base_node_t *base) {
70   return (mach_read_from_4(base + FLST_LEN));
71 }
72 
flst_read_addr(const fil_faddr_t * faddr)73 inline fil_addr_t flst_read_addr(const fil_faddr_t *faddr) {
74   fil_addr_t addr;
75 
76   addr.page = mach_read_ulint(faddr + FIL_ADDR_PAGE, MLOG_4BYTES);
77   addr.boffset = mach_read_ulint(faddr + FIL_ADDR_BYTE, MLOG_2BYTES);
78   ut_a(addr.page == FIL_NULL || addr.boffset >= FIL_PAGE_DATA);
79   ut_a(ut_align_offset(faddr, UNIV_PAGE_SIZE) >= FIL_PAGE_DATA);
80   return (addr);
81 }
82 
flst_get_prev_addr(const flst_node_t * node)83 inline fil_addr_t flst_get_prev_addr(const flst_node_t *node) {
84   return (flst_read_addr(node + FLST_PREV));
85 }
86 
flst_get_last(const flst_base_node_t * base)87 inline fil_addr_t flst_get_last(const flst_base_node_t *base) {
88   return (flst_read_addr(base + FLST_LAST));
89 }
90 
flst_get_next_addr(const flst_node_t * node)91 inline fil_addr_t flst_get_next_addr(const flst_node_t *node) {
92   return (flst_read_addr(node + FLST_NEXT));
93 }
94 
95 bool flst_validate(const flst_base_node_t *base);
96 
flst_get_first(const flst_base_node_t * base)97 inline fil_addr_t flst_get_first(const flst_base_node_t *base) {
98   return (flst_read_addr(base + FLST_FIRST));
99 }
100 
101 void flst_remove(flst_base_node_t *base, flst_node_t *node2);
102 void flst_add_first(flst_base_node_t *base, flst_node_t *node);
103 
104 /** Insert node2 after node1 in the list base. */
105 void flst_insert_after(flst_base_node_t *base, flst_node_t *node1,
106                        flst_node_t *node2);
107 void flst_insert_before(flst_base_node_t *base, flst_node_t *node2,
108                         flst_node_t *node3);
109 
110 void flst_write_addr(fil_faddr_t *faddr, fil_addr_t addr);
111 
112 /** In-memory representation of flst_base_node_t */
113 struct flst_bnode_t {
114   ulint len;
115   fil_addr_t first;
116   fil_addr_t last;
117 
flst_bnode_tflst_bnode_t118   flst_bnode_t(const flst_base_node_t *base)
119       : len(flst_get_len(base)),
120         first(flst_get_first(base)),
121         last(flst_get_last(base)) {}
122 
setflst_bnode_t123   void set(const flst_base_node_t *base) {
124     len = flst_get_len(base);
125     first = flst_get_first(base);
126     last = flst_get_last(base);
127   }
128 
printflst_bnode_t129   std::ostream &print(std::ostream &out) const {
130     out << "[flst_base_node_t: len=" << len << ", first=" << first
131         << ", last=" << last << "]";
132     return (out);
133   }
134 };
135 
136 inline std::ostream &operator<<(std::ostream &out, const flst_bnode_t &obj) {
137   return (obj.print(out));
138 }
139 
140 #endif  // _fut0lst_h_
141