summaryrefslogtreecommitdiff
path: root/storage/xtradb/include/buf0buf.ic
blob: b4cbba29f3c35e7668d830f33da23a8ade5b388a (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
/*****************************************************************************

Copyright (c) 1995, 2013, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.

Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file include/buf0buf.ic
The database buffer buf_pool

Created 11/5/1995 Heikki Tuuri
*******************************************************/

#include "mtr0mtr.h"
#ifndef UNIV_HOTBACKUP
#include "buf0flu.h"
#include "buf0lru.h"
#include "buf0rea.h"

/** A chunk of buffers. The buffer pool is allocated in chunks. */
struct buf_chunk_t{
	ulint		mem_size;	/*!< allocated size of the chunk */
	ulint		size;		/*!< size of frames[] and blocks[] */
	void*		mem;		/*!< pointer to the memory area which
					was allocated for the frames */
	buf_block_t*	blocks;		/*!< array of buffer control blocks */
};


#include "srv0srv.h"

/*********************************************************************//**
Gets the current size of buffer buf_pool in bytes.
@return size in bytes */
UNIV_INLINE
ulint
buf_pool_get_curr_size(void)
/*========================*/
{
	return(srv_buf_pool_curr_size);
}

/********************************************************************//**
Calculates the index of a buffer pool to the buf_pool[] array.
@return	the position of the buffer pool in buf_pool[] */
UNIV_INLINE
ulint
buf_pool_index(
/*===========*/
	const buf_pool_t*	buf_pool)	/*!< in: buffer pool */
{
	ulint	i = buf_pool - buf_pool_ptr;
	ut_ad(i < MAX_BUFFER_POOLS);
	ut_ad(i < srv_buf_pool_instances);
	return(i);
}

/******************************************************************//**
Returns the buffer pool instance given a page instance
@return buf_pool */
UNIV_INLINE
buf_pool_t*
buf_pool_from_bpage(
/*================*/
	const buf_page_t*	bpage) /*!< in: buffer pool page */
{
	ulint	i;
	i = bpage->buf_pool_index;
	ut_ad(i < srv_buf_pool_instances);
	return(&buf_pool_ptr[i]);
}

/******************************************************************//**
Returns the buffer pool instance given a block instance
@return buf_pool */
UNIV_INLINE
buf_pool_t*
buf_pool_from_block(
/*================*/
	const buf_block_t*	block) /*!< in: block */
{
	return(buf_pool_from_bpage(&block->page));
}

/*********************************************************************//**
Gets the current size of buffer buf_pool in pages.
@return size in pages*/
UNIV_INLINE
ulint
buf_pool_get_n_pages(void)
/*======================*/
{
	return(buf_pool_get_curr_size() / UNIV_PAGE_SIZE);
}

/********************************************************************//**
Reads the freed_page_clock of a buffer block.
@return	freed_page_clock */
UNIV_INLINE
ulint
buf_page_get_freed_page_clock(
/*==========================*/
	const buf_page_t*	bpage)	/*!< in: block */
{
	/* This is sometimes read without holding any buffer pool mutex. */
	return(bpage->freed_page_clock);
}

/********************************************************************//**
Reads the freed_page_clock of a buffer block.
@return	freed_page_clock */
UNIV_INLINE
ulint
buf_block_get_freed_page_clock(
/*===========================*/
	const buf_block_t*	block)	/*!< in: block */
{
	return(buf_page_get_freed_page_clock(&block->page));
}

/********************************************************************//**
Tells if a block is still close enough to the MRU end of the LRU list
meaning that it is not in danger of getting evicted and also implying
that it has been accessed recently.
Note that this is for heuristics only and does not reserve buffer pool
mutex.
@return	TRUE if block is close to MRU end of LRU */
UNIV_INLINE
ibool
buf_page_peek_if_young(
/*===================*/
	const buf_page_t*	bpage)	/*!< in: block */
{
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);

	/* FIXME: bpage->freed_page_clock is 31 bits */
	return((buf_pool->freed_page_clock & ((1UL << 31) - 1))
	       < ((ulint) bpage->freed_page_clock
		  + (buf_pool->curr_size
		     * (BUF_LRU_OLD_RATIO_DIV - buf_pool->LRU_old_ratio)
		     / (BUF_LRU_OLD_RATIO_DIV * 4))));
}

/********************************************************************//**
Recommends a move of a block to the start of the LRU list if there is danger
of dropping from the buffer pool. NOTE: does not reserve the buffer pool
mutex.
@return	TRUE if should be made younger */
UNIV_INLINE
ibool
buf_page_peek_if_too_old(
/*=====================*/
	const buf_page_t*	bpage)	/*!< in: block to make younger */
{
	buf_pool_t*		buf_pool = buf_pool_from_bpage(bpage);

	if (buf_pool->freed_page_clock == 0) {
		/* If eviction has not started yet, do not update the
		statistics or move blocks in the LRU list.  This is
		either the warm-up phase or an in-memory workload. */
		return(FALSE);
	} else if (buf_LRU_old_threshold_ms && bpage->old) {
		unsigned	access_time = buf_page_is_accessed(bpage);

		if (access_time > 0
		    && ((ib_uint32_t) (ut_time_ms() - access_time))
		    >= buf_LRU_old_threshold_ms) {
			return(TRUE);
		}

		buf_pool->stat.n_pages_not_made_young++;
		return(FALSE);
	} else {
		return(!buf_page_peek_if_young(bpage));
	}
}
#endif /* !UNIV_HOTBACKUP */

