summaryrefslogtreecommitdiffstats
path: root/src/H5Oalloc.c
blob: 4eadc315e05fc1c136aee47eec4446903b7309d8 (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
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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 COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5Oalloc.c
 *
 * Purpose:		Object header allocation routines.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5Omodule.h" /* This source code file is part of the H5O module */

/***********/
/* Headers */
/***********/
#include "H5private.h"   /* Generic Functions			*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5FLprivate.h" /* Free lists                           */
#include "H5MFprivate.h" /* File memory management		*/
#include "H5MMprivate.h" /* Memory management			*/
#include "H5Opkg.h"      /* Object headers			*/

/****************/
/* Local Macros */
/****************/

/******************/
/* Local Typedefs */
/******************/

/********************/
/* Package Typedefs */
/********************/

/********************/
/* Local Prototypes */
/********************/

static herr_t H5O__add_gap(H5F_t *f, H5O_t *oh, unsigned chunkno, bool *chk_dirtied, size_t idx,
                           uint8_t *new_gap_loc, size_t new_gap_size);
static herr_t H5O__eliminate_gap(H5O_t *oh, bool *chk_dirtied, H5O_mesg_t *mesg, uint8_t *new_gap_loc,
                                 size_t new_gap_size);
static herr_t H5O__alloc_null(H5F_t *f, H5O_t *oh, size_t null_idx, const H5O_msg_class_t *new_type,
                              void *new_native, size_t new_size);
static htri_t H5O__alloc_extend_chunk(H5F_t *f, H5O_t *oh, unsigned chunkno, size_t size, size_t *msg_idx);
static herr_t H5O__alloc_find_best_nonnull(const H5F_t *f, const H5O_t *oh, size_t *size,
                                           H5O_msg_alloc_info_t *found_msg);
static herr_t H5O__alloc_new_chunk(H5F_t *f, H5O_t *oh, size_t size, size_t *new_idx);
static herr_t H5O__alloc_find_best_null(const H5O_t *oh, size_t size, size_t *mesg_idx);
static htri_t H5O__move_cont(H5F_t *f, H5O_t *oh, unsigned cont_u);
static htri_t H5O__move_msgs_forward(H5F_t *f, H5O_t *oh);
static htri_t H5O__merge_null(H5F_t *f, H5O_t *oh);
static htri_t H5O__remove_empty_chunks(H5F_t *f, H5O_t *oh);
static herr_t H5O__alloc_shrink_chunk(H5F_t *f, H5O_t *oh, unsigned chunkno);

/*********************/
/* Package Variables */
/*********************/

/* Declare extern the free list for H5O_cont_t's */
H5FL_EXTERN(H5O_cont_t);

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/*-------------------------------------------------------------------------
 * Function:    H5O__add_gap
 *
 * Purpose:     Add a gap to a chunk
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__add_gap(H5F_t H5_ATTR_NDEBUG_UNUSED *f, H5O_t *oh, unsigned chunkno, bool *chk_dirtied, size_t idx,
             uint8_t *new_gap_loc, size_t new_gap_size)
{
    bool   merged_with_null;    /* Whether the gap was merged with a null message */
    size_t u;                   /* Local index variable */
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);
    assert(oh->version > H5O_VERSION_1);
    assert(chk_dirtied);
    assert(new_gap_loc);
    assert(new_gap_size);

#ifndef NDEBUG
    if (chunkno > 0) {
        unsigned chk_proxy_status = 0; /* Object header chunk proxy entry cache status */

        /* Check the object header chunk proxy's status in the metadata cache */
        if (H5AC_get_entry_status(f, oh->chunk[chunkno].addr, &chk_proxy_status) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL,
                        "unable to check metadata cache status for object header chunk proxy");

        /* Make certain that object header is protected */
        assert(chk_proxy_status & H5AC_ES__IS_PROTECTED);
    }  /* end if */
#endif /* NDEBUG */

    /* Check for existing null message in chunk */
    merged_with_null = false;
    for (u = 0; u < oh->nmesgs && !merged_with_null; u++) {
        /* Find a null message in the chunk with the new gap */
        /* (a null message that's not the one we are eliminating) */
        if (H5O_NULL_ID == oh->mesg[u].type->id && oh->mesg[u].chunkno == chunkno && u != idx) {
            /* Sanity check - chunks with null messages shouldn't have a gap */
            assert(oh->chunk[chunkno].gap == 0);

            /* Eliminate the gap in the chunk */
            if (H5O__eliminate_gap(oh, chk_dirtied, &oh->mesg[u], new_gap_loc, new_gap_size) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't eliminate gap in chunk");

            /* Set flag to indicate that the gap was handled */
            merged_with_null = true;
        } /* end if */
    }     /* end for */

    /* If we couldn't find a null message in the chunk, move the gap to the end */
    if (!merged_with_null) {
        /* Adjust message offsets after new gap forward in chunk */
        for (u = 0; u < oh->nmesgs; u++)
            if (oh->mesg[u].chunkno == chunkno && oh->mesg[u].raw > new_gap_loc)
                oh->mesg[u].raw -= new_gap_size;

        /* Slide raw message info forward in chunk image */
        memmove(new_gap_loc, new_gap_loc + new_gap_size,
                (size_t)((oh->chunk[chunkno].image + (oh->chunk[chunkno].size - H5O_SIZEOF_CHKSUM_OH(oh))) -
                         (new_gap_loc + new_gap_size)));

        /* Add existing gap size to new gap size */
        new_gap_size += oh->chunk[chunkno].gap;

        /* Merging with existing gap will allow for a new null message */
        if (new_gap_size >= (size_t)H5O_SIZEOF_MSGHDR_OH(oh)) {
            H5O_mesg_t *null_msg; /* Pointer to new null message */

            /* Check if we need to extend message table to hold the new null message */
            if (oh->nmesgs >= oh->alloc_nmesgs)
                if (H5O__alloc_msgs(oh, (size_t)1) < 0)
                    HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't allocate more space for messages");

            /* Increment new gap size */
            oh->chunk[chunkno].gap += new_gap_size;

            /* Create new null message, with the tail of the previous null message */
            null_msg           = &(oh->mesg[oh->nmesgs++]);
            null_msg->type     = H5O_MSG_NULL;
            null_msg->native   = NULL;
            null_msg->raw_size = new_gap_size - (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
            null_msg->raw      = (oh->chunk[chunkno].image + oh->chunk[chunkno].size) -
                            (H5O_SIZEOF_CHKSUM_OH(oh) + null_msg->raw_size);
            null_msg->chunkno = chunkno;

            /* Zero out new null message's raw data */
            if (null_msg->raw_size)
                memset(null_msg->raw, 0, null_msg->raw_size);

            /* Mark message as dirty */
            null_msg->dirty = true;

            /* Reset size of gap in chunk */
            oh->chunk[chunkno].gap = 0;
        } /* end if */
        else
            oh->chunk[chunkno].gap = new_gap_size;

        /* Mark the chunk as modified */
        *chk_dirtied = true;
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__add_gap() */

/*-------------------------------------------------------------------------
 * Function:    H5O__eliminate_gap
 *
 * Purpose:     Eliminate a gap in a chunk with a null message.
 *
 * Note:        Sometimes this happens as a result of converting an existing
 *              non-null message to a null message, so we zero out the gap
 *              here, even though it might already be zero (when we're adding
 *              a gap to a chunk with an existing null message).  (Mostly,
 *              this just simplifies the code, esp. with the necessary chunk
 *              locking -QAK)
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__eliminate_gap(H5O_t *oh, bool *chk_dirtied, H5O_mesg_t *mesg, uint8_t *gap_loc, size_t gap_size)
{
    uint8_t *move_start, *move_end; /* Pointers to area of messages to move */
    bool     null_before_gap;       /* Flag whether the null message is before the gap or not */

    FUNC_ENTER_PACKAGE_NOERR

    /* check args */
    assert(oh);
    assert(oh->version > H5O_VERSION_1);
    assert(chk_dirtied);
    assert(mesg);
    assert(gap_loc);
    assert(gap_size);

    /* Check if the null message is before or after the gap produced */
    null_before_gap = (bool)(mesg->raw < gap_loc);

    /* Set up information about region of messages to move */
    if (null_before_gap) {
        move_start = mesg->raw + mesg->raw_size;
        move_end   = gap_loc;
    } /* end if */
    else {
        move_start = gap_loc + gap_size;
        move_end   = mesg->raw - H5O_SIZEOF_MSGHDR_OH(oh);
    } /* end else */

    /* Check for messages between null message and gap */
    if (move_end > move_start) {
        unsigned u; /* Local index variable */

        /* Look for messages that need to move, to adjust raw pointers in chunk */
        /* (this doesn't change the moved messages 'dirty' state) */
        for (u = 0; u < oh->nmesgs; u++) {
            uint8_t *msg_start; /* Start of encoded message in chunk */

            msg_start = oh->mesg[u].raw - H5O_SIZEOF_MSGHDR_OH(oh);
            if (oh->mesg[u].chunkno == mesg->chunkno && (msg_start >= move_start && msg_start < move_end)) {
                /* Move message's raw pointer in appropriate direction */
                if (null_before_gap)
                    oh->mesg[u].raw += gap_size;
                else
                    oh->mesg[u].raw -= gap_size;
            } /* end if */
        }     /* end for */

        /* Slide raw message info in chunk image */
        if (null_before_gap)
            /* Slide messages down */
            memmove(move_start + gap_size, move_start, (size_t)(move_end - move_start));
        else {
            /* Slide messages up */
            memmove(move_start - gap_size, move_start, (size_t)(move_end - move_start));

            /* Adjust start of null message */
            mesg->raw -= gap_size;
        } /* end else */
    }
    else if (move_end == move_start && !null_before_gap) {
        /* Slide null message up */
        memmove(move_start - gap_size, move_start, mesg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));

        /* Adjust start of null message */
        mesg->raw -= gap_size;
    } /* end if */

    /* Zero out addition to null message */
    memset(mesg->raw + mesg->raw_size, 0, gap_size);

    /* Adjust size of null message */
    mesg->raw_size += gap_size;

    /* Set the gap size to zero for the chunk */
    oh->chunk[mesg->chunkno].gap = 0;

    /* Mark null message as dirty */
    mesg->dirty  = true;
    *chk_dirtied = true;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5O__eliminate_gap() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__alloc_null
 *
 * Purpose:     Allocate room for a new message from a null message
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__alloc_null(H5F_t *f, H5O_t *oh, size_t null_idx, const H5O_msg_class_t *new_type, void *new_native,
                size_t new_size)
{
    H5O_chunk_proxy_t *chk_proxy   = NULL;  /* Chunk that message is in */
    bool               chk_dirtied = false; /* Flags for unprotecting chunk */
    H5O_mesg_t        *alloc_msg;           /* Pointer to null message to allocate out of */
    herr_t             ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);
    assert(new_type);
    assert(new_size);

    /* Point to null message to allocate out of */
    alloc_msg = &oh->mesg[null_idx];

    /* Protect chunk */
    if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, alloc_msg->chunkno)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to load object header chunk");

    /* Check if there's a need to split the null message */
    if (alloc_msg->raw_size > new_size) {
        /* Check for producing a gap in the chunk */
        if ((alloc_msg->raw_size - new_size) < (size_t)H5O_SIZEOF_MSGHDR_OH(oh)) {
            size_t gap_size = alloc_msg->raw_size - new_size; /* Size of gap produced */

            /* Adjust the size of the null message being eliminated */
            alloc_msg->raw_size = new_size;

            /* Add the gap to the chunk */
            if (H5O__add_gap(f, oh, alloc_msg->chunkno, &chk_dirtied, null_idx,
                             alloc_msg->raw + alloc_msg->raw_size, gap_size) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't insert gap in chunk");
        } /* end if */
        else {
            size_t new_mesg_size =
                new_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh); /* Total size of newly allocated message */
            H5O_mesg_t *null_msg;                            /* Pointer to new null message */

            /* Check if we need to extend message table to hold the new null message */
            if (oh->nmesgs >= oh->alloc_nmesgs) {
                if (H5O__alloc_msgs(oh, (size_t)1) < 0)
                    HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't allocate more space for messages");

                /* "Retarget" 'alloc_msg' pointer into newly re-allocated array of messages */
                alloc_msg = &oh->mesg[null_idx];
            } /* end if */

            /* Create new null message, with the tail of the previous null message */
            null_msg           = &(oh->mesg[oh->nmesgs++]);
            null_msg->type     = H5O_MSG_NULL;
            null_msg->native   = NULL;
            null_msg->raw      = alloc_msg->raw + new_mesg_size;
            null_msg->raw_size = alloc_msg->raw_size - new_mesg_size;
            null_msg->chunkno  = alloc_msg->chunkno;

            /* Mark the message as dirty */
            null_msg->dirty = true;
            chk_dirtied     = true;

            /* Check for gap in new null message's chunk */
            if (oh->chunk[null_msg->chunkno].gap > 0) {
                unsigned null_chunkno = null_msg->chunkno; /* Chunk w/gap */

                /* Eliminate the gap in the chunk */
                if (H5O__eliminate_gap(oh, &chk_dirtied, null_msg,
                                       ((oh->chunk[null_chunkno].image + oh->chunk[null_chunkno].size) -
                                        (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[null_chunkno].gap)),
                                       oh->chunk[null_chunkno].gap) < 0)
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTREMOVE, FAIL, "can't eliminate gap in chunk");
            } /* end if */

            /* Set the size of the new "real" message */
            alloc_msg->raw_size = new_size;
        } /* end else */
    }     /* end if */

    /* Initialize the new message */
    alloc_msg->type   = new_type;
    alloc_msg->native = new_native;

    /* Mark the new message as dirty */
    alloc_msg->dirty = true;
    chk_dirtied      = true;

