summaryrefslogtreecommitdiffstats
path: root/src/H5AC.c
blob: 605808f08694a793c5a521653822237657431534 (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
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:             H5AC.c
 *                      Jul  9 1997
 *                      Robb Matzke <matzke@llnl.gov>
 *
 * Purpose:             Functions in this file implement a cache for
 *                      things which exist on disk.  All "things" associated
 *                      with a particular HDF file share the same cache; each
 *                      HDF file has it's own cache.
 *
 * Modifications:
 *
 *      Robb Matzke, 4 Aug 1997
 *      Added calls to H5E.
 *
 *      Quincey Koziol, 22 Apr 2000
 *      Turned on "H5AC_SORT_BY_ADDR"
 *
 *-------------------------------------------------------------------------
 */

#define H5F_PACKAGE		/*suppress error about including H5Fpkg	  */

/* Pablo information */
/* (Put before include files to avoid problems with inline functions) */
#define PABLO_MASK	H5AC_mask

#include "H5private.h"		/* Generic Functions			*/
#include "H5ACprivate.h"	/* Metadata cache			*/
#include "H5Dprivate.h"		/* Dataset functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Fpkg.h"		/* Files				*/
#include "H5FDprivate.h"	/* File drivers				*/
#include "H5FLprivate.h"	/* Free Lists                           */
#include "H5Iprivate.h"		/* IDs			  		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5Pprivate.h"         /* Property lists                       */

/* Interface initialization */
static int             interface_initialize_g = 0;
#define INTERFACE_INIT H5AC_init_interface
static herr_t H5AC_init_interface(void);

/*
 * Private macros
 */

/* Hash an address in the file to an offset in the cache */
#define H5AC_HASH_DIVISOR 8     /* Attempt to spread out the hashing */
                                /* This should be the same size as the alignment of */
                                /* of the smallest file format object written to the file.  */
#define H5AC_HASH(F,ADDR) H5F_addr_hash((ADDR/H5AC_HASH_DIVISOR),(F)->shared->cache->nslots)

/*
 * Private typedefs & structs
 */

#ifdef H5AC_DEBUG
typedef struct H5AC_prot_t {
    int		nprots;		/*number of things protected	     */
    int		aprots;		/*nelmts of `prot' array	     */
    H5AC_info_t	**slot;		/*array of pointers to protected things	     */
} H5AC_prot_t;
#endif /* H5AC_DEBUG */

struct H5AC_t {
    unsigned	nslots;			/*number of cache slots		     */
    H5AC_info_t **slot;		/*the cache slots, an array of pointers to the cached objects */
    H5AC_info_t **dslot;	/*"held object" cache slots, an array of pointers to dirty cached objects */
#ifdef H5AC_DEBUG
    H5AC_prot_t *prot;		/*the protected slots		     */
#endif /* H5AC_DEBUG */
    int	nprots;			/*number of protected objects	     */
#ifdef H5AC_DEBUG
    struct {
	unsigned	nhits;			/*number of cache hits		     */
	unsigned	nmisses;		/*number of cache misses	     */
	unsigned	ninits;			/*number of cache inits		     */
	unsigned	nflushes;		/*number of flushes to disk	     */
#ifdef H5_HAVE_PARALLEL
	unsigned	ndestroys;		/*number of cache destroys	     */
	unsigned	nholds;			/*number of cache holds	     */
	unsigned	nrestores;		/*number of cache restores	     */
#endif /* H5_HAVE_PARALLEL */
    } diagnostics[H5AC_NTYPES];		/*diagnostics for each type of object*/
#endif /* H5AC_DEBUG */
};

/*
 * Private file-scope variables.
 */

/* Default dataset transfer property list for metadata I/O calls */
/* (Collective set, "block before metadata write" set and "library internal" set) */
/* (Global variable definition, declaration is in H5ACprivate.h also) */
hid_t H5AC_dxpl_id=(-1);

/* Private dataset transfer property list for metadata I/O calls */
/* (Collective set and "library internal" set) */
/* (Static variable definition) */
static hid_t H5AC_noblock_dxpl_id=(-1);

/* Dataset transfer property list for independent metadata I/O calls */
/* (just "library internal" set - i.e. independent transfer mode) */
/* (Global variable definition, declaration is in H5ACprivate.h also) */
hid_t H5AC_ind_dxpl_id=(-1);

static H5AC_t          *current_cache_g = NULL;         /*for sorting */

/* Declare a free list to manage the H5AC_t struct */
H5FL_DEFINE_STATIC(H5AC_t);

/* Declare a PQ free list to manage the cache mapping array information */
H5FL_ARR_DEFINE_STATIC(unsigned,-1);

/* Declare a PQ free list to manage the cache slot array information */
H5FL_ARR_DEFINE_STATIC(H5AC_info_ptr_t,-1);

#ifdef H5AC_DEBUG
/* Declare a PQ free list to manage the protected slot array information */
H5FL_ARR_DEFINE_STATIC(H5AC_prot_t,-1);
#endif /* H5AC_DEBUG */


/*-------------------------------------------------------------------------
 * Function:	H5AC_init
 *
 * Purpose:	Initialize the interface from some other layer.
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Saturday, January 18, 2003
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_init(void)
{
    herr_t ret_value=SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI(H5AC_init, FAIL)
    /* FUNC_ENTER() does all the work */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:	H5AC_init_interface
 *
 * Purpose:	Initialize interface-specific information
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *              Thursday, July 18, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5AC_init_interface(void)
{
    H5P_genclass_t  *xfer_pclass;   /* Dataset transfer property list class object */
#ifdef H5_HAVE_PARALLEL
    H5P_genplist_t  *xfer_plist;    /* Dataset transfer property list object */
    unsigned block_before_meta_write; /* "block before meta write" property value */
    unsigned library_internal=1;    /* "library internal" property value */
    H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */
#endif /* H5_HAVE_PARALLEL */
    herr_t ret_value=SUCCEED;           /* Return value */

    FUNC_ENTER_NOINIT(H5AC_init_interface)

    /* Sanity check */
    assert(H5P_CLS_DATASET_XFER_g!=(-1));

    /* Get the dataset transfer property list class object */
    if (NULL == (xfer_pclass = H5I_object(H5P_CLS_DATASET_XFER_g)))
        HGOTO_ERROR(H5E_CACHE, H5E_BADATOM, FAIL, "can't get property list class")

    /* Get an ID for the blocking, collective H5AC dxpl */
    if ((H5AC_dxpl_id=H5P_create_id(xfer_pclass)) < 0)
        HGOTO_ERROR(H5E_CACHE, H5E_CANTCREATE, FAIL, "unable to register property list")

#ifdef H5_HAVE_PARALLEL
    /* Get the property list object */
    if (NULL == (xfer_plist = H5I_object(H5AC_dxpl_id)))
        HGOTO_ERROR(H5E_CACHE, H5E_BADATOM, FAIL, "can't get new property list object")

    /* Insert 'block before metadata write' property */
    block_before_meta_write=1;
    if(H5P_insert(xfer_plist,H5AC_BLOCK_BEFORE_META_WRITE_NAME,H5AC_BLOCK_BEFORE_META_WRITE_SIZE,&block_before_meta_write,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Insert 'library internal' property */
    if(H5P_insert(xfer_plist,H5AC_LIBRARY_INTERNAL_NAME,H5AC_LIBRARY_INTERNAL_SIZE,&library_internal,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Set the transfer mode */
    xfer_mode=H5FD_MPIO_COLLECTIVE;
    if (H5P_set(xfer_plist,H5D_XFER_IO_XFER_MODE_NAME,&xfer_mode)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set value")
#endif /* H5_HAVE_PARALLEL */

    /* Get an ID for the non-blocking, collective H5AC dxpl */
    if ((H5AC_noblock_dxpl_id=H5P_create_id(xfer_pclass)) < 0)
        HGOTO_ERROR(H5E_CACHE, H5E_CANTCREATE, FAIL, "unable to register property list")

#ifdef H5_HAVE_PARALLEL
    /* Get the property list object */
    if (NULL == (xfer_plist = H5I_object(H5AC_noblock_dxpl_id)))
        HGOTO_ERROR(H5E_CACHE, H5E_BADATOM, FAIL, "can't get new property list object")

    /* Insert 'block before metadata write' property */
    block_before_meta_write=0;
    if(H5P_insert(xfer_plist,H5AC_BLOCK_BEFORE_META_WRITE_NAME,H5AC_BLOCK_BEFORE_META_WRITE_SIZE,&block_before_meta_write,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Insert 'library internal' property */
    if(H5P_insert(xfer_plist,H5AC_LIBRARY_INTERNAL_NAME,H5AC_LIBRARY_INTERNAL_SIZE,&library_internal,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Set the transfer mode */
    xfer_mode=H5FD_MPIO_COLLECTIVE;
    if (H5P_set(xfer_plist,H5D_XFER_IO_XFER_MODE_NAME,&xfer_mode)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set value")
#endif /* H5_HAVE_PARALLEL */

    /* Get an ID for the non-blocking, independent H5AC dxpl */
    if ((H5AC_ind_dxpl_id=H5P_create_id(xfer_pclass)) < 0)
        HGOTO_ERROR(H5E_CACHE, H5E_CANTCREATE, FAIL, "unable to register property list")

#ifdef H5_HAVE_PARALLEL
    /* Get the property list object */
    if (NULL == (xfer_plist = H5I_object(H5AC_ind_dxpl_id)))
        HGOTO_ERROR(H5E_CACHE, H5E_BADATOM, FAIL, "can't get new property list object")

    /* Insert 'block before metadata write' property */
    block_before_meta_write=0;
    if(H5P_insert(xfer_plist,H5AC_BLOCK_BEFORE_META_WRITE_NAME,H5AC_BLOCK_BEFORE_META_WRITE_SIZE,&block_before_meta_write,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Insert 'library internal' property */
    if(H5P_insert(xfer_plist,H5AC_LIBRARY_INTERNAL_NAME,H5AC_LIBRARY_INTERNAL_SIZE,&library_internal,NULL,NULL,NULL,NULL,NULL)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "can't insert metadata cache dxpl property")

    /* Set the transfer mode */
    xfer_mode=H5FD_MPIO_INDEPENDENT;
    if (H5P_set(xfer_plist,H5D_XFER_IO_XFER_MODE_NAME,&xfer_mode)<0)
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, FAIL, "unable to set value")
#endif /* H5_HAVE_PARALLEL */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5AC_init_interface() */


/*-------------------------------------------------------------------------
 * Function:	H5AC_term_interface
 *
 * Purpose:	Terminate this interface.
 *
 * Return:	Success:	Positive if anything was done that might
 *				affect other interfaces; zero otherwise.
 *
 * 		Failure:	Negative.
 *
 * Programmer:	Quincey Koziol
 *              Thursday, July 18, 2002
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5AC_term_interface(void)
{
    int		n=0;

    FUNC_ENTER_NOINIT(H5AC_term_interface)

    if (interface_initialize_g) {
        if(H5AC_dxpl_id>0 || H5AC_noblock_dxpl_id>0 || H5AC_ind_dxpl_id>0) {
            /* Indicate more work to do */
            n = 1; /* H5I */

            /* Close H5AC dxpl */
            if (H5I_dec_ref(H5AC_dxpl_id) < 0 ||
                    H5I_dec_ref(H5AC_noblock_dxpl_id) < 0 ||
                    H5I_dec_ref(H5AC_ind_dxpl_id) < 0)
                H5E_clear(NULL); /*ignore error*/
            else {
                /* Reset static IDs */
                H5AC_dxpl_id=(-1);
                H5AC_noblock_dxpl_id=(-1);
                H5AC_ind_dxpl_id=(-1);

                /* Reset interface initialization flag */
                interface_initialize_g = 0;
            } /* end else */
        } /* end if */
        else
            /* Reset interface initialization flag */
            interface_initialize_g = 0;
    } /* end if */

    FUNC_LEAVE_NOAPI(n)
} /* end H5AC_term_interface() */


/*-------------------------------------------------------------------------
 * Function:    H5AC_create
 *
 * Purpose:     Initialize the cache just after a file is opened.  The
 *              SIZE_HINT is the number of cache slots desired.  If you
 *              pass an invalid value then H5AC_NSLOTS is used.  You can
 *              turn off caching by using 1 for the SIZE_HINT value.
 *
 * Return:      Success:        Number of slots actually used.
 *
 *              Failure:        Negative
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5AC_create(const H5F_t *f, int size_hint)
{
    H5AC_t                 *cache = NULL;
    int ret_value;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_create, FAIL)

    assert(f);
    assert(NULL == f->shared->cache);

    /* If size hint is negative, use the default size */
    if (size_hint < 1)
        size_hint = H5AC_NSLOTS;

    if (NULL==(f->shared->cache = cache = H5FL_CALLOC(H5AC_t)))
	HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
    cache->nslots = (unsigned)size_hint;
    if (NULL==( cache->slot = H5FL_ARR_CALLOC(H5AC_info_ptr_t,cache->nslots)))
        HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
    if (NULL==( cache->dslot = H5FL_ARR_CALLOC(H5AC_info_ptr_t,cache->nslots)))
        HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
#ifdef H5AC_DEBUG
    if ((cache->prot = H5FL_ARR_CALLOC(H5AC_prot_t,cache->nslots))==NULL)
        HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
#endif /* H5AC_DEBUG */

    /* Set return value */
    ret_value=size_hint;

done:
    if(ret_value<0) {
        if(cache!=NULL) {
            if(cache->dslot !=NULL)
                cache->dslot = H5FL_ARR_FREE (H5AC_info_ptr_t,cache->dslot);
            if(cache->slot !=NULL)
                cache->slot = H5FL_ARR_FREE (H5AC_info_ptr_t,cache->slot);
#ifdef H5AC_DEBUG
            if(cache->prot !=NULL)
                cache->prot = H5FL_ARR_FREE (H5AC_prot_t,cache->prot);
#endif /* H5AC_DEBUG */
            f->shared->cache = H5FL_FREE (H5AC_t,f->shared->cache);
        } /* end if */
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_dest
 *
 * Purpose:     Flushes all data to disk and destroys the cache.
 *              This function fails if any object are protected since the
 *              resulting file might not be consistent.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_dest(H5F_t *f, hid_t dxpl_id)
{
    H5AC_t *cache = NULL;
    herr_t ret_value=SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_dest, FAIL)

    assert(f);
    assert(f->shared->cache);
    cache = f->shared->cache;

    if (H5AC_flush(f, dxpl_id, NULL, HADDR_UNDEF, H5F_FLUSH_INVALIDATE) < 0)
        HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush cache")

#ifdef H5AC_DEBUG
    {
        unsigned i;
        for (i=0; i<cache->nslots; i++) {
            cache->prot[i].slot = H5MM_xfree(cache->prot[i].slot);
            cache->prot[i].aprots = 0;
            cache->prot[i].nprots = 0;
        }
        cache->prot = H5FL_ARR_FREE(H5AC_prot_t,cache->prot);
    }
#endif

    cache->dslot = H5FL_ARR_FREE(H5AC_info_ptr_t,cache->dslot);
    cache->slot = H5FL_ARR_FREE(H5AC_info_ptr_t,cache->slot);
    cache->nslots = 0;
    f->shared->cache = cache = H5FL_FREE(H5AC_t,cache);

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_find
 *
 * Purpose:     Given an object type and the address at which that object
 *              is located in the file, return a pointer to the object.
 *              The optional UDATA1 and UDATA2 structures are passed down to
 *              the function that is responsible for loading the object into
 *              memory.
 *
 *              The returned pointer is guaranteed to be valid until the next
 *              call to an H5AC function (if you want a pointer which is valid
 *              indefinately then see H5AC_protect()).
 *
 *              If H5AC_DEBUG is defined then this function also
 *              checks that the requested object is not currently
 *              protected since it is illegal to modify a protected object
 *              except through the pointer returned by H5AC_protect().
 *
 * Return:      Success:        Pointer to the object.  The pointer is
 *                              valid until some other cache function
 *                              is called.
 *
 *              Failure:        NULL
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 *
 *      Robb Matzke, 4 Aug 1997
 *      Fails immediately if the cached object is at the correct address
 *      but is of the wrong type.  This happens if the caller doesn't know
 *      what type of object is at the address and calls this function with
 *      various type identifiers until one succeeds (cf., the debugger).
 *
 *      Robb Matzke, 30 Oct 1997
 *      Keeps track of hits, misses, and flushes per object type so we have
 *      some cache performance diagnostics.
 *
 * 	Robb Matzke, 1999-07-27
 *	The ADDR argument is passed by value.
 *
 *-------------------------------------------------------------------------
 */
void *
H5AC_find(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t addr,
	    const void *udata1, void *udata2)
{
    unsigned                idx;
    void                   *thing;
    H5AC_flush_func_t       flush;
    H5AC_info_t           **info;
#ifdef H5_HAVE_PARALLEL
    H5AC_info_t           **dinfo = NULL;
#endif /* H5_HAVE_PARALLEL */
    H5AC_t                 *cache;
    void                   *ret_value;          /* Return value */

    FUNC_ENTER_NOAPI(H5AC_find, NULL)

    assert(f);
    assert(f->shared->cache);
    assert(type);
    assert(type->load);
    assert(type->flush);
    assert(H5F_addr_defined(addr));

    /* Get local pointers to the file's cache information */
    idx = H5AC_HASH(f, addr);
    cache = f->shared->cache;
    info = cache->slot + idx;

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5AC_dest_func_t        dest;

        /* Get local pointer to file's dirty cache information */
        dinfo = cache->dslot + idx;

        /* Check if the cache has 'held' information for this cache slot */
        if (*dinfo) {
            /* Sanity check that the 'clean' item is really clean */
            assert(*info);
            assert((*info)->dirty==0);

            /* Destroy 'current' information */
            dest = (*info)->type->dest;
            if ((dest)(f, (*info))<0)
                HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, NULL, "unable to free cached object")

            /* Restore 'held' information back to 'current' information */
            (*info)=(*dinfo);

            /* Clear 'held' information */
            (*dinfo)=NULL;

#ifdef H5AC_DEBUG
            cache->diagnostics[type->id].nrestores++;
#endif /* H5AC_DEBUG */
        } /* end if */
    } /* end if */
#endif /* H5_HAVE_PARALLEL */

    /*
     * Return right away if the item is in the cache.
     */
    if ((*info) && H5F_addr_eq(addr,(*info)->addr)
#ifdef H5AC_DEBUG
            && (*info)->type==type
#endif /* H5AC_DEBUG */
            ) {
#ifndef H5AC_DEBUG
        /* Sanity check that the object in the cache is the correct type */
        assert((*info)->type==type);
#endif /* H5AC_DEBUG */

#ifdef H5AC_DEBUG
        cache->diagnostics[type->id].nhits++;
#endif /* H5AC_DEBUG */
        HGOTO_DONE(*info)
    }
#ifdef H5AC_DEBUG
    cache->diagnostics[type->id].nmisses++;
#endif /* H5AC_DEBUG */

#ifdef H5AC_DEBUG
    /*
     * Check that the requested thing isn't protected, for protected things
     * can only be modified through the pointer already handed out by the
     * H5AC_protect() function.
     */
    {
        H5AC_prot_t            *prot = NULL;
        int                    i;

        prot = cache->prot + idx;
        for (i = 0; i < prot->nprots; i++) {
            assert(H5F_addr_ne(addr, prot->slot[i]->addr));
        }
    }
#endif /* H5AC_DEBUG */

    /*
     * Load a new thing.  If it can't be loaded, then return an error
     * without preempting anything.
     */
    if (NULL == (thing = (type->load)(f, dxpl_id, addr, udata1, udata2)))
        HGOTO_ERROR(H5E_CACHE, H5E_CANTLOAD, NULL, "unable to load object")

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5P_genplist_t *dxpl;           /* Dataset transfer property list */
        H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

        /* Get the dataset transfer property list */
        if (NULL == (dxpl = H5I_object(dxpl_id)))
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a dataset creation property list")

        /* Get the transfer mode property */
        if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
            HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, NULL, "can't retrieve xfer mode")

        /* Make certain there is no 'held' info for this slot */
        assert((*dinfo)==NULL);

        /* Must be using collective I/O to flush metadata in parallel */
        if(xfer_mode==H5FD_MPIO_INDEPENDENT) {
            /* Check if there is dirty metadata in this slot */
            if((*info) && (*info)->dirty) {
                /* 'Hold' the current metadata for later */
                (*dinfo)=(*info);

                /* Reset the 'current' metadata, so it doesn't get flushed */
                (*info)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[(*dinfo)->type->id].nholds++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end else */
    } /* end if */
#endif /* H5_HAVE_PARALLEL */

    /*
     * Flush & destroy the previous cache entry if there is one.
     */
    if (*info) {
#ifdef H5AC_DEBUG
        H5AC_subid_t type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

        flush = (*info)->type->flush;
        if ( (flush)(f, dxpl_id, TRUE, (*info)->addr, (*info)) < 0) {
            /*
             * The old thing could not be removed from the stack.
             * Release the new thing and fail.
             */
            if ((type->dest)(f, thing) < 0)
                HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, NULL, "unable to destroy just-loaded object")
            HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, NULL, "unable to flush existing cached object")
        }
#ifdef H5AC_DEBUG
        cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
    } /* end if */

    /*
     * Make the cache point to the new thing.
     */
    (*info)=thing;
    (*info)->type = type;
    (*info)->addr = addr;
    assert((*info)->dirty==0);  /* Should be clean after being loaded */

    /* Set the return value */
    ret_value=thing;

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_compare
 *
 * Purpose:     Compare two hash entries by address.  Unused entries are
 *              all equal to one another and greater than all used entries.
 *
 * Return:      Success:        -1, 0, 1
 *
 *              Failure:        never fails
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Aug 12 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
H5AC_compare(const void *_a, const void *_b)
{
    unsigned               a = *((const unsigned *) _a);
    unsigned               b = *((const unsigned *) _b);
    H5AC_info_t           *slot_a;
    H5AC_info_t           *slot_b;
    int                    ret_value=0;

    /* Use FUNC_ENTER_NOINIT here to avoid performance issues */
    FUNC_ENTER_NOINIT(H5AC_compare)

    assert(current_cache_g);

    /* Create aliases for slots */
    slot_a=current_cache_g->slot[a];
    slot_b=current_cache_g->slot[b];

    assert(slot_a);
    assert(slot_b);
    assert(slot_a->type);
    assert(slot_b->type);

    if (slot_a->addr < slot_b->addr) {
        ret_value=(-1);
    } else if (slot_a->addr > slot_b->addr) {
        ret_value=1;
    }

    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_flush
 *
 * Purpose:     Flushes (and destroys if DESTROY is non-zero) the specified
 *              entry from the cache.  If the entry TYPE is CACHE_FREE and
 *              ADDR is HADDR_UNDEF then all types of entries are
 *              flushed. If TYPE is CACHE_FREE and ADDR is defined then
 *              whatever is cached at ADDR is flushed.  Otherwise the thing
 *              at ADDR is flushed if it is the correct type.
 *
 *              If there are protected objects they will not be flushed.
 *              However, an attempt will be made to flush all non-protected
 *              items before this function returns failure.
 *
 * Return:      Non-negative on success/Negative on failure if there was a
 *              request to flush all items and something was protected.
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 * 		Robb Matzke, 1999-07-27
 *		The ADDR argument is passed by value.
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_flush(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t addr, unsigned flags)
{
    herr_t                  status;
    H5AC_flush_func_t       flush=NULL; /* 'flush' callback for an object */
    H5AC_info_t           **info;
    unsigned               *map = NULL; /* Mapping of cache entries */
    hbool_t                 destroy=(flags&H5F_FLUSH_INVALIDATE)>0;     /* Flag for destroying objects */
    hbool_t                 clear_only=(flags&H5F_FLUSH_CLEAR_ONLY)>0;  /* Flag for only clearing objects */
    unsigned               nslots;
    H5AC_t                 *cache;
    unsigned                u;     /* Local index variable */
    herr_t ret_value=SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_flush, FAIL)

    assert(f);
    assert(f->shared->cache);

    /* Get local copy of this information */
    cache = f->shared->cache;

    if (!H5F_addr_defined(addr)) {
        unsigned first_flush=1;     /* Indicate if this is the first flush */

        /*
         * Sort the cache entries by address since flushing them in
         * ascending order by address is much more efficient.
         */
        if (NULL==(map=H5FL_ARR_MALLOC(unsigned,cache->nslots)))
            HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")
#ifdef H5_HAVE_PARALLEL
        /* If MPI based VFD is used, do special parallel I/O actions */
        if(IS_H5FD_MPI(f)) {
            H5AC_info_t       **dinfo;
#ifdef H5AC_DEBUG
            H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */
#ifndef NDEBUG
            H5P_genplist_t *dxpl;           /* Dataset transfer property list */
            H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

            /* Get the dataset transfer property list */
            if (NULL == (dxpl = H5I_object(dxpl_id)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset creation property list")

            /* Get the transfer mode property */
            if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
                HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't retrieve xfer mode")

            /* Sanity check transfer mode */
            assert(xfer_mode == H5FD_MPIO_COLLECTIVE || IS_H5FD_FPHDF5(f));
#endif /* NDEBUG */

            /* Create the mapping */
            for (u = nslots = 0; u < cache->nslots; u++) {
                info = cache->slot + u;
                dinfo = cache->dslot + u;

                /* Move dirty metadata from 'held' slots into 'regular' slots */
                if((*dinfo)!=NULL) {
                    H5AC_dest_func_t        dest;

                    /* Various sanity checks */
                    assert((*dinfo)->dirty);
                    assert((*info)!=NULL);
                    assert((*info)->dirty==0);

#ifdef H5AC_DEBUG
                    type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                    /* Destroy 'current' information */
                    dest = (*info)->type->dest;
                    if ((dest)(f, (*info))<0)
                        HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free cached object")

                    /* Restore 'held' information back to 'current' information */
                    (*info)=(*dinfo);

                    /* Clear 'held' information */
                    (*dinfo)=NULL;

#ifdef H5AC_DEBUG
                    cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
                } /* end if */
                if ((*info))
                    map[nslots++] = u;
            } /* end for */
        } /* end if */
        else {
#endif /* H5_HAVE_PARALLEL */
            for (u = nslots = 0; u < cache->nslots; u++) { /*lint !e539 Positive indention is OK */
                if (cache->slot[u]!=NULL)
                    map[nslots++] = u;
            }
#ifdef H5_HAVE_PARALLEL
        } /* end else */
#endif /* H5_HAVE_PARALLEL */
        assert(NULL == current_cache_g);
        current_cache_g = cache;
        HDqsort(map, nslots, sizeof(unsigned), H5AC_compare);
        current_cache_g = NULL;
#ifndef NDEBUG
        for (u = 1; u < nslots; u++)
            assert(H5F_addr_lt(cache->slot[map[u - 1]]->addr, cache->slot[map[u]]->addr));
#endif

        /*
         * Look at all cache entries.
         */
        for (u = 0; u < nslots; u++) {
            info = cache->slot + map[u];
            assert(*info);
            if (!type || type == (*info)->type) {
#ifdef H5AC_DEBUG
                H5AC_subid_t type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                /* Clear the dirty flag only, if requested */
                if(clear_only) {
                    /* Call the callback routine to clear all dirty flags for object */
                    if(((*info)->type->clear)(f, *info, destroy)<0)
                        HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to clear cache");
                } /* end if */
                else {
                    flush = (*info)->type->flush;

                    /* Only block for all the processes on the first piece of metadata */
                    if(first_flush && (*info)->dirty) {
                        status = (flush)(f, dxpl_id, destroy, (*info)->addr, (*info));
                        first_flush=0;
                    } /* end if */
                    else
                        status = (flush)(f, H5AC_noblock_dxpl_id, destroy, (*info)->addr, (*info));
                    if (status < 0)
                        HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush cache")
#ifdef H5AC_DEBUG
                    cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
                } /* end else */

                /* Destroy entry also, if asked */
                if (destroy)
                    (*info)= NULL;
            }
        }

        /*
         * If there are protected object then fail.  However, everything
         * else should have been flushed.
         */
        if (cache->nprots > 0)
            HGOTO_ERROR(H5E_CACHE, H5E_PROTECT, FAIL, "cache has protected items")
    } else {
        u = H5AC_HASH(f, addr);
        info = cache->slot + u;
#ifdef H5_HAVE_PARALLEL
        /* If MPI based VFD is used, do special parallel I/O actions */
        if(IS_H5FD_MPI(f)) {
            H5AC_info_t       **dinfo;
#ifdef H5AC_DEBUG
            H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */
#ifndef NDEBUG
            H5P_genplist_t *dxpl;           /* Dataset transfer property list */
            H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

            /* Get the dataset transfer property list */
            if (NULL == (dxpl = H5I_object(dxpl_id)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset creation property list")

            /* Get the transfer mode property */
            if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
                HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't retrieve xfer mode")

            /* Sanity check transfer mode */
            assert(xfer_mode==H5FD_MPIO_COLLECTIVE);
#endif /* NDEBUG */

            dinfo = cache->dslot + u;

            /* Restore dirty metadata from 'held' slot to 'current' slot */
            if((*dinfo)!=NULL) {
                H5AC_dest_func_t        dest;

                /* Various sanity checks */
                assert((*dinfo)->dirty);
                assert((*info)!=NULL);
                assert((*info)->dirty==0);

#ifdef H5AC_DEBUG
                type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                /* Destroy 'current' information */
                dest = (*info)->type->dest;
                if ((dest)(f, (*info))<0)
                    HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free cached object")

                /* Restore 'held' information back to 'current' information */
                (*info)=(*dinfo);

                /* Clear 'held' information */
                (*dinfo)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end if */
#endif /* H5_HAVE_PARALLEL */
        if ((*info) && (!type || (*info)->type == type) &&
                H5F_addr_eq((*info)->addr, addr)) {
#ifdef H5AC_DEBUG
            H5AC_subid_t type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

            /*
             * Flush just this entry.
             */

            /* Clear the dirty flag only, if requested */
            if(clear_only) {
                /* Call the callback routine to clear all dirty flags for object */
                if(((*info)->type->clear)(f, *info, destroy)<0)
                    HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to clear cache");
            } /* end if */
            else {
                flush = (*info)->type->flush;
                if((flush)(f, dxpl_id, destroy, (*info)->addr, (*info)) < 0)
                    HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush object")
#ifdef H5AC_DEBUG
                cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
            } /* end else */

            /* Destroy entry also, if asked */
            if (destroy)
                (*info)= NULL;
        } /* end if */
    } /* end else */

done:
    if(map!=NULL)
        map = H5FL_ARR_FREE(unsigned,map);

    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_set
 *
 * Purpose:     Adds the specified thing to the cache.  The thing need not
 *              exist on disk yet, but it must have an address and disk
 *              space reserved.
 *
 *              If H5AC_DEBUG is defined then this function checks
 *              that the object being inserted isn't a protected object.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 * 		Robb Matzke, 1999-07-27
 *		The ADDR argument is passed by value.
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_set(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t addr, void *thing)
{
    unsigned                idx;
    H5AC_flush_func_t       flush;
    H5AC_info_t           **info;
    H5AC_t                 *cache;
    herr_t ret_value=SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_set, FAIL)

    assert(f);
    assert(f->shared->cache);
    assert(type);
    assert(type->flush);
    assert(H5F_addr_defined(addr));
    assert(thing);

    /* Get local copy of this information */
    idx = H5AC_HASH(f, addr);
    cache = f->shared->cache;
    info = cache->slot + idx;

#ifdef H5AC_DEBUG
    {
        H5AC_prot_t            *prot = NULL;
        int                    i;

        prot = cache->prot + idx;
        for (i = 0; i < prot->nprots; i++)
            assert(H5F_addr_ne(addr, prot->slot[i]->addr));
    }
#endif

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5AC_info_t       **dinfo;
#ifdef H5AC_DEBUG
        H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */
        H5P_genplist_t *dxpl;           /* Dataset transfer property list */
        H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

        /* Get the dataset transfer property list */
        if (NULL == (dxpl = H5I_object(dxpl_id)))
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset creation property list")

        /* Get the transfer mode property */
        if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
            HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't retrieve xfer mode")

        /* Get pointer to 'held' information */
        dinfo = cache->dslot + idx;

        /* Sanity check transfer mode */
        if(xfer_mode==H5FD_MPIO_COLLECTIVE) {
            /* Check for dirty metadata */
            if(*dinfo) {
                H5AC_dest_func_t        dest;

                /* Various sanity checks */
                assert((*dinfo)->dirty);
                assert((*info)!=NULL);
                assert((*info)->dirty==0);

#ifdef H5AC_DEBUG
                type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                /* Destroy 'current' information */
                dest = (*info)->type->dest;
                if ((dest)(f, (*info))<0)
                    HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free cached object")

                /* Restore 'held' information back to 'current' information */
                (*info)=(*dinfo);

                /* Clear 'held' information */
                (*dinfo)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end if */
        else {
            /* Sanity check */
            assert((*dinfo)==NULL);
            assert(xfer_mode==H5FD_MPIO_INDEPENDENT);

            /* Make certain there will be no write of dirty metadata */
            if((*info) && (*info)->dirty) {
                /* Sanity check new item */
                assert(((H5AC_info_t*)thing)->dirty==0);

                /* 'Hold' the current metadata for later */
                (*dinfo)=(*info);

                /* Reset the 'current' metadata, so it doesn't get flushed */
                (*info)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[(*dinfo)->type->id].nholds++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end else */
    } /* end if */
#endif /* H5_HAVE_PARALLEL */

    /* Flush any object already in cache slot */
    if ((*info)) {
#ifdef H5AC_DEBUG
        H5AC_subid_t type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

        flush = (*info)->type->flush;
        if ((flush)(f, dxpl_id, TRUE, (*info)->addr, (*info)) < 0)
            HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush object")
#ifdef H5AC_DEBUG
        cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
    } /* end if */

    /* Cache this item */
    (*info)=thing;
    (*info)->type = type;
    (*info)->addr = addr;
#ifdef H5AC_DEBUG
    cache->diagnostics[type->id].ninits++;
#endif /* H5AC_DEBUG */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_rename
 *
 * Purpose:     Use this function to notify the cache that an object's
 *              file address changed.
 *
 *              If H5AC_DEBUG is defined then this function checks
 *              that the old and new addresses don't correspond to the
 *              address of a protected object.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Jul  9 1997
 *
 * Modifications:
 * 		Robb Matzke, 1999-07-27
 *		The OLD_ADDR and NEW_ADDR arguments are passed by value.
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_rename(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t old_addr,
	    haddr_t new_addr)
{
    unsigned                old_idx, new_idx;
    H5AC_flush_func_t       flush;
    H5AC_t                 *cache;
    H5AC_info_t           **new_info = NULL;
    H5AC_info_t           **old_info = NULL;
    herr_t ret_value=SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_rename, FAIL)

    assert(f);
    assert(f->shared->cache);
    assert(type);

    /* Get local copy of this information */
    old_idx = H5AC_HASH(f, old_addr);
    new_idx = H5AC_HASH(f, new_addr);
    cache = f->shared->cache;
    new_info = cache->slot + new_idx;
    old_info = cache->slot + old_idx;

#ifdef H5AC_DEBUG
    {
        H5AC_prot_t            *prot = NULL;
        int                     i;

        prot = cache->prot + old_idx;
        for (i = 0; i < prot->nprots; i++)
            assert(H5F_addr_ne(old_addr, prot->slot[i]->addr));
        prot = cache->prot + new_idx;
        for (i = 0; i < prot->nprots; i++)
            assert(H5F_addr_ne(new_addr, prot->slot[i]->addr));
    }
#endif

    /*
     * We don't need to do anything if the object isn't cached or if the
     * new hash value is the same as the old one.
     */
    if (H5F_addr_ne((*old_info)->addr, old_addr) || (*old_info)->type!=type)
        HGOTO_DONE(SUCCEED)
    if (old_idx == new_idx) {
        (*old_info)->addr = new_addr;
        HGOTO_DONE(SUCCEED)
    }

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5AC_info_t       **new_dinfo;
#ifdef H5AC_DEBUG
        H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */
        H5P_genplist_t *dxpl;           /* Dataset transfer property list */
        H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

        /* Get the dataset transfer property list */
        if (NULL == (dxpl = H5I_object(dxpl_id)))
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset creation property list")

        /* Get the transfer mode property */
        if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
            HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't retrieve xfer mode")

        /* Get pointer to new 'held' information */
        new_dinfo = cache->dslot + new_idx;

        /* Sanity check transfer mode */
        if(xfer_mode==H5FD_MPIO_COLLECTIVE) {
            /* Check for dirty metadata */
            if(*new_dinfo) {
                H5AC_dest_func_t        dest;

                /* Various sanity checks */
                assert((*new_dinfo)->dirty);
                assert((*new_info)!=NULL);
                assert((*new_info)->dirty==0);

#ifdef H5AC_DEBUG
                type_id=(*new_info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                /* Destroy 'current' information */
                dest = (*new_info)->type->dest;
                if ((dest)(f, (*new_info))<0)
                    HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free cached object")

                /* Restore 'held' information back to 'current' information */
                (*new_info)=(*new_dinfo);

                /* Clear 'held' information */
                (*new_dinfo)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end if */
        else {
            /* Sanity check that there will be no write of dirty metadata */
            assert((*new_dinfo)==NULL);
            assert(xfer_mode==H5FD_MPIO_INDEPENDENT);

            /* Make certain there will be no write of dirty metadata */
            if((*new_info) && (*new_info)->dirty) {
                /* Sanity check that we won't put two pieces of dirty metadata in same cache location */
                assert((*old_info)->dirty==0);

                /* 'Hold' the current metadata for later */
                (*new_dinfo)=(*new_info);

                /* Reset the 'current' metadata, so it doesn't get flushed */
                (*new_info)=NULL;

#ifdef H5AC_DEBUG
                cache->diagnostics[(*new_dinfo)->type->id].nholds++;
#endif /* H5AC_DEBUG */
            } /* end if */
        } /* end else */
    } /* end if */
#endif /* H5_HAVE_PARALLEL */

    /*
     * Free the item from the destination cache line.
     */
    if (*new_info) {
#ifdef H5AC_DEBUG
        H5AC_subid_t type_id=(*new_info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

        flush = (*new_info)->type->flush;
        if ( (flush)(f, dxpl_id, TRUE, (*new_info)->addr, (*new_info)) < 0)
            HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush object")
#ifdef H5AC_DEBUG
        cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
    }

    /*
     * Move the source to the destination (it might not be cached)
     */
    (*new_info)= (*old_info);
    (*new_info)->addr = new_addr;

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5AC_info_t       **old_dinfo;
#ifdef H5AC_DEBUG
        H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */

        /* Get pointer to new 'held' information */
        old_dinfo = cache->dslot + old_idx;

        /* Check for 'held' metadata in old location & restore it, if so */
        if(*old_dinfo) {
            /* Sanity check */
            assert((*old_dinfo)->dirty);

#ifdef H5AC_DEBUG
            type_id=(*old_info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

            /* Restore 'held' information back to 'current' information */
            (*old_info)=(*old_dinfo);

            /* Clear 'held' information */
            (*old_dinfo)=NULL;

#ifdef H5AC_DEBUG
            cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
        } /* end if */
        else
            (*old_info)= NULL;
    } /* end if */
    else {
#endif /* H5_HAVE_PARALLEL */

        (*old_info)= NULL;
#ifdef H5_HAVE_PARALLEL
    } /* end else */
#endif /* H5_HAVE_PARALLEL */

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_protect
 *
 * Purpose:     Similar to H5AC_find() except the object is removed from
 *              the cache and given to the caller, preventing other parts
 *              of the program from modifying the protected object or
 *              preempting it from the cache.
 *
 *              The caller must call H5AC_unprotect() when finished with
 *              the pointer.
 *
 *              If H5AC_DEBUG is defined then we check that the
 *              requested object isn't already protected.
 *
 * Return:      Success:        Ptr to the object.
 *
 *              Failure:        NULL
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Sep  2 1997
 *
 * Modifications:
 *		Robb Matzke, 1999-07-27
 *		The ADDR argument is passed by value.
 *-------------------------------------------------------------------------
 */
void *
H5AC_protect(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t addr,
	     const void *udata1, void *udata2)
{
    unsigned                idx;                /* Index in cache */
    void                   *thing=NULL;
    H5AC_t                 *cache;
    H5AC_info_t           **info;
    void                   *ret_value;          /* Return value */

#ifdef H5AC_DEBUG
    H5AC_prot_t            *prot = NULL;
    static int ncalls = 0;

    if (0 == ncalls++) {
	if (H5DEBUG(AC)) {
	    fprintf(H5DEBUG(AC), "H5AC: debugging cache (expensive)\n");
	}
    }
#endif

    FUNC_ENTER_NOAPI(H5AC_protect, NULL)

    /* check args */
    assert(f);
    assert(f->shared->cache);
    assert(type);
    assert(type->load);
    assert(type->flush);
    assert(H5F_addr_defined(addr));

    /* Get local copy of this information */
    idx = H5AC_HASH(f, addr);
    cache = f->shared->cache;
    info = cache->slot + idx;
#ifdef H5AC_DEBUG
    prot = cache->prot + idx;
#endif /* H5AC_DEBUG */

#ifdef H5_HAVE_PARALLEL
    /* If MPI based VFD is used, do special parallel I/O actions */
    if(IS_H5FD_MPI(f)) {
        H5AC_info_t       **dinfo;

        /* Get pointer to new 'held' information */
        dinfo = cache->dslot + idx;

        /* Check for 'held' metadata in location & handle it */
        if(*dinfo) {
            /* Sanity checks */
            assert((*dinfo)->dirty);
            assert((*info));
            assert((*info)->dirty==0);
            assert((*dinfo)->addr!=(*info)->addr);

            /* Is 'held' metadata the metadata we are looking for? */
            if (H5F_addr_eq((*dinfo)->addr, addr) 
#ifdef H5AC_DEBUG
                    && (*dinfo)->type==type
#endif /* H5AC_DEBUG */
                    ) {
#ifndef H5AC_DEBUG
                /* Sanity check that the object in the cache is the correct type */
                assert((*dinfo)->type==type);
#endif /* H5AC_DEBUG */
                /*
                 * The object is already cached; simply remove it from the cache.
                 */
                thing = (*dinfo);
                (*dinfo)->type = NULL;
                (*dinfo)->addr = HADDR_UNDEF;
                (*dinfo)= NULL;
#ifdef H5AC_DEBUG
                cache->diagnostics[(*dinfo)->type->id].nhits++;
#endif /* H5AC_DEBUG */
            } /* end if */
            /* 'held' metadata isn't what we are looking for, but check for 'current' metadata */
            else {
                if(H5F_addr_eq((*info)->addr, addr)
#ifdef H5AC_DEBUG
                        && (*info)->type==type
#endif /* H5AC_DEBUG */
                        ) {
#ifndef H5AC_DEBUG
                    /* Sanity check that the object in the cache is the correct type */
                    assert((*info)->type==type);
#endif /* H5AC_DEBUG */
                    /*
                     * The object is already cached; remove it from the cache.
                     * and bring the 'held' object into the 'regular' information
                     */
                    thing = (*info);
                    (*info)->type = NULL;
                    (*info)->addr = HADDR_UNDEF;
                    (*info)= (*dinfo);
                    (*dinfo)= NULL;
#ifdef H5AC_DEBUG
                    cache->diagnostics[(*info)->type->id].nhits++;
#endif /* H5AC_DEBUG */
                } /* end if */
            } /* end else */
        } /* end if */
    } /* end if */

    /* Check if we've already found the object to protect */
    if(thing==NULL) {
#endif /* H5_HAVE_PARALLEL */
        if ((*info) && H5F_addr_eq(addr,(*info)->addr)
#ifdef H5AC_DEBUG
                && (*info)->type==type
#endif /* H5AC_DEBUG */
                ) {
#ifndef H5AC_DEBUG
            /* Sanity check that the object in the cache is the correct type */
            assert((*info)->type==type);
#endif /* H5AC_DEBUG */

            /*
             * The object is already cached; simply remove it from the cache.
             */
            thing = (*info);
#ifdef H5AC_DEBUG
            cache->diagnostics[(*info)->type->id].nhits++;
#endif /* H5AC_DEBUG */
            (*info)->type = NULL;
            (*info)->addr = HADDR_UNDEF;
            (*info)= NULL;
        } else {
#ifdef H5AC_DEBUG
            /*
             * Check that the requested thing isn't protected, for protected things
             * can only be modified through the pointer already handed out by the
             * H5AC_protect() function.
             */
            int                    i;

            for (i = 0; i < prot->nprots; i++)
                assert(H5F_addr_ne(addr, prot->slot[i]->addr));
#endif /* H5AC_DEBUG */

            /*
             * Load a new thing.  If it can't be loaded, then return an error
             * without preempting anything.
             */
            if (NULL == (thing = (type->load)(f, dxpl_id, addr, udata1, udata2)))
                HGOTO_ERROR(H5E_CACHE, H5E_CANTLOAD, NULL, "unable to load object")
#ifdef H5AC_DEBUG
            cache->diagnostics[type->id].nmisses++;
#endif /* H5AC_DEBUG */
        }
#ifdef H5_HAVE_PARALLEL
    } /* end if */
#endif /* H5_HAVE_PARALLEL */

#ifdef H5AC_DEBUG
    /*
     * Add the protected object to the protect debugging fields of the
     * cache.
     */
    if (prot->nprots >= prot->aprots) {
        size_t na = prot->aprots + 10;
        H5AC_info_t **x = H5MM_realloc(prot->slot, na * sizeof(H5AC_info_t *));

        if (NULL==x)
            HGOTO_ERROR (H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
        prot->aprots = (int)na;
        prot->slot = x;
    }
    prot->slot[prot->nprots]= thing;
    prot->slot[prot->nprots]->type = type;
    prot->slot[prot->nprots]->addr = addr;
    prot->nprots += 1;
#endif /* H5AC_DEBUG */

    cache->nprots += 1;

    /* Set return value */
    ret_value=thing;

done:
    FUNC_LEAVE_NOAPI(ret_value)
}


/*-------------------------------------------------------------------------
 * Function:    H5AC_unprotect
 *
 * Purpose:     This function should be called to undo the effect of
 *              H5AC_protect().  The TYPE and ADDR arguments should be the
 *              same as the corresponding call to H5AC_protect() and the
 *              THING argument should be the value returned by H5AC_protect().
 *              If the DELETED flag is set, then this object has been deleted
 *              from the file and should not be returned to the cache.
 *
 *              If H5AC_DEBUG is defined then this function fails
 *              if the TYPE and ADDR arguments are not what was used when the
 *              object was protected or if the object was never protected.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Robb Matzke
 *              matzke@llnl.gov
 *              Sep  2 1997
 *
 * Modifications:
 * 		Robb Matzke, 1999-07-27
 *		The ADDR argument is passed by value.
 *
 * 		Quincey Koziol, 2003-03-19
 *		Added "deleted" argument
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_unprotect(H5F_t *f, hid_t dxpl_id, const H5AC_class_t *type, haddr_t addr, void *thing, hbool_t deleted)
{
    unsigned                idx;
    H5AC_flush_func_t       flush;
    H5AC_t                 *cache;
    H5AC_info_t           **info;
    herr_t                  ret_value=SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(H5AC_unprotect, FAIL)

    /* check args */
    assert(f);
    assert(f->shared->cache);
    assert(type);
    assert(type->flush);
    assert(H5F_addr_defined(addr));
    assert(thing);

    /* Get local copy of this information */
    idx = H5AC_HASH(f, addr);
    cache = f->shared->cache;
    info = cache->slot + idx;

#ifdef H5AC_DEBUG
    /*
     * Remove the object's protect data to indicate that it is no longer
     * protected.
     */
    {
        H5AC_prot_t            *prot = NULL;
        int                     found, i;

        prot = cache->prot + idx;
        for (i = 0, found = FALSE; i < prot->nprots && !found; i++) {
            if (H5F_addr_eq(addr, prot->slot[i]->addr)) {
                assert(prot->slot[i]->type == type);
                HDmemmove(prot->slot + i, prot->slot + i + 1,
                          ((prot->nprots - i) - 1) * sizeof(H5AC_info_t *));
                prot->nprots -= 1;
                found = TRUE;
            }
        }
        assert(found);
    }
#endif /* H5AC_DEBUG */

    /* Don't restore deleted objects to the cache */
    if(!deleted) {
#ifdef H5_HAVE_PARALLEL
        /* If MPI based VFD is used, do special parallel I/O actions */
        if(IS_H5FD_MPI(f)) {
            H5AC_info_t       **dinfo;
#ifdef H5AC_DEBUG
            H5AC_subid_t        type_id;
#endif /* H5AC_DEBUG */
            H5P_genplist_t *dxpl;           /* Dataset transfer property list */
            H5FD_mpio_xfer_t xfer_mode;     /* I/O transfer mode property value */

            /* Get the dataset transfer property list */
            if (NULL == (dxpl = H5I_object(dxpl_id)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset creation property list")

            /* Get the transfer mode property */
            if(H5P_get(dxpl, H5D_XFER_IO_XFER_MODE_NAME, &xfer_mode) < 0)
                HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't retrieve xfer mode")

            /* Get pointer to 'held' information */
            dinfo = cache->dslot + idx;

            /* Sanity check transfer mode */
            if(xfer_mode==H5FD_MPIO_COLLECTIVE) {
                /* Check for dirty metadata */
                if(*dinfo) {
                    H5AC_dest_func_t        dest;

                    /* Various sanity checks */
                    assert((*dinfo)->dirty);
                    assert((*info)!=NULL);
                    assert((*info)->dirty==0);

#ifdef H5AC_DEBUG
                    type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

                    /* Destroy 'current' information */
                    dest = (*info)->type->dest;
                    if ((dest)(f, (*info))<0)
                        HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free cached object")

                    /* Restore 'held' information back to 'current' information */
                    (*info)=(*dinfo);

                    /* Clear 'held' information */
                    (*dinfo)=NULL;

#ifdef H5AC_DEBUG
                    cache->diagnostics[type_id].nrestores++;
#endif /* H5AC_DEBUG */
                } /* end if */
            } /* end if */
            else {
                /* Sanity check */
                assert((*dinfo)==NULL);
                assert(xfer_mode==H5FD_MPIO_INDEPENDENT);

                /* Make certain there will be no write of dirty metadata */
                if((*info) && (*info)->dirty) {
                    /* Sanity check new item */
                    assert(((H5AC_info_t*)thing)->dirty==0);

                    /* 'Hold' the current metadata for later */
                    (*dinfo)=(*info);

                    /* Reset the 'current' metadata, so it doesn't get flushed */
                    (*info)=NULL;

#ifdef H5AC_DEBUG
                    cache->diagnostics[(*dinfo)->type->id].nholds++;
#endif /* H5AC_DEBUG */
                } /* end if */
            } /* end else */
        } /* end if */
#endif /* H5_HAVE_PARALLEL */

        /*
         * Flush any object already in the cache at that location.  It had
         * better not be another copy of the protected object.
         */
        if (*info) {
#ifdef H5AC_DEBUG
            H5AC_subid_t type_id=(*info)->type->id;  /* Remember this for later */
#endif /* H5AC_DEBUG */

            assert(H5F_addr_ne((*info)->addr, addr));
            flush = (*info)->type->flush;
            if ((flush)(f, dxpl_id, TRUE, (*info)->addr, (*info)) < 0)
                HGOTO_ERROR(H5E_CACHE, H5E_CANTFLUSH, FAIL, "unable to flush object")
#ifdef H5AC_DEBUG
            cache->diagnostics[type_id].nflushes++;
#endif /* H5AC_DEBUG */
        }

        /*
         * Insert the object back into the cache; it is no longer protected.
         */
        (*info)=thing;
        (*info)->type = type;
        (*info)->addr = addr;
    } /* end if */
    else {
        /* Destroy previously cached thing */
        if ((type->clear)(f, thing, TRUE)<0)
            HGOTO_ERROR(H5E_CACHE, H5E_CANTFREE, FAIL, "unable to free object")
    } /* end else */

    /* Decrement the number of protected items outstanding */
    cache->nprots -= 1;

done:
    FUNC_LEAVE_NOAPI(ret_value)
}

#ifdef H5AC_DEBUG

/*-------------------------------------------------------------------------
 * Function:    H5AC_stats
 *
 * Purpose:     Prints statistics about the cache.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Robb Matzke
 *              Thursday, October 30, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5AC_stats(H5F_t UNUSED *f)
{
    H5AC_subid_t            i;
    char                    s[32], ascii[32];
    H5AC_t                 *cache = f->shared->cache;
    double                  miss_rate;
    herr_t ret_value=SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI(H5AC_stats, FAIL)

    if (H5DEBUG(AC)) {
	fprintf(H5DEBUG(AC), "H5AC: meta data cache statistics for file %s\n",
		f->name);
	fprintf(H5DEBUG(AC), "   %-18s %8s %8s %8s %8s+%-8s\n",
		"Layer", "Hits", "Misses", "MissRate", "Inits", "Flushes");
	fprintf(H5DEBUG(AC), "   %-18s %8s %8s %8s %8s-%-8s\n",
		"-----", "----", "------", "--------", "-----", "-------");

	for (i = H5AC_BT_ID; i < H5AC_NTYPES; i++) {

	    switch (i) {
	    case H5AC_BT_ID:
		HDstrcpy(s, "B-tree nodes");
		break;
	    case H5AC_SNODE_ID:
		HDstrcpy(s, "symbol table nodes");
		break;
	    case H5AC_LHEAP_ID:
		HDstrcpy (s, "local heaps");
		break;
	    case H5AC_GHEAP_ID:
		HDstrcpy (s, "global heaps");
		break;
	    case H5AC_OHDR_ID:
		HDstrcpy(s, "object headers");
		break;
	    default:
		sprintf(s, "unknown id %d", i);
	    }

	    if (cache->diagnostics[i].nhits>0 ||
		cache->diagnostics[i].nmisses>0) {
		miss_rate = 100.0 * cache->diagnostics[i].nmisses /
			    (cache->diagnostics[i].nhits+
			     cache->diagnostics[i].nmisses);
	    } else {
		miss_rate = 0.0;
	    }

	    if (miss_rate > 100) {
		sprintf(ascii, "%7d%%", (int) (miss_rate + 0.5));
	    } else {
		sprintf(ascii, "%7.2f%%", miss_rate);
	    }
	    fprintf(H5DEBUG(AC), "   %-18s %8u %8u %7s %8u%+-9ld\n", s,
		    cache->diagnostics[i].nhits,
		    cache->diagnostics[i].nmisses,
		    ascii,
		    cache->diagnostics[i].ninits,
		    ((long)(cache->diagnostics[i].nflushes) -
		     (long)(cache->diagnostics[i].ninits)));
	}
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
}
#endif /* H5AC_DEBUG */