/*********************************************************************//**
Gets the state of a block.
@return	state */
UNIV_INLINE
enum buf_page_state
buf_page_get_state(
/*===============*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	enum buf_page_state	state = (enum buf_page_state) bpage->state;

#ifdef UNIV_DEBUG
	switch (state) {
	case BUF_BLOCK_POOL_WATCH:
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_FILE_PAGE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		break;
	default:
		ut_error;
	}
#endif /* UNIV_DEBUG */

	return(state);
}
/*********************************************************************//**
Gets the state of a block.
@return	state */
UNIV_INLINE
enum buf_page_state
buf_block_get_state(
/*================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	return(buf_page_get_state(&block->page));
}
/*********************************************************************//**
Sets the state of a block. */
UNIV_INLINE
void
buf_page_set_state(
/*===============*/
	buf_page_t*		bpage,	/*!< in/out: pointer to control block */
	enum buf_page_state	state)	/*!< in: state */
{
#ifdef UNIV_DEBUG
	enum buf_page_state	old_state	= buf_page_get_state(bpage);

	switch (old_state) {
	case BUF_BLOCK_POOL_WATCH:
		ut_error;
		break;
	case BUF_BLOCK_ZIP_PAGE:
		ut_a(state == BUF_BLOCK_ZIP_DIRTY);
		break;
	case BUF_BLOCK_ZIP_DIRTY:
		ut_a(state == BUF_BLOCK_ZIP_PAGE);
		break;
	case BUF_BLOCK_NOT_USED:
		ut_a(state == BUF_BLOCK_READY_FOR_USE);
		break;
	case BUF_BLOCK_READY_FOR_USE:
		ut_a(state == BUF_BLOCK_MEMORY
		     || state == BUF_BLOCK_FILE_PAGE
		     || state == BUF_BLOCK_NOT_USED);
		break;
	case BUF_BLOCK_MEMORY:
		ut_a(state == BUF_BLOCK_NOT_USED);
		break;
	case BUF_BLOCK_FILE_PAGE:
		ut_a(state == BUF_BLOCK_NOT_USED
		     || state == BUF_BLOCK_REMOVE_HASH);
		break;
	case BUF_BLOCK_REMOVE_HASH:
		ut_a(state == BUF_BLOCK_MEMORY);
		break;
	}
#endif /* UNIV_DEBUG */

	bpage->state = state;
#ifdef UNIV_DEBUG
	if( buf_page_get_state(bpage) != state) {
		fprintf(stderr, "InnoDB: Error: Requested state %d current state %d old_state %d\n",
			state, buf_page_get_state(bpage), old_state);
	}
#endif
	ut_ad(buf_page_get_state(bpage) == state);
}

/*********************************************************************//**
Sets the state of a block. */
UNIV_INLINE
void
buf_block_set_state(
/*================*/
	buf_block_t*		block,	/*!< in/out: pointer to control block */
	enum buf_page_state	state)	/*!< in: state */
{
	buf_page_set_state(&block->page, state);
}

/*********************************************************************//**
Determines if a block is mapped to a tablespace.
@return	TRUE if mapped */
UNIV_INLINE
ibool
buf_page_in_file(
/*=============*/
	const buf_page_t*	bpage)	/*!< in: pointer to control block */
{
	switch (buf_page_get_state(bpage)) {
	case BUF_BLOCK_POOL_WATCH:
		ut_error;
		break;
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
	case BUF_BLOCK_FILE_PAGE:
		return(TRUE);
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		break;
	}

	return(FALSE);
}

#ifndef UNIV_HOTBACKUP
/*********************************************************************//**
Determines if a block should be on unzip_LRU list.
@return	TRUE if block belongs to unzip_LRU */
UNIV_INLINE
ibool
buf_page_belongs_to_unzip_LRU(
/*==========================*/
	const buf_page_t*	bpage)	/*!< in: pointer to control block */
{
	ut_ad(buf_page_in_file(bpage));

	return(bpage->zip.data
	       && buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE);
}

/*********************************************************************//**
Gets the mutex of a block.
@return	pointer to mutex protecting bpage */
UNIV_INLINE
ib_mutex_t*
buf_page_get_mutex(
/*===============*/
	const buf_page_t*	bpage)	/*!< in: pointer to control block */
{
	switch (buf_page_get_state(bpage)) {
	case BUF_BLOCK_POOL_WATCH:
		ut_error;
		return(NULL);
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY: {
		buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);

		return(&buf_pool->zip_mutex);
		}
	default:
		return(&((buf_block_t*) bpage)->mutex);
	}
}

/*********************************************************************//**
Get the flush type of a page.
@return	flush type */
UNIV_INLINE
buf_flush_t
buf_page_get_flush_type(
/*====================*/
	const buf_page_t*	bpage)	/*!< in: buffer page */
{
	buf_flush_t	flush_type = (buf_flush_t) bpage->flush_type;

#ifdef UNIV_DEBUG
	switch (flush_type) {
	case BUF_FLUSH_LRU:
	case BUF_FLUSH_LIST:
	case BUF_FLUSH_SINGLE_PAGE:
		return(flush_type);
	case BUF_FLUSH_N_TYPES:
		ut_error;
	}
	ut_error;
#endif /* UNIV_DEBUG */
	return(flush_type);
}
/*********************************************************************//**
Set the flush type of a page. */
UNIV_INLINE
void
buf_page_set_flush_type(
/*====================*/
	buf_page_t*	bpage,		/*!< in: buffer page */
	buf_flush_t	flush_type)	/*!< in: flush type */
{
	bpage->flush_type = flush_type;
	ut_ad(buf_page_get_flush_type(bpage) == flush_type);
}