done:
    /* Release chunk */
    if (chk_proxy && H5O__chunk_unprotect(f, chk_proxy, chk_dirtied) < 0)
        HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_null() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__alloc_msgs
 *
 * Purpose:     Allocate more messages for a header
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O__alloc_msgs(H5O_t *oh, size_t min_alloc)
{
    size_t      old_alloc;           /* Old number of messages allocated */
    size_t      na;                  /* New number of messages allocated */
    H5O_mesg_t *new_mesg;            /* Pointer to new message array */
    herr_t      ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);

    /* Initialize number of messages information */
    old_alloc = oh->alloc_nmesgs;
    na        = oh->alloc_nmesgs + MAX(oh->alloc_nmesgs, min_alloc); /* At least double */

    /* Attempt to allocate more memory */
    if (NULL == (new_mesg = H5FL_SEQ_REALLOC(H5O_mesg_t, oh->mesg, na)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");

    /* Update ohdr information */
    oh->alloc_nmesgs = na;
    oh->mesg         = new_mesg;

    /* Set new object header info to zeros */
    memset(&oh->mesg[old_alloc], 0, (oh->alloc_nmesgs - old_alloc) * sizeof(H5O_mesg_t));

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_msgs() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__alloc_extend_chunk
 *
 * Purpose:     Attempt to extend a chunk that is allocated on disk.
 *
 *              If the extension is successful, and if the last message
 *		of the chunk is the null message, then that message will
 *		be extended with the chunk.  Otherwise a new null message
 *		is created.
 *
 *              f is the file in which the chunk will be written.  It is
 *              included to ensure that there is enough space to extend
 *              this chunk.
 *
 * Return:      true:		The chunk has been extended, and *msg_idx
 *				contains the message index for null message
 *				which is large enough to hold size bytes.
 *
 *		false:		The chunk cannot be extended, and *msg_idx
 *				is undefined.
 *
 *		FAIL:		Some internal error has been detected.
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5O__alloc_extend_chunk(H5F_t *f, H5O_t *oh, unsigned chunkno, size_t size, size_t *msg_idx)
{
    H5O_chunk_proxy_t *chk_proxy   = NULL;  /* Chunk that message is in */
    bool               chk_dirtied = false; /* Flag for unprotecting chunk */
    size_t             delta;               /* Change in chunk's size */
    size_t             aligned_size = H5O_ALIGN_OH(oh, size);
    uint8_t           *old_image;                 /* Old address of chunk's image in memory */
    size_t             old_size;                  /* Old size of chunk */
    htri_t             was_extended;              /* If chunk can be extended */
    size_t             extend_msg        = 0;     /* Index of null message to extend */
    bool               extended_msg      = false; /* Whether an existing message was extended */
    uint8_t            new_size_flags    = 0;     /* New chunk #0 size flags */
    bool               adjust_size_flags = false; /* Whether to adjust the chunk #0 size flags */
    size_t             extra_prfx_size   = 0;     /* Extra bytes added to object header prefix */
    size_t             u;                         /* Local index variable */
    htri_t             ret_value = true;          /* return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(f != NULL);
    assert(oh != NULL);
    assert(chunkno < oh->nchunks);
    assert(size > 0);
    assert(msg_idx != NULL);
    assert(H5_addr_defined(oh->chunk[chunkno].addr));

    /* Test to see if the specified chunk ends with a null messages.
     * If successful, set the index of the null message in extend_msg.
     */
    for (u = 0; u < oh->nmesgs; u++) {
        /* Check for null message at end of proper chunk */
        /* (account for possible checksum at end of chunk) */
        if (oh->mesg[u].chunkno == chunkno && H5O_NULL_ID == oh->mesg[u].type->id &&
            ((oh->mesg[u].raw + oh->mesg[u].raw_size) ==
             ((oh->chunk[chunkno].image + oh->chunk[chunkno].size) -
              (oh->chunk[chunkno].gap + H5O_SIZEOF_CHKSUM_OH(oh))))) {

            extend_msg   = u;
            extended_msg = true;
            break;
        } /* end if */
    }     /* end for */

    /* If we can extend an existing null message, adjust the delta appropriately */
    if (extended_msg) {
        assert(oh->chunk[chunkno].gap == 0);
        delta = aligned_size - oh->mesg[extend_msg].raw_size;
    } /* end if */
    else
        delta = (aligned_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh)) - oh->chunk[chunkno].gap;
    delta = H5O_ALIGN_OH(oh, delta);

    /* Check for changing the chunk #0 data size enough to need adjusting the flags */
    if (oh->version > H5O_VERSION_1 && chunkno == 0) {
        uint64_t chunk0_size;                                                     /* Size of chunk 0's data */
        size_t   orig_prfx_size = (size_t)1 << (oh->flags & H5O_HDR_CHUNK0_SIZE); /* Original prefix size */

        assert(oh->chunk[0].size >= (size_t)H5O_SIZEOF_HDR(oh));
        chunk0_size = oh->chunk[0].size - (size_t)H5O_SIZEOF_HDR(oh);

        /* Check for moving to a 8-byte size encoding */
        if (orig_prfx_size < 8 && (chunk0_size + delta) > 4294967295) {
            extra_prfx_size   = 8 - orig_prfx_size;
            new_size_flags    = H5O_HDR_CHUNK0_8;
            adjust_size_flags = true;
        } /* end if */
        /* Check for moving to a 4-byte size encoding */
        else if (orig_prfx_size < 4 && (chunk0_size + delta) > 65535) {
            extra_prfx_size   = 4 - orig_prfx_size;
            new_size_flags    = H5O_HDR_CHUNK0_4;
            adjust_size_flags = true;
        } /* end if */
        /* Check for moving to a 2-byte size encoding */
        else if (orig_prfx_size < 2 && (chunk0_size + delta) > 255) {
            extra_prfx_size   = 2 - orig_prfx_size;
            new_size_flags    = H5O_HDR_CHUNK0_2;
            adjust_size_flags = true;
        } /* end if */
    }     /* end if */

    /* Protect chunk */
    if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, chunkno)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to load object header chunk");

    /* Determine whether the chunk can be extended */
    was_extended = H5MF_try_extend(f, H5FD_MEM_OHDR, oh->chunk[chunkno].addr,
                                   (hsize_t)(oh->chunk[chunkno].size), (hsize_t)(delta + extra_prfx_size));
    if (was_extended < 0) /* error */
        HGOTO_ERROR(H5E_OHDR, H5E_CANTEXTEND, FAIL, "can't tell if we can extend chunk");
    else if (was_extended == false) /* can't extend -- we are done */
        HGOTO_DONE(false);

    /* Adjust object header prefix flags */
    if (adjust_size_flags) {
        oh->flags = (uint8_t)(oh->flags & ~H5O_HDR_CHUNK0_SIZE);
        oh->flags |= new_size_flags;

        /* Mark object header as dirty in cache */
        if (H5AC_mark_entry_dirty(oh) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTMARKDIRTY, FAIL, "unable to mark object header as dirty");
    } /* end if */

    /* If we can extend an existing null message, take care of that */
    if (extended_msg) {
        /* Adjust message size of existing null message */
        oh->mesg[extend_msg].raw_size += delta;
    } /* end if */
    /* Create new null message for end of chunk */
    else {
        /* Create a new null message */
        if (oh->nmesgs >= oh->alloc_nmesgs)
            if (H5O__alloc_msgs(oh, (size_t)1) < 0)
                HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't allocate more space for messages");

        /* Set extension message */
        extend_msg = oh->nmesgs++;

        /* Initialize new null message */
        oh->mesg[extend_msg].type   = H5O_MSG_NULL;
        oh->mesg[extend_msg].native = NULL;
        oh->mesg[extend_msg].raw    = ((oh->chunk[chunkno].image + oh->chunk[chunkno].size) -
                                    (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[chunkno].gap)) +
                                   H5O_SIZEOF_MSGHDR_OH(oh);
        oh->mesg[extend_msg].raw_size = (delta + oh->chunk[chunkno].gap) - (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
        oh->mesg[extend_msg].chunkno  = chunkno;
    } /* end else */

    /* Mark the extended message as dirty */
    oh->mesg[extend_msg].dirty = true;
    chk_dirtied                = true;

    /* Allocate more memory space for chunk's image */
    old_image = oh->chunk[chunkno].image;
    old_size  = oh->chunk[chunkno].size;
    oh->chunk[chunkno].size += delta + extra_prfx_size;
    oh->chunk[chunkno].image = H5FL_BLK_REALLOC(chunk_image, old_image, oh->chunk[chunkno].size);
    if (NULL == oh->chunk[chunkno].image)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "can't reallocate extended object header chunk");
    oh->chunk[chunkno].gap = 0;

    /* Wipe new space for chunk */
    memset(oh->chunk[chunkno].image + old_size, 0, oh->chunk[chunkno].size - old_size);

    /* Move chunk 0 data up if the size flags changed */
    if (adjust_size_flags)
        memmove(oh->chunk[0].image + H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh),
                oh->chunk[0].image + H5O_SIZEOF_HDR(oh) - H5O_SIZEOF_CHKSUM_OH(oh) - extra_prfx_size,
                old_size - (size_t)H5O_SIZEOF_HDR(oh) + extra_prfx_size);

    /* Spin through existing messages, adjusting them */
    for (u = 0; u < oh->nmesgs; u++) {
        /* Adjust raw addresses for messages in this chunk to reflect new 'image' address */
        if (oh->mesg[u].chunkno == chunkno)
            oh->mesg[u].raw = oh->chunk[chunkno].image + extra_prfx_size + (oh->mesg[u].raw - old_image);

        /* Find continuation message which points to this chunk and adjust chunk's size */
        /* (Chunk 0 doesn't have a continuation message that points to it,
         * its size is directly encoded in the object header) */
        if (chunkno > 0 && (H5O_CONT_ID == oh->mesg[u].type->id) &&
            (((H5O_cont_t *)(oh->mesg[u].native))->chunkno == chunkno)) {
            H5O_chunk_proxy_t *chk_proxy2   = NULL;                /* Chunk that continuation message is in */
            bool               chk_dirtied2 = false;               /* Flag for unprotecting chunk */
            unsigned           cont_chunkno = oh->mesg[u].chunkno; /* Chunk # for continuation message */

            /* Protect chunk containing continuation message */
            if (NULL == (chk_proxy2 = H5O__chunk_protect(f, oh, cont_chunkno)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to load object header chunk");

            /* Adjust size in continuation message */
            assert(((H5O_cont_t *)(oh->mesg[u].native))->size == old_size);
            ((H5O_cont_t *)(oh->mesg[u].native))->size = oh->chunk[chunkno].size;

            /* Flag continuation message as dirty */
            oh->mesg[u].dirty = true;
            chk_dirtied2      = true;

            /* Release chunk containing continuation message */
            if (H5O__chunk_unprotect(f, chk_proxy2, chk_dirtied2) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");
        } /* end if */
    }     /* end for */

    /* Resize the chunk in the cache */
    if (H5O__chunk_resize(oh, chk_proxy) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRESIZE, FAIL, "unable to resize object header chunk");

    /* Set new message index */
    *msg_idx = extend_msg;

done:
    /* Release chunk */
    if (chk_proxy && H5O__chunk_unprotect(f, chk_proxy, chk_dirtied) < 0)
        HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_extend_chunk() */

/*-------------------------------------------------------------------------
 * Function:    H5O__alloc_find_best_nonnull
 *
 * Purpose:     Find the best fit non-null message for a given size of message
 *              to allocate.
 *
 * Note:	The algorithm for finding a message to replace with a
 *		continuation message is still fairly limited.  It's possible
 *		that two (or more) messages smaller than a continuation message
 *		might occupy a chunk and need to be moved in order to make
 *		room for the continuation message.
 *
 *		Also, we aren't checking for NULL messages in front of another
 *		message right now...
 *
 * Return:      Success:        Index number of the null message for the
 *                              new chunk.  The null message will be at
 *                              least SIZE bytes not counting the message
 *                              ID or size fields.
 *
 *              Failure:        Negative
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__alloc_find_best_nonnull(const H5F_t *f, const H5O_t *oh, size_t *size, H5O_msg_alloc_info_t *found_msg)
{
    H5O_mesg_t *curr_msg;   /* Pointer to current message to operate on */
    size_t      cont_size;  /* Continuation message size */
    size_t      multi_size; /* Size of all the messages in the last chunk */
    unsigned    u;          /* Local index variable */

    FUNC_ENTER_PACKAGE_NOERR

    /* Check args */
    assert(f);
    assert(oh);
    assert(size);
    assert(*size > 0);
    assert(found_msg);

    /*
     * Find the smallest message that could be moved to make room for the
     * continuation message.
     *
     * Don't ever move continuation message from one chunk to another.
     *
     * Avoid moving attributes when possible to preserve their
     * ordering (although ordering is *not* guaranteed!).
     *
     */
    cont_size  = H5O_ALIGN_OH(oh, (size_t)(H5F_SIZEOF_ADDR(f) + H5F_SIZEOF_SIZE(f)));
    multi_size = 0;
    for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++) {
        /* Don't consider continuation messages (for now) */
        if (H5O_CONT_ID != curr_msg->type->id) {
            unsigned msg_chunkno = curr_msg->chunkno; /* Chunk that the message is in */
            uint8_t *end_chunk_data =
                (oh->chunk[msg_chunkno].image + oh->chunk[msg_chunkno].size) -
                (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[msg_chunkno].gap); /* End of message data in chunk */
            uint8_t *end_msg    = curr_msg->raw + curr_msg->raw_size;    /* End of current message */
            size_t   gap_size   = 0; /* Size of gap after current message */
            size_t   null_size  = 0; /* Size of NULL message after current message */
            unsigned null_msgno = 0; /* Index of NULL message after current message */
            size_t   total_size;     /* Total size of available space "around" current message */

            /* Check if the message is the last one in the chunk */
            if (end_msg == end_chunk_data)
                gap_size = oh->chunk[msg_chunkno].gap;
            else {
                H5O_mesg_t *tmp_msg; /* Temp. pointer to message to operate on */
                unsigned    v;       /* Local index variable */

                /* Check for null message after this message, in same chunk */
                for (v = 0, tmp_msg = &oh->mesg[0]; v < oh->nmesgs; v++, tmp_msg++) {
                    if (tmp_msg->type->id == H5O_NULL_ID &&
                        (tmp_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh)) == end_msg) {
                        null_msgno = v;
                        null_size  = (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + tmp_msg->raw_size;
                        break;
                    } /* end if */

                    /* XXX: Should also check for NULL message in front of current message... */

                } /* end for */
            }     /* end else */

            /* Add up current message's total available space */
            total_size = curr_msg->raw_size + gap_size + null_size;

            /* Check if message is large enough to hold continuation info */
            if (total_size >= cont_size) {
                bool better = false; /* Whether the current message is better than a previous one */

                /* Check for first message that can be moved */
                if (found_msg->msgno < 0)
                    better = true;
                else {
                    /* Prioritize moving non-attributes above attributes */
                    /* (Even attributes with an otherwise better fit */
                    if (found_msg->id == H5O_ATTR_ID && curr_msg->type->id != H5O_ATTR_ID)
                        better = true;
                    /* Either two attributes, or two non-attributes */
                    else {
                        /* Take a smaller one */
                        if (total_size < found_msg->total_size)
                            better = true;
                        /* If they are the same size, choose the earliest one
                         * in the chunk array */
                        /* (Could also bias toward message earlier / later
                         *      chunk in, but shouldn't be a big deal - QAK, 2016/10/21)
                         */
                        else if (total_size == found_msg->total_size) {
                            if (msg_chunkno < found_msg->chunkno)
                                better = true;
                        } /* end else-if */
                    }     /* end else */
                }         /* end else */

                /* If we found a better message, keep its info */
                if (better) {
                    found_msg->msgno      = (int)u;
                    found_msg->id         = curr_msg->type->id;
                    found_msg->chunkno    = msg_chunkno;
                    found_msg->gap_size   = gap_size;
                    found_msg->null_size  = null_size;
                    found_msg->total_size = total_size;
                    found_msg->null_msgno = null_msgno;
                } /* end if */
            }     /* end if */
            else if (found_msg->msgno < 0 && msg_chunkno == oh->nchunks - 1)
                /* Keep track of the total size of smaller messages in the last
                 * chunk, in case we need to move more than 1 message.
                 */
                multi_size += curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
        } /* end if */
    }     /* end for */

    /*
     * If we must move some other message to make room for the null
     * message, then make sure the new chunk has enough room for that
     * other message.
     *
     * Move other messages first, and attributes only as a last resort.
     *
     * If all else fails, move every message in the last chunk.
     *
     */
    if (found_msg->msgno < 0)
        *size += multi_size;
    else
        *size += (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + oh->mesg[found_msg->msgno].raw_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5O__alloc_find_best_nonnull() */

/*-------------------------------------------------------------------------
 * Function:    H5O__alloc_chunk
 *
 * Purpose:     Allocates and initializes a new chunk for the object header,
 *		including file space.
 *
 * Return:      Success:        SUCCEED, with chunk number for the
 *                              new chunk and a pointer to the location in its
 *				image where the first message should be placed.
 *
 *              Failure:        Negative
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O__alloc_chunk(H5F_t *f, H5O_t *oh, size_t size, size_t found_null, const H5O_msg_alloc_info_t *found_msg,
                 size_t *new_idx)
{
    H5O_mesg_t        *curr_msg;            /* Pointer to current message to operate on */
    H5O_chunk_proxy_t *chk_proxy;           /* Chunk that message is in */
    size_t             cont_size;           /*continuation message size     */
    size_t             idx;                 /* Message number */
    uint8_t           *p    = NULL;         /* Pointer into new chunk image */
    H5O_cont_t        *cont = NULL;         /*native continuation message   */
    unsigned           chunkno;             /* Chunk allocated */
    haddr_t            new_chunk_addr;      /* Address of new chunk in file */
    unsigned           u;                   /* Local index variable */
    herr_t             ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(f);
    assert(oh);
    assert(found_msg);
    assert(new_idx);

    /*
     * The total chunk size must include the requested space plus enough
     * for the message header.  This must be at least some minimum and
     * aligned properly.
     */
    size = MAX(H5O_MIN_SIZE, size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));
    assert(size == H5O_ALIGN_OH(oh, size));

    /*
     * The total chunk size must include enough space for the checksum
     * on the chunk and the continuation chunk magic #. (which are only present
     * in later versions of the object header)
     */
    size += H5O_SIZEOF_CHKHDR_OH(oh);

    /* Allocate space in file to hold the new chunk */
    new_chunk_addr = H5MF_alloc(f, H5FD_MEM_OHDR, (hsize_t)size);
    if (!H5_addr_defined(new_chunk_addr))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "unable to allocate space for new chunk");

    /* Create the new chunk giving it a file address. */
    if (oh->nchunks >= oh->alloc_nchunks) {
        size_t       na = MAX(H5O_NCHUNKS, oh->alloc_nchunks * 2); /* Double # of chunks allocated */
        H5O_chunk_t *x;

        if (NULL == (x = H5FL_SEQ_REALLOC(H5O_chunk_t, oh->chunk, na)))
            HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "can't allocate larger chunk array, na = %zu", na);
        oh->alloc_nchunks = na;
        oh->chunk         = x;
    } /* end if */

    H5_CHECKED_ASSIGN(chunkno, unsigned, oh->nchunks, size_t);
    oh->nchunks++;
    oh->chunk[chunkno].addr = new_chunk_addr;
    oh->chunk[chunkno].size = size;
    oh->chunk[chunkno].gap  = 0;
    if (NULL == (oh->chunk[chunkno].image = p = H5FL_BLK_CALLOC(chunk_image, size)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "can't allocate image for chunk, size = %zu", size);
    oh->chunk[chunkno].chunk_proxy = NULL;

    /* If this is a later version of the object header format, put the magic
     *  # at the beginning of the chunk image.
     */
    if (oh->version > H5O_VERSION_1) {
        H5MM_memcpy(p, H5O_CHK_MAGIC, (size_t)H5_SIZEOF_MAGIC);
        p += H5_SIZEOF_MAGIC;
    } /* end if */

    /*
     * Make sure we have enough space for all possible new messages
     * that could be generated below.
     */
    if (oh->nmesgs + 3 > oh->alloc_nmesgs)
        if (H5O__alloc_msgs(oh, (size_t)3) < 0)
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "can't allocate more space for messages");

    /* Check if we need to move multiple messages, in order to make room for the new message */
    cont_size = H5O_ALIGN_OH(oh, (size_t)(H5F_SIZEOF_ADDR(f) + H5F_SIZEOF_SIZE(f)));
    if (found_null >= oh->nmesgs) {
        if (found_msg->msgno < 0) {
            /* Move all non-null messages in the last chunk to the new chunk.  This
             * should be extremely rare so we don't care too much about minimizing
             * the space used.
             */
            H5O_mesg_t *null_msg; /* Pointer to new null message */

            /* Protect last chunk */
            if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, chunkno - 1)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to load object header chunk");

            /* Copy each message to the new location */
            for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++)
                if (curr_msg->chunkno == chunkno - 1) {
                    if (curr_msg->type->id == H5O_NULL_ID) {
                        /* Delete the null message */
                        if (u < oh->nmesgs - 1)
                            memmove(curr_msg, curr_msg + 1, ((oh->nmesgs - 1) - u) * sizeof(H5O_mesg_t));
                        oh->nmesgs--;
                    } /* end if */
                    else {
                        assert(curr_msg->type->id != H5O_CONT_ID);

                        if (size < curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh))
                            HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "invalid size");

                        /* Copy the raw data */
                        H5MM_memcpy(p, curr_msg->raw - (size_t)H5O_SIZEOF_MSGHDR_OH(oh),
                                    curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));

                        /* Update the message info */
                        curr_msg->chunkno = chunkno;
                        curr_msg->raw     = p + H5O_SIZEOF_MSGHDR_OH(oh);

                        /* Account for copied message in new chunk */
                        p += (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + curr_msg->raw_size;
                        size -= (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + curr_msg->raw_size;
                    } /* end else */
                }     /* end if */

            /* Create a null message spanning the entire last chunk */
            found_null       = oh->nmesgs++;
            null_msg         = &(oh->mesg[found_null]);
            null_msg->type   = H5O_MSG_NULL;
            null_msg->dirty  = true;
            null_msg->native = NULL;
            null_msg->raw    = oh->chunk[chunkno - 1].image +
                            ((chunkno == 1) ? H5O_SIZEOF_HDR(oh) : H5O_SIZEOF_CHKHDR_OH(oh)) -
                            H5O_SIZEOF_CHKSUM_OH(oh) + H5O_SIZEOF_MSGHDR_OH(oh);
            null_msg->raw_size =
                oh->chunk[chunkno - 1].size -
                ((chunkno == 1) ? (size_t)H5O_SIZEOF_HDR(oh) : (size_t)H5O_SIZEOF_CHKHDR_OH(oh)) -
                (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
            null_msg->chunkno = chunkno - 1;

            assert(null_msg->raw_size >= cont_size);

            /* Remove any gap in the chunk */
            oh->chunk[chunkno - 1].gap = 0;

            /* Release chunk, marking it dirty */
            if (H5O__chunk_unprotect(f, chk_proxy, true) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");
        } /* end if */
        else {
            /* Move message (that will be replaced with continuation message)
             *  to new chunk, if necessary.
             */
            H5O_mesg_t *null_msg; /* Pointer to new null message */

            /* Protect chunk */
            if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, oh->mesg[found_msg->msgno].chunkno)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to load object header chunk");

            /* Create null message for space that message to copy currently occupies */
            found_null         = oh->nmesgs++;
            null_msg           = &(oh->mesg[found_null]);
            null_msg->type     = H5O_MSG_NULL;
            null_msg->native   = NULL;
            null_msg->raw      = oh->mesg[found_msg->msgno].raw;
            null_msg->raw_size = oh->mesg[found_msg->msgno].raw_size;
            null_msg->chunkno  = oh->mesg[found_msg->msgno].chunkno;

            /* Copy the message to move (& its prefix) to its new location */
            H5MM_memcpy(p, oh->mesg[found_msg->msgno].raw - H5O_SIZEOF_MSGHDR_OH(oh),
                        oh->mesg[found_msg->msgno].raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));

            /* Switch moved message to point to new location */
            oh->mesg[found_msg->msgno].raw     = p + H5O_SIZEOF_MSGHDR_OH(oh);
            oh->mesg[found_msg->msgno].chunkno = chunkno;

            /* Account for copied message in new chunk */
            p += (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + oh->mesg[found_msg->msgno].raw_size;
            size -= (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + oh->mesg[found_msg->msgno].raw_size;

            /* Add any available space after the message to move to the new null message */
            if (found_msg->gap_size > 0) {
                /* Absorb a gap after the moved message */
                assert(oh->chunk[null_msg->chunkno].gap == found_msg->gap_size);
                null_msg->raw_size += found_msg->gap_size;
                oh->chunk[null_msg->chunkno].gap = 0;
            } /* end if */
            else if (found_msg->null_size > 0) {
                H5O_mesg_t *old_null_msg =
                    &oh->mesg[found_msg->null_msgno]; /* Pointer to NULL message to eliminate */

                /* Absorb a null message after the moved message */
                assert((null_msg->raw + null_msg->raw_size) ==
                       (old_null_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh)));
                null_msg->raw_size += found_msg->null_size;

                /* Release any information/memory for message */
                H5O__msg_free_mesg(old_null_msg);

                /* Remove null message from list of messages */
                if (found_msg->null_msgno < (oh->nmesgs - 1))
                    memmove(old_null_msg, old_null_msg + 1,
                            ((oh->nmesgs - 1) - found_msg->null_msgno) * sizeof(H5O_mesg_t));

                /* Decrement # of messages */
                /* (Don't bother reducing size of message array for now -QAK) */
                oh->nmesgs--;

                /* Adjust message index for new NULL message */
                found_null--;
            } /* end if */

            /* Mark the new null message as dirty */
            null_msg->dirty = true;

            /* Release chunk, marking it dirty */
            if (H5O__chunk_unprotect(f, chk_proxy, true) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");
        } /* end if */
    }     /* end if */

    /* Create null message for [rest of] space in new chunk */
    /* (account for chunk's magic # & checksum) */
    idx                    = oh->nmesgs++;
    oh->mesg[idx].type     = H5O_MSG_NULL;
    oh->mesg[idx].dirty    = true;
    oh->mesg[idx].native   = NULL;
    oh->mesg[idx].raw      = p + H5O_SIZEOF_MSGHDR_OH(oh);
    oh->mesg[idx].raw_size = size - (size_t)(H5O_SIZEOF_CHKHDR_OH(oh) + H5O_SIZEOF_MSGHDR_OH(oh));
    oh->mesg[idx].chunkno  = chunkno;

    /* Insert the new chunk into the cache */
    if (H5O__chunk_add(f, oh, chunkno, oh->mesg[found_null].chunkno) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't add new chunk to cache");

    /* Initialize the continuation information */
    if (NULL == (cont = H5FL_MALLOC(H5O_cont_t)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");
    cont->addr    = oh->chunk[chunkno].addr;
    cont->size    = oh->chunk[chunkno].size;
    cont->chunkno = chunkno;

    /* Split the null message and point at continuation message */
    if (H5O__alloc_null(f, oh, found_null, H5O_MSG_CONT, cont, cont_size) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't split null message");

    /* Set new message index value */
    *new_idx = idx;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_chunk() */

/*-------------------------------------------------------------------------
 * Function:    H5O__alloc_new_chunk
 *
 * Purpose:     Allocates a new chunk for the object header, including
 *		file space.
 *
 *              One of the other chunks will get an object continuation
 *		message.  If there isn't room in any other chunk for the
 *		object continuation message, then some message from
 *		another chunk is moved into this chunk to make room.
 *
 *              SIZE need not be aligned.
 *
 * Note:	The algorithm for finding a message to replace with a
 *		continuation message is still fairly limited.  It's possible
 *		that two (or more) messages smaller than a continuation message
 *		might occupy a chunk and need to be moved in order to make
 *		room for the continuation message.
 *
 *		Also, we aren't checking for NULL messages in front of another
 *		message right now...
 *
 * Return:      Success:        Index number of the null message for the
 *                              new chunk.  The null message will be at
 *                              least SIZE bytes not counting the message
 *                              ID or size fields.
 *
 *              Failure:        Negative
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__alloc_new_chunk(H5F_t *f, H5O_t *oh, size_t size, size_t *new_idx)
{
    size_t               cont_size;           /*continuation message size     */
    size_t               idx;                 /* Message number */
    H5O_msg_alloc_info_t found_msg;           /* Best fit non-null message */
    herr_t               ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);
    assert(size > 0);
    size = H5O_ALIGN_OH(oh, size);

    /* Find the smallest null message that could hold a continuation message */
    idx       = oh->nmesgs;
    cont_size = H5O_ALIGN_OH(oh, (size_t)(H5F_SIZEOF_ADDR(f) + H5F_SIZEOF_SIZE(f)));
    if (H5O__alloc_find_best_null(oh, cont_size, &idx) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_NOTFOUND, FAIL, "error while locating best null header message");

    /* If we couldn't find a null message, locate the best message to move to new chunk */
    if (idx >= oh->nmesgs) {
        found_msg.msgno = -1;
        if (H5O__alloc_find_best_nonnull(f, oh, &size, &found_msg) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_NOTFOUND, FAIL, "error while locating best non-null header message");
    } /* end if */

    /* Allocate and initialize new chunk in the file */
    if (H5O__alloc_chunk(f, oh, size, idx, &found_msg, new_idx) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "can't allocate new object header chunk");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_new_chunk() */

/*-------------------------------------------------------------------------
 * Function:    H5O__alloc_find_best_null
 *
 * Purpose:     Find the best fit null message for a given size of message
 *              to allocate.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__alloc_find_best_null(const H5O_t *oh, size_t size, size_t *mesg_idx)
{
    size_t  idx;        /* Index of message which fits allocation */
    ssize_t found_null; /* Best fit null message         */

    FUNC_ENTER_PACKAGE_NOERR

    /* check args */
    assert(oh);
    assert(size > 0);
    assert(mesg_idx);

    /* Find the smallest null message that could hold the new object header message */
    found_null = -1;
    for (idx = 0; idx < oh->nmesgs; idx++) {
        if (H5O_NULL_ID == oh->mesg[idx].type->id) {
            /* If we found an exact fit, use it */
            if (oh->mesg[idx].raw_size == size) {
                /* Keep first exact fit */
                if (found_null < 0)
                    found_null = (ssize_t)idx;
                else
                    /* If we've got more than one exact fit, choose the one in the earliest chunk */
                    if (oh->mesg[idx].chunkno < oh->mesg[found_null].chunkno) {
                        found_null = (ssize_t)idx;

                        /* If we found an exact fit in object header chunk #0, we can get out */
                        /* (Could extend this to look for earliest message in
                         *      chunk #0 - QAK, 2016/10/21)
                         */
                        if (0 == oh->mesg[idx].chunkno)
                            break;
                    } /* end if */
            }         /* end if */
            /* Look for null message that's larger than needed */
            else if (oh->mesg[idx].raw_size > size) {
                /* Keep first one found */
                if (found_null < 0)
                    found_null = (ssize_t)idx;
                /* Check for better fit */
                else if (oh->mesg[idx].raw_size < oh->mesg[found_null].raw_size) {
                    found_null = (ssize_t)idx;
                }
                else {
                    /* If they are the same size, choose the one in the earliest chunk */
                    if (oh->mesg[idx].raw_size == oh->mesg[found_null].raw_size) {
                        if (oh->mesg[idx].chunkno < oh->mesg[found_null].chunkno)
                            found_null = (ssize_t)idx;
                    } /* end if */
                }     /* end else */
            }         /* end else-if */
            /* else: Ignore too-small null messages */
        } /* end if */
    }     /* end for */
    if (found_null >= 0)
        *mesg_idx = (size_t)found_null;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5O__alloc_find_best_null() */

/*-------------------------------------------------------------------------
 * Function:    H5O__alloc
 *
 * Purpose:     Allocate enough space in the object header for this message.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O__alloc(H5F_t *f, H5O_t *oh, const H5O_msg_class_t *type, const void *mesg, size_t *mesg_idx)
{
    size_t raw_size;            /* Raw size of message */
    size_t aligned_size;        /* Size of message including alignment */
    size_t idx;                 /* Index of message which fits allocation */
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);
    assert(type);
    assert(mesg);
    assert(mesg_idx);

    /* Compute the size needed to store the message in the object header */
    raw_size = (type->raw_size)(f, false, mesg);
    if (0 == raw_size)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "can't compute object header message size");
    if (raw_size >= H5O_MESG_MAX_SIZE)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "object header message is too large");
    aligned_size = H5O_ALIGN_OH(oh, raw_size);

    /* Find the smallest null message that could hold the new object header message */
    idx = oh->nmesgs;
    if (H5O__alloc_find_best_null(oh, aligned_size, &idx) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_NOTFOUND, FAIL, "error while locating best null header message");

    /* if we didn't find one, then allocate more header space */
    if (idx >= oh->nmesgs) {
        unsigned chunkno;

        /* check to see if we can extend one of the chunks.  If we can,
         * do so.  Otherwise, we will have to allocate a new chunk.
         *
         * Note that in this new version of this function, all chunks
         * must have file space allocated to them.
         */
        for (chunkno = 0; chunkno < oh->nchunks; chunkno++) {
            htri_t tri_result; /* Status from attempting to extend chunk */

            if ((tri_result = H5O__alloc_extend_chunk(f, oh, chunkno, raw_size, &idx)) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTEXTEND, FAIL, "can't extend existing chunk");
            if (tri_result == true)
                break;
        } /* end for */

        /* If we were not able to extend a chunk, create a new one */
        if (idx >= oh->nmesgs)
            if (H5O__alloc_new_chunk(f, oh, raw_size, &idx) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_NOSPACE, FAIL, "unable to create a new object header data chunk");
    } /* end if */
    assert(idx < oh->nmesgs);

    /* Split the null message and point at continuation message */
    if (H5O__alloc_null(f, oh, idx, type, NULL, aligned_size) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't split null message");

    /* Mark object header as dirty in cache */
    if (H5AC_mark_entry_dirty(oh) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTMARKDIRTY, FAIL, "unable to mark object header as dirty");

    /* Set message index value */
    *mesg_idx = idx;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__release_mesg
 *
 * Purpose:     Convert a message into a null message
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O__release_mesg(H5F_t *f, H5O_t *oh, H5O_mesg_t *mesg, bool adj_link)
{
    H5O_chunk_proxy_t *chk_proxy   = NULL;    /* Chunk that message is in */
    bool               chk_dirtied = false;   /* Flag for unprotecting chunk */
    herr_t             ret_value   = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(f);
    assert(oh);
    assert(mesg);

    /* Check if we should operate on the message */
    if (adj_link)
        /* Free any space referred to in the file from this message */
        if (H5O__delete_mesg(f, oh, mesg) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL,
                        "unable to delete file space for object header message");

    /* Protect chunk */
    if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, mesg->chunkno)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to protect object header chunk");

    /* Free any native information */
    H5O__msg_free_mesg(mesg);

    /* Change message type to nil and zero it */
    mesg->type = H5O_MSG_NULL;
    assert(mesg->raw + mesg->raw_size <= (oh->chunk[mesg->chunkno].image + oh->chunk[mesg->chunkno].size) -
                                             (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[mesg->chunkno].gap));
    memset(mesg->raw, 0, mesg->raw_size);

    /* Clear message flags */
    mesg->flags = 0;

    /* Mark the message as modified */
    mesg->dirty = true;
    chk_dirtied = true;

    /* Check if chunk has a gap currently */
    if (oh->chunk[mesg->chunkno].gap) {
        /* Eliminate the gap in the chunk */
        if (H5O__eliminate_gap(oh, &chk_dirtied, mesg,
                               ((oh->chunk[mesg->chunkno].image + oh->chunk[mesg->chunkno].size) -
                                (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[mesg->chunkno].gap)),
                               oh->chunk[mesg->chunkno].gap) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTREMOVE, FAIL, "can't eliminate gap in chunk");
    } /* end if */

done:
    /* Release chunk, if not already done */
    if (chk_proxy && H5O__chunk_unprotect(f, chk_proxy, chk_dirtied) < 0)
        HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__release_mesg() */

/*-------------------------------------------------------------------------
 * Function:    H5O__move_cont
 *
 * Purpose:     Check and move message(s) forward into a continuation message
 *
 * Return:      Success:        non-negative (true/false)
 *              Failure:        negative
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5O__move_cont(H5F_t *f, H5O_t *oh, unsigned cont_u)
{
    H5O_chunk_proxy_t *chk_proxy = NULL;    /* Chunk that continuation message is in */
    H5O_mesg_t        *cont_msg;            /* Pointer to the continuation message */
    unsigned           deleted_chunkno;     /* Chunk # to delete */
    bool               chk_dirtied = false; /* Flags for unprotecting chunk */
    htri_t             ret_value   = true;  /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    assert(f);
    assert(oh);

    /* Get initial information */
    cont_msg = &oh->mesg[cont_u];
    H5O_LOAD_NATIVE(f, 0, oh, cont_msg, FAIL)
    deleted_chunkno = ((H5O_cont_t *)(cont_msg->native))->chunkno;

    /* Check if continuation message is pointing to the last chunk */
    if (deleted_chunkno == (oh->nchunks - 1)) {
        size_t      nonnull_size; /* Total size of nonnull messages in the chunk pointed to by cont message */
        H5O_mesg_t *curr_msg;     /* Pointer to the current message to operate on */
        size_t      gap_size;     /* Size of gap produced */
        size_t      v;            /* Local index variable */

        /* Spin through messages */
        nonnull_size = 0;
        for (v = 0, curr_msg = &oh->mesg[0]; v < oh->nmesgs; v++, curr_msg++) {
            if (curr_msg->chunkno == deleted_chunkno) {
                /* Find size of all non-null messages in the chunk pointed to by the continuation message */
                if (curr_msg->type->id != H5O_NULL_ID) {
                    assert(curr_msg->type->id != H5O_CONT_ID);
                    nonnull_size += curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
                } /* end if */
            }     /* end if */
        }         /* end for */

        /* Size of gap in chunk w/continuation message */
        gap_size = oh->chunk[cont_msg->chunkno].gap;

        /* Check if messages can fit into the continuation message + gap size */
        /* (Could count any null messages in the chunk w/the continuation
         *      message also, but that is pretty complex. -QAK)
         */
        if (nonnull_size &&
            nonnull_size <= (gap_size + cont_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh))) {
            uint8_t *move_start, *move_end; /* Pointers to area of messages to move */
            unsigned cont_chunkno;          /* Chunk number for continuation message */

            /* Get continuation info */
            move_start   = cont_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh);
            move_end     = cont_msg->raw + cont_msg->raw_size;
            cont_chunkno = cont_msg->chunkno;

            /* Convert continuation message into a null message.  Do not delete
             * the target chunk yet, so we can still copy messages from it. */
            if (H5O__release_mesg(f, oh, cont_msg, false) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL, "unable to convert into null message");

            /* Protect chunk */
            if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, cont_chunkno)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to protect object header chunk");

            /* Move message(s) forward into continuation message */
            for (v = 0, curr_msg = &oh->mesg[0]; v < oh->nmesgs; v++, curr_msg++)
                /* Look for messages in chunk to delete */
                if (curr_msg->chunkno == deleted_chunkno) {
                    /* Move messages out of chunk to delete */
                    if (curr_msg->type->id != H5O_NULL_ID) {
                        size_t move_size; /* Size of the message to be moved */

                        /* Compute size of message to move */
                        move_size = curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh);

                        /* Move message out of deleted chunk */
                        H5MM_memcpy(move_start, curr_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh), move_size);
                        curr_msg->raw     = move_start + H5O_SIZEOF_MSGHDR_OH(oh);
                        curr_msg->chunkno = cont_chunkno;
                        chk_dirtied       = true;

                        /* Adjust location to move messages to */
                        move_start += move_size;
                    } /* end else */
                }     /* end if */

            /* Delete the target chunk */
            if (H5O__chunk_delete(f, oh, deleted_chunkno) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL, "unable to remove chunk from cache");

            assert(move_start <= (move_end + gap_size));

            /* Check if there is space remaining in the continuation message */
            /* (The remaining space can be gap or a null message) */
            gap_size += (size_t)(move_end - move_start);
            if (gap_size >= (size_t)H5O_SIZEOF_MSGHDR_OH(oh)) {
                /* Adjust size of null (was continuation) message */
                cont_msg->raw_size = gap_size - (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
                cont_msg->raw      = move_start + H5O_SIZEOF_MSGHDR_OH(oh);
                cont_msg->dirty    = true;
                chk_dirtied        = true;
            } /* end if */
            else {
                /* Check if there is space that should be a gap */
                if (gap_size > 0) {
                    /* Convert remnant into gap in chunk */
                    if (H5O__add_gap(f, oh, cont_chunkno, &chk_dirtied, cont_u, move_start, gap_size) < 0)
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't insert gap in chunk");
                } /* end if */

                /* Release any information/memory for continuation message */
                H5O__msg_free_mesg(cont_msg);
                if (cont_u < (oh->nmesgs - 1))
                    memmove(&oh->mesg[cont_u], &oh->mesg[cont_u + 1],
                            ((oh->nmesgs - 1) - cont_u) * sizeof(H5O_mesg_t));
                oh->nmesgs--;
            } /* end else */

            /* Move message(s) forward into continuation message */
            /*      Note: unsigned v wrapping around at the end */
            for (v = oh->nmesgs - 1, curr_msg = &oh->mesg[v]; v < oh->nmesgs; v--, curr_msg--)
                /* Look for messages in chunk to delete */
                if (curr_msg->chunkno == deleted_chunkno) {
                    /* Remove all null messages in deleted chunk from list of messages */
                    if (curr_msg->type->id == H5O_NULL_ID) {
                        /* Release any information/memory for message */
                        H5O__msg_free_mesg(curr_msg);
                        chk_dirtied = true;

                        /* Remove from message list */
                        if (v < (oh->nmesgs - 1))
                            memmove(&oh->mesg[v], &oh->mesg[v + 1],
                                    ((oh->nmesgs - 1) - v) * sizeof(H5O_mesg_t));
                        oh->nmesgs--;
                    } /* end if */
                }     /* end if */

            /* Remove chunk from list of chunks */
            oh->chunk[deleted_chunkno].image = H5FL_BLK_FREE(chunk_image, oh->chunk[deleted_chunkno].image);
            oh->nchunks--;
        } /* end if */
        else
            ret_value = false;
    } /* end if */
    else
        ret_value = false;

