summaryrefslogtreecommitdiffstats
path: root/src/H5Oshared.c
blob: 001b094f53a20f5b1d2b4dd7e6109d0d5d686250 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:	Robb Matzke <matzke@llnl.gov>
 *		Wednesday, April  1, 1998
 *
 * Purpose:	Functions that operate on a shared message.  The shared
 *		message doesn't ever actually appear in the object header as
 *		a normal message.  Instead, if a message is shared, the
 *		H5O_FLAG_SHARED bit is set and the message body is that
 *		defined here for H5O_SHARED.  The message ID is the ID of the
 *		pointed-to message and the pointed-to message is stored in
 *		the global heap.
 */

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


#include "H5private.h"		/* Generic Functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Fpkg.h"             /* File access				*/
#include "H5FLprivate.h"	/* Free lists                           */
#include "H5Gprivate.h"		/* Groups				*/
#include "H5HFprivate.h"        /* Fractal heap				*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5Opkg.h"             /* Object headers			*/
#include "H5SMprivate.h"        /* Shared object header messages        */

static void *H5O_shared_decode(H5F_t*, hid_t dxpl_id, unsigned mesg_flags, const uint8_t*);
static herr_t H5O_shared_encode(H5F_t*, uint8_t*, const void*);
static void *H5O_shared_copy(const void *_mesg, void *_dest);
static size_t H5O_shared_size(const H5F_t*, const void *_mesg);
static herr_t H5O_shared_delete(H5F_t *f, hid_t dxpl_id, const void *_mesg,
    hbool_t adj_link);
static herr_t H5O_shared_link(H5F_t *f, hid_t dxpl_id, const void *_mesg);
static herr_t H5O_shared_pre_copy_file(H5F_t *file_src, const H5O_msg_class_t *type,
    const void *mesg_src, hbool_t *deleted, const H5O_copy_t *cpy_info, void *_udata);
static void *H5O_shared_copy_file(H5F_t *file_src, const H5O_msg_class_t *mesg_type,
    void *native_src, H5F_t *file_dst, hid_t dxpl_id, H5O_copy_t *cpy_info, void *udata);
static herr_t H5O_shared_debug(H5F_t*, hid_t dxpl_id, const void*, FILE*, int, int);

/* This message derives from H5O message class */
const H5O_msg_class_t H5O_MSG_SHARED[1] = {{
    H5O_SHARED_ID,	    	/*message id number			*/
    "shared",		    	/*message name for debugging		*/
    sizeof(H5O_shared_t), 	/*native message size			*/
    H5O_shared_decode,	    	/*decode method				*/
    H5O_shared_encode,	    	/*encode method				*/
    H5O_shared_copy,		/*copy the native value			*/
    H5O_shared_size,	    	/*size method				*/
    NULL,		    	/*no reset method			*/
    NULL,		        /*no free method			*/
    H5O_shared_delete,		/*file delete method			*/
    H5O_shared_link,		/*link method				*/
    NULL,			/*get share method			*/
    NULL, 			/*set share method			*/
    NULL,		    	/*can share method		*/
    NULL, 			/*is shared method		*/
    H5O_shared_pre_copy_file,	/* pre copy native value to file */
    H5O_shared_copy_file,	/* copy native value to file    */
    NULL,			/* post copy native value to file    */
    NULL,			/* get creation index		*/
    NULL,			/* set creation index		*/
    H5O_shared_debug	    	/*debug method				*/
}};

/* First version, with full symbol table entry as link for object header sharing */
#define H5O_SHARED_VERSION_1	1

/* Older version, with just address of object as link for object header sharing */
#define H5O_SHARED_VERSION_2	2

/* Newest version, which recognizes messages that are stored in the heap */
#define H5O_SHARED_VERSION_3	3
#define H5O_SHARED_VERSION_LATEST	H5O_SHARED_VERSION_3

/* Size of stack buffer for serialized messages */
#define H5O_MESG_BUF_SIZE               128

