summaryrefslogtreecommitdiffstats
path: root/src/H5FD.c
blob: c025090ad842c8f04e4d798add62167051b5c51a (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
/*
 * Copyright © 1999 NCSA
 *                  All rights reserved.
 *
 * Programmer:  Robb Matzke <matzke@llnl.gov>
 *              Monday, July 26, 1999
 *
 * Purpose:	The Virtual File Layer as described in documentation. This is
 *		the greatest common denominator for all types of storage
 *		access whether a file, memory, network, etc. This layer
 *		usually just dispatches the request to an actual file driver
 *		layer.
 */

/* Packages needed by this file */
#include <H5private.h>		/*library functions			*/
#include <H5Eprivate.h>		/*error handling			*/
#include <H5Fprivate.h>		/*files					*/
#include <H5FDprivate.h>	/*virtual file driver			*/
#include <H5Iprivate.h>		/*interface abstraction layer		*/
#include <H5MMprivate.h>	/*memory management			*/
#include <H5Pprivate.h>		/*property lists			*/

/* Interface initialization */
#define PABLO_MASK	H5FD_mask
#define INTERFACE_INIT	H5FD_init_interface
static intn interface_initialize_g = 0;

/* static prototypes */
static herr_t H5FD_init_interface(void);
static herr_t H5FD_free_cls(H5FD_class_t *cls);


/*-------------------------------------------------------------------------
 * Function:	H5FD_init_interface
 *
 * Purpose:	Initialize the virtual file layer.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, July 26, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FD_init_interface(void)
{
    FUNC_ENTER(H5FD_init_interface, FAIL);

    if (H5I_init_group(H5I_VFL, H5I_VFL_HASHSIZE, 0,
		       (H5I_free_t)H5FD_free_cls)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "unable to initialize interface");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_term_interface
 *
 * Purpose:	Terminate this interface: free all memory and reset global
 *		variables to their initial values.  Release all ID groups
 *		associated with this interface.
 *
 * Return:	Success:	Positive if anything was done that might
 *				have affected other interfaces; zero
 *				otherwise.
 *
 *		Failure:        Never fails.
 *
 * Programmer:	Robb Matzke
 *              Friday, February 19, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
intn
H5FD_term_interface(void)
{
    intn	n = 0;

    if (interface_initialize_g) {
	if ((n=H5I_nmembers(H5I_VFL))) {
	    H5I_clear_group(H5I_VFL, FALSE);
	} else {
	    H5I_destroy_group(H5I_VFL);
	    interface_initialize_g = 0;
	    n = 1; /*H5I*/
	}
    }
    return n;
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_free_cls
 *
 * Purpose:	Frees a file driver class struct and returns an indication of
 *		success. This function is used as the free callback for the
 *		virtual file layer object identifiers (cf H5FD_init_interface).
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, July 26, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5FD_free_cls(H5FD_class_t *cls)
{
    FUNC_ENTER(H5FD_free_cls, FAIL);
    H5MM_xfree(cls);
    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDregister
 *
 * Purpose:	Registers a new file driver as a member of the virtual file
 *		driver class.  Certain fields of the class struct are
 *		required and that is checked here so it doesn't have to be
 *		checked every time the field is accessed.
 *
 * Return:	Success:	A file driver ID which is good until the
 *				library is closed or the driver is
 *				unregistered.
 *
 *		Failure:	A negative value.
 *
 * Programmer:	Robb Matzke
 *              Monday, July 26, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5FDregister(const H5FD_class_t *cls)
{
    hid_t		retval;
    H5FD_class_t	*saved;
    H5FD_mem_t		type;

    FUNC_ENTER(H5FDregister, FAIL);
    H5TRACE1("i","x",cls);

    /* Check arguments */
    if (!cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL,
		      "null class pointer is disallowed");
    }
    if (!cls->open || !cls->close) {
	HRETURN_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL,
		      "`open' and/or `close' methods are not defined");
    }
    if (!cls->get_eoa || !cls->set_eoa) {
	HRETURN_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL,
		      "`get_eoa' and/or `set_eoa' methods are not defined");
    }
    if (!cls->get_eof) {
	HRETURN_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL,
		      "`get_eof' method is not defined");
    }
    if (!cls->read || !cls->write) {
	HRETURN_ERROR(H5E_ARGS, H5E_UNINITIALIZED, FAIL,
		      "`read' and/or `write' method is not defined");
    }
    for (type=0; type<H5FD_MEM_NTYPES; type++) {
	if (cls->fl_map[type]<H5FD_MEM_NOLIST ||
	    cls->fl_map[type]>=H5FD_MEM_NTYPES) {
	    HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
			  "invalid free-list mapping");
	}
    }

    /* Copy the class structure so the caller can reuse or free it */
    if (NULL==(saved=H5MM_malloc(sizeof(H5FD_class_t)))) {
	HRETURN_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
		      "memory allocation failed for file driver class struct");
    }
    *saved = *cls;

    /* Create the new class ID */
    if ((retval=H5I_register(H5I_VFL, saved))<0) {
	HRETURN_ERROR(H5E_ATOM, H5E_CANTREGISTER, FAIL,
		      "unable to register file driver ID");
    }

    FUNC_LEAVE(retval);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDunregister
 *
 * Purpose:	Removes a driver ID from the library. This in no way affects
 *		file access property lists which have been defined to use
 *		this driver or files which are already opened under this
 *		driver.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, July 26, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDunregister(hid_t driver_id)
{
    FUNC_ENTER(H5FDunregister, FAIL);
    H5TRACE1("e","i",driver_id);

    /* Check arguments */
    if (H5I_VFL!=H5I_get_type(driver_id) ||
	NULL==H5I_object(driver_id)) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL,
		      "not a file driver");
    }

    /* The H5FD_class_t struct will be freed by this function */
    if (H5I_dec_ref(driver_id)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "unable to unregister file driver");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_get_class
 *
 * Purpose:	Optains a pointer to the driver struct containing all the
 *		callback pointers, etc. The PLIST_ID argument can be a file
 *		access property list, a data transfer property list, or a
 *		file driver identifier.
 *
 * Return:	Success:	Ptr to the driver information. The pointer is
 *				only valid as long as the driver remains
 *				registered or some file or property list
 *				exists which references the driver.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Friday, August 20, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5FD_class_t *