done:
    /* Release chunk, if not already done */
    if (chk_proxy && H5O__chunk_unprotect(f, chk_proxy, chk_dirtied) < 0)
        HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__move_cont() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__move_msgs_forward
 *
 * Purpose:     Move messages toward first chunk
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5O__move_msgs_forward(H5F_t *f, H5O_t *oh)
{
    H5O_chunk_proxy_t *null_chk_proxy      = NULL;  /* Chunk that null message is in */
    H5O_chunk_proxy_t *curr_chk_proxy      = NULL;  /* Chunk that message is in */
    H5O_chunk_proxy_t *cont_targ_chk_proxy = NULL;  /* Chunk that continuation message points to */
    bool               null_chk_dirtied    = false; /* Flags for unprotecting null chunk */
    bool               curr_chk_dirtied    = false; /* Flags for unprotecting curr chunk */
    bool               packed_msg;                  /* Flag to indicate that messages were packed */
    bool               did_packing = false;         /* Whether any messages were packed */
    htri_t             ret_value   = FAIL;          /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh);

    /* Loop until no messages packed */
    /* (Double loop is not very efficient, but it would be some extra work to
     *      add a list of messages to each chunk -QAK)
     */
    do {
        H5O_mesg_t *curr_msg; /* Pointer to current message to operate on */
        unsigned    u;        /* Local index variable */

        /* Reset packed messages flag */
        packed_msg = false;

        /* Scan through messages for messages that can be moved earlier in chunks */
        for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++) {
            if (H5O_NULL_ID == curr_msg->type->id) {
                H5O_chunk_t *chunk; /* Pointer to chunk that null message is in */

                /* Check if null message is not last in chunk */
                chunk = &(oh->chunk[curr_msg->chunkno]);
                if ((curr_msg->raw + curr_msg->raw_size) !=
                    ((chunk->image + chunk->size) - (H5O_SIZEOF_CHKSUM_OH(oh) + chunk->gap))) {
                    H5O_mesg_t *nonnull_msg; /* Pointer to current message to operate on */
                    unsigned    v;           /* Local index variable */

                    /* Loop over messages again, looking for the message in the chunk after the null message
                     */
                    for (v = 0, nonnull_msg = &oh->mesg[0]; v < oh->nmesgs; v++, nonnull_msg++) {
                        /* Locate message that is immediately after the null message */
                        if ((curr_msg->chunkno == nonnull_msg->chunkno) &&
                            ((curr_msg->raw + curr_msg->raw_size) ==
                             (nonnull_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh)))) {
                            /* Don't swap messages if the second message is also a null message */
                            /* (We'll merge them together later, in another routine) */
                            if (H5O_NULL_ID != nonnull_msg->type->id) {
                                /* Protect chunk */
                                if (NULL == (null_chk_proxy = H5O__chunk_protect(f, oh, curr_msg->chunkno)))
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL,
                                                "unable to load object header chunk");

                                /* Copy raw data for non-null message to new location */
                                memmove(curr_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh),
                                        nonnull_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh),
                                        nonnull_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));

                                /* Adjust non-null message's offset in chunk */
                                nonnull_msg->raw = curr_msg->raw;

                                /* Adjust null message's offset in chunk */
                                curr_msg->raw =
                                    nonnull_msg->raw + nonnull_msg->raw_size + H5O_SIZEOF_MSGHDR_OH(oh);

                                /* Mark null message dirty */
                                /* (since we need to re-encode its message header) */
                                curr_msg->dirty = true;

                                /* Release chunk, marking it dirty */
                                if (H5O__chunk_unprotect(f, null_chk_proxy, true) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                                "unable to unprotect object header chunk");
                                null_chk_proxy = NULL;

                                /* Set the flag to indicate that the null message
                                 * was packed - if its not at the end its chunk,
                                 * we'll move it again on the next pass.
                                 */
                                packed_msg = true;
                            } /* end if */

                            /* Break out of loop */
                            break;
                        } /* end if */
                    }     /* end for */
                    /* Should have been message after null message */
                    assert(v < oh->nmesgs);
                } /* end if */
            }     /* end if */
            else {
                H5O_mesg_t *null_msg; /* Pointer to current message to operate on */
                size_t      v;        /* Local index variable */

                /* Check if messages in chunk pointed to can replace continuation message */
                if (H5O_CONT_ID == curr_msg->type->id) {
                    htri_t status; /* Status from moving messages */

                    if ((status = H5O__move_cont(f, oh, u)) < 0)
                        HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL,
                                    "Error in moving messages into cont message");
                    else if (status > 0) { /* Message(s) got moved into "continuation" message */
                        packed_msg = true;
                        break;
                    } /* end else-if */
                }     /* end if */

                /* Loop over messages again, looking for large enough null message in earlier chunk */
                for (v = 0, null_msg = &oh->mesg[0]; v < oh->nmesgs; v++, null_msg++) {
                    if (H5O_NULL_ID == null_msg->type->id && curr_msg->chunkno > null_msg->chunkno &&
                        curr_msg->raw_size <= null_msg->raw_size) {
                        unsigned old_chunkno; /* Old message information */
                        uint8_t *old_raw;

                        /* Keep old information about non-null message */
                        old_chunkno = curr_msg->chunkno;
                        old_raw     = curr_msg->raw;

                        /* Protect chunks */
                        if (NULL == (null_chk_proxy = H5O__chunk_protect(f, oh, null_msg->chunkno)))
                            HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL,
                                        "unable to load object header chunk");
                        if (NULL == (curr_chk_proxy = H5O__chunk_protect(f, oh, curr_msg->chunkno)))
                            HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL,
                                        "unable to load object header chunk");

                        /* If the message being moved is a continuation
                         * message and we are doing SWMR writes, we must
                         * update the flush dependencies */
                        if (oh->swmr_write && (H5O_CONT_ID == curr_msg->type->id)) {
                            void *null_chk_mdc_obj; /* The metadata cache object for the null_msg chunk */

                            /* Point to the metadata cache object for the
                             * null message chunk, oh if in chunk 0 or the
                             * proxy otherwise */
                            null_chk_mdc_obj = (null_msg->chunkno == 0 ? (void *)oh : (void *)null_chk_proxy);

                            /* The other chunks involved should never be
                             * chunk 0 */
                            assert(curr_msg->chunkno > 0);
                            assert(((H5O_cont_t *)(curr_msg->native))->chunkno > 0);

                            /* Protect continuation message target chunk */
                            if (NULL == (cont_targ_chk_proxy = H5O__chunk_protect(
                                             f, oh, ((H5O_cont_t *)(curr_msg->native))->chunkno)))
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL,
                                            "unable to load object header chunk");

                            /* Check for flush dependency on previous continuation chunk */
                            /* (As opposed to chunk 0) */
                            if (cont_targ_chk_proxy->fd_parent) {
                                /* Remove flush dependency on old continuation
                                 * message chunk */
                                assert(cont_targ_chk_proxy);
                                assert(curr_chk_proxy);
                                assert((void *)curr_chk_proxy == cont_targ_chk_proxy->fd_parent);

                                if (H5AC_destroy_flush_dependency(curr_chk_proxy, cont_targ_chk_proxy) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTUNDEPEND, FAIL,
                                                "unable to destroy flush dependency");

                                cont_targ_chk_proxy->fd_parent = NULL;
                            } /* end if */

                            /* Avoid (another) flush dependency on chunk 0 */
                            if (0 != null_msg->chunkno) {
                                /* Sanity checks */
                                assert(null_chk_mdc_obj);
                                assert(((H5C_cache_entry_t *)null_chk_mdc_obj)->type);
                                assert(((H5C_cache_entry_t *)null_chk_mdc_obj)->type->id == H5AC_OHDR_CHK_ID);

                                /* Create flush dependency on new continuation
                                 * message chunk */
                                if (H5AC_create_flush_dependency(null_chk_mdc_obj, cont_targ_chk_proxy) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTDEPEND, FAIL,
                                                "unable to create flush dependency");

                                cont_targ_chk_proxy->fd_parent = null_chk_mdc_obj;
                            } /* end if */

                            /* Unprotect continuation message target chunk */
                            if (H5O__chunk_unprotect(f, cont_targ_chk_proxy, false) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");
                            cont_targ_chk_proxy = NULL;
                        } /* end if */

                        /* Copy raw data for non-null message to new chunk */
                        H5MM_memcpy(null_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh),
                                    curr_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh),
                                    curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh));

                        /* Point non-null message at null message's space */
                        curr_msg->chunkno = null_msg->chunkno;
                        curr_msg->raw     = null_msg->raw;
                        curr_chk_dirtied  = true;

                        /* Change information for null message */
                        if (curr_msg->raw_size == null_msg->raw_size) {
                            /* Point null message at old non-null space */
                            /* (Instead of freeing it and allocating new message) */
                            null_msg->chunkno = old_chunkno;
                            null_msg->raw     = old_raw;

                            /* Mark null message dirty */
                            null_msg->dirty  = true;
                            null_chk_dirtied = true;

                            /* Release current chunk, marking it dirty */
                            if (H5O__chunk_unprotect(f, curr_chk_proxy, curr_chk_dirtied) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");
                            curr_chk_proxy   = NULL;
                            curr_chk_dirtied = false;

                            /* Check for gap in null message's chunk */
                            if (oh->chunk[old_chunkno].gap > 0) {
                                /* Eliminate the gap in the chunk */
                                if (H5O__eliminate_gap(
                                        oh, &null_chk_dirtied, null_msg,
                                        ((oh->chunk[old_chunkno].image + oh->chunk[old_chunkno].size) -
                                         (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[old_chunkno].gap)),
                                        oh->chunk[old_chunkno].gap) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTREMOVE, FAIL,
                                                "can't eliminate gap in chunk");
                            } /* end if */

                            /* Release null chunk, marking it dirty */
                            if (H5O__chunk_unprotect(f, null_chk_proxy, null_chk_dirtied) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");
                            null_chk_proxy   = NULL;
                            null_chk_dirtied = false;
                        } /* end if */
                        else {
                            size_t new_null_msg; /* Message index for new null message */

                            /* Check if null message is large enough to still exist */
                            if ((null_msg->raw_size - curr_msg->raw_size) <
                                (size_t)H5O_SIZEOF_MSGHDR_OH(oh)) {
                                size_t gap_size =
                                    null_msg->raw_size - curr_msg->raw_size; /* Size of gap produced */

                                /* Adjust the size of the null message being eliminated */
                                null_msg->raw_size = curr_msg->raw_size;

                                /* Mark null message dirty */
                                null_msg->dirty  = true;
                                null_chk_dirtied = true;

                                /* Add the gap to the chunk */
                                if (H5O__add_gap(f, oh, null_msg->chunkno, &null_chk_dirtied, v,
                                                 null_msg->raw + null_msg->raw_size, gap_size) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "can't insert gap in chunk");

                                /* Reuse message # for new null message taking place of non-null message */
                                new_null_msg = v;
                            } /* end if */
                            else {
                                /* Adjust null message's size & offset */
                                null_msg->raw += curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh);
                                null_msg->raw_size -= curr_msg->raw_size + (size_t)H5O_SIZEOF_MSGHDR_OH(oh);

                                /* Mark null message dirty */
                                null_msg->dirty  = true;
                                null_chk_dirtied = true;

                                /* Create new null message for previous location of non-null message */
                                if (oh->nmesgs >= oh->alloc_nmesgs) {
                                    if (H5O__alloc_msgs(oh, (size_t)1) < 0)
                                        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
                                                    "can't allocate more space for messages");

                                    /* "Retarget" 'curr_msg' pointer into newly re-allocated array of messages
                                     */
                                    curr_msg = &oh->mesg[u];
                                } /* end if */

                                /* Get message # for new null message */
                                new_null_msg = oh->nmesgs++;
                            } /* end else */

                            /* Release null message's chunk, marking it dirty */
                            if (H5O__chunk_unprotect(f, null_chk_proxy, null_chk_dirtied) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");
                            null_chk_proxy   = NULL;
                            null_chk_dirtied = false;

                            /* Initialize new null message to take over non-null message's location */
                            oh->mesg[new_null_msg].type     = H5O_MSG_NULL;
                            oh->mesg[new_null_msg].native   = NULL;
                            oh->mesg[new_null_msg].raw      = old_raw;
                            oh->mesg[new_null_msg].raw_size = curr_msg->raw_size;
                            oh->mesg[new_null_msg].chunkno  = old_chunkno;

                            /* Mark new null message dirty */
                            oh->mesg[new_null_msg].dirty = true;
                            curr_chk_dirtied             = true;

                            /* Check for gap in new null message's chunk */
                            if (oh->chunk[old_chunkno].gap > 0) {
                                /* Eliminate the gap in the chunk */
                                if (H5O__eliminate_gap(
                                        oh, &curr_chk_dirtied, &oh->mesg[new_null_msg],
                                        ((oh->chunk[old_chunkno].image + oh->chunk[old_chunkno].size) -
                                         (H5O_SIZEOF_CHKSUM_OH(oh) + oh->chunk[old_chunkno].gap)),
                                        oh->chunk[old_chunkno].gap) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTREMOVE, FAIL,
                                                "can't eliminate gap in chunk");
                            } /* end if */

                            /* Release new null message's chunk, marking it dirty */
                            if (H5O__chunk_unprotect(f, curr_chk_proxy, curr_chk_dirtied) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");
                            curr_chk_proxy   = NULL;
                            curr_chk_dirtied = false;
                        } /* end else */

                        /* Indicate that we packed messages */
                        packed_msg = true;

                        /* Break out of loop */
                        /* (If it's possible to move message to even earlier chunk
                         *      we'll get it on the next pass - QAK)
                         */
                        break;
                    } /* end if */
                }     /* end for */

                /* If we packed messages, get out of loop and start over */
                /* (Don't know if this has any benefit one way or the other -QAK) */
                if (packed_msg)
                    break;
            } /* end else */
        }     /* end for */

        /* If we did any packing, remember that */
        if (packed_msg)
            did_packing = true;
    } while (packed_msg);

    /* Set return value */
    ret_value = (htri_t)did_packing;