/* Declare a free list to manage the serialized message information */
H5FL_BLK_DEFINE(ser_mesg);


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_read
 *
 * Purpose:	Reads a message referred to by a shared message.
 *
 * Return:	Success:	Ptr to message in native format.  The message
 *				should be freed by calling H5O_msg_reset().  If
 *				MESG is a null pointer then the caller should
 *				also call H5MM_xfree() on the return value
 *				after calling H5O_msg_reset().
 *
 *		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Sep 24 2003
 *
 *-------------------------------------------------------------------------
 */
void *
H5O_shared_read(H5F_t *f, hid_t dxpl_id, const H5O_shared_t *shared,
    const H5O_msg_class_t *type, void *mesg)
{
    H5HF_t *fheap = NULL;
    uint8_t mesg_buf[H5O_MESG_BUF_SIZE]; /* Buffer for deserializing messages */
    uint8_t *buf = NULL;        /* Pointer to raw message in heap */
    void *native_mesg = NULL;   /* Used for messages shared in heap */
    void *ret_value = NULL;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_read)

    /* check args */
    HDassert(f);
    HDassert(shared);
    HDassert(type);
    HDassert(type->set_share);

    /* This message could have a heap ID (SOHM) or the address of an object
     * header on disk (named datatype)
     */
    HDassert(shared->flags != H5O_NOT_SHARED);

    /* Check for implicit shared object header message */
    if(shared->flags & H5O_SHARED_IN_HEAP_FLAG) {
        haddr_t fheap_addr;
        size_t buf_size;

        /* Retrieve the fractal heap address for shared messages */
        if(H5SM_get_fheap_addr(f, dxpl_id, type->id, &fheap_addr) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, NULL, "can't get fheap address for shared messages")

        /* Open the fractal heap */
        if(NULL == (fheap = H5HF_open(f, dxpl_id, fheap_addr)))
            HGOTO_ERROR(H5E_OHDR, H5E_CANTOPENOBJ, NULL, "unable to open fractal heap")

        /* Get the size of the message in the heap */
        if(H5HF_get_obj_len(fheap, dxpl_id, &(shared->u.heap_id), &buf_size) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, NULL, "can't get message size from fractal heap.")

        /* Allocate space for serialized message, if necessary */
        if(buf_size > sizeof(mesg_buf)) {
            if(NULL == (buf = H5FL_BLK_MALLOC(ser_mesg, buf_size)))
                HGOTO_ERROR(H5E_OHDR, H5E_NOSPACE, NULL, "memory allocation failed")
        } /* end if */
        else
            buf = mesg_buf;

        /* Retrieve the message from the heap */
        if(H5HF_read(fheap, dxpl_id, &(shared->u.heap_id), buf) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "can't read message from fractal heap.")

        /* Decode the message */
        if(NULL == (native_mesg = H5O_msg_decode(f, dxpl_id, type->id, buf)))
            HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, NULL, "can't decode shared message.")

        /* Check if there is a user buffer to fill */
        if(mesg) {
            /* Copy this message to the user's buffer */
            if(NULL == (ret_value = (type->copy)(native_mesg, mesg)))
                HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "unable to copy message to user space")
        } /* end if */
        else {
            /* Otherwise, take ownership of the decoded native message */
            ret_value = native_mesg;
            native_mesg = NULL;
        } /* end else */
    } /* end if */
    else {
        HDassert(shared->flags & H5O_COMMITTED_FLAG);

        /* Get the shared message from an object header */
        if(NULL == (ret_value = H5O_msg_read(&(shared->u.oloc), type->id, 0, mesg, dxpl_id)))
            HGOTO_ERROR(H5E_OHDR, H5E_READERROR, NULL, "unable to read message")
    } /* end else */

    /* Mark the message as shared */
    if(type->set_share && (type->set_share)(ret_value, shared) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "unable to set sharing information")