/*********************************************************************//**
Map a block to a file page. */
UNIV_INLINE
void
buf_block_set_file_page(
/*====================*/
	buf_block_t*		block,	/*!< in/out: pointer to control block */
	ulint			space,	/*!< in: tablespace id */
	ulint			page_no)/*!< in: page number */
{
	buf_block_set_state(block, BUF_BLOCK_FILE_PAGE);
	block->page.space = static_cast<ib_uint32_t>(space);
	block->page.offset = static_cast<ib_uint32_t>(page_no);
}

/*********************************************************************//**
Gets the io_fix state of a block.
@return	io_fix state */
UNIV_INLINE
enum buf_io_fix
buf_page_get_io_fix(
/*================*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
	return buf_page_get_io_fix_unlocked(bpage);
}

/*********************************************************************//**
Gets the io_fix state of a block.  Does not assert that the
buf_page_get_mutex() mutex is held, to be used in the cases where it is safe
not to hold it.
@return	io_fix state */
UNIV_INLINE
enum buf_io_fix
buf_page_get_io_fix_unlocked(
/*=========================*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	enum buf_io_fix	io_fix = (enum buf_io_fix) bpage->io_fix;
#ifdef UNIV_DEBUG
	switch (io_fix) {
	case BUF_IO_NONE:
	case BUF_IO_READ:
	case BUF_IO_WRITE:
	case BUF_IO_PIN:
		return(io_fix);
	}
	ut_error;
#endif /* UNIV_DEBUG */
	return(io_fix);
}

/*********************************************************************//**
Gets the io_fix state of a block.
@return	io_fix state */
UNIV_INLINE
enum buf_io_fix
buf_block_get_io_fix(
/*=================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	return(buf_page_get_io_fix(&block->page));
}

/*********************************************************************//**
Gets the io_fix state of a block.  Does not assert that the
buf_page_get_mutex() mutex is held, to be used in the cases where it is safe
not to hold it.
@return	io_fix state */
UNIV_INLINE
enum buf_io_fix
buf_block_get_io_fix_unlocked(
/*==========================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	return(buf_page_get_io_fix_unlocked(&block->page));
}


/*********************************************************************//**
Sets the io_fix state of a block. */
UNIV_INLINE
void
buf_page_set_io_fix(
/*================*/
	buf_page_t*	bpage,	/*!< in/out: control block */
	enum buf_io_fix	io_fix)	/*!< in: io_fix state */
{
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));

	bpage->io_fix = io_fix;
	ut_ad(buf_page_get_io_fix(bpage) == io_fix);
}

/*********************************************************************//**
Sets the io_fix state of a block. */
UNIV_INLINE
void
buf_block_set_io_fix(
/*=================*/
	buf_block_t*	block,	/*!< in/out: control block */
	enum buf_io_fix	io_fix)	/*!< in: io_fix state */
{
	buf_page_set_io_fix(&block->page, io_fix);
}

/*********************************************************************//**
Makes a block sticky. A sticky block implies that even after we release
the buf_pool->LRU_list_mutex and the block->mutex:
* it cannot be removed from the flush_list
* the block descriptor cannot be relocated
* it cannot be removed from the LRU list
Note that:
* the block can still change its position in the LRU list
* the next and previous pointers can change. */
UNIV_INLINE
void
buf_page_set_sticky(
/*================*/
	buf_page_t*	bpage)	/*!< in/out: control block */
{
#ifdef UNIV_DEBUG
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
	ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
#endif
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
	ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_NONE);
	ut_ad(bpage->in_LRU_list);

	bpage->io_fix = BUF_IO_PIN;
}

/*********************************************************************//**
Removes stickiness of a block. */
UNIV_INLINE
void
buf_page_unset_sticky(
/*==================*/
	buf_page_t*	bpage)	/*!< in/out: control block */
{
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
	ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_PIN);

	bpage->io_fix = BUF_IO_NONE;
}

/********************************************************************//**
Determine if a buffer block can be relocated in memory.  The block
can be dirty, but it must not be I/O-fixed or bufferfixed. */
UNIV_INLINE
ibool
buf_page_can_relocate(
/*==================*/
	const buf_page_t*	bpage)	/*!< control block being relocated */
{
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));
	ut_ad(buf_page_in_file(bpage));
	ut_ad(bpage->in_LRU_list);

	return(buf_page_get_io_fix(bpage) == BUF_IO_NONE
	       && bpage->buf_fix_count == 0);
}

/*********************************************************************//**
Determine if a block has been flagged old.
@return	TRUE if old */
UNIV_INLINE
ibool
buf_page_is_old(
/*============*/
	const buf_page_t*	bpage)	/*!< in: control block */
{
#ifdef UNIV_DEBUG
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
#endif
	/* Buffer page mutex is not strictly required here for heuristic
	purposes even if LRU mutex is not being held.  Keep the assertion
	for now since all the callers hold it.  */
	ut_ad(mutex_own(buf_page_get_mutex(bpage))
	      || mutex_own(&buf_pool->LRU_list_mutex));
	ut_ad(buf_page_in_file(bpage));

	return(bpage->old);
}

