summaryrefslogtreecommitdiffstats
path: root/src/H5O.c
blob: 9130de36ad55f732d95131528e3330ac7cd3c11f (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
/*-------------------------------------------------------------------------
 * Copyright (C) 1997	National Center for Supercomputing Applications.
 *                      All rights reserved.
 *
 *-------------------------------------------------------------------------
 *
 * Created:		H5O.c
 * 			Aug  5 1997
 * 			Robb Matzke <matzke@llnl.gov>
 *
 * Purpose:		Object header virtual functions.
 *
 * Modifications:	
 *
 *-------------------------------------------------------------------------
 */
#include <H5private.h>
#include <H5ACprivate.h>
#include <H5Eprivate.h>
#include <H5Fprivate.h>
#include <H5MFprivate.h>
#include <H5MMprivate.h>
#include <H5Oprivate.h>

#define PABLO_MASK	H5O_mask

/* PRIVATE PROTOTYPES */
static herr_t H5O_flush (H5F_t *f, hbool_t destroy, haddr_t addr, H5O_t *oh);
static H5O_t *H5O_load (H5F_t *f, haddr_t addr, void *_udata1, void *_udata2);
static intn H5O_find_in_ohdr (H5F_t *f, haddr_t addr,
			      const H5O_class_t **type_p, intn sequence);
static intn H5O_alloc (H5F_t *f, H5O_t *oh, const H5O_class_t *type,
		       size_t size);
static intn H5O_alloc_extend_chunk (H5O_t *oh, intn chunkno, size_t size);
static intn H5O_alloc_new_chunk (H5F_t *f, H5O_t *oh, size_t size);

/* H5O inherits cache-like properties from H5AC */
static const H5AC_class_t H5AC_OHDR[1] = {{
   (void*(*)(H5F_t*,haddr_t,void*,void*))H5O_load,
   (herr_t(*)(H5F_t*,hbool_t,haddr_t,void*))H5O_flush,
}};

/* Is the interface initialized? */
static intn interface_initialize_g = FALSE;

/* ID to type mapping */
static const H5O_class_t *const message_type_g[] = {
   H5O_NULL,      /*0x0000 Null 					*/
   H5O_SIM_DIM,   /*0x0001 Simple dimensionality			*/
   NULL,		/*0x0002 Data space (fiber bundle?)		*/
   H5O_SIM_DTYPE, /*0x0003 Simple data type				*/
   NULL,		/*0x0004 Compound data type			*/
   H5O_STD_STORE, /*0x0005 Data storage -- standard object		*/
   NULL,		/*0x0006 Data storage -- compact object		*/
   NULL,		/*0x0007 Data storage -- external object	*/
   H5O_ISTORE, 	  /*0x0008 Data storage -- indexed object		*/
   NULL, 		/*0x0009 Not assigned				*/
   NULL,		/*0x000A Not assigned				*/
   NULL,		/*0x000B Data storage -- compressed object	*/
   NULL,		/*0x000C Attribute list				*/
   H5O_NAME,		/*0x000D Object name				*/
   NULL,		/*0x000E Object modification date and time	*/
   NULL,		/*0x000F Shared header message			*/
   H5O_CONT,		/*0x0010 Object header continuation		*/
   H5O_STAB,		/*0x0011 Symbol table				*/
};


/*-------------------------------------------------------------------------
 * Function:	H5O_new
 *
 * Purpose:	Creates a new object header, sets the link count
 *		to NLINK, and caches the header.
 *
 * Return:	Success:	Address of new header.
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  5 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
haddr_t
H5O_new (H5F_t *f, intn nlink, size_t size_hint)
{
   size_t	size;		/*total size of object header	*/
   haddr_t	addr = FAIL;	/*address of object header	*/
   H5O_t	*oh = NULL;

   FUNC_ENTER (H5O_new, NULL, FAIL);

   /* check args */
   assert (f);
   assert (nlink>=0);
   if (size_hint<H5O_MIN_SIZE) size_hint = H5O_MIN_SIZE;
   H5O_ALIGN (size_hint, H5O_ALIGNMENT);

   /* allocate disk space for header and first chunk */
   size = H5O_SIZEOF_HDR(f) + size_hint;
   if ((addr = H5MF_alloc (f, size))<0) {
      HRETURN_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL);
   }

   /* allocate the object header and fill in header fields */
   oh = H5MM_xcalloc (1, sizeof(H5O_t));
   oh->dirty = TRUE;
   oh->version = H5O_VERSION;
   oh->alignment = H5O_ALIGNMENT;
   oh->nlink = nlink;

   /* create the chunk list and initialize the first chunk */
   oh->nchunks = 1;
   oh->alloc_nchunks = H5O_NCHUNKS;
   oh->chunk = H5MM_xmalloc (oh->alloc_nchunks * sizeof (H5O_chunk_t));
   
   oh->chunk[0].dirty = TRUE;
   oh->chunk[0].addr = addr + H5O_SIZEOF_HDR(f);
   oh->chunk[0].size = size_hint;
   oh->chunk[0].image = H5MM_xcalloc (1, size_hint);

   /* create the message list and initialize the first message */
   oh->nmesgs = 1;
   oh->alloc_nmesgs = H5O_NMESGS;
   oh->mesg = H5MM_xcalloc (oh->alloc_nmesgs, sizeof(H5O_mesg_t));

   oh->mesg[0].type = H5O_NULL;
   oh->mesg[0].dirty = TRUE;
   oh->mesg[0].native = NULL;
   oh->mesg[0].raw = oh->chunk[0].image + 4; /*skip id and size fields */
   oh->mesg[0].raw_size = size_hint - 4;
   oh->mesg[0].chunkno = 0;

   /* cache it */
   if (H5AC_set (f, H5AC_OHDR, addr, oh)<0) {
      H5MM_xfree (oh);
      HRETURN_ERROR (H5E_OHDR, H5E_CANTINIT, FAIL);
   }

   FUNC_LEAVE (addr);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_load
 *
 * Purpose:	Loads an object header from disk.
 *
 * Return:	Success:	Pointer to the new object header.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  5 1997
 *
 * Modifications:
 *
 * 	Robb Matzke, 30 Aug 1997
 *	Plugged memory leaks that occur during error handling.
 *
 *-------------------------------------------------------------------------
 */