done:
    /* Release resources */
    if(buf && buf != mesg_buf)
        H5FL_BLK_FREE(ser_mesg, buf);
    if(native_mesg)
        H5O_msg_free(type->id, native_mesg);
    if(fheap && H5HF_close(fheap, dxpl_id) < 0)
        HDONE_ERROR(H5E_HEAP, H5E_CANTFREE, NULL, "can't close fractal heap")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_shared_read() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_link_adj
 *
 * Purpose:	Changes the link count for the object referenced by a shared
 *              message.
 *
 *              This function changes the object header link count and is
 *              only relevant for committed messages.  Messages shared in
 *              the heap are re-shared each time they're written, so their
 *              reference count is stored in the file-wide shared message
 *              index and is changed in a different place in the code.
 *
 * Return:	Success:	New link count, or 1 for messages in heap
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Sep 26 2003
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
H5O_shared_link_adj(H5F_t *f, hid_t dxpl_id, const H5O_shared_t *shared, int adjust)
{
    int ret_value;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_link_adj)

    /* check args */
    HDassert(f);
    HDassert(shared);

    /*
     * The shared message is stored in some other object header.
     * The other object header must be in the same file as the
     * new object header. Adjust the reference count on that
     * object header.
     */
    if(shared->flags & H5O_COMMITTED_FLAG)
    {
        if(shared->u.oloc.file->shared != f->shared)
            HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "interfile hard links are not allowed")
        if((ret_value = H5O_link(&(shared->u.oloc), adjust, dxpl_id)) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared object link count")
    }
    else
    {
        /* Messages in the heap don't have file object ref counts; they
         * return 1 as a dummy value.
         */
        HDassert(shared->flags & H5O_SHARED_IN_HEAP_FLAG);
        ret_value = 1;
    }

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


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_decode_new
 *
 * Purpose:	Decodes a shared object message
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *              Monday, January 22, 2007
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_shared_decode_new(H5F_t *f, const uint8_t *buf, H5O_shared_t *sh_mesg)
{
    unsigned version;           /* Shared message version */
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_decode_new)

    /* Check args */
    HDassert(f);
    HDassert(buf);
    HDassert(sh_mesg);

    /* Version */
    version = *buf++;
    if(version < H5O_SHARED_VERSION_1 || version > H5O_SHARED_VERSION_LATEST)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, FAIL, "bad version number for shared object message")

    /* Get the shared information flags
     * Flags are unused before version 3.
     */
    if(version >= H5O_SHARED_VERSION_2)
        sh_mesg->flags = *buf++;
    else {
        sh_mesg->flags = H5O_COMMITTED_FLAG;
        buf++;
    } /* end else */

    /* Skip reserved bytes (for version 1) */
    if(version == H5O_SHARED_VERSION_1)
        buf += 6;

    /* Body */
    if(version == H5O_SHARED_VERSION_1)
        H5G_obj_ent_decode(f, &buf, &(sh_mesg->u.oloc));
    else if (version >= H5O_SHARED_VERSION_2) {
        /* If this message is in the heap, copy a heap ID.
         * Otherwise, it is a named datatype, so copy an H5O_loc_t.
         */
        if(sh_mesg->flags & H5O_SHARED_IN_HEAP_FLAG) {
            HDassert(version >= H5O_SHARED_VERSION_3 );
            HDmemcpy(&(sh_mesg->u.heap_id), buf, sizeof(sh_mesg->u.heap_id));
        }
        else {
            /* The H5O_COMMITTED_FLAG should be set if this message
             * is from an older version before the flag existed.
             */
            if(version < H5O_SHARED_VERSION_3)
                sh_mesg->flags = H5O_COMMITTED_FLAG;

            HDassert(sh_mesg->flags & H5O_COMMITTED_FLAG);

            H5F_addr_decode(f, &buf, &(sh_mesg->u.oloc.addr));
            sh_mesg->u.oloc.file = f;
        } /* end else */
    } /* end else if */

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


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_decode
 *
 * Purpose:	Decodes a shared object message and returns it.
 *
 * Return:	Success:	Ptr to a new shared object message.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 *-------------------------------------------------------------------------
 */