done:
    if (ret_value < 0) {
        if (null_chk_proxy && H5O__chunk_unprotect(f, null_chk_proxy, null_chk_dirtied) < 0)
            HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect null object header chunk");
        if (curr_chk_proxy && H5O__chunk_unprotect(f, curr_chk_proxy, curr_chk_dirtied) < 0)
            HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect current object header chunk");
        if (cont_targ_chk_proxy && H5O__chunk_unprotect(f, cont_targ_chk_proxy, false) < 0)
            HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                        "unable to unprotect continuation message target object header chunk");
    } /* end if */
    else
        assert(!null_chk_proxy && !curr_chk_proxy && !cont_targ_chk_proxy);

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__move_msgs_forward() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__merge_null
 *
 * Purpose:     Merge neighboring null messages in an object header
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5O__merge_null(H5F_t *f, H5O_t *oh)
{
    bool   merged_msg;          /* Flag to indicate that messages were merged */
    bool   did_merging = false; /* Whether any messages were merged */
    htri_t ret_value   = FAIL;  /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh != NULL);

    /* Loop until no messages merged */
    /* (Double loop is not very efficient, but it would be some extra work to add
     *      a list of messages to each chunk -QAK)
     */
    do {
        H5O_mesg_t *curr_msg; /* Pointer to current message to operate on */
        unsigned    u;        /* Local index variable */

        /* Reset merged messages flag */
        merged_msg = false;

        /* Scan messages for adjacent null messages & merge them */
        for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++) {
            if (H5O_NULL_ID == curr_msg->type->id) {
                H5O_mesg_t *curr_msg2; /* Pointer to current message to operate on */
                unsigned    v;         /* Local index variable */

                /* Should be no gaps in chunk with null message */
                assert(oh->chunk[curr_msg->chunkno].gap == 0);

                /* Loop over messages again, looking for null message in same chunk */
                for (v = 0, curr_msg2 = &oh->mesg[0]; v < oh->nmesgs; v++, curr_msg2++) {
                    if (u != v && H5O_NULL_ID == curr_msg2->type->id &&
                        curr_msg->chunkno == curr_msg2->chunkno) {
                        ssize_t adj_raw      = 0; /* Amount to adjust raw message pointer */
                        size_t  adj_raw_size = 0; /* Amount to adjust raw message size */

                        /* Check for second message after first message */
                        if ((curr_msg->raw + curr_msg->raw_size) ==
                            (curr_msg2->raw - H5O_SIZEOF_MSGHDR_OH(oh))) {
                            /* Extend first null message length to cover second null message */
                            adj_raw      = 0;
                            adj_raw_size = (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + curr_msg2->raw_size;

                            /* Message has been merged */
                            merged_msg = true;
                        } /* end if */
                        /* Check for second message before first message */
                        else if ((curr_msg->raw - H5O_SIZEOF_MSGHDR_OH(oh)) ==
                                 (curr_msg2->raw + curr_msg2->raw_size)) {
                            /* Adjust first message address and extend length to cover second message */
                            adj_raw = -((ssize_t)((size_t)H5O_SIZEOF_MSGHDR_OH(oh) + curr_msg2->raw_size));
                            adj_raw_size = (size_t)H5O_SIZEOF_MSGHDR_OH(oh) + curr_msg2->raw_size;

                            /* Message has been merged */
                            merged_msg = true;
                        } /* end if */

                        /* Second message has been merged, delete it */
                        if (merged_msg) {
                            H5O_chunk_proxy_t *curr_chk_proxy; /* Chunk that message is in */
                            htri_t             result;

                            /* Release any information/memory for second message */
                            H5O__msg_free_mesg(curr_msg2);

                            /* Protect chunk */
                            if (NULL == (curr_chk_proxy = H5O__chunk_protect(f, oh, curr_msg->chunkno)))
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL,
                                            "unable to load object header chunk");

                            /* Adjust first message address and extend length to cover second message */
                            curr_msg->raw += adj_raw;
                            curr_msg->raw_size += adj_raw_size;

                            /* Mark first message as dirty */
                            curr_msg->dirty = true;

                            /* Release new null message's chunk, marking it dirty */
                            if (H5O__chunk_unprotect(f, curr_chk_proxy, true) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL,
                                            "unable to unprotect object header chunk");

                            /* Remove second message from list of messages */
                            if (v < (oh->nmesgs - 1))
                                memmove(&oh->mesg[v], &oh->mesg[v + 1],
                                        ((oh->nmesgs - 1) - v) * sizeof(H5O_mesg_t));

                            /* Decrement # of messages */
                            /* (Don't bother reducing size of message array for now -QAK) */
                            oh->nmesgs--;

                            /* The merge null message might span the entire chunk: scan for empty chunk to
                             * remove */
                            if ((result = H5O__remove_empty_chunks(f, oh)) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTPACK, FAIL, "can't remove empty chunk");
                            else if (result > 0)
                                /* Get out of loop */
                                break;

                            /* If the merged message is too large, shrink the chunk */
                            if (curr_msg->raw_size >= H5O_MESG_MAX_SIZE)
                                if (H5O__alloc_shrink_chunk(f, oh, curr_msg->chunkno) < 0)
                                    HGOTO_ERROR(H5E_OHDR, H5E_CANTPACK, FAIL, "unable to shrink chunk");

                            /* Get out of loop */
                            break;
                        } /* end if */
                    }     /* end if */
                }         /* end for */

                /* Get out of loop if we merged messages */
                if (merged_msg)
                    break;
            } /* end if */
        }     /* end for */

        /* If we did any merging, remember that */
        if (merged_msg)
            did_merging = true;
    } while (merged_msg);

    /* Set return value */
    ret_value = (htri_t)did_merging;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__merge_null() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__remove_empty_chunks
 *
 * Purpose:     Attempt to eliminate empty chunks from object header.
 *
 *              This examines a chunk to see if it's empty
 *              and removes it (and the continuation message that points to it)
 *              from the object header.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static htri_t