H5FD_get_class(hid_t id)
{
    H5FD_class_t	*ret_value=NULL;
    H5F_access_t	*fapl=NULL;
    H5F_xfer_t		*dxpl=NULL;
    
    FUNC_ENTER(H5FD_get_class, NULL);

    if (H5P_DEFAULT==id) {
	ret_value = H5FD_get_class(H5F_access_dflt.driver_id);
    } else if (H5I_VFL==H5I_get_type(id)) {
	ret_value = H5I_object(id);
    } else {
	switch (H5P_get_class(id)) {
	case H5P_FILE_ACCESS:
	    if (NULL==(fapl=H5I_object(id))) {
		HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL,
			      "not a file access property list");
	    }
	    ret_value = H5FD_get_class(fapl->driver_id);
	    break;

	case H5P_DATA_XFER:
	    if (NULL==(dxpl=H5I_object(id))) {
		HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL,
			      "not a data transfer property list");
	    }
	    ret_value = H5FD_get_class(dxpl->driver_id);
	    break;

	default:
	    HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL,
			  "not a driver id, file access property list or "
			  "data transfer property list");
	}
    }
    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_sb_size
 *
 * Purpose:	Obtains the number of bytes required to store the driver file
 *		access data in the HDF5 superblock.
 *
 * Return:	Success:	Number of bytes required.
 *
 *		Failure:	0 if an error occurs or if the driver has no
 *				data to store in the superblock.
 *
 * Programmer:	Robb Matzke
 *              Monday, August 16, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hsize_t