/*********************************************************************//**
Flag a block old. */
UNIV_INLINE
void
buf_page_set_old(
/*=============*/
	buf_page_t*	bpage,	/*!< in/out: control block */
	ibool		old)	/*!< in: old */
{
#ifdef UNIV_DEBUG
	buf_pool_t*	buf_pool = buf_pool_from_bpage(bpage);
#endif /* UNIV_DEBUG */
	ut_a(buf_page_in_file(bpage));
	ut_ad(mutex_own(&buf_pool->LRU_list_mutex));
	ut_ad(bpage->in_LRU_list);

#ifdef UNIV_LRU_DEBUG
	ut_a((buf_pool->LRU_old_len == 0) == (buf_pool->LRU_old == NULL));
	/* If a block is flagged "old", the LRU_old list must exist. */
	ut_a(!old || buf_pool->LRU_old);

	if (UT_LIST_GET_PREV(LRU, bpage) && UT_LIST_GET_NEXT(LRU, bpage)) {
		const buf_page_t*	prev = UT_LIST_GET_PREV(LRU, bpage);
		const buf_page_t*	next = UT_LIST_GET_NEXT(LRU, bpage);
		if (prev->old == next->old) {
			ut_a(prev->old == old);
		} else {
			ut_a(!prev->old);
			ut_a(buf_pool->LRU_old == (old ? bpage : next));
		}
	}
#endif /* UNIV_LRU_DEBUG */

	bpage->old = old;
}

/*********************************************************************//**
Determine the time of first access of a block in the buffer pool.
@return	ut_time_ms() at the time of first access, 0 if not accessed */
UNIV_INLINE
unsigned
buf_page_is_accessed(
/*=================*/
	const buf_page_t*	bpage)	/*!< in: control block */
{
	ut_ad(buf_page_in_file(bpage));

	return(bpage->access_time);
}

/*********************************************************************//**
Flag a block accessed. */
UNIV_INLINE
void
buf_page_set_accessed(
/*==================*/
	buf_page_t*	bpage)		/*!< in/out: control block */
{
	ut_ad(mutex_own(buf_page_get_mutex(bpage)));

	ut_a(buf_page_in_file(bpage));

	if (bpage->access_time == 0) {
		/* Make this the time of the first access. */
		bpage->access_time = static_cast<uint>(ut_time_ms());
	}
}

/*********************************************************************//**
Gets the buf_block_t handle of a buffered file block if an uncompressed
page frame exists, or NULL.
@return	control block, or NULL */
UNIV_INLINE
buf_block_t*
buf_page_get_block(
/*===============*/
	buf_page_t*	bpage)	/*!< in: control block, or NULL */
{
	if (bpage != NULL) {
#ifdef UNIV_DEBUG
		buf_pool_t*	buf_pool	= buf_pool_from_bpage(bpage);
		ut_ad(buf_page_hash_lock_held_s_or_x(buf_pool, bpage)
		      || mutex_own(&buf_pool->LRU_list_mutex));
#endif
		ut_ad(buf_page_in_file(bpage));

		if (buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE) {
			return((buf_block_t*) bpage);
		}
	}

	return(NULL);
}
#endif /* !UNIV_HOTBACKUP */

#ifdef UNIV_DEBUG
/*********************************************************************//**
Gets a pointer to the memory frame of a block.
@return	pointer to the frame */
UNIV_INLINE
buf_frame_t*
buf_block_get_frame(
/*================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	SRV_CORRUPT_TABLE_CHECK(block, return(0););

	switch (buf_block_get_state(block)) {
	case BUF_BLOCK_POOL_WATCH:
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
	case BUF_BLOCK_NOT_USED:
		ut_error;
		break;
	case BUF_BLOCK_FILE_PAGE:
# ifndef UNIV_HOTBACKUP
		ut_a(block->page.buf_fix_count > 0);
# endif /* !UNIV_HOTBACKUP */
		/* fall through */
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		goto ok;
	}
	ut_error;
ok:
	return((buf_frame_t*) block->frame);
}
#endif /* UNIV_DEBUG */