static H5O_t *
H5O_load (H5F_t *f, haddr_t addr, void *_udata1, void *_udata2)
{
   H5O_t	*oh = NULL;
   H5O_t	*ret_value = (void*)1; /*kludge for HGOTO_ERROR*/
   uint8	buf[16], *p;
   size_t	hdr_size, mesg_size;
   uintn	id;
   intn		mesgno, chunkno, curmesg=0, nmesgs;
   haddr_t	chunk_addr;
   size_t	chunk_size;
   H5O_cont_t	*cont=NULL;

   FUNC_ENTER (H5O_load, NULL, NULL);

   /* check args */
   assert (f);
   assert (addr>=0);
   assert (!_udata1);
   assert (!_udata2);

   /* allocate ohdr and init chunk list */
   oh = H5MM_xcalloc (1, sizeof(H5O_t));

   /* read fixed-lenth part of object header */
   hdr_size = H5O_SIZEOF_HDR (f);
   if (H5F_block_read (f, addr, hdr_size, buf)<0) {
      HGOTO_ERROR (H5E_OHDR, H5E_READERROR, NULL);
   }
   p = buf;

   /* decode version */
   oh->version = *p++;
   if (H5O_VERSION!=oh->version) {
      HGOTO_ERROR (H5E_OHDR, H5E_VERSION, NULL);
   }

   /* decode alignment */
   oh->alignment = *p++;
   if (4!=oh->alignment) {
      HGOTO_ERROR (H5E_OHDR, H5E_ALIGNMENT, NULL);
   }

   /* decode number of messages */
   UINT16DECODE (p, nmesgs);

   /* decode link count */
   UINT32DECODE (p, oh->nlink);

   /* decode first chunk info */
   chunk_addr = addr + H5O_SIZEOF_HDR(f);
   UINT32DECODE (p, chunk_size);

   /* build the message array */
   oh->alloc_nmesgs = MAX (H5O_NMESGS, nmesgs);
   oh->mesg = H5MM_xcalloc (oh->alloc_nmesgs, sizeof(H5O_mesg_t));
      
   /* read each chunk from disk */
   while (chunk_addr) {

      /* increase chunk array size */
      if (oh->nchunks>=oh->alloc_nchunks) {
	 oh->alloc_nchunks += H5O_NCHUNKS;
	 oh->chunk = H5MM_xrealloc (oh->chunk,
				    oh->alloc_nchunks * sizeof(H5O_chunk_t));
      }

      /* read the chunk raw data */
      chunkno = oh->nchunks++;
      oh->chunk[chunkno].dirty = FALSE;
      oh->chunk[chunkno].addr = chunk_addr;
      oh->chunk[chunkno].size = chunk_size;
      oh->chunk[chunkno].image = H5MM_xmalloc (chunk_size);
      if (H5F_block_read (f, chunk_addr, chunk_size,
			  oh->chunk[chunkno].image)<0) {
	 HGOTO_ERROR (H5E_OHDR, H5E_READERROR, NULL);
      }
      

      /* load messages from this chunk */
      for (p = oh->chunk[chunkno].image;
	   p < oh->chunk[chunkno].image + chunk_size;
	   p += mesg_size) {
	 UINT16DECODE (p, id);
	 UINT16DECODE (p, mesg_size);
      
	 if (id>=NELMTS(message_type_g) || NULL==message_type_g[id]) {
	    HGOTO_ERROR (H5E_OHDR, H5E_BADMESG, NULL);
	 }
	 if (p + mesg_size > oh->chunk[chunkno].image + chunk_size) {
	    HGOTO_ERROR (H5E_OHDR, H5E_CANTINIT, NULL);
	 }

	 if (H5O_NULL_ID==id && oh->nmesgs>0 &&
	     H5O_NULL_ID==oh->mesg[oh->nmesgs-1].type->id &&
	     oh->mesg[oh->nmesgs-1].chunkno==chunkno) {
	    /* combine adjacent null messages */
	    mesgno = oh->nmesgs - 1;
	    oh->mesg[mesgno].raw_size += 4 + mesg_size;
	 } else {
	    /* new message */
	    if (oh->nmesgs>=nmesgs) {
	       HGOTO_ERROR (H5E_OHDR, H5E_CANTLOAD, NULL);
	    }
	    mesgno = oh->nmesgs++;
	    oh->mesg[mesgno].type = message_type_g[id];
	    oh->mesg[mesgno].dirty = FALSE;
	    oh->mesg[mesgno].native = NULL;
	    oh->mesg[mesgno].raw = p;
	    oh->mesg[mesgno].raw_size = mesg_size;
	    oh->mesg[mesgno].chunkno = chunkno;
	 }
      }
      assert (p == oh->chunk[chunkno].image + chunk_size);

      /* decode next object header continuation message */
      for (chunk_addr=0; 0==chunk_addr && curmesg<oh->nmesgs; curmesg++) {
	 if (H5O_CONT_ID==oh->mesg[curmesg].type->id) {
	    uint8 *p2 = oh->mesg[curmesg].raw;
	    cont = (H5O_CONT->decode)(f, oh->mesg[curmesg].raw_size, p2);
	    oh->mesg[curmesg].native = cont;
	    chunk_addr = cont->addr;
	    chunk_size = cont->size;
	    cont->chunkno = oh->nchunks; /*the next chunk to allocate*/
	 }
      }
   }

done:
   if (!ret_value && oh) {
      /*
       * Free resources.
       */
      int i;
      for (i=0; i<oh->nchunks; i++) {
	 oh->chunk[i].image = H5MM_xfree (oh->chunk[i].image);
      }
      oh->chunk = H5MM_xfree (oh->chunk);
      oh->mesg = H5MM_xfree (oh->mesg);
      oh = H5MM_xfree (oh);
   }

   FUNC_LEAVE (oh);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_flush
 *
 * Purpose:	Flushes (and destroys) an object header.
 *
 * Return:	Success:	SUCCESS
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  5 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_flush (H5F_t *f, hbool_t destroy, haddr_t addr, H5O_t *oh)
{
   uint8	buf[16], *p;
   int		i;
   H5O_cont_t	*cont = NULL;
   
   FUNC_ENTER (H5O_flush, NULL, FAIL);

   /* check args */
   assert (f);
   assert (addr>=0);
   assert (oh);
   
   /* flush */
   if (oh->dirty) {
      p = buf;

      /* encode version */
      *p++ = oh->version;

      /* encode alingment */
      *p++ = oh->alignment;

      /* encode number of messages */
      UINT16ENCODE (p, oh->nmesgs);

      /* encode link count */
      UINT32ENCODE (p, oh->nlink);

      /* encode body size */
      UINT32ENCODE (p, oh->chunk[0].size);

      /* write the object header header */
      if (H5F_block_write (f, addr, H5O_SIZEOF_HDR(f), buf)<0) {
	 HRETURN_ERROR (H5E_OHDR, H5E_WRITEERROR, FAIL);
      }

      /* encode messages */
      for (i=0; i<oh->nmesgs; i++) {
	 if (oh->mesg[i].dirty) {
	    p = oh->mesg[i].raw - 4;
	    UINT16ENCODE (p, oh->mesg[i].type->id);
	    UINT16ENCODE (p, oh->mesg[i].raw_size);
	    if (oh->mesg[i].native) {
	       assert (oh->mesg[i].type->encode);

	       /* allocate file space for chunks that have none yet */
	       if (H5O_CONT_ID==oh->mesg[i].type->id &&
		   ((H5O_cont_t*)(oh->mesg[i].native))->addr<0) {
		  cont = (H5O_cont_t*)(oh->mesg[i].native);
		  assert (cont->chunkno >= 0);
		  assert (cont->chunkno < oh->nchunks);
		  assert (oh->chunk[cont->chunkno].addr<0);
		  cont->size = oh->chunk[cont->chunkno].size;
		  cont->addr = H5MF_alloc (f, cont->size);
		  if (cont->addr<0) {
		     HRETURN_ERROR (H5E_RESOURCE, H5E_NOSPACE, FAIL);
		  }
		  oh->chunk[cont->chunkno].addr = cont->addr;
	       }

	       /* encode the message */
	       assert (oh->mesg[i].raw >=
		       oh->chunk[oh->mesg[i].chunkno].image);
	       assert (oh->mesg[i].raw + oh->mesg[i].raw_size <=
		       oh->chunk[oh->mesg[i].chunkno].image +
		       oh->chunk[oh->mesg[i].chunkno].size);
	       if ((oh->mesg[i].type->encode)(f, oh->mesg[i].raw_size,
					      oh->mesg[i].raw,
					      oh->mesg[i].native)<0) {
		  HRETURN_ERROR (H5E_OHDR, H5E_CANTENCODE, FAIL);
	       }
	    }
	    oh->mesg[i].dirty = FALSE;
	    oh->chunk[oh->mesg[i].chunkno].dirty = TRUE;
	 }
      }

      /* write each chunk to disk */
      for (i=0; i<oh->nchunks; i++) {
	 if (oh->chunk[i].dirty) {
	    assert (oh->chunk[i].addr>0);
	    if (H5F_block_write (f, oh->chunk[i].addr, oh->chunk[i].size,
				 oh->chunk[i].image)<0) {
	       HRETURN_ERROR (H5E_OHDR, H5E_WRITEERROR, FAIL);
	    }
	    oh->chunk[i].dirty = FALSE;
	 }
      }
      oh->dirty = FALSE;
   }


   if (destroy) {
      /* destroy chunks */
      for (i=0; i<oh->nchunks; i++) {
	 oh->chunk[i].image = H5MM_xfree (oh->chunk[i].image);
      }
      oh->chunk = H5MM_xfree (oh->chunk);

      /* destroy messages */
      for (i=0; i<oh->nmesgs; i++) {
	 H5O_reset (oh->mesg[i].type, oh->mesg[i].native);
	 oh->mesg[i].native = H5MM_xfree (oh->mesg[i].native);
      }
      oh->mesg = H5MM_xfree (oh->mesg);

      /* destroy object header */
      H5MM_xfree (oh);
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_reset
 *
 * Purpose:	Some message data structures have internal fields that
 *		need to be freed.  This function does that if appropriate
 *		but doesn't free NATIVE.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug 12 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_reset (const H5O_class_t *type, void *native)
{
   FUNC_ENTER (H5O_reset, NULL, FAIL);

   if (native) {
      if (type->reset) {
	 if ((type->reset)(native)<0) {
	    /* reset class method failed */
	    HRETURN_ERROR (H5E_OHDR, H5E_CANTINIT, FAIL);
	 }
      } else {
	 HDmemset (native, 0, type->native_size);
      }
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_link
 *
 * Purpose:	Adjust the link count for an object header by adding
 *		ADJUST to the link count.
 *
 * Return:	Success:       	New link count
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  5 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
intn
H5O_link (H5F_t *f, H5G_entry_t *ent, intn adjust)
{
   H5O_t	*oh = NULL;
   haddr_t	addr;
   
   FUNC_ENTER (H5O_link, NULL, FAIL);

   /* check args */
   assert (f);
   assert (ent && H5G_ent_addr (ent)>0);
   addr = H5G_ent_addr (ent);
   
   /* get header */
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, FAIL);
   }

   /* adjust link count */
   if (adjust<0) {
      if (oh->nlink + adjust < 0) {
	 HRETURN_ERROR (H5E_OHDR, H5E_LINKCOUNT, FAIL);
      }
      oh->nlink += adjust;
      if (1==oh->nlink && ent) {
	 fprintf (stderr, "H5O_link: no symbol table entry caching "
		  "(not implemented yet)\n");
      }
   } else {
      oh->nlink += adjust;
      if (oh->nlink>1) H5G_ent_invalidate (ent);
   }

   oh->dirty = TRUE;
   FUNC_LEAVE (oh->nlink);
}
   

/*-------------------------------------------------------------------------
 * Function:	H5O_read
 *
 * Purpose:	Reads a message from an object header and returns a pointer
 *		to it.  The caller will usually supply the memory through
 *		MESG and the return value will be MESG.  But if MESG is
 *		the null pointer, then this function will malloc() memory
 *		to hold the result and return its pointer instead.
 *
 * Return:	Success:	Ptr to message in native format.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void *
H5O_read (H5F_t *f, haddr_t addr, H5G_entry_t *ent,
	  const H5O_class_t *type, intn sequence, void *mesg)
{
   H5O_t	*oh = NULL;
   void		*retval = NULL;
   intn		idx;
   H5G_cache_t	*cache = NULL;
   H5G_type_t	cache_type;
   
   FUNC_ENTER (H5O_read, NULL, NULL);

   /* check args */
   assert (f);
   if (addr<=0 && !ent) {
      HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, NULL);
   }
   if (addr<=0 && (addr=H5G_ent_addr (ent))<=0) {
      HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, NULL);
   }
   assert (sequence>=0);

   /* can we get it from the symbol table? */
   if (ent) {
      cache = H5G_ent_cache (ent, &cache_type);
      if (type && cache_type==type->cache_type && type->fast) {
	 retval = (type->fast)(cache, mesg);
	 if (retval) HRETURN (retval);
	 H5ECLEAR;
      }
   }

   /* can we get it from the object header? */
   if ((idx = H5O_find_in_ohdr (f, addr, &type, sequence))<0) {
      HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, NULL);
   }