static void *
H5O_shared_decode(H5F_t *f, hid_t UNUSED dxpl_id, unsigned UNUSED mesg_flags,
    const uint8_t *buf)
{
    H5O_shared_t	*mesg = NULL;
    unsigned		version;
    void                *ret_value;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_decode)

    /* Check args */
    HDassert(f);
    HDassert(buf);

    /* Decode */
    if(NULL == (mesg = H5MM_calloc(sizeof(H5O_shared_t))))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Version */
    version = *buf++;
    if(version < H5O_SHARED_VERSION_1 || version > H5O_SHARED_VERSION_LATEST)
        HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "bad version number for shared object message")

    /* Get the shared information flags
     * Flags are unused before version 3.
     */
    if(version >= H5O_SHARED_VERSION_2)
        mesg->flags = *buf++;
    else {
        mesg->flags = H5O_COMMITTED_FLAG;
        buf++;
    } /* end else */

    /* Skip reserved bytes (for version 1) */
    if(version == H5O_SHARED_VERSION_1)
        buf += 6;

    /* Body */
    if(version == H5O_SHARED_VERSION_1)
        H5G_obj_ent_decode(f, &buf, &(mesg->u.oloc));
    else if (version >= H5O_SHARED_VERSION_2) {
        /* If this message is in the heap, copy a heap ID.
         * Otherwise, it is a named datatype, so copy an H5O_loc_t.
         */
        if(mesg->flags & H5O_SHARED_IN_HEAP_FLAG) {
            HDassert(version >= H5O_SHARED_VERSION_3 );
            HDmemcpy(&(mesg->u.heap_id), buf, sizeof(mesg->u.heap_id));
        }
        else {
            /* The H5O_COMMITTED_FLAG should be set if this message
             * is from an older version before the flag existed.
             */
            if(version < H5O_SHARED_VERSION_3)
                mesg->flags = H5O_COMMITTED_FLAG;

            HDassert(mesg->flags & H5O_COMMITTED_FLAG);

            H5F_addr_decode(f, &buf, &(mesg->u.oloc.addr));
            mesg->u.oloc.file = f;
        } /* end else */
    } /* end else if */

    /* Set return value */
    ret_value = mesg;

done:
    if(ret_value == NULL)
        if(mesg != NULL)
            H5MM_xfree(mesg);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_shared_decode() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_encode_new
 *
 * Purpose:	Encodes message _MESG into buffer BUF.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_shared_encode_new(const H5F_t *f, uint8_t *buf/*out*/, const H5O_shared_t *sh_mesg)
{
    unsigned    version;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_shared_encode_new)

    /* Check args */
    HDassert(f);
    HDassert(buf);
    HDassert(sh_mesg);

    /* If this message is shared in the heap, we need to use version 3 of the
     * encoding and encode the SHARED_IN_HEAP flag.
     */
    if(sh_mesg->flags & H5O_SHARED_IN_HEAP_FLAG || H5F_USE_LATEST_FORMAT(f))
        version = H5O_SHARED_VERSION_LATEST;
    else {
        HDassert(sh_mesg->flags & H5O_COMMITTED_FLAG);
        version = H5O_SHARED_VERSION_2; /* version 1 is no longer used */
    } /* end else */

    *buf++ = version;
    *buf++ = (unsigned)sh_mesg->flags;

    /* Encode either the heap ID of the message or the address of the
     * object header that holds it.
     */
    if(sh_mesg->flags & H5O_SHARED_IN_HEAP_FLAG)
        HDmemcpy(buf, &(sh_mesg->u.heap_id), sizeof(sh_mesg->u.heap_id));
    else
        H5F_addr_encode(f, &buf, sh_mesg->u.oloc.addr);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_shared_encode_new() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_encode
 *
 * Purpose:	Encodes message _MESG into buffer BUF.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 * Modifications:
 *	Robb Matzke, 1998-07-20
 *	Added a version number to the beginning of the message.
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_shared_encode(H5F_t *f, uint8_t *buf/*out*/, const void *_mesg)
{
    const H5O_shared_t	*mesg = (const H5O_shared_t *)_mesg;
    unsigned    version;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_shared_encode)

    /* Check args */
    HDassert(f);
    HDassert(buf);
    HDassert(mesg);

    /* If this message is shared in the heap, we need to use version 3 of the
     * encoding and encode the SHARED_IN_HEAP flag.
     */
    if(mesg->flags & H5O_SHARED_IN_HEAP_FLAG || H5F_USE_LATEST_FORMAT(f))
        version = H5O_SHARED_VERSION_LATEST;
    else {
        HDassert(mesg->flags & H5O_COMMITTED_FLAG);
        version = H5O_SHARED_VERSION_2; /* version 1 is no longer used */
    } /* end else */

    *buf++ = version;
    *buf++ = (unsigned)mesg->flags;

    /* Encode either the heap ID of the message or the address of the
     * object header that holds it.
     */
    if(mesg->flags & H5O_SHARED_IN_HEAP_FLAG)
        HDmemcpy(buf, &(mesg->u.heap_id), sizeof(mesg->u.heap_id));
    else
        H5F_addr_encode(f, &buf, mesg->u.oloc.addr);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_shared_encode() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_copy
 *
 * Purpose:	Copies a message from _MESG to _DEST, allocating _DEST if
 *		necessary.
 *
 * Return:	Success:	Ptr to _DEST
 *
 *		Failure:	NULL
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		Sep 26 2003
 *
 *-------------------------------------------------------------------------
 */