/*********************************************************************//**
Gets the space id of a block.
@return	space id */
UNIV_INLINE
ulint
buf_page_get_space(
/*===============*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	ut_ad(bpage);
	ut_a(buf_page_in_file(bpage));

	return(bpage->space);
}

/*********************************************************************//**
Gets the space id of a block.
@return	space id */
UNIV_INLINE
ulint
buf_block_get_space(
/*================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	ut_ad(block);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

	return(block->page.space);
}

/*********************************************************************//**
Gets the page number of a block.
@return	page number */
UNIV_INLINE
ulint
buf_page_get_page_no(
/*=================*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	ut_ad(bpage);
	ut_a(buf_page_in_file(bpage));

	return(bpage->offset);
}
/***********************************************************************
FIXME_FTS Gets the frame the pointer is pointing to. */
UNIV_INLINE
buf_frame_t*
buf_frame_align(
/*============*/
                        /* out: pointer to frame */
        byte*   ptr)    /* in: pointer to a frame */
{
        buf_frame_t*    frame;

        ut_ad(ptr);

        frame = (buf_frame_t*) ut_align_down(ptr, UNIV_PAGE_SIZE);

        return(frame);
}

/*********************************************************************//**
Gets the page number of a block.
@return	page number */
UNIV_INLINE
ulint
buf_block_get_page_no(
/*==================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	ut_ad(block);
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

	return(block->page.offset);
}

/*********************************************************************//**
Gets the compressed page size of a block.
@return	compressed page size, or 0 */
UNIV_INLINE
ulint
buf_page_get_zip_size(
/*==================*/
	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
{
	return(bpage->zip.ssize
	       ? (UNIV_ZIP_SIZE_MIN >> 1) << bpage->zip.ssize : 0);
}

/*********************************************************************//**
Gets the compressed page size of a block.
@return	compressed page size, or 0 */
UNIV_INLINE
ulint
buf_block_get_zip_size(
/*===================*/
	const buf_block_t*	block)	/*!< in: pointer to the control block */
{
	return(block->page.zip.ssize
	       ? (UNIV_ZIP_SIZE_MIN >> 1) << block->page.zip.ssize : 0);
}

#ifndef UNIV_HOTBACKUP
#if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
/*********************************************************************//**
Gets the compressed page descriptor corresponding to an uncompressed page
if applicable.
@return	compressed page descriptor, or NULL */
UNIV_INLINE
const page_zip_des_t*
buf_frame_get_page_zip(
/*===================*/
	const byte*	ptr)	/*!< in: pointer to the page */
{
	return(buf_block_get_page_zip(buf_block_align(ptr)));
}
#endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
#endif /* !UNIV_HOTBACKUP */

/**********************************************************************//**
Gets the space id, page offset, and byte offset within page of a
pointer pointing to a buffer frame containing a file page. */
UNIV_INLINE
void
buf_ptr_get_fsp_addr(
/*=================*/
	const void*	ptr,	/*!< in: pointer to a buffer frame */
	ulint*		space,	/*!< out: space id */
	fil_addr_t*	addr)	/*!< out: page offset and byte offset */
{
	const page_t*	page = (const page_t*) ut_align_down(ptr,
							     UNIV_PAGE_SIZE);

	*space = mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
	addr->page = mach_read_from_4(page + FIL_PAGE_OFFSET);
	addr->boffset = ut_align_offset(ptr, UNIV_PAGE_SIZE);
}

#ifndef UNIV_HOTBACKUP
/**********************************************************************//**
Gets the hash value of the page the pointer is pointing to. This can be used
in searches in the lock hash table.
@return	lock hash value */
UNIV_INLINE
ulint
buf_block_get_lock_hash_val(
/*========================*/
	const buf_block_t*	block)	/*!< in: block */
{
	ut_ad(block);
	ut_ad(buf_page_in_file(&block->page));
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&(((buf_block_t*) block)->lock), RW_LOCK_EXCLUSIVE)
	      || rw_lock_own(&(((buf_block_t*) block)->lock), RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */
	return(block->lock_hash_val);
}

/********************************************************************//**
Allocates a buf_page_t descriptor. This function must succeed. In case
of failure we assert in this function.
@return: the allocated descriptor. */
UNIV_INLINE
buf_page_t*
buf_page_alloc_descriptor(void)
/*===========================*/
{
	buf_page_t*	bpage;

	bpage = (buf_page_t*) ut_malloc(sizeof *bpage);
	ut_d(memset(bpage, 0, sizeof *bpage));
	UNIV_MEM_ALLOC(bpage, sizeof *bpage);

	return(bpage);
}

/********************************************************************//**
Free a buf_page_t descriptor. */
UNIV_INLINE
void
buf_page_free_descriptor(
/*=====================*/
	buf_page_t*	bpage)	/*!< in: bpage descriptor to free. */
{
	ut_free(bpage);
}

/********************************************************************//**
Frees a buffer block which does not contain a file page. */
UNIV_INLINE
void
buf_block_free(
/*===========*/
	buf_block_t*	block)	/*!< in, own: block to be freed */
{
	mutex_enter(&block->mutex);

	ut_a(buf_block_get_state(block) != BUF_BLOCK_FILE_PAGE);

	buf_LRU_block_free_non_file_page(block);

	mutex_exit(&block->mutex);
}

/********************************************************************//**
Get crypt buffer. */
UNIV_INLINE
byte*
buf_page_get_crypt_buffer(
/*=========================*/
	const buf_page_t*	bpage) /*!< in: buffer pool page */
{
	return bpage->crypt_buf;
}

/********************************************************************//**
Get buf frame. */
UNIV_INLINE
void *
buf_page_get_frame(
/*=========================*/
	const buf_page_t*	bpage) /*!< in: buffer pool page */
{
	if (bpage->crypt_buf) {
		return buf_page_get_crypt_buffer(bpage);
	} else if (bpage->zip.data) {
		return bpage->zip.data;
	} else {
		return ((buf_block_t*) bpage)->frame;
	}
}

#endif /* !UNIV_HOTBACKUP */

/*********************************************************************//**
Copies contents of a buffer frame to a given buffer.
@return	buf */
UNIV_INLINE
byte*
buf_frame_copy(
/*===========*/
	byte*			buf,	/*!< in: buffer to copy to */
	const buf_frame_t*	frame)	/*!< in: buffer frame */
{
	ut_ad(buf && frame);

	ut_memcpy(buf, frame, UNIV_PAGE_SIZE);

	return(buf);
}

#ifndef UNIV_HOTBACKUP
/********************************************************************//**
Calculates a folded value of a file page address to use in the page hash
table.
@return	the folded value */
UNIV_INLINE
ulint
buf_page_address_fold(
/*==================*/
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: offset of the page within space */
{
	return((space << 20) + space + offset);
}

/********************************************************************//**
Gets the youngest modification log sequence number for a frame.
Returns zero if not file page or no modification occurred yet.
@return	newest modification to page */
UNIV_INLINE
lsn_t
buf_page_get_newest_modification(
/*=============================*/
	const buf_page_t*	bpage)	/*!< in: block containing the
					page frame */
{
	lsn_t		lsn;
	ib_mutex_t*	block_mutex = buf_page_get_mutex(bpage);

	mutex_enter(block_mutex);

	if (buf_page_in_file(bpage)) {
		lsn = bpage->newest_modification;
	} else {
		lsn = 0;
	}

	mutex_exit(block_mutex);

	return(lsn);
}

/********************************************************************//**
Increments the modify clock of a frame by 1. The caller must (1) own the
LRU list mutex and block bufferfix count has to be zero, (2) or own an x-lock
on the block. */
UNIV_INLINE
void
buf_block_modify_clock_inc(
/*=======================*/
	buf_block_t*	block)	/*!< in: block */
{
#ifdef UNIV_SYNC_DEBUG
	buf_pool_t*	buf_pool = buf_pool_from_bpage((buf_page_t*) block);

	ut_ad((mutex_own(&buf_pool->LRU_list_mutex)
	       && (block->page.buf_fix_count == 0))
	      || rw_lock_own(&(block->lock), RW_LOCK_EXCLUSIVE));
#endif /* UNIV_SYNC_DEBUG */

	block->modify_clock++;
}

/********************************************************************//**
Returns the value of the modify clock. The caller must have an s-lock
or x-lock on the block.
@return	value */
UNIV_INLINE
ib_uint64_t
buf_block_get_modify_clock(
/*=======================*/
	buf_block_t*	block)	/*!< in: block */
{
#ifdef UNIV_SYNC_DEBUG
	ut_ad(rw_lock_own(&(block->lock), RW_LOCK_SHARED)
	      || rw_lock_own(&(block->lock), RW_LOCK_EXCLUSIVE));
#endif /* UNIV_SYNC_DEBUG */

	return(block->modify_clock);
}

/*******************************************************************//**
Increments the bufferfix count. */
UNIV_INLINE
void
buf_block_fix(
/*===========*/
	buf_block_t*	block)	/*!< in/out: block to bufferfix */
{
	ut_ad(!mutex_own(buf_page_get_mutex(&block->page)));
#ifdef PAGE_ATOMIC_REF_COUNT
	os_atomic_increment_uint32(&block->page.buf_fix_count, 1);
#else
	ib_mutex_t*	block_mutex = buf_page_get_mutex(&block->page);

	mutex_enter(block_mutex);
	++block->page.buf_fix_count;
	mutex_exit(block_mutex);
#endif /* PAGE_ATOMIC_REF_COUNT */
}

/*******************************************************************//**
Increments the bufferfix count. */
UNIV_INLINE
void
buf_block_buf_fix_inc_func(
/*=======================*/
#ifdef UNIV_SYNC_DEBUG
	const char*	file,	/*!< in: file name */
	ulint		line,	/*!< in: line */
#endif /* UNIV_SYNC_DEBUG */
	buf_block_t*	block)	/*!< in/out: block to bufferfix */
{
#ifdef UNIV_SYNC_DEBUG
	ibool	ret;

	ret = rw_lock_s_lock_nowait(&(block->debug_latch), file, line);
	ut_a(ret);
#endif /* UNIV_SYNC_DEBUG */

#ifdef PAGE_ATOMIC_REF_COUNT
	os_atomic_increment_uint32(&block->page.buf_fix_count, 1);
#else
	ut_ad(mutex_own(&block->mutex));

	++block->page.buf_fix_count;
#endif /* PAGE_ATOMIC_REF_COUNT */
}

/*******************************************************************//**
Decrements the bufferfix count. */
UNIV_INLINE
void
buf_block_unfix(
/*============*/
	buf_block_t*	block)	/*!< in/out: block to bufferunfix */
{
	ut_ad(block->page.buf_fix_count > 0);
	ut_ad(!mutex_own(buf_page_get_mutex(&block->page)));

#ifdef PAGE_ATOMIC_REF_COUNT
	os_atomic_decrement_uint32(&block->page.buf_fix_count, 1);
#else
	ib_mutex_t*	block_mutex = buf_page_get_mutex(&block->page);

	mutex_enter(block_mutex);
	--block->page.buf_fix_count;
	mutex_exit(block_mutex);
#endif /* PAGE_ATOMIC_REF_COUNT */
}

/*******************************************************************//**
Decrements the bufferfix count. */
UNIV_INLINE
void
buf_block_buf_fix_dec(
/*==================*/
	buf_block_t*	block)	/*!< in/out: block to bufferunfix */
{
	ut_ad(block->page.buf_fix_count > 0);

#ifdef PAGE_ATOMIC_REF_COUNT
	os_atomic_decrement_uint32(&block->page.buf_fix_count, 1);
#else
	mutex_enter(&block->mutex);
	--block->page.buf_fix_count;
	mutex_exit(&block->mutex);
#endif /* PAGE_ATOMIC_REF_COUNT */

#ifdef UNIV_SYNC_DEBUG
	rw_lock_s_unlock(&block->debug_latch);
#endif
}

/******************************************************************//**
Returns the buffer pool instance given space and offset of page
@return buffer pool */
UNIV_INLINE
buf_pool_t*
buf_pool_get(
/*==========*/
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: offset of the page within space */
{
	ulint	fold;
	ulint	index;
	ulint	ignored_offset;

	ignored_offset = offset >> 6; /* 2log of BUF_READ_AHEAD_AREA (64)*/
	fold = buf_page_address_fold(space, ignored_offset);
	index = fold % srv_buf_pool_instances;
	return(&buf_pool_ptr[index]);
}

/******************************************************************//**
Returns the buffer pool instance given its array index
@return buffer pool */
UNIV_INLINE
buf_pool_t*
buf_pool_from_array(
/*================*/
	ulint	index)		/*!< in: array index to get
				buffer pool instance from */
{
	ut_ad(index < MAX_BUFFER_POOLS);
	ut_ad(index < srv_buf_pool_instances);
	return(&buf_pool_ptr[index]);
}

/******************************************************************//**
Returns the control block of a file page, NULL if not found.
@return	block, NULL if not found */
UNIV_INLINE
buf_page_t*
buf_page_hash_get_low(
/*==================*/
	buf_pool_t*	buf_pool,/*!< buffer pool instance */
	ulint		space,	/*!< in: space id */
	ulint		offset,	/*!< in: offset of the page within space */
	ulint		fold)	/*!< in: buf_page_address_fold(space, offset) */
{
	buf_page_t*	bpage;

#ifdef UNIV_SYNC_DEBUG
	ulint		hash_fold;
	prio_rw_lock_t*	hash_lock;

	hash_fold = buf_page_address_fold(space, offset);
	ut_ad(hash_fold == fold);

	hash_lock = hash_get_lock(buf_pool->page_hash, fold);
	ut_ad(rw_lock_own(hash_lock, RW_LOCK_EX)
	      || rw_lock_own(hash_lock, RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */

	/* Look for the page in the hash table */

	HASH_SEARCH(hash, buf_pool->page_hash, fold, buf_page_t*, bpage,
		    ut_ad(bpage->in_page_hash && !bpage->in_zip_hash
			  && buf_page_in_file(bpage)),
		    bpage->space == space && bpage->offset == offset);
	if (bpage) {
		ut_a(buf_page_in_file(bpage));
		ut_ad(bpage->in_page_hash);
		ut_ad(!bpage->in_zip_hash);
	}

	return(bpage);
}

/******************************************************************//**
Returns the control block of a file page, NULL if not found.
If the block is found and lock is not NULL then the appropriate
page_hash lock is acquired in the specified lock mode. Otherwise,
mode value is ignored. It is up to the caller to release the
lock. If the block is found and the lock is NULL then the page_hash
lock is released by this function.
@return	block, NULL if not found */
UNIV_INLINE
buf_page_t*
buf_page_hash_get_locked(
/*=====================*/
					/*!< out: pointer to the bpage,
					or NULL; if NULL, hash_lock
					is also NULL. */
	buf_pool_t*	buf_pool,	/*!< buffer pool instance */
	ulint		space,		/*!< in: space id */
	ulint		offset,		/*!< in: page number */
	prio_rw_lock_t**	lock,	/*!< in/out: lock of the page
					hash acquired if bpage is
					found. NULL otherwise. If NULL
					is passed then the hash_lock
					is released by this function */
	ulint		lock_mode)	/*!< in: RW_LOCK_EX or
					RW_LOCK_SHARED. Ignored if
					lock == NULL */
{
	buf_page_t*	bpage = NULL;
	ulint		fold;
	prio_rw_lock_t*	hash_lock;
	ulint		mode = RW_LOCK_SHARED;

	if (lock != NULL) {
		*lock = NULL;
		ut_ad(lock_mode == RW_LOCK_EX
		      || lock_mode == RW_LOCK_SHARED);
		mode = lock_mode;
	}

	fold = buf_page_address_fold(space, offset);
	hash_lock = hash_get_lock(buf_pool->page_hash, fold);

#ifdef UNIV_SYNC_DEBUG
	ut_ad(!rw_lock_own(hash_lock, RW_LOCK_EX)
	      && !rw_lock_own(hash_lock, RW_LOCK_SHARED));
#endif /* UNIV_SYNC_DEBUG */

	if (mode == RW_LOCK_SHARED) {
		rw_lock_s_lock(hash_lock);
	} else {
		rw_lock_x_lock(hash_lock);
	}

	bpage = buf_page_hash_get_low(buf_pool, space, offset, fold);

	if (!bpage || buf_pool_watch_is_sentinel(buf_pool, bpage)) {
		bpage = NULL;
		goto unlock_and_exit;
	}

	ut_ad(buf_page_in_file(bpage));
	ut_ad(offset == bpage->offset);
	ut_ad(space == bpage->space);

	if (lock == NULL) {
		/* The caller wants us to release the page_hash lock */
		goto unlock_and_exit;
	} else {
		/* To be released by the caller */
		*lock = hash_lock;
		goto exit;
	}

unlock_and_exit:
	if (mode == RW_LOCK_SHARED) {
		rw_lock_s_unlock(hash_lock);
	} else {
		rw_lock_x_unlock(hash_lock);
	}
exit:
	return(bpage);
}

/******************************************************************//**
Returns the control block of a file page, NULL if not found.
If the block is found and lock is not NULL then the appropriate
page_hash lock is acquired in the specified lock mode. Otherwise,
mode value is ignored. It is up to the caller to release the
lock. If the block is found and the lock is NULL then the page_hash
lock is released by this function.
@return	block, NULL if not found */
UNIV_INLINE
buf_block_t*
buf_block_hash_get_locked(
/*=====================*/
					/*!< out: pointer to the bpage,
					or NULL; if NULL, hash_lock
					is also NULL. */
	buf_pool_t*	buf_pool,	/*!< buffer pool instance */
	ulint		space,		/*!< in: space id */
	ulint		offset,		/*!< in: page number */
	prio_rw_lock_t**	lock,	/*!< in/out: lock of the page
					hash acquired if bpage is
					found. NULL otherwise. If NULL
					is passed then the hash_lock
					is released by this function */
	ulint		lock_mode)	/*!< in: RW_LOCK_EX or
					RW_LOCK_SHARED. Ignored if
					lock == NULL */
{
	buf_page_t*	bpage = buf_page_hash_get_locked(buf_pool,
							 space,
							 offset,
							 lock,
							 lock_mode);
	buf_block_t*	block = buf_page_get_block(bpage);

	if (block) {
		ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
#ifdef UNIV_SYNC_DEBUG
		ut_ad(!lock || rw_lock_own(*lock, lock_mode));
#endif /* UNIV_SYNC_DEBUG */
		return(block);
	} else if (bpage) {
		/* It is not a block. Just a bpage */
		ut_ad(buf_page_in_file(bpage));

		if (lock) {
			if (lock_mode == RW_LOCK_SHARED) {
				rw_lock_s_unlock(*lock);
			} else {
				rw_lock_x_unlock(*lock);
			}
		}
		*lock = NULL;
		return(NULL);
	}

	ut_ad(!bpage);
	ut_ad(lock == NULL ||*lock == NULL);
	return(NULL);
}

/********************************************************************//**
Returns TRUE if the page can be found in the buffer pool hash table.

NOTE that it is possible that the page is not yet read from disk,
though.

@return	TRUE if found in the page hash table */
UNIV_INLINE
ibool
buf_page_peek(
/*==========*/
	ulint	space,	/*!< in: space id */
	ulint	offset)	/*!< in: page number */
{
	buf_pool_t*		buf_pool = buf_pool_get(space, offset);

	return(buf_page_hash_get(buf_pool, space, offset) != NULL);
}

/********************************************************************//**
Releases a compressed-only page acquired with buf_page_get_zip(). */
UNIV_INLINE
void
buf_page_release_zip(
/*=================*/
	buf_page_t*	bpage)		/*!< in: buffer block */
{
	buf_block_t*	block;

	block = (buf_block_t*) bpage;

	switch (buf_page_get_state(bpage)) {
	case BUF_BLOCK_FILE_PAGE:
#ifdef UNIV_SYNC_DEBUG
		rw_lock_s_unlock(&block->debug_latch);
#endif /* UNUV_SYNC_DEBUG */
		/* Fall through */
	case BUF_BLOCK_ZIP_PAGE:
	case BUF_BLOCK_ZIP_DIRTY:
		buf_block_unfix(block);
		return;

	case BUF_BLOCK_POOL_WATCH:
	case BUF_BLOCK_NOT_USED:
	case BUF_BLOCK_READY_FOR_USE:
	case BUF_BLOCK_MEMORY:
	case BUF_BLOCK_REMOVE_HASH:
		break;
	}

	ut_error;
}

/********************************************************************//**
Decrements the bufferfix count of a buffer control block and releases
a latch, if specified. */
UNIV_INLINE
void
buf_page_release(
/*=============*/
	buf_block_t*	block,		/*!< in: buffer block */
	ulint		rw_latch)	/*!< in: RW_S_LATCH, RW_X_LATCH,
					RW_NO_LATCH */
{
	ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);

#ifdef UNIV_SYNC_DEBUG
	rw_lock_s_unlock(&(block->debug_latch));
#endif
	if (rw_latch == RW_S_LATCH) {
		rw_lock_s_unlock(&(block->lock));
	} else if (rw_latch == RW_X_LATCH) {
		rw_lock_x_unlock(&(block->lock));
	}

	buf_block_unfix(block);
}

#ifdef UNIV_SYNC_DEBUG
/*********************************************************************//**
Adds latch level info for the rw-lock protecting the buffer frame. This
should be called in the debug version after a successful latching of a
page if we know the latching order level of the acquired latch. */
UNIV_INLINE
void
buf_block_dbg_add_level(
/*====================*/
	buf_block_t*	block,	/*!< in: buffer page
				where we have acquired latch */
	ulint		level)	/*!< in: latching order level */
{
	sync_thread_add_level(&block->lock, level, FALSE);
}

#endif /* UNIV_SYNC_DEBUG */
/*********************************************************************//**
Get the nth chunk's buffer block in the specified buffer pool.
@return the nth chunk's buffer block. */
UNIV_INLINE
buf_block_t*
buf_get_nth_chunk_block(
/*====================*/
	const buf_pool_t* buf_pool,	/*!< in: buffer pool instance */
	ulint		n,		/*!< in: nth chunk in the buffer pool */
	ulint*		chunk_size)	/*!< in: chunk size */
{
	const buf_chunk_t*	chunk;

	chunk = buf_pool->chunks + n;
	*chunk_size = chunk->size;
	return(chunk->blocks);
}

#ifdef UNIV_DEBUG
/********************************************************************//**
Checks if buf_pool->zip_mutex is owned and is serving for a given page as its
block mutex.
@return true if buf_pool->zip_mutex is owned. */
UNIV_INLINE
bool
buf_own_zip_mutex_for_page(
/*=======================*/
	const buf_page_t*	bpage)
{
	buf_pool_t*	buf_pool	= buf_pool_from_bpage(bpage);

	ut_ad(buf_page_get_state(bpage) == BUF_BLOCK_ZIP_PAGE
	      || buf_page_get_state(bpage) == BUF_BLOCK_ZIP_DIRTY);
	ut_ad(buf_page_get_mutex(bpage) == &buf_pool->zip_mutex);

	return(mutex_own(&buf_pool->zip_mutex));
}
#endif /* UNIV_DEBUG */

#endif /* !UNIV_HOTBACKUP */