H5FD_sb_size(H5FD_t *file)
{
    hsize_t	ret_value=0;
    
    FUNC_ENTER(H5FD_sb_size, 0);

    assert(file && file->cls);
    if (file->cls->sb_size) {
	ret_value = (file->cls->sb_size)(file);
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_sb_encode
 *
 * Purpose:	Encode driver-specific data into the output arguments. The
 *		NAME is a nine-byte buffer which should get an
 *		eight-character driver name and/or version followed by a null
 *		terminator. The BUF argument is a buffer to receive the
 *		encoded driver-specific data. The size of the BUF array is
 *		the size returned by the H5FD_sb_size() call.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, August 16, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_sb_encode(H5FD_t *file, char *name/*out*/, uint8_t *buf)
{
    FUNC_ENTER(H5FD_sb_encode, FAIL);

    assert(file && file->cls);
    if (file->cls->sb_encode &&
	(file->cls->sb_encode)(file, name/*out*/, buf/*out*/)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver sb_encode request failed");
    }
    
    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_sb_decode
 *
 * Purpose:	Decodes the driver information block.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Monday, August 16, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_sb_decode(H5FD_t *file, const char *name, const uint8_t *buf)
{
    FUNC_ENTER(H5FD_sb_decode, FAIL);

    assert(file && file->cls);
    if (file->cls->sb_decode &&
	(file->cls->sb_decode)(file, name, buf)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver sb_decode request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_fapl_get
 *
 * Purpose:	Gets the file access property list associated with a file.
 *		Usually the file will copy what it needs from the original
 *		file access property list when the file is created. The
 *		purpose of this function is to create a new file access
 *		property list based on the settings in the file, which may
 *		have been modified from the original file access property
 *		list.
 *
 * Return:	Success:	Pointer to a new file access property list
 *				with all members copied.  If the file is
 *				closed then this property list lives on, and
 *				vice versa.
 *
 *		Failure:	NULL, including when the file has no
 *				properties.
 *
 * Programmer:	Robb Matzke
 *              Friday, August 13, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5FD_fapl_get(H5FD_t *file)
{
    void	*ret_value=NULL;
    
    FUNC_ENTER(H5FD_fapl_get, NULL);
    assert(file);

    if (file->cls->fapl_get) {
	ret_value = (file->cls->fapl_get)(file);
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_fapl_copy
 *
 * Purpose:	Copies the driver-specific part of the file access property
 *		list.
 *
 * Return:	Success:	Pointer to new driver-specific file access
 *				properties.
 *
 *		Failure:	NULL, but also returns null with no error
 *				pushed onto the error stack if the OLD_FAPL
 *				is null.
 *
 * Programmer:	Robb Matzke
 *              Tuesday, August  3, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5FD_fapl_copy(hid_t driver_id, const void *old_fapl)
{
    void		*new_fapl = NULL;
    H5FD_class_t	*driver=NULL;
    
    FUNC_ENTER(H5FD_fapl_copy, NULL);

    /* Check args */
    if (H5I_VFL!=H5I_get_type(driver_id) ||
	NULL==(driver=H5I_object(driver_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a driver ID");
    }
    if (!old_fapl) HRETURN(NULL); /*but no error*/

    /* Allow the driver to copy or do it ourselves */
    if (driver->fapl_copy) {
	new_fapl = (driver->fapl_copy)(old_fapl);
    } else if (driver->fapl_size>0) {
	new_fapl = H5MM_malloc(driver->fapl_size);
	HDmemcpy(new_fapl, old_fapl, driver->fapl_size);
    } else {
	HRETURN_ERROR(H5E_VFL, H5E_UNSUPPORTED, NULL,
		      "no way to copy driver file access property list");
    }

    FUNC_LEAVE(new_fapl);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_fapl_free
 *
 * Purpose:	Frees the driver-specific file access property list.
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Robb Matzke
 *              Tuesday, August  3, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_fapl_free(hid_t driver_id, void *fapl)
{
    H5FD_class_t	*driver=NULL;

    FUNC_ENTER(H5FD_fapl_free, FAIL);
    H5TRACE2("e","ix",driver_id,fapl);

    /* Check args */
    if (H5I_VFL!=H5I_get_type(driver_id) ||
	NULL==(driver=H5I_object(driver_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a driver ID");
    }
    
    /* Allow driver to free or do it ourselves */
    if (fapl && driver->fapl_free) {
	if ((driver->fapl_free)(fapl)<0) {
	    HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
			  "driver fapl_free request failed");
	}
    } else {
	H5MM_xfree(fapl);
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_dxpl_copy
 *
 * Purpose:	Copies the driver-specific part of the data transfer property
 *		list.
 *
 * Return:	Success:	Pointer to new driver-specific data transfer
 *				properties.
 *
 *		Failure:	NULL, but also returns null with no error
 *				pushed onto the error stack if the OLD_DXPL
 *				is null.
 *
 * Programmer:	Robb Matzke
 *              Tuesday, August  3, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5FD_dxpl_copy(hid_t driver_id, const void *old_dxpl)
{
    void		*new_dxpl = NULL;
    H5FD_class_t	*driver=NULL;
    
    FUNC_ENTER(H5FD_dxpl_copy, NULL);

    /* Check args */
    if (H5I_VFL!=H5I_get_type(driver_id) ||
	NULL==(driver=H5I_object(driver_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL,
		      "not a driver ID");
    }
    if (!old_dxpl) HRETURN(NULL); /*but no error*/

    /* Allow the driver to copy or do it ourselves */
    if (driver->dxpl_copy) {
	new_dxpl = (driver->dxpl_copy)(old_dxpl);
    } else if (driver->dxpl_size>0) {
	new_dxpl = H5MM_malloc(driver->dxpl_size);
	HDmemcpy(new_dxpl, old_dxpl, driver->dxpl_size);
    } else {
	HRETURN_ERROR(H5E_VFL, H5E_UNSUPPORTED, NULL,
		      "no way to copy driver file access property list");
    }

    FUNC_LEAVE(new_dxpl);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_dxpl_free
 *
 * Purpose:	Frees the driver-specific data transfer property list.
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Robb Matzke
 *              Tuesday, August  3, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_dxpl_free(hid_t driver_id, void *dxpl)
{
    H5FD_class_t	*driver=NULL;

    FUNC_ENTER(H5FD_dxpl_free, FAIL);
    H5TRACE2("e","ix",driver_id,dxpl);

    /* Check args */
    if (H5I_VFL!=H5I_get_type(driver_id) ||
	NULL==(driver=H5I_object(driver_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a driver ID");
    }
    
    /* Allow driver to free or do it ourselves */
    if (dxpl && driver->dxpl_free) {
	if ((driver->dxpl_free)(dxpl)<0) {
	    HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
			  "driver dxpl_free request failed");
	}
    } else {
	H5MM_xfree(dxpl);
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDopen
 *
 * Purpose:	Opens a file named NAME for the type(s) of access described
 *		by the bit vector FLAGS according to a file access property
 *		list FAPL_ID (which may be the constant H5P_DEFAULT). The
 *		file should expect to handle format addresses in the range [0,
 *		MAXADDR] (if MAXADDR is the undefined address then the caller
 *		doesn't care about the address range).
 *
 * 		Possible values for the FLAGS bits are:
 *
 *		H5F_ACC_RDWR:	Open the file for read and write access. If
 *				this bit is not set then open the file for
 *				read only access. It is permissible to open a
 *				file for read and write access when only read
 *				access is requested by the library (the
 *				library will never attempt to write to a file
 *				which it opened with only read access).
 *
 *		H5F_ACC_CREATE:	Create the file if it doesn't already exist.
 *				However, see H5F_ACC_EXCL below.
 *
 *		H5F_ACC_TRUNC:	Truncate the file if it already exists. This
 *				is equivalent to deleting the file and then
 *				creating a new empty file.
 *
 *		H5F_ACC_EXCL:	When used with H5F_ACC_CREATE, if the file
 *				already exists then the open should fail.
 *				Note that this is unsupported/broken with
 *				some file drivers (e.g., sec2 across nfs) and
 *				will contain a race condition when used to
 *				perform file locking.
 *
 *		The MAXADDR is the maximum address which will be requested by
 *		the library during an allocation operation. Usually this is
 *		the same value as the MAXADDR field of the class structure,
 *		but it can be smaller if the driver is being used under some
 *		other driver.
 *
 *		Note that when the driver `open' callback gets control that
 *		the public part of the file struct (the H5FD_t part) will be
 *		incomplete and will be filled in after that callback returns.
 *
 * Return:	Success:	Pointer to a new file driver struct.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Tuesday, July 27, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5FD_t *
H5FDopen(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
{
    H5FD_t	*ret_value=NULL;

    FUNC_ENTER(H5FDopen, NULL);

    if (NULL==(ret_value=H5FD_open(name, flags, fapl_id, maxaddr))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, NULL,
		      "unable to open file");
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_open
 *
 * Purpose:	Private version of H5FDopen()
 *
 * Return:	Success:	Pointer to a new file driver struct
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5FD_t *
H5FD_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr)
{
    const H5F_access_t	*fapl=NULL;
    H5FD_class_t	*driver;
    H5FD_t		*file=NULL;
    
    FUNC_ENTER(H5FD_open, NULL);

    /* Check arguments */
    if (H5P_DEFAULT==fapl_id) {
	fapl = &H5F_access_dflt;
    } else if (H5P_FILE_ACCESS != H5P_get_class(fapl_id) ||
	       NULL == (fapl = H5I_object(fapl_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, NULL,
		      "not a file access property list");
    }
    if (0==maxaddr) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, NULL,
		      "zero format address range");
    }

    /* Get driver info */
    if (H5I_VFL!=H5I_get_type(fapl->driver_id) ||
	NULL==(driver=H5I_object(fapl->driver_id))) {
	HRETURN_ERROR(H5E_VFL, H5E_BADVALUE, NULL,
		      "invalid driver ID in file access property list");
    }
    if (NULL==driver->open) {
	HRETURN_ERROR(H5E_VFL, H5E_UNSUPPORTED, NULL,
		      "file driver has no `open' method");
    }
    
    /* Dispatch to file driver */
    if (HADDR_UNDEF==maxaddr) maxaddr = driver->maxaddr;
    if (NULL==(file=(driver->open)(name, flags, fapl_id, maxaddr))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, NULL, "open failed");
    }

    /*
     * Fill in public fields. We must increment the reference count on the
     * driver ID to prevent it from being freed while this file is open.
     */
    file->driver_id = fapl->driver_id;
    H5I_inc_ref(file->driver_id);
    file->cls = driver;
    file->maxaddr = maxaddr;
    HDmemset(file->fl, 0, sizeof(file->fl));
    
    FUNC_LEAVE(file);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDclose
 *
 * Purpose:     Closes the file by calling the driver `close' callback, which
 *		should free all driver-private data and free the file struct.
 *		Note that the public part of the file struct (the H5FD_t part)
 *		will be all zero during the driver close callback like during
 *		the `open' callback.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Tuesday, July 27, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDclose(H5FD_t *file)
{
    FUNC_ENTER(H5FDclose, FAIL);
    H5TRACE1("e","x",file);

    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }

    if (H5FD_close(file)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "unable to close file");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_close
 *
 * Purpose:	Private version of H5FDclose()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_close(H5FD_t *file)
{
    const H5FD_class_t	*driver;
    H5FD_free_t		*cur, *next;
    H5FD_mem_t		i;
#ifdef H5F_DEBUG
    uintn		nblocks=0;
    hsize_t		nbytes=0;
#endif
    
    FUNC_ENTER(H5FD_close, FAIL);
    assert(file && file->cls);

    /* Free all free-lists, leaking any memory thus described */
    for (i=0; i<H5FD_MEM_NTYPES; i++) {
	for (cur=file->fl[i]; cur; cur=next) {
#ifdef H5F_DEBUG
	    nblocks++;
	    nbytes += cur->size;
#endif
	    next = cur->next;
	    H5MM_xfree(cur);
	}
	file->fl[i]=NULL;
    }
#ifdef H5F_DEBUG
    if (nblocks && H5DEBUG(F)) {
	fprintf(H5DEBUG(F),
		"H5F: leaked %lu bytes of file memory in %u blocks\n",
		(unsigned long)nbytes, nblocks);
    }
#endif

    /* Prepare to close file by clearing all public fields */
    driver = file->cls;
    H5I_dec_ref(file->driver_id);
    HDmemset(file, 0, sizeof(H5FD_t));

    /*
     * Dispatch to the driver for actual close. If the driver fails to
     * close the file then the file will be in an unusable state.
     */
    assert(driver->close);
    if ((driver->close)(file)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "close failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDcmp
 *
 * Purpose:	Compare the keys of two files using the file driver callback
 *		if the files belong to the same driver, otherwise sort the
 *		files by driver class pointer value.
 *
 * Return:	Success:	A value like strcmp()
 *
 *		Failure:	Must never fail. If both file handles are
 *				invalid then they compare equal. If one file
 *				handle is invalid then it compares less than
 *				the other.  If both files belong to the same
 *				driver and the driver doesn't provide a
 *				comparison callback then the file pointers
 *				themselves are compared.
 *
 * Programmer:	Robb Matzke
 *              Tuesday, July 27, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5FDcmp(const H5FD_t *f1, const H5FD_t *f2)
{
    intn	ret_value;
    
    FUNC_ENTER(H5FDcmp, -1); /*return value is arbitrary*/
    H5TRACE2("Is","xx",f1,f2);
    
    ret_value = H5FD_cmp(f1, f2);
    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_cmp
 *
 * Purpose:	Private version of H5FDcmp()
 *
 * Return:	Success:	A value like strcmp()
 *
 *		Failure:	Must never fail.
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
H5FD_cmp(const H5FD_t *f1, const H5FD_t *f2)
{
    intn	ret_value;

    FUNC_ENTER(H5FD_cmp, -1); /*return value is arbitrary*/

    if ((!f1 || !f1->cls) && (!f2 || !f2->cls)) return 0;
    if (!f1 || !f1->cls) return -1;
    if (!f2 || !f2->cls) return 1;
    if (f1->cls < f2->cls) return -1;
    if (f1->cls > f2->cls) return 1;

    /* Files are same driver; no cmp callback */
    if (!f1->cls->cmp) {
	if (f1<f2) return -1;
	if (f1>f2) return 1;
	return 0;
    }

    ret_value = (f1->cls->cmp)(f1, f2);

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDalloc
 *
 * Purpose:	Allocates SIZE bytes of memory from the FILE. The memory will
 *		be used according to the allocation class TYPE. First we try
 *		to satisfy the request from one of the free lists, according
 *		to the free list map provided by the driver. The free list
 *		array has one entry for each request type and the value of
 *		that array element can be one of four possibilities:
 *
 *		      It can be the constant H5FD_MEM_DEFAULT (or zero) which
 *		      indicates that the identity mapping is used. In other
 *		      words, the request type maps to its own free list.
 *
 *		      It can be the request type itself, which has the same
 *		      effect as the H5FD_MEM_DEFAULT value above.
 *
 *		      It can be the ID for another request type, which
 *		      indicates that the free list for the specified type
 *		      should be used instead.
 *
 *		      It can be the constant H5FD_MEM_NOLIST which means that
 *		      no free list should be used for this type of request.
 *
 *		If the request cannot be satisfied from a free list then
 *		either the driver's `alloc' callback is invoked (if one was
 *		supplied) or the end-of-address marker is extended. The
 *		`alloc' callback is always called with the same arguments as
 * 		the H5FDalloc().
 *
 * Return:	Success:	The format address of the new file memory.
 *
 *		Failure:	The undefined address HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Tuesday, July 27, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FDalloc(H5FD_t *file, H5FD_mem_t type, hsize_t size)
{
    haddr_t	ret_value = HADDR_UNDEF;
    
    FUNC_ENTER(H5FDalloc, HADDR_UNDEF);
    H5TRACE3("a","xMth",file,type,size);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, HADDR_UNDEF,
		      "invalid file pointer");
    }
    if (type<0 || type>=H5FD_MEM_NTYPES) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, HADDR_UNDEF,
		      "invalid request type");
    }
    if (size<=0) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, HADDR_UNDEF,
		      "zero-size request");
    }

    /* Do the real work */
    if (HADDR_UNDEF==(ret_value=H5FD_alloc(file, type, size))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
		      "unable to allocate file memory");
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_alloc
 *
 * Purpose:	Private version of H5FDalloc()
 *
 * Return:	Success:	The format address of the new file memory.
 *
 *		Failure:	The undefined address HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FD_alloc(H5FD_t *file, H5FD_mem_t type, hsize_t size)
{
    haddr_t	ret_value = HADDR_UNDEF;
    H5FD_mem_t	mapped_type;

    FUNC_ENTER(H5FD_alloc, HADDR_UNDEF);

    /* Check args */
    assert(file && file->cls);
    assert(type>=0 && type<H5FD_MEM_NTYPES);
    assert(size>0);
    
    /* Map the allocation request to a free list */
    if (H5FD_MEM_DEFAULT==file->cls->fl_map[type]) {
	mapped_type = type;
    } else {
	mapped_type = file->cls->fl_map[type];
    }

    /*
     * Try to satisfy the request from the free list. First try to find an
     * exact match, otherwise use the best match.
     */
    if (mapped_type>=0) {
	H5FD_free_t *prev=NULL, *best=NULL;
	H5FD_free_t *cur = file->fl[mapped_type];
	while (cur) {
	    if (cur->size==size) {
		ret_value = cur->addr;
		if (prev) prev->next = cur->next;
		else file->fl[mapped_type] = cur->next;
		H5MM_xfree(cur);
		HRETURN(ret_value);
	    } else if (cur->size>size &&
		       (!best || cur->size<best->size)) {
		best = cur;
	    }
	    prev = cur;
	    cur = cur->next;
	}
	if (best) {
	    ret_value = best->addr;
	    best->addr += size;
	    best->size -= size;
	    HRETURN(ret_value);
	}
    }

    /*
     * Dispatch to driver `alloc' callback or extend the end-of-address
     * marker
     */
    if (file->cls->alloc) {
	ret_value = (file->cls->alloc)(file, type, size);
	if (HADDR_UNDEF==ret_value) {
	    HRETURN_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF,
			  "driver allocation request failed");
	}
    } else {
	haddr_t eoa = (file->cls->get_eoa)(file);
	if (H5F_addr_overflow(eoa, size) || eoa+size>file->maxaddr) {
	    HRETURN_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF,
			  "file allocation request failed");
	}
	ret_value = eoa;
	eoa += size;
	if ((file->cls->set_eoa)(file, eoa)<0) {
	    HRETURN_ERROR(H5E_VFL, H5E_NOSPACE, HADDR_UNDEF,
			  "file allocation request failed");
	}
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDfree
 *
 * Purpose:	Frees format addresses starting with ADDR and continuing for
 *		SIZE bytes in the file FILE. The type of space being freed is
 *		specified by TYPE, which is mapped to a free list as
 *		described for the H5FDalloc() function above.  If the request
 *		doesn't map to a free list then either the application `free'
 *		callback is invoked (if defined) or the memory is leaked.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, July 28, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDfree(H5FD_t *file, H5FD_mem_t type, haddr_t addr, hsize_t size)
{
    FUNC_ENTER(H5FDfree, FAIL);
    H5TRACE4("e","xMtah",file,type,addr,size);
    
    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }
    if (type<0 || type>=H5FD_MEM_NTYPES) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid request type");
    }

    /* Do the real work */
    if (H5FD_free(file, type, addr, size)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "file deallocation request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_free
 *
 * Purpose:	Private version of H5FDfree()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_free(H5FD_t *file, H5FD_mem_t type, haddr_t addr, hsize_t size)
{
    H5FD_mem_t		mapped_type;
    
    FUNC_ENTER(H5FD_free, FAIL);

    /* Check args */
    assert(file && file->cls);
    assert(type>=0 && type<H5FD_MEM_NTYPES);
    if (!H5F_addr_defined(addr) || addr>file->maxaddr || 0==size ||
	H5F_addr_overflow(addr, size) || addr+size>file->maxaddr) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid region");
    }

    /* Map request type to free list */
    if (H5FD_MEM_DEFAULT==file->cls->fl_map[type]) {
	mapped_type = type;
    } else {
	mapped_type = file->cls->fl_map[type];
    }

    /*
     * If the request maps to a free list then add memory to the free list
     * without ever telling the driver that it was freed.  Otherwise let the
     * driver deallocate the memory.
     */
    if (mapped_type>=0) {
	H5FD_free_t *cur = H5MM_malloc(sizeof(H5FD_free_t));
	cur->addr = addr;
	cur->size = size;
	cur->next = file->fl[mapped_type];
	file->fl[mapped_type] = cur;
    } else if (file->cls->free) {
	if ((file->cls->free)(file, type, addr, size)<0) {
	    HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
			  "driver free request failed");
	}
    } else {
	/* leak memory */
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDrealloc
 *
 * Purpose:	Changes the size of an allocated chunk of memory, possibly
 *		also changing its location in the file.
 *
 * Return:	Success:	New address of the block of memory, not
 *				necessarily the same as the original address.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Tuesday, August  3, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FDrealloc(H5FD_t *file, H5FD_mem_t type, haddr_t old_addr, hsize_t old_size,
	    hsize_t new_size)
{
    haddr_t	ret_value=HADDR_UNDEF;

    FUNC_ENTER(H5FDrealloc, HADDR_UNDEF);
    H5TRACE5("a","xMtahh",file,type,old_addr,old_size,new_size);

    if (HADDR_UNDEF==(ret_value=H5FD_realloc(file, type, old_addr, old_size,
					     new_size))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
		      "file reallocation request failed");
    }

    FUNC_LEAVE(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_realloc
 *
 * Purpose:	Private version of H5FDrealloc()
 *
 * Return:	Success:	New address of the block of memory, not
 *				necessarily the same as the original address.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FD_realloc(H5FD_t *file, H5FD_mem_t type, haddr_t old_addr, hsize_t old_size,
	     hsize_t new_size)
{
    haddr_t	new_addr=old_addr;
    uint8_t	_buf[8192];
    uint8_t	*buf=_buf;
    
    FUNC_ENTER(H5FD_realloc, HADDR_UNDEF);

    if (new_size==old_size) {
	/*nothing to do*/
	
    } else if (0==old_size) {
	/* allocate memory */
	assert(!H5F_addr_defined(old_addr));
	if (HADDR_UNDEF==(new_addr=H5FDalloc(file, type, new_size))) {
	    HRETURN_ERROR(H5E_FILE, H5E_NOSPACE, HADDR_UNDEF,
			  "file allocation failed");
	}
	
    } else if (0==new_size) {
	/* free memory */
	assert(H5F_addr_defined(old_addr));
	H5FDfree(file, type, old_addr, old_size);
	new_addr = HADDR_UNDEF;
	
    } else if (new_size<old_size) {
	/* free the end of the block */
	H5FDfree(file, type, old_addr+old_size, old_size-new_size);
	
    } else {
	/* move memory to new location */
	if (HADDR_UNDEF==(new_addr=H5FDalloc(file, type, new_size))) {
	    HRETURN_ERROR(H5E_FILE, H5E_NOSPACE, HADDR_UNDEF,
			  "file allocation failed");
	}
	if (old_size>sizeof(_buf) && NULL==(buf=H5MM_malloc(old_size))) {
	    H5FDfree(file, type, new_addr, new_size);
	    HRETURN_ERROR(H5E_RESOURCE, H5E_NOSPACE, HADDR_UNDEF,
			  "memory allocation failed");
	}
	if (H5FDread(file, H5P_DEFAULT, old_addr, old_size, buf)<0 ||
	    H5FDwrite(file, H5P_DEFAULT, new_addr, old_size, buf)) {
	    H5FDfree(file, type, new_addr, new_size);
	    H5MM_xfree(buf);
	    HRETURN_ERROR(H5E_FILE, H5E_READERROR, HADDR_UNDEF,
			  "unable to move file block");
	}
	
	if (buf!=_buf) H5MM_xfree(buf);
	H5FDfree(file, type, old_addr, old_size);
    }

    FUNC_LEAVE(new_addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDget_eoa
 *
 * Purpose:	Returns the address of the first byte after the last
 *		allocated memory in the file.
 *
 * Return:	Success:	First byte after allocated memory.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Friday, July 30, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FDget_eoa(H5FD_t *file)
{
    haddr_t	addr;

    FUNC_ENTER(H5FDget_eoa, HADDR_UNDEF);
    H5TRACE1("a","x",file);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, HADDR_UNDEF,
		      "invalid file pointer");
    }

    /* The real work */
    if (HADDR_UNDEF==(addr=H5FD_get_eoa(file))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
		      "file get eoa request failed");
    }

    FUNC_LEAVE(addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_get_eoa
 *
 * Purpose:	Private version of H5FDget_eoa()
 *
 * Return:	Success:	First byte after allocated memory.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FD_get_eoa(H5FD_t *file)
{
    haddr_t	addr;
    
    FUNC_ENTER(H5FD_get_eoa, HADDR_UNDEF);
    assert(file && file->cls);
    
    /* Dispatch to driver */
    if (HADDR_UNDEF==(addr=(file->cls->get_eoa)(file))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
		      "driver get_eoa request failed");
    }

    FUNC_LEAVE(addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDset_eoa
 *
 * Purpose:	Set the end-of-address marker for the file. The ADDR is the
 *		address of the first byte past the last allocated byte of the
 *		file. This function is called from two places:
 *
 *		    It is called after an existing file is opened in order to
 *		    "allocate" enough space to read the superblock and then
 *		    to "allocate" the entire hdf5 file based on the contents
 *		    of the superblock.
 *
 *		    It is called during file memory allocation if the
 *		    allocation request cannot be satisfied from the free list
 *		    and the driver didn't supply an allocation callback.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative, no side effect
 *
 * Programmer:	Robb Matzke
 *              Friday, July 30, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDset_eoa(H5FD_t *file, haddr_t addr)
{
    FUNC_ENTER(H5FDset_eoa, FAIL);
    H5TRACE2("e","xa",file,addr);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }
    if (!H5F_addr_defined(addr) || addr>file->maxaddr) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
		      "invalid end-of-address value");
    }

    /* The real work */
    if (H5FD_set_eoa(file, addr)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "file set eoa request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_set_eoa
 *
 * Purpose:	Private version of H5FDset_eoa()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative, no side effect
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_set_eoa(H5FD_t *file, haddr_t addr)
{
    FUNC_ENTER(H5FD_set_eoa, FAIL);
    assert(file && file->cls);
    assert(H5F_addr_defined(addr) && addr<=file->maxaddr);
    
    /* Dispatch to driver */
    if ((file->cls->set_eoa)(file, addr)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver set_eoa request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDget_eof
 *
 * Purpose:	Returns the end-of-file address, which is the greater of the
 *		end-of-format address and the actual EOF marker. This
 *		function is called after an existing file is opened in order
 *		for the library to learn the true size of the underlying file
 *		and to determine whether the hdf5 data has been truncated.
 *
 *		It is also used when a file is first opened to learn whether
 *		the file is empty or not.
 *
 * 		It is permissible for the driver to return the maximum address
 *		for the file size if the file is not empty.
 *
 * Return:	Success:	The EOF address.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Thursday, July 29, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FDget_eof(H5FD_t *file)
{
    haddr_t	addr;
    
    FUNC_ENTER(H5FDget_eof, HADDR_UNDEF);
    H5TRACE1("a","x",file);

    /* Check arguments */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, HADDR_UNDEF,
		      "invalid file pointer");
    }

    /* The real work */
    if (HADDR_UNDEF==(addr=H5FD_get_eof(file))) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
		      "file get eof request failed");
    }

    FUNC_LEAVE(addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_get_eof
 *
 * Purpose:	Private version of H5FDget_eof()
 *
 * Return:	Success:	The EOF address.
 *
 *		Failure:	HADDR_UNDEF
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5FD_get_eof(H5FD_t *file)
{
    haddr_t	addr=HADDR_UNDEF;

    FUNC_ENTER(H5FD_get_eof, HADDR_UNDEF);
    assert(file && file->cls);
    
    /* Dispatch to driver */
    if (file->cls->get_eof) {
	if (HADDR_UNDEF==(addr=(file->cls->get_eof)(file))) {
	    HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, HADDR_UNDEF,
			  "driver get_eof request failed");
	}
    } else {
	addr = file->maxaddr;
    }

    FUNC_LEAVE(addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDread
 *
 * Purpose:	Reads SIZE bytes from FILE beginning at address ADDR
 *		according to the data transfer property list DXPL_ID (which may
 *		be the constant H5P_DEFAULT). The result is written into the
 *		buffer BUF.
 *
 * Return:	Success:	Non-negative. The read result is written into
 *				the BUF buffer which should be allocated by
 *				the caller.
 *
 *		Failure:	Negative. The contents of BUF is undefined.
 *
 * Programmer:	Robb Matzke
 *              Thursday, July 29, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDread(H5FD_t *file, hid_t dxpl_id, haddr_t addr, hsize_t size,
	 void *buf/*out*/)
{
    FUNC_ENTER(H5FDread, FAIL);
    H5TRACE5("e","xiahx",file,dxpl_id,addr,size,buf);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }
    if (H5P_DEFAULT!=dxpl_id &&
	(H5P_DATA_XFER!=H5P_get_class(dxpl_id) ||
	 NULL==H5I_object(dxpl_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL,
		      "not a data transfer property list");
    }
    if (!buf) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "null result buffer");
    }

    /* Do the real work */
    if (H5FD_read(file, dxpl_id, addr, size, buf)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL, "file read request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_read
 *
 * Purpose:	Private version of H5FDread()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_read(H5FD_t *file, hid_t dxpl_id, haddr_t addr, hsize_t size,
	  void *buf/*out*/)
{
    FUNC_ENTER(H5FD_read, FAIL);
    assert(file && file->cls);
    assert(H5P_DEFAULT==dxpl_id ||
	   (H5P_DATA_XFER==H5P_get_class(dxpl_id) || H5I_object(dxpl_id)));
    assert(buf);

    /* The no-op case */
    if (0==size) HRETURN(SUCCEED);

    /* Dispatch to driver */
    if ((file->cls->read)(file, dxpl_id, addr, size, buf)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver read request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDwrite
 *
 * Purpose:	Writes SIZE bytes to FILE beginning at address ADDR according
 *		to the data transfer property list DXPL_ID (which may be the
 *		constant H5P_DEFAULT). The bytes to be written come from the
 *		buffer BUF.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Thursday, July 29, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDwrite(H5FD_t *file, hid_t dxpl_id, haddr_t addr, hsize_t size,
	  const void *buf)
{
    FUNC_ENTER(H5FDwrite, FAIL);
    H5TRACE5("e","xiahx",file,dxpl_id,addr,size,buf);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }
    if (H5P_DEFAULT!=dxpl_id &&
	(H5P_DATA_XFER!=H5P_get_class(dxpl_id) ||
	 NULL==H5I_object(dxpl_id))) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL,
		      "not a data transfer property list");
    }
    if (!buf) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "null buffer");
    }

    /* The real work */
    if (H5FD_write(file, dxpl_id, addr, size, buf)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "file write request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_write
 *
 * Purpose:	Private version of H5FDwrite()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_write(H5FD_t *file, hid_t dxpl_id, haddr_t addr, hsize_t size,
	   const void *buf)
{
    FUNC_ENTER(H5FD_write, FAIL);
    assert(file && file->cls);
    assert(H5P_DEFAULT==dxpl_id ||
	   (H5P_DATA_XFER==H5P_get_class(dxpl_id) && H5I_object(dxpl_id)));
    assert(buf);
    
    /* The no-op case */
    if (0==size) HRETURN(SUCCEED);

    /* Dispatch to driver */
    if ((file->cls->write)(file, dxpl_id, addr, size, buf)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver write request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FDflush
 *
 * Purpose:	Notify driver to flush all cached data.  If the driver has no
 *		flush method then nothing happens.
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Thursday, July 29, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FDflush(H5FD_t *file)
{
    FUNC_ENTER(H5FDflush, FAIL);
    H5TRACE1("e","x",file);

    /* Check args */
    if (!file || !file->cls) {
	HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid file pointer");
    }

    /* Do the real work */
    if (H5FD_flush(file)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "file flush request failed");
    }

    FUNC_LEAVE(SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5FD_flush
 *
 * Purpose:	Private version of H5FDflush()
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Wednesday, August  4, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5FD_flush(H5FD_t *file)
{
    FUNC_ENTER(H5FD_flush, FAIL);
    assert(file && file->cls);

    if (file->cls->flush &&
	(file->cls->flush)(file)<0) {
	HRETURN_ERROR(H5E_VFL, H5E_CANTINIT, FAIL,
		      "driver flush request failed");
    }

    FUNC_LEAVE(SUCCEED);
}