H5O__remove_empty_chunks(H5F_t *f, H5O_t *oh)
{
    bool   deleted_chunk;        /* Whether to a chunk was deleted */
    bool   did_deleting = false; /* Whether any chunks were deleted */
    htri_t ret_value    = FAIL;  /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh != NULL);

    /* Loop until no chunks are freed */
    do {
        H5O_mesg_t *null_msg; /* Pointer to null message found */
        H5O_mesg_t *cont_msg; /* Pointer to continuation message found */
        unsigned    u, v;     /* Local index variables */

        /* Reset 'chunk deleted' flag */
        deleted_chunk = false;

        /* Scan messages for null messages that fill an entire chunk */
        for (u = 0, null_msg = &oh->mesg[0]; u < oh->nmesgs; u++, null_msg++) {
            /* If a null message takes up an entire object header chunk (and
             * its not the "base" chunk), delete that chunk from object header
             */
            if (H5O_NULL_ID == null_msg->type->id && null_msg->chunkno > 0 &&
                ((size_t)H5O_SIZEOF_MSGHDR_OH(oh) + null_msg->raw_size) ==
                    (oh->chunk[null_msg->chunkno].size - H5O_SIZEOF_CHKHDR_OH(oh))) {
                H5O_mesg_t *curr_msg;        /* Pointer to current message to operate on */
                unsigned    null_msg_no;     /* Message # for null message */
                unsigned    deleted_chunkno; /* Chunk # to delete */

                /* Locate continuation message that points to chunk */
                for (v = 0, cont_msg = &oh->mesg[0]; v < oh->nmesgs; v++, cont_msg++) {
                    if (H5O_CONT_ID == cont_msg->type->id) {
                        /* Decode current continuation message if necessary */
                        H5O_LOAD_NATIVE(f, 0, oh, cont_msg, FAIL)

                        /* Check if the chunkno needs to be set */
                        /* (should only occur when the continuation message is first decoded) */
                        if (0 == ((H5O_cont_t *)(cont_msg->native))->chunkno) {
                            unsigned w; /* Local index variable */

                            /* Find chunk that this continuation message points to */
                            for (w = 0; w < oh->nchunks; w++)
                                if (oh->chunk[w].addr == ((H5O_cont_t *)(cont_msg->native))->addr) {
                                    ((H5O_cont_t *)(cont_msg->native))->chunkno = w;
                                    break;
                                } /* end if */
                            assert(((H5O_cont_t *)(cont_msg->native))->chunkno > 0);
                        } /* end if */

                        /* Check for correct chunk to delete */
                        if (oh->chunk[null_msg->chunkno].addr == ((H5O_cont_t *)(cont_msg->native))->addr)
                            break;
                    } /* end if */
                }     /* end for */
                /* Must be a continuation message that points to chunk containing null message */
                assert(v < oh->nmesgs);
                assert(cont_msg);
                assert(((H5O_cont_t *)(cont_msg->native))->chunkno == null_msg->chunkno);

                /* Initialize information about null message */
                null_msg_no     = u;
                deleted_chunkno = null_msg->chunkno;

                /* Convert continuation message into a null message */
                if (H5O__release_mesg(f, oh, cont_msg, true) < 0)
                    HGOTO_ERROR(H5E_OHDR, H5E_CANTDELETE, FAIL, "unable to convert into null message");

                /*
                 * Remove chunk from object header's data structure
                 */

                /* Free memory for chunk image */
                oh->chunk[null_msg->chunkno].image =
                    H5FL_BLK_FREE(chunk_image, oh->chunk[null_msg->chunkno].image);

                /* Remove chunk from list of chunks */
                if (null_msg->chunkno < (oh->nchunks - 1)) {
                    memmove(&oh->chunk[null_msg->chunkno], &oh->chunk[null_msg->chunkno + 1],
                            ((oh->nchunks - 1) - null_msg->chunkno) * sizeof(H5O_chunk_t));

                    /* Adjust chunk number for any chunk proxies that are in the cache */
                    for (u = null_msg->chunkno; u < (oh->nchunks - 1); u++) {
                        unsigned chk_proxy_status = 0; /* Metadata cache status of chunk proxy for chunk */

                        /* Check the chunk proxy's status in the metadata cache */
                        if (H5AC_get_entry_status(f, oh->chunk[u].addr, &chk_proxy_status) < 0)
                            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL,
                                        "unable to check metadata cache status for chunk proxy");

                        /* If the entry is in the cache, update its chunk index */
                        if (chk_proxy_status & H5AC_ES__IN_CACHE) {
                            if (H5O__chunk_update_idx(f, oh, u) < 0)
                                HGOTO_ERROR(H5E_OHDR, H5E_CANTSET, FAIL,
                                            "unable to update index for chunk proxy");
                        } /* end if */
                    }     /* end for */
                }         /* end if */

                /* Decrement # of chunks */
                /* (Don't bother reducing size of chunk array for now -QAK) */
                oh->nchunks--;

                /*
                 * Delete null message (in empty chunk that was be freed) from list of messages
                 */

                /* Release any information/memory for message */
                H5O__msg_free_mesg(null_msg);

                /* Remove null message from list of messages */
                if (null_msg_no < (oh->nmesgs - 1))
                    memmove(&oh->mesg[null_msg_no], &oh->mesg[null_msg_no + 1],
                            ((oh->nmesgs - 1) - null_msg_no) * sizeof(H5O_mesg_t));

                /* Decrement # of messages */
                /* (Don't bother reducing size of message array for now -QAK) */
                oh->nmesgs--;

                /* Adjust chunk # for messages in chunks after deleted chunk */
                for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++) {
                    /* Sanity check - there should be no messages in deleted chunk */
                    assert(curr_msg->chunkno != deleted_chunkno);

                    /* Adjust chunk index for messages in later chunks */
                    if (curr_msg->chunkno > deleted_chunkno)
                        curr_msg->chunkno--;

                    /* Check for continuation message */
                    if (H5O_CONT_ID == curr_msg->type->id) {
                        /* Decode current continuation message if necessary */
                        H5O_LOAD_NATIVE(f, 0, oh, curr_msg, FAIL)

                        /* Check if the chunkno needs to be set */
                        /* (should only occur when the continuation message is first decoded) */
                        if (0 == ((H5O_cont_t *)(curr_msg->native))->chunkno) {
                            unsigned w; /* Local index variable */

                            /* Find chunk that this continuation message points to */
                            for (w = 0; w < oh->nchunks; w++)
                                if (oh->chunk[w].addr == ((H5O_cont_t *)(curr_msg->native))->addr) {
                                    ((H5O_cont_t *)(curr_msg->native))->chunkno = w;
                                    break;
                                } /* end if */
                            assert(((H5O_cont_t *)(curr_msg->native))->chunkno > 0);
                        } /* end if */
                        else {
                            /* Check for pointer to chunk after deleted chunk */
                            if (((H5O_cont_t *)(curr_msg->native))->chunkno > deleted_chunkno)
                                ((H5O_cont_t *)(curr_msg->native))->chunkno--;
                        } /* end else */
                    }     /* end if */
                }         /* end for */

                /* Found chunk to delete */
                deleted_chunk = true;
                break;
            } /* end if */
        }     /* end for */

        /* If we deleted any chunks, remember that */
        if (deleted_chunk)
            did_deleting = true;
    } while (deleted_chunk);

    /* Set return value */
    ret_value = (htri_t)did_deleting;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__remove_empty_chunks() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__condense_header
 *
 * Purpose:     Attempt to eliminate empty chunks from object header.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O__condense_header(H5F_t *f, H5O_t *oh)
{
    bool   rescan_header;       /* Whether to rescan header */
    htri_t result;              /* Result from packing/merging/etc */
    herr_t ret_value = SUCCEED; /* return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(oh != NULL);

    /* Loop until no changed to the object header messages & chunks */
    do {
        /* Reset 'rescan chunks' flag */
        rescan_header = false;

        /* Scan for messages that can be moved earlier in chunks */
        result = H5O__move_msgs_forward(f, oh);
        if (result < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTPACK, FAIL, "can't move header messages forward");
        if (result > 0)
            rescan_header = true;

        /* Scan for adjacent null messages & merge them */
        result = H5O__merge_null(f, oh);
        if (result < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTPACK, FAIL, "can't pack null header messages");
        if (result > 0)
            rescan_header = true;

        /* Scan for empty chunks to remove */
        result = H5O__remove_empty_chunks(f, oh);
        if (result < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTPACK, FAIL, "can't remove empty chunk");
        if (result > 0)
            rescan_header = true;
    } while (rescan_header);
#ifdef H5O_DEBUG
    H5O__assert(oh);
#endif /* H5O_DEBUG */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__condense_header() */

/*-------------------------------------------------------------------------
 *
 * Function:    H5O__alloc_shrink_chunk
 *
 * Purpose:     Shrinks a chunk, removing all null messages and any gap.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O__alloc_shrink_chunk(H5F_t *f, H5O_t *oh, unsigned chunkno)
{
    H5O_chunk_t       *chunk     = &oh->chunk[chunkno];      /* Chunk to shrink */
    H5O_chunk_proxy_t *chk_proxy = NULL;                     /* Metadata cache proxy for chunk to shrink */
    H5O_mesg_t        *curr_msg;                             /* Current message to examine */
    uint8_t           *old_image = chunk->image;             /* Old address of chunk's image in memory */
    size_t             old_size  = chunk->size;              /* Old size of chunk */
    size_t             new_size  = chunk->size - chunk->gap; /* Size of shrunk chunk */
    size_t             total_msg_size;                       /* Size of the messages in this chunk */
    size_t             min_chunk_size    = H5O_ALIGN_OH(oh, H5O_MIN_SIZE); /* Minimum chunk size */
    size_t             sizeof_chksum     = H5O_SIZEOF_CHKSUM_OH(oh);       /* Size of chunk checksum */
    size_t             sizeof_msghdr     = H5O_SIZEOF_MSGHDR_OH(oh);       /* Size of message header */
    uint8_t            new_size_flags    = 0;                              /* New chunk #0 size flags */
    bool               adjust_size_flags = false; /* Whether to adjust the chunk #0 size flags */
    size_t             less_prfx_size    = 0;     /* Bytes removed from object header prefix */
    size_t             u;                         /* Index */
    herr_t             ret_value = SUCCEED;       /* Return value */

    FUNC_ENTER_PACKAGE

    /* check args */
    assert(f);
    assert(oh);

    /* Protect chunk */
    if (NULL == (chk_proxy = H5O__chunk_protect(f, oh, chunkno)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to protect object header chunk");

    /* Loop backwards to increase the chance of seeing more null messages at the
     * end of the chunk.  Note that we rely on unsigned u wrapping around at the
     * end.
     */
    for (u = oh->nmesgs - 1, curr_msg = &oh->mesg[u]; u < oh->nmesgs; u--, curr_msg--) {
        if ((H5O_NULL_ID == curr_msg->type->id) && (chunkno == curr_msg->chunkno)) {
            size_t shrink_size = curr_msg->raw_size + sizeof_msghdr; /* Amount to shrink the chunk by */

            /* If the current message is not at the end of the chunk, copy the
             * data after it (except the checksum).
             */
            if (curr_msg->raw + curr_msg->raw_size < old_image + new_size - sizeof_chksum) {
                unsigned    v; /* Index */
                H5O_mesg_t *curr_msg2;
                uint8_t    *src = curr_msg->raw + curr_msg->raw_size; /* Source location */

                /* Slide down the raw data */
                memmove(curr_msg->raw - sizeof_msghdr, src,
                        (size_t)(old_image + new_size - sizeof_chksum - src));

                /* Update the raw data pointers for messages after this one */
                for (v = 0, curr_msg2 = &oh->mesg[0]; v < oh->nmesgs; v++, curr_msg2++)
                    if ((chunkno == curr_msg2->chunkno) && (curr_msg2->raw > curr_msg->raw))
                        curr_msg2->raw -= shrink_size;
            } /* end if */

            /* Adjust the new chunk size */
            new_size -= shrink_size;

            /* Release any information/memory for the message */
            H5O__msg_free_mesg(curr_msg);

            /* Remove the deleted null message from list of messages */
            if (u < (oh->nmesgs - 1))
                memmove(&oh->mesg[u], &oh->mesg[u + 1], ((oh->nmesgs - 1) - u) * sizeof(H5O_mesg_t));

            /* Decrement # of messages */
            /* (Don't bother reducing size of message array for now) */
            oh->nmesgs--;
        } /* end if */
    }     /* end for */

    /* Check if the chunk is too small, extend if necessary */
    total_msg_size = new_size - (size_t)(chunkno == 0 ? H5O_SIZEOF_HDR(oh) : H5O_SIZEOF_CHKHDR_OH(oh));
    if (total_msg_size < min_chunk_size) {
        assert(oh->alloc_nmesgs > oh->nmesgs);
        oh->nmesgs++;

        /* Initialize new null message to make the chunk large enough */
        curr_msg         = &oh->mesg[oh->nmesgs - 1];
        curr_msg->type   = H5O_MSG_NULL;
        curr_msg->dirty  = true;
        curr_msg->native = NULL;
        curr_msg->raw    = old_image + new_size + sizeof_msghdr - sizeof_chksum;
        curr_msg->raw_size =
            MAX(H5O_ALIGN_OH(oh, min_chunk_size - total_msg_size), sizeof_msghdr) - sizeof_msghdr;
        curr_msg->chunkno = chunkno;

        /* update the new chunk size */
        new_size += curr_msg->raw_size + sizeof_msghdr;
    } /* end if */

    /* Check for changing the chunk #0 data size enough to need adjusting the flags */
    if (oh->version > H5O_VERSION_1 && chunkno == 0) {
        uint64_t chunk0_newsize = new_size - (size_t)H5O_SIZEOF_HDR(oh); /* New size of chunk 0's data */
        size_t   orig_prfx_size = (size_t)1 << (oh->flags & H5O_HDR_CHUNK0_SIZE); /* Original prefix size */

        /* Check for moving to a 1-byte size encoding */
        if (orig_prfx_size > 1 && chunk0_newsize <= 255) {
            less_prfx_size    = orig_prfx_size - 1;
            new_size_flags    = H5O_HDR_CHUNK0_1;
            adjust_size_flags = true;
        } /* end if */
        /* Check for moving to a 2-byte size encoding */
        else if (orig_prfx_size > 2 && chunk0_newsize <= 65535) {
            less_prfx_size    = orig_prfx_size - 2;
            new_size_flags    = H5O_HDR_CHUNK0_2;
            adjust_size_flags = true;
        } /* end if */
        /* Check for moving to a 4-byte size encoding */
        else if (orig_prfx_size > 4 && chunk0_newsize <= 4294967295) {
            less_prfx_size    = orig_prfx_size - 4;
            new_size_flags    = H5O_HDR_CHUNK0_4;
            adjust_size_flags = true;
        } /* end if */
    }     /* end if */

    if (adjust_size_flags) {
        /* Adjust object header prefix flags */
        oh->flags = (uint8_t)(oh->flags & ~H5O_HDR_CHUNK0_SIZE);
        oh->flags |= new_size_flags;

        /* Slide chunk 0 data down */
        memmove(chunk->image + H5O_SIZEOF_HDR(oh) - sizeof_chksum,
                chunk->image + H5O_SIZEOF_HDR(oh) - sizeof_chksum + less_prfx_size,
                new_size - (size_t)H5O_SIZEOF_HDR(oh));

        /* Adjust chunk size */
        new_size -= less_prfx_size;
    } /* end if */

    /* Allocate less memory space for chunk's image */
    chunk->size  = new_size;
    chunk->image = H5FL_BLK_REALLOC(chunk_image, old_image, chunk->size);
    chunk->gap   = 0;
    if (NULL == oh->chunk[chunkno].image)
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed");

    /* Spin through existing messages, adjusting them */
    for (u = 0, curr_msg = &oh->mesg[0]; u < oh->nmesgs; u++, curr_msg++) {
        if (adjust_size_flags || (chunk->image != old_image))
            /* Adjust raw addresses for messages in this chunk to reflect new 'image' address */
            if (curr_msg->chunkno == chunkno)
                curr_msg->raw = chunk->image - less_prfx_size + (curr_msg->raw - old_image);

        /* Find continuation message which points to this chunk and adjust chunk's size */
        /* (Chunk 0 doesn't have a continuation message that points to it and
         * its size is directly encoded in the object header) */
        if (chunkno > 0 && (H5O_CONT_ID == curr_msg->type->id) &&
            (((H5O_cont_t *)(curr_msg->native))->chunkno == chunkno)) {
            H5O_chunk_proxy_t *cont_chk_proxy; /* Chunk that message is in */

            /* Protect chunk */
            if (NULL == (cont_chk_proxy = H5O__chunk_protect(f, oh, curr_msg->chunkno)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTPROTECT, FAIL, "unable to protect object header chunk");

            /* Adjust size of continuation message */
            assert(((H5O_cont_t *)(curr_msg->native))->size == old_size);
            ((H5O_cont_t *)(curr_msg->native))->size = chunk->size;

            /* Flag continuation message as dirty */
            curr_msg->dirty = true;

            /* Release chunk, marking it dirty */
            if (H5O__chunk_unprotect(f, cont_chk_proxy, true) < 0)
                HGOTO_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");
        } /* end if */
    }     /* end for */

    assert(new_size <= old_size);

    /* Resize the chunk in the cache */
    if (H5O__chunk_resize(oh, chk_proxy) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTRESIZE, FAIL, "unable to resize object header chunk");

    /* Free the unused space in the file */
    if (H5MF_xfree(f, H5FD_MEM_OHDR, chunk->addr + new_size, (hsize_t)(old_size - new_size)) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTFREE, FAIL, "unable to shrink object header chunk");

done:
    /* Release chunk, marking it dirty */
    if (chk_proxy && H5O__chunk_unprotect(f, chk_proxy, true) < 0)
        HDONE_ERROR(H5E_OHDR, H5E_CANTUNPROTECT, FAIL, "unable to unprotect object header chunk");

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O__alloc_shrink_chunk() */