1/*****************************************************************************
2
3Copyright (c) 1995, 2009, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2.0,
7as published by the Free Software Foundation.
8
9This program is also distributed with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation.  The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have included with MySQL.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU General Public License, version 2.0, for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24
25*****************************************************************************/
26
27/******************************************************************//**
28@file include/fut0lst.ic
29File-based list utilities
30
31Created 11/28/1995 Heikki Tuuri
32***********************************************************************/
33
34#include "fut0fut.h"
35#include "mtr0log.h"
36#include "buf0buf.h"
37
38/* We define the field offsets of a node for the list */
39#define FLST_PREV	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	FIL_ADDR_SIZE	/* 6-byte address of the next
43				list element; the page part of address
44				is FIL_NULL, if no next element */
45
46/* We define the field offsets of a base node for the list */
47#define FLST_LEN	0	/* 32-bit list length field */
48#define	FLST_FIRST	4	/* 6-byte address of the first element
49				of the list; undefined if empty list */
50#define	FLST_LAST	(4 + FIL_ADDR_SIZE) /* 6-byte address of the
51				last element of the list; undefined
52				if empty list */
53
54/********************************************************************//**
55Writes a file address. */
56UNIV_INLINE
57void
58flst_write_addr(
59/*============*/
60	fil_faddr_t*	faddr,	/*!< in: pointer to file faddress */
61	fil_addr_t	addr,	/*!< in: file address */
62	mtr_t*		mtr)	/*!< in: mini-transaction handle */
63{
64	ut_ad(faddr && mtr);
65	ut_ad(mtr_memo_contains_page(mtr, faddr, MTR_MEMO_PAGE_X_FIX));
66	ut_a(addr.page == FIL_NULL || addr.boffset >= FIL_PAGE_DATA);
67	ut_a(ut_align_offset(faddr, UNIV_PAGE_SIZE) >= FIL_PAGE_DATA);
68
69	mlog_write_ulint(faddr + FIL_ADDR_PAGE, addr.page, MLOG_4BYTES, mtr);
70	mlog_write_ulint(faddr + FIL_ADDR_BYTE, addr.boffset,
71			 MLOG_2BYTES, mtr);
72}
73
74/********************************************************************//**
75Reads a file address.
76@return	file address */
77UNIV_INLINE
78fil_addr_t
79flst_read_addr(
80/*===========*/
81	const fil_faddr_t*	faddr,	/*!< in: pointer to file faddress */
82	mtr_t*			mtr)	/*!< in: mini-transaction handle */
83{
84	fil_addr_t	addr;
85
86	ut_ad(faddr && mtr);
87
88	addr.page = mtr_read_ulint(faddr + FIL_ADDR_PAGE, MLOG_4BYTES, mtr);
89	addr.boffset = mtr_read_ulint(faddr + FIL_ADDR_BYTE, MLOG_2BYTES,
90				      mtr);
91	ut_a(addr.page == FIL_NULL || addr.boffset >= FIL_PAGE_DATA);
92	ut_a(ut_align_offset(faddr, UNIV_PAGE_SIZE) >= FIL_PAGE_DATA);
93	return(addr);
94}
95
96/********************************************************************//**
97Initializes a list base node. */
98UNIV_INLINE
99void
100flst_init(
101/*======*/
102	flst_base_node_t*	base,	/*!< in: pointer to base node */
103	mtr_t*			mtr)	/*!< in: mini-transaction handle */
104{
105	ut_ad(mtr_memo_contains_page(mtr, base, MTR_MEMO_PAGE_X_FIX));
106
107	mlog_write_ulint(base + FLST_LEN, 0, MLOG_4BYTES, mtr);
108	flst_write_addr(base + FLST_FIRST, fil_addr_null, mtr);
109	flst_write_addr(base + FLST_LAST, fil_addr_null, mtr);
110}
111
112/********************************************************************//**
113Gets list length.
114@return	length */
115UNIV_INLINE
116ulint
117flst_get_len(
118/*=========*/
119	const flst_base_node_t*	base,	/*!< in: pointer to base node */
120	mtr_t*			mtr)	/*!< in: mini-transaction handle */
121{
122	return(mtr_read_ulint(base + FLST_LEN, MLOG_4BYTES, mtr));
123}
124
125/********************************************************************//**
126Gets list first node address.
127@return	file address */
128UNIV_INLINE
129fil_addr_t
130flst_get_first(
131/*===========*/
132	const flst_base_node_t*	base,	/*!< in: pointer to base node */
133	mtr_t*			mtr)	/*!< in: mini-transaction handle */
134{
135	return(flst_read_addr(base + FLST_FIRST, mtr));
136}
137
138/********************************************************************//**
139Gets list last node address.
140@return	file address */
141UNIV_INLINE
142fil_addr_t
143flst_get_last(
144/*==========*/
145	const flst_base_node_t*	base,	/*!< in: pointer to base node */
146	mtr_t*			mtr)	/*!< in: mini-transaction handle */
147{
148	return(flst_read_addr(base + FLST_LAST, mtr));
149}
150
151/********************************************************************//**
152Gets list next node address.
153@return	file address */
154UNIV_INLINE
155fil_addr_t
156flst_get_next_addr(
157/*===============*/
158	const flst_node_t*	node,	/*!< in: pointer to node */
159	mtr_t*			mtr)	/*!< in: mini-transaction handle */
160{
161	return(flst_read_addr(node + FLST_NEXT, mtr));
162}
163
164/********************************************************************//**
165Gets list prev node address.
166@return	file address */
167UNIV_INLINE
168fil_addr_t
169flst_get_prev_addr(
170/*===============*/
171	const flst_node_t*	node,	/*!< in: pointer to node */
172	mtr_t*			mtr)	/*!< in: mini-transaction handle */
173{
174	return(flst_read_addr(node + FLST_PREV, mtr));
175}
176