#ifdef LATER
   /* should we cache it in ENT? */
#endif

   /* copy the message to the user-supplied buffer */
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, NULL);
   }
   retval = (type->copy)(oh->mesg[idx].native, mesg);
   if (!retval) HRETURN_ERROR (H5E_OHDR, H5E_CANTINIT, NULL);

   FUNC_LEAVE (retval);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_find_in_ohdr
 *
 * Purpose:	Find a message in the object header without consulting
 *		a symbol table entry.
 *
 * Return:	Success:	Index number of message.
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static intn
H5O_find_in_ohdr (H5F_t *f, haddr_t addr, const H5O_class_t **type_p,
		  intn sequence)
{
   H5O_t	*oh = NULL;
   int		i;
   
   FUNC_ENTER (H5O_find_in_ohdr, NULL, FAIL);
   
   /* check args */
   assert (f);
   assert (addr>=0);
   assert (type_p);

   /* load the object header */
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, FAIL);
   }

   /* scan through the messages looking for the right one */
   for (i=0; i<oh->nmesgs; i++) {
      if (*type_p && (*type_p)->id!=oh->mesg[i].type->id) continue;
      if (--sequence<0) break;
   }
   if (sequence>=0) HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, FAIL);
   
   /* decode the message if necessary */
   if (NULL==oh->mesg[i].native) {
      assert (oh->mesg[i].type->decode);
      oh->mesg[i].native = (oh->mesg[i].type->decode)(f,
						      oh->mesg[i].raw_size,
						      oh->mesg[i].raw);
      if (NULL==oh->mesg[i].native) {
	 HRETURN_ERROR (H5E_OHDR, H5E_CANTDECODE, FAIL);
      }
   }

   /*return the message type */
   *type_p = oh->mesg[i].type;

   FUNC_LEAVE (i);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_peek
 *
 * Purpose:	Returns a pointer to a message stored in native format.
 *		The returned memory is read-only, and points directly into
 *		the cache.  It is therefore valid only until the next cache
 *		function is called.
 *
 * Return:	Success:	Ptr to read-only message in native format.
 *				The pointer is guranteed to be valid only
 *				until the next call (directly or indirectly)
 *				to an H5AC function.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