static void *
H5O_shared_copy(const void *_mesg, void *_dest)
{
    const H5O_shared_t  *mesg = (const H5O_shared_t *) _mesg;
    H5O_shared_t	*dest = (H5O_shared_t *) _dest;
    void        *ret_value;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_copy)

    /* check args */
    HDassert(mesg);
    if(!dest && NULL == (dest = H5MM_malloc(sizeof(H5O_shared_t))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* copy */
    dest->flags = mesg->flags;
    if(mesg->flags & H5O_COMMITTED_FLAG)
        H5O_loc_copy(&(dest->u.oloc), &(mesg->u.oloc), H5_COPY_DEEP);
    else if(mesg->flags & H5O_SHARED_IN_HEAP_FLAG)
        dest->u.heap_id = mesg->u.heap_id;
    else
        /* This message's sharing information is being reset */
        HDassert(mesg->flags == H5O_NOT_SHARED);

    /* Set return value */
    ret_value = dest;

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


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_size_new
 *
 * Purpose:	Returns the length of a shared object message.
 *
 * Return:	Success:	Length
 *		Failure:	0
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 *-------------------------------------------------------------------------
 */
size_t
H5O_shared_size_new(const H5F_t *f, const H5O_shared_t *sh_mesg)
{
    size_t ret_value;           /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_shared_size_new)

    if(sh_mesg->flags & H5O_COMMITTED_FLAG) {
        ret_value = 1 +			/*version			*/
            1 +				/*the flags field		*/
            H5F_SIZEOF_ADDR(f);		/*sharing by another obj hdr	*/
    } /* end if */
    else {
        HDassert(sh_mesg->flags & H5O_SHARED_IN_HEAP_FLAG);
        ret_value = 1 +			/*version			*/
            1 +				/*the flags field	*/
            H5O_FHEAP_ID_LEN;		/* Shared in the heap   */
    } /* end else */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_shared_size_new() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_size
 *
 * Purpose:	Returns the length of a shared object message.
 *
 * Return:	Success:	Length
 *
 *		Failure:	0
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 *-------------------------------------------------------------------------
 */
static size_t
H5O_shared_size(const H5F_t *f, const void *_mesg)
{
    const H5O_shared_t *shared = (const H5O_shared_t *)_mesg;
    size_t ret_value;           /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_shared_size)

    if(shared->flags & H5O_COMMITTED_FLAG) {
        ret_value = 1 +			/*version			*/
            1 +				/*the flags field		*/
            H5F_SIZEOF_ADDR(f);		/*sharing by another obj hdr	*/
    } /* end if */
    else {
        HDassert(shared->flags & H5O_SHARED_IN_HEAP_FLAG);
        ret_value = 1 +			/*version			*/
            1 +				/*the flags field	*/
            H5O_FHEAP_ID_LEN;		/* Shared in the heap   */
    } /* end else */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_shared_size() */


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_delete_new
 *
 * Purpose:     Free file space referenced by message
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              Friday, September 26, 2003
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_shared_delete_new(H5F_t *f, hid_t dxpl_id, const H5O_shared_t *sh_mesg, hbool_t adj_link)
{
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_delete_new)

    /* check args */
    HDassert(f);
    HDassert(sh_mesg);

    /* 
     * Committed datatypes increment the OH of the original message when they
     * are written (in H5O_shared_link) and decrement it here.
     * SOHMs in the heap behave differently; their refcount is incremented
     * during H5SM_share when they are going to be written (in H5O_msg_append
     * or H5O_msg_write). Their refcount in the SOHM indexes still needs to
     * be decremented when they're deleted (in H5O_shared_link_adj).
     */

    /* Decrement the reference count on the shared object, if requested */
    if(adj_link)
        if(H5O_shared_link_adj(f, dxpl_id, sh_mesg, -1) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared object link count")

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


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_delete
 *
 * Purpose:     Free file space referenced by message
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              Friday, September 26, 2003
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_shared_delete(H5F_t *f, hid_t dxpl_id, const void *_mesg, hbool_t adj_link)
{
    const H5O_shared_t       *shared = (const H5O_shared_t *) _mesg;
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_delete)

    /* check args */
    HDassert(f);
    HDassert(shared);

    /* 
     * Committed datatypes increment the OH of the original message when they
     * are written (in H5O_shared_link) and decrement it here.
     * SOHMs in the heap behave differently; their refcount is incremented
     * during H5SM_share when they are going to be written (in H5O_msg_append
     * or H5O_msg_write). Their refcount in the SOHM indexes still needs to
     * be decremented when they're deleted (in H5O_shared_link_adj).
     */

    /* Decrement the reference count on the shared object, if requested */
    if(adj_link)
        if(H5O_shared_link_adj(f, dxpl_id, shared, -1) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared object link count")

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


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_link_new
 *
 * Purpose:     Increment reference count on any objects referenced by
 *              message
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              Friday, September 26, 2003
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5O_shared_link_new(H5F_t *f, hid_t dxpl_id, const H5O_shared_t *sh_mesg)
{
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_link_new)

    /* check args */
    HDassert(f);
    HDassert(sh_mesg);

    /* Increment the reference count on the shared object */
    if(H5O_shared_link_adj(f, dxpl_id, sh_mesg, 1) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared object link count")

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


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_link
 *
 * Purpose:     Increment reference count on any objects referenced by
 *              message
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:  Quincey Koziol
 *              Friday, September 26, 2003
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_shared_link(H5F_t *f, hid_t dxpl_id, const void *_mesg)
{
    const H5O_shared_t       *shared = (const H5O_shared_t *) _mesg;
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_link)

    /* check args */
    HDassert(f);
    HDassert(shared);

    /* Increment the reference count on the shared object */
    if(H5O_shared_link_adj(f, dxpl_id, shared, 1) < 0)
        HGOTO_ERROR(H5E_OHDR, H5E_LINKCOUNT, FAIL, "unable to adjust shared object link count")

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


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_pre_copy_file
 *
 * Purpose:     Perform any necessary actions before copying message between
 *              files for shared messages.
 *
 * Return:      Success:        Non-negative
 *
 *              Failure:        Negative
 *
 * Programmer:  Peter Cao
 *              Saturday, February 11, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_shared_pre_copy_file(H5F_t *file_src, const H5O_msg_class_t *type,
    const void *native_src, hbool_t *deleted, const H5O_copy_t *cpy_info,
    void *udata)
{
    const H5O_shared_t   *shared_src = (const H5O_shared_t *)native_src;
    void           *mesg_native = NULL;
    herr_t         ret_value = SUCCEED;          /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_pre_copy_file)

    /* check args */
    HDassert(file_src);
    HDassert(type);

    if(type->pre_copy_file) {
        /* Go get the actual shared message */
        if(NULL == (mesg_native = H5O_shared_read(file_src, H5AC_dxpl_id, shared_src, type, NULL)))
            HGOTO_ERROR(H5E_OHDR, H5E_READERROR, FAIL, "unable to load object header")

        /* Perform "pre copy" operation on messge */
        if((type->pre_copy_file)(file_src, type, mesg_native, deleted, cpy_info, udata) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "unable to perform 'pre copy' operation on message")
    } /* end of if */

done:
    if(mesg_native)
        H5O_msg_free_real(type, mesg_native);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_shared_pre_copy_file() */


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_copy_file_new
 *
 * Purpose:     Copies a message from _MESG to _DEST in file
 *
 * Return:      Success:        Ptr to _DEST
 *              Failure:        NULL
 *
 * Programmer:  Quincey Koziol
 *              November 1, 2005
 *
 *-------------------------------------------------------------------------
 */
void *
H5O_shared_copy_file_new(H5F_t *file_src, const H5O_msg_class_t *mesg_type,
    const H5O_shared_t *shared_src, H5F_t *file_dst, hid_t dxpl_id, H5O_copy_t *cpy_info,
    void *udata)
{
    H5O_shared_t        *shared_dst = NULL;     /* The destination message if
                                                 * it is a shared message */
    void                *dst_mesg = NULL;       /* The destination message if
                                                 * it's an unshared message */
    void                *ret_value;             /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_copy_file_new)

    /* check args */
    HDassert(shared_src);
    HDassert(file_dst);
    HDassert(cpy_info);

HDfprintf(stderr, "%s: Copying shared messages not supported yet!\n", FUNC);
HGOTO_ERROR(H5E_OHDR, H5E_UNSUPPORTED, NULL, "copying shared messages not supported yet")

    /* Committed shared messages create a shared message at the destination
     * and also copy the committed object that they point to.
     * SOHMs actually write a non-shared message at the destination.
     */
    if(shared_src->flags & H5O_COMMITTED_FLAG) {
        /* Allocate space for the destination message */
        if(NULL == (shared_dst = H5MM_malloc(sizeof(H5O_shared_t))))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

        /* Reset group entry for new object */
        H5O_loc_reset(&(shared_dst->u.oloc));
        shared_dst->u.oloc.file = file_dst;
    
        /* Set flags for new shared object */
        shared_dst->flags = shared_src->flags;

        /* Copy the shared object from source to destination */
        if(H5O_copy_header_map(&(shared_src->u.oloc), &(shared_dst->u.oloc), dxpl_id, cpy_info, FALSE) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, NULL, "unable to copy object")

        /* Set return value */
        ret_value = shared_dst;
    } /* end if */
    else {
        HDassert(shared_src->flags & H5O_SHARED_IN_HEAP_FLAG);

        /* Read the shared message to get the original message */
        if(NULL == (dst_mesg = H5O_shared_read(file_src, dxpl_id, shared_src, mesg_type, NULL)))
            HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, NULL, "unable to read shared message")

        if(mesg_type->copy_file) {
            /* Copy the original, un-shared message and return it */
            ret_value = (mesg_type->copy_file)(file_src, mesg_type, dst_mesg, file_dst, dxpl_id, cpy_info, udata);
            H5O_msg_free(mesg_type->id, dst_mesg);
        } /* end else */
        else
            ret_value = dst_mesg;
    } /* end else */

done:
    if(!ret_value) {
        if(shared_dst)
            H5O_msg_free(H5O_SHARED_ID, shared_dst);
        if(dst_mesg)
            H5O_msg_free(mesg_type->id, dst_mesg);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O_shared_copy_file_new() */


/*-------------------------------------------------------------------------
 * Function:    H5O_shared_copy_file
 *
 * Purpose:     Copies a message from _MESG to _DEST in file
 *
 * Return:      Success:        Ptr to _DEST
 *
 *              Failure:        NULL
 *
 * Programmer:  Quincey Koziol
 *              November 1, 2005
 *
 *-------------------------------------------------------------------------
 */
static void *
H5O_shared_copy_file(H5F_t *file_src, const H5O_msg_class_t *mesg_type,
    void *native_src, H5F_t *file_dst, hid_t dxpl_id, H5O_copy_t *cpy_info,
    void *udata)
{
    H5O_shared_t        *shared_src = (H5O_shared_t *)native_src;
    H5O_shared_t        *shared_dst = NULL;     /* The destination message if
                                                 * it is a shared message */
    void                *dst_mesg = NULL;       /* The destination message if
                                                 * it's an unshared message */
    void                *ret_value;             /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5O_shared_copy_file)

    /* check args */
    HDassert(shared_src);
    HDassert(file_dst);
    HDassert(cpy_info);

    /* Committed shared messages create a shared message at the destination
     * and also copy the committed object that they point to.
     * SOHMs actually write a non-shared message at the destination.
     */
    if(shared_src->flags & H5O_COMMITTED_FLAG) {
        /* Allocate space for the destination message */
        if(NULL == (shared_dst = H5MM_malloc(sizeof(H5O_shared_t))))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

        /* Reset group entry for new object */
        H5O_loc_reset(&(shared_dst->u.oloc));
        shared_dst->u.oloc.file = file_dst;
    
        /* Set flags for new shared object */
        shared_dst->flags = shared_src->flags;

        /* Copy the shared object from source to destination */
        if(H5O_copy_header_map(&(shared_src->u.oloc), &(shared_dst->u.oloc), dxpl_id, cpy_info, FALSE) < 0)
            HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, NULL, "unable to copy object")

        /* Set return value */
        ret_value = shared_dst;
    } /* end if */
    else {
        HDassert(shared_src->flags & H5O_SHARED_IN_HEAP_FLAG);

        /* Read the shared message to get the original message */
        if(NULL == (dst_mesg = H5O_shared_read(file_src, dxpl_id, shared_src, mesg_type, NULL)))
            HGOTO_ERROR(H5E_OHDR, H5E_BADMESG, NULL, "unable to read shared message")

        if(mesg_type->copy_file) {
            /* Copy the original, un-shared message and return it */
            ret_value = (mesg_type->copy_file)(file_src, mesg_type, dst_mesg, file_dst, dxpl_id, cpy_info, udata);
            H5O_msg_free(mesg_type->id, dst_mesg);
        } /* end else */
        else
            ret_value = dst_mesg;
    } /* end else */

done:
    if(!ret_value) {
        if(shared_dst)
            H5O_msg_free(H5O_SHARED_ID, shared_dst);
        if(dst_mesg)
            H5O_msg_free(mesg_type->id, dst_mesg);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5O_shared_copy_file() */


/*-------------------------------------------------------------------------
 * Function:	H5O_shared_debug
 *
 * Purpose:	Prints debugging info for the message
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Robb Matzke
 *              Thursday, April  2, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5O_shared_debug (H5F_t UNUSED *f, hid_t UNUSED dxpl_id, const void *_mesg,
		  FILE *stream, int indent, int fwidth)
{
    const H5O_shared_t	*mesg = (const H5O_shared_t *)_mesg;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5O_shared_debug)

    /* Check args */
    HDassert(f);
    HDassert(mesg);
    HDassert(stream);
    HDassert(indent >= 0);
    HDassert(fwidth >= 0);

    if(mesg->flags & H5O_COMMITTED_FLAG)
    {
        HDfprintf(stream, "%*s%-*s %s\n", indent, "", fwidth,
                "Sharing method:",
                "Obj Hdr");
        HDfprintf(stream, "%*s%-*s %a\n", indent, "", fwidth,
                "Object address:",
                mesg->u.oloc.addr);
    }
    else
    {
        HDfprintf(stream, "%*s%-*s %s\n", indent, "", fwidth,
                "Sharing method:",
                "SOHM Heap");
        HDfprintf(stream, "%*s%-*s %a\n", indent, "", fwidth,
                "Heap ID:",
                mesg->u.heap_id);
    }

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5O_shared_debug() */