const void *
H5O_peek (H5F_t *f, haddr_t addr, const H5O_class_t *type, intn sequence)
{
   intn		idx;
   H5O_t	*oh = NULL;
   
   FUNC_ENTER (H5O_peek, NULL, NULL);

   /* check args */
   assert (f);
   assert (addr>0);

   if ((idx = H5O_find_in_ohdr (f, addr, &type, sequence))<0) {
      HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, NULL);
   }
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, NULL);
   }

   FUNC_LEAVE (oh->mesg[idx].native);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_modify
 *
 * Purpose:	Modifies an existing message or creates a new message.
 *		The object header is at file address ADDR of file F (but if
 *		ENT is present then its `header' field is used instead).  An
 *		optional symbol table entry ENT can be supplied in which
 *		case the cache fields in that symbol table are updated if
 *		appropriate.
 *
 * 		The OVERWRITE argument is either a sequence number of a
 *		message to overwrite (usually zero) or the constant
 *		H5O_NEW_MESSAGE (-1) to indicate that a new message is to
 *		be created.  If the message to overwrite doesn't exist then
 *		it is created (but only if it can be inserted so its sequence
 *		number is OVERWRITE; that is, you can create a message with
 *		the sequence number 5 if there is no message with sequence
 *		number 4).
 *
 * Return:	Success:	The sequence number of the message that
 *				was modified or created.
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
intn
H5O_modify (H5F_t *f, haddr_t addr, H5G_entry_t *ent,
	    const H5O_class_t *type, intn overwrite, const void *mesg)
{
   H5O_t	*oh = NULL;
   intn		idx, sequence;
   size_t	size;
   
   FUNC_ENTER (H5O_modify, NULL, FAIL);

   /* check args */
   assert (f);
   assert (addr>0 || (ent && H5G_ent_addr (ent)>0));
   assert (type);
   assert (mesg);
   if (addr<=0) addr = H5G_ent_addr (ent);
   
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, FAIL);
   }

   /* Count similar messages */
   for (idx=0,sequence=-1; idx<oh->nmesgs; idx++) {
      if (type->id != oh->mesg[idx].type->id) continue;
      if (++sequence==overwrite) break;
   }

   /* Was the right message found? */
   if (overwrite>=0 &&
       (idx>=oh->nmesgs || sequence!=overwrite)) {

      /* But can we insert a new one with this sequence number? */
      if (overwrite==sequence+1) {
	 overwrite = -1;
      } else {
	 HRETURN_ERROR (H5E_OHDR, H5E_NOTFOUND, FAIL); /*message not found*/
      }
   }

   /* Allocate space for the new message */
   if (overwrite<0) {
      size = (type->raw_size)(f, mesg);
      H5O_ALIGN (size, oh->alignment);
      idx = H5O_alloc (f, oh, type, size);
      if (idx<0) HRETURN_ERROR (H5E_OHDR, H5E_CANTINIT, FAIL);
      sequence++;
   }

   /* Copy the native value into the object header */
   oh->mesg[idx].native = (type->copy)(mesg, oh->mesg[idx].native);
   if (NULL==oh->mesg[idx].native) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTINIT, FAIL);
   }
   oh->mesg[idx].dirty = TRUE;
   oh->dirty = TRUE;

   /* Copy into the symbol table entry */
   if (oh->nlink<=1 && ent && type->cache) {
      H5G_type_t cache_type;
      H5G_cache_t *cache = H5G_ent_cache (ent, &cache_type);
      hbool_t modified = (type->cache)(&cache_type, cache, mesg);
      if (modified) H5G_ent_modified (ent, cache_type);
      
   }

   FUNC_LEAVE (sequence);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_remove
 *
 * Purpose:	Removes the specified message from the object header.
 *		If sequence is H5O_ALL (-1) then all messages of the
 *		specified type are removed.  Removing a message causes
 *		the sequence numbers to change for subsequent messages of
 *		the same type.
 *
 * 		If the messaage was cached in the symbol table entry then
 *		the type field of the symbol table entry is changed to
 *		H5G_NOTHING_CACHED and the ENT_MODIFIED argument will point
 *		to non-zero (the ENT_MODIFIED argument is unchanged if
 *		the ENT type field doesn't change).
 *
 * 		No attempt is made to join adjacent free areas of the
 *		object header into a single larger free area.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug 28 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_remove (H5F_t *f, haddr_t addr, H5G_entry_t *ent,
	    const H5O_class_t *type, intn sequence)
{
   H5O_t	*oh = NULL;
   intn		i, seq;
   
   FUNC_ENTER (H5O_remove, NULL, FAIL);

   /* check args */
   assert (f);
   assert (addr>0 || (ent && H5G_ent_addr (ent)>0));
   assert (type);
   if (addr<=0) addr = H5G_ent_addr (ent);

   /* load the object header */
   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, FAIL);
   }

   for (i=seq=0; i<oh->nmesgs; i++) {
      if (type->id != oh->mesg[i].type->id) continue;
      if (seq++ == sequence || H5O_ALL==sequence) {
	 H5G_type_t cache_type;
	 H5G_ent_cache (ent, &cache_type);

	 /* clear symbol table entry cache */
	 if (ent && type->cache && type->cache_type==cache_type) {
	    H5G_ent_invalidate (ent);
	 }

	 /* change message type to nil and zero it */
	 oh->mesg[i].type = H5O_NULL;
	 HDmemset (oh->mesg[i].raw, 0, oh->mesg[i].raw_size);
	 H5O_reset (type, oh->mesg[i].native);
	 oh->mesg[i].native = H5MM_xfree (oh->mesg[i].native);

	 oh->mesg[i].dirty = TRUE;
	 oh->dirty = TRUE;
      }
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_alloc_extend_chunk
 *
 * Purpose:	Extends a chunk which hasn't been allocated on disk yet
 *		to make the chunk large enough to contain a message whose
 *		data size is at least SIZE bytes.
 *
 * 		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.
 *
 * Return:	Success:	Message index for null message which
 *				is large enough to hold SIZE bytes.
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  7 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static intn
H5O_alloc_extend_chunk (H5O_t *oh, intn chunkno, size_t size)
{
   intn		idx, i;
   size_t	delta;
   uint8	*old_addr;

   FUNC_ENTER (H5O_alloc_extend_chunk, NULL, FAIL);

   /* check args */
   assert (oh);
   assert (chunkno>=0 && chunkno<oh->nchunks);
   assert (size>0);

   if (H5O_NO_ADDR!=oh->chunk[chunkno].addr) {
      HRETURN_ERROR (H5E_OHDR, H5E_NOSPACE, FAIL); /*chunk is on disk*/
   }

   /* try to extend a null message */
   for (idx=0; idx<oh->nmesgs; idx++) {
      if (H5O_NULL_ID==oh->mesg[idx].type->id &&
	  (oh->mesg[idx].raw + oh->mesg[idx].raw_size ==
	   oh->chunk[chunkno].image + oh->chunk[chunkno].size)) {
	 
	 delta = MAX (H5O_MIN_SIZE, size-oh->mesg[idx].raw_size);
	 H5O_ALIGN (delta, oh->alignment);
	 oh->mesg[idx].dirty = TRUE;
	 oh->mesg[idx].raw_size += delta;

	 old_addr = oh->chunk[chunkno].image;

	 /* Be careful not to indroduce garbage */
	 oh->chunk[chunkno].image = H5MM_xrealloc (old_addr,
						   (oh->chunk[chunkno].size +
						    delta));
	 HDmemset (oh->chunk[chunkno].image + oh->chunk[chunkno].size,
		   0, delta);
	 oh->chunk[chunkno].size += delta;

	 /* adjust raw addresses for messages of this chunk */
	 if (old_addr != oh->chunk[chunkno].image) {
	    for (i=0; i<oh->nmesgs; i++) {
	       if (oh->mesg[i].chunkno==chunkno) {
		  oh->mesg[i].raw = oh->chunk[chunkno].image +
				    (oh->mesg[i].raw - old_addr);
	       }
	    }
	 }
	 HRETURN (idx);
      }
   }

   /* create a new null message */
   if (oh->nmesgs >= oh->alloc_nmesgs) {
      oh->alloc_nmesgs += H5O_NMESGS;
      oh->mesg = H5MM_xrealloc (oh->mesg,
				oh->alloc_nmesgs * sizeof(H5O_mesg_t));
   }
   
   delta = MAX (H5O_MIN_SIZE, 4+size);
   H5O_ALIGN (delta, oh->alignment);
   idx = oh->nmesgs++;
   oh->mesg[idx].type = H5O_NULL;
   oh->mesg[idx].dirty = TRUE;
   oh->mesg[idx].native = NULL;
   oh->mesg[idx].raw = oh->chunk[chunkno].image + oh->chunk[chunkno].size + 4;
   oh->mesg[idx].raw_size = delta-4;
   oh->mesg[idx].chunkno = chunkno;

   old_addr = oh->chunk[chunkno].image;
   oh->chunk[chunkno].size += delta;
   oh->chunk[chunkno].image = H5MM_xrealloc (old_addr,
					     oh->chunk[chunkno].size);

   /* adjust raw addresses for messages of this chunk */
   if (old_addr != oh->chunk[chunkno].image) {
      for (i=0; i<oh->nmesgs; i++) {
	 if (oh->mesg[i].chunkno==chunkno) {
	    oh->mesg[i].raw = oh->chunk[chunkno].image +
			      (oh->mesg[i].raw - old_addr);
	 }
      }
   }

   FUNC_LEAVE (idx);
}


/*-------------------------------------------------------------------------
 * Function:	H5O_alloc_new_chunk
 *
 * Purpose:	Allocates a new chunk for the object header but doen't
 *		give the new chunk a file address yet.  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.
 *
 * 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:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  7 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static intn
H5O_alloc_new_chunk (H5F_t *f, H5O_t *oh, size_t size)
{
   size_t	cont_size;		/*continuation message size	*/
   intn		found_null=(-1);	/*best fit null message		*/
   intn		found_other=(-1);	/*best fit other message	*/
   intn		idx=FAIL;		/*message number return value	*/
   uint8	*p = NULL;		/*ptr into new chunk		*/
   H5O_cont_t	*cont = NULL;		/*native continuation message	*/
   intn		i, chunkno;
   
   FUNC_ENTER (H5O_alloc_new_chunk, NULL, FAIL);

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

   /*
    * Find the smallest null message that will hold an object
    * continuation message.  Failing that, 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.
    */
   cont_size = H5F_SIZEOF_OFFSET(f) + H5F_SIZEOF_SIZE(f);
   for (i=0; i<oh->nmesgs; i++) {
      if (H5O_NULL_ID == oh->mesg[i].type->id) {
	 if (cont_size == oh->mesg[i].raw_size) {
	    found_null = i;
	    break;
	 } else if (oh->mesg[i].raw_size >= cont_size &&
		    (found_null<0 ||
		     oh->mesg[i].raw_size < oh->mesg[found_null].raw_size)) {
	    found_null = i;
	 }
      } else if (H5O_CONT_ID == oh->mesg[i].type->id) {
	 /*don't consider continuation messages */
      } else if (oh->mesg[i].raw_size >= cont_size &&
		 (found_other<0 ||
		  oh->mesg[i].raw_size < oh->mesg[found_other].raw_size)) {
	 found_other = i;
      }
   }
   assert (found_null>=0 || found_other>=0);

   /*
    * 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.
    */
   if (found_null<0) size += 4 + oh->mesg[found_other].raw_size;

   /*
    * The total chunk size must include the requested space plus enough
    * for the message header.  This must be at least some minimum and a
    * multiple of the alignment size.
    */
   size = MAX (H5O_MIN_SIZE, size+4);
   H5O_ALIGN (size, oh->alignment);

   /*
    * Create the new chunk without giving it a file address.
    */
   if (oh->nchunks >= oh->alloc_nchunks) {
      oh->alloc_nchunks += H5O_NCHUNKS;
      oh->chunk = H5MM_xrealloc (oh->chunk,
				 oh->alloc_nchunks * sizeof(H5O_chunk_t));
   }
   chunkno = oh->nchunks++;
   oh->chunk[chunkno].dirty = TRUE;
   oh->chunk[chunkno].addr = H5O_NO_ADDR;
   oh->chunk[chunkno].size = size;
   oh->chunk[chunkno].image = p = H5MM_xcalloc (1, size);

   /*
    * Make sure we have enough space for all possible new messages
    * that could be generated below.
    */
   if (oh->nmesgs+3 > oh->alloc_nmesgs) {
      oh->alloc_nmesgs += MAX (H5O_NMESGS, 3);
      oh->mesg = H5MM_xrealloc (oh->mesg,
				oh->alloc_nmesgs * sizeof(H5O_mesg_t));
   }
			  
   /*
    * Describe the messages of the new chunk.
    */
   if (found_null<0) {
      found_null = i = oh->nmesgs++;
      oh->mesg[i].type = H5O_NULL;
      oh->mesg[i].dirty = TRUE;
      oh->mesg[i].native = NULL;
      oh->mesg[i].raw = oh->mesg[found_other].raw;
      oh->mesg[i].raw_size = oh->mesg[found_other].raw_size;
      oh->mesg[i].chunkno = oh->mesg[found_other].chunkno;

      oh->mesg[found_other].dirty = TRUE;
      oh->mesg[found_other].raw = p+4;
      oh->mesg[found_other].chunkno = chunkno;
      p += 4 + oh->mesg[found_other].raw_size;
      size -= 4 + oh->mesg[found_other].raw_size;
   }

   idx = oh->nmesgs++;
   oh->mesg[idx].type = H5O_NULL;
   oh->mesg[idx].dirty = TRUE;
   oh->mesg[idx].native = NULL;
   oh->mesg[idx].raw = p+4;
   oh->mesg[idx].raw_size = size-4;
   oh->mesg[idx].chunkno = chunkno;

   /*
    * If the null message that will receive the continuation message
    * is larger than the continuation message, then split it into
    * two null messages.
    */
   if (oh->mesg[found_null].raw_size > cont_size) {
      i = oh->nmesgs++;
      oh->mesg[i].type = H5O_NULL;
      oh->mesg[i].dirty = TRUE;
      oh->mesg[i].native = NULL;
      oh->mesg[i].raw = oh->mesg[found_null].raw + cont_size + 4;
      oh->mesg[i].raw_size = oh->mesg[found_null].raw_size - (cont_size+4);
      oh->mesg[i].chunkno = oh->mesg[found_null].chunkno;

      oh->mesg[found_null].dirty = TRUE;
      oh->mesg[found_null].raw_size = cont_size;
   }

   /*
    * Initialize the continuation message.
    */
   oh->mesg[found_null].type = H5O_CONT;
   oh->mesg[found_null].dirty = TRUE;
   cont = H5MM_xcalloc (1, sizeof(H5O_cont_t));
   cont->addr = H5O_NO_ADDR;
   cont->size = 0;
   cont->chunkno = chunkno;
   oh->mesg[found_null].native = cont;


   FUNC_LEAVE (idx);
}
   

/*-------------------------------------------------------------------------
 * Function:	H5O_alloc
 *
 * Purpose:	Allocate enough space in the object header for this message.
 *
 * Return:	Success:	Index of message
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static intn
H5O_alloc (H5F_t *f, H5O_t *oh, const H5O_class_t *type, size_t size)
{
   intn		chunkno;
   intn		idx;
   intn		null_idx;
   
   FUNC_ENTER (H5O_alloc, NULL, FAIL);

   /* check args */
   assert (oh);
   assert (type);
   H5O_ALIGN (size, oh->alignment);

   /* look for a null message which is large enough */
   for (idx=0; idx<oh->nmesgs; idx++) {
      if (H5O_NULL_ID==oh->mesg[idx].type->id &&
	  oh->mesg[idx].raw_size>=size) break;
   }

#ifdef LATER
   /*
    * Perhaps if we join adjacent null messages we could make one
    * large enough... we leave this as an exercise for future
    * programmers :-)  This isn't a high priority because when an
    * object header is read from disk the null messages are combined
    * anyway.
    */
#endif
   
   /* if we didn't find one, then allocate more header space */
   if (idx>=oh->nmesgs) {

      /*
       * Look for a chunk which hasn't had disk space allocated yet
       * since we can just increase the size of that chunk.
       */
      for (chunkno=0; chunkno<oh->nchunks; chunkno++) {
	 if ((idx=H5O_alloc_extend_chunk (oh, chunkno, size))>=0) break;
	 H5ECLEAR;
      }

      /*
       * Create a new chunk
       */
      if (idx<0) {
	 if ((idx=H5O_alloc_new_chunk (f, oh, size))<0) {
	    HRETURN_ERROR (H5E_OHDR, H5E_NOSPACE, FAIL);
	 }
      }
   }

   /* do we need to split the null message? */
   if (oh->mesg[idx].raw_size > size) {
      assert (oh->mesg[idx].raw_size - size >= 4); /*room for type & size */

      if (oh->nmesgs >= oh->alloc_nmesgs) {
	 oh->alloc_nmesgs += H5O_NMESGS;
	 oh->mesg = H5MM_xrealloc (oh->mesg,
				   oh->alloc_nmesgs * sizeof(H5O_mesg_t));
      }

      null_idx = oh->nmesgs++;
      oh->mesg[null_idx].type = H5O_NULL;
      oh->mesg[null_idx].dirty = TRUE;
      oh->mesg[null_idx].native = NULL;
      oh->mesg[null_idx].raw = oh->mesg[idx].raw + size + 4;
      oh->mesg[null_idx].raw_size = oh->mesg[idx].raw_size - (size + 4);
      oh->mesg[null_idx].chunkno = oh->mesg[idx].chunkno;
      oh->mesg[idx].raw_size = size;
   }

   /* initialize the new message */
   oh->mesg[idx].type = type;
   oh->mesg[idx].dirty = TRUE;
   oh->mesg[idx].native = NULL;
   
   oh->dirty = TRUE;
   FUNC_LEAVE (idx);
}
	 

/*-------------------------------------------------------------------------
 * Function:	H5O_debug
 *
 * Purpose:	Prints debugging info about an object header.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *		matzke@llnl.gov
 *		Aug  6 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_debug (H5F_t *f, haddr_t addr, FILE *stream, intn indent, intn fwidth)
{
   H5O_t	*oh = NULL;
   intn		i, chunkno;
   size_t	mesg_total=0, chunk_total=0;
   int		*sequence;
   
   FUNC_ENTER (H5O_debug, NULL, FAIL);

   /* check args */
   assert (f);
   assert (addr>=0);
   assert (stream);
   assert (indent>=0);
   assert (fwidth>=0);

   if (NULL==(oh=H5AC_find (f, H5AC_OHDR, addr, NULL, NULL))) {
      HRETURN_ERROR (H5E_OHDR, H5E_CANTLOAD, FAIL);
   }

   /* debug */
   fprintf (stream, "%*sObject Header...\n", indent, "");
   
   fprintf (stream, "%*s%-*s %d\n", indent, "", fwidth,
	    "Dirty:",
	    (int)(oh->dirty));
   fprintf (stream, "%*s%-*s %d\n", indent, "", fwidth,
	    "Version:",
	    (int)(oh->version));
   fprintf (stream, "%*s%-*s %d\n", indent, "", fwidth,
	    "Alignment:",
	    (int)(oh->alignment));
   fprintf (stream, "%*s%-*s %d\n", indent, "", fwidth,
	    "Number of links:",
	    (int)(oh->nlink));
   fprintf (stream, "%*s%-*s %d (%d)\n", indent, "", fwidth,
	    "Number of messages (allocated):",
	    (int)(oh->nmesgs), (int)(oh->alloc_nmesgs));
   fprintf (stream, "%*s%-*s %d (%d)\n", indent, "", fwidth,
	    "Number of chunks (allocated):",
	    (int)(oh->nchunks), (int)(oh->alloc_nchunks));

   /* debug each chunk */
   for (i=chunk_total=0; i<oh->nchunks; i++) {
      chunk_total += oh->chunk[i].size;
      fprintf (stream, "%*sChunk %d...\n", indent, "", i);
      
      fprintf (stream, "%*s%-*s %d\n", indent+3, "", MAX(0,fwidth-3),
	       "Dirty:",
	       (int)(oh->chunk[i].dirty));
      fprintf (stream, "%*s%-*s %lu\n", indent+3, "", MAX(0,fwidth-3),
	       "Address:",
	       (unsigned long)(oh->chunk[i].addr));
      if (0==i && oh->chunk[i].addr!=addr+H5O_SIZEOF_HDR(f)) {
	 fprintf (stream, "*** WRONG ADDRESS!\n");
      }
      fprintf (stream, "%*s%-*s %lu\n", indent+3, "", MAX(0,fwidth-3),
	       "Size in bytes:",
	       (unsigned long)(oh->chunk[i].size));
   }

   /* debug each message */
   sequence = H5MM_xcalloc (NELMTS(message_type_g), sizeof(int));
   for (i=mesg_total=0; i<oh->nmesgs; i++) {
      mesg_total += 4 + oh->mesg[i].raw_size;
      fprintf (stream, "%*sMessage %d...\n", indent, "", i);

      /* check for bad message id */
      if (oh->mesg[i].type->id<0 ||
	  oh->mesg[i].type->id>=NELMTS(message_type_g)) {
	 fprintf (stream, "*** BAD MESSAGE ID 0x%04x\n",
		  oh->mesg[i].type->id);
	 continue;
      }

      /* message name and size */
      fprintf (stream, "%*s%-*s 0x%04x %s(%d)\n",
	       indent+3, "", MAX (0, fwidth-3),
	       "Message ID:",
	       (unsigned)(oh->mesg[i].type->id),
	       oh->mesg[i].type->name,
	       sequence[oh->mesg[i].type->id]++);
      fprintf (stream, "%*s%-*s %lu\n", indent+3, "", MAX (0, fwidth-3),
	       "Raw size in bytes:",
	       (unsigned long)(oh->mesg[i].raw_size));

      fprintf (stream, "%*s%-*s %d\n", indent+3, "", MAX(0,fwidth-3),
	       "Chunk number:",
	       (int)(oh->mesg[i].chunkno));
      chunkno = oh->mesg[i].chunkno;
      if (chunkno<0 || chunkno>=oh->nchunks) {
	 fprintf (stream, "*** BAD CHUNK NUMBER\n");
      }

      /* check the size */
      if ((oh->mesg[i].raw + oh->mesg[i].raw_size >
	   oh->chunk[chunkno].image + oh->chunk[chunkno].size) ||
	  (oh->mesg[i].raw < oh->chunk[chunkno].image)) {
	 fprintf (stream, "*** BAD MESSAGE RAW ADDRESS\n");
      }
      
      /* decode the message */
      if (NULL==oh->mesg[i].native && oh->mesg[i].type->decode) {
	 oh->mesg[i].native = (oh->mesg[i].type->decode)(f,
							 oh->mesg[i].raw_size,
							 oh->mesg[i].raw);
      }

      /* print the message */
      if (oh->mesg[i].type->debug) {
	 (oh->mesg[i].type->debug)(f, oh->mesg[i].native, stream, indent+3,
				   MAX(0, fwidth-3));
      } else {
	 fprintf (stream, "%*sNo info for this message.\n", indent+3, "");
      }
   }
   sequence = H5MM_xfree (sequence);

   if (mesg_total != chunk_total) {
      fprintf (stream, "*** TOTAL SIZE DOES NOT MATCH ALLOCATED SIZE!\n");
   }

   FUNC_LEAVE (SUCCEED);
}