summaryrefslogtreecommitdiffstats
path: root/src/H5Gshad.c
blob: 861f7e54a8f0432be83abac15fed962b451504c1 (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
/*
 * Copyright (C) 1997 National Center for Supercomputing Applications.
 *                    All rights reserved.
 *
 * Programmer: Robb Matzke <matzke@llnl.gov>
 *             Thursday, September 18, 1997
 */
#define H5G_PACKAGE	/*suppress error message about including H5Gpkg.h*/

#include <H5private.h>			/*library			*/
#include <H5Eprivate.h>			/*error handling		*/
#include <H5Gpkg.h>			/*symbol tables			*/
#include <H5Hprivate.h>			/*heap functions		*/
#include <H5MMprivate.h>		/*memory management		*/
#include <H5Oprivate.h>			/*object header messages	*/

#define PABLO_MASK	H5G_shadow_mask

/* Interface initialization */
static hbool_t interface_initialize_g = FALSE;
#define INTERFACE_INIT	NULL

typedef struct H5G_hash_t {
   haddr_t		grp_addr;
   H5G_shadow_t		*head;
   struct H5G_hash_t	*next;
   struct H5G_hash_t	*prev;
} H5G_hash_t;


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_check
 *
 * Purpose:	Checks the shadow data structures for validity.  This is a
 *		debugging function only--it aborts on failure!
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Sunday, September 21, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
#ifdef H5G_DEBUG
static void
H5G_shadow_check (H5F_t *f)
{
   H5G_hash_t	*hash=NULL;
   H5G_shadow_t	*shadow=NULL, *prev_shadow=NULL;
   uintn	idx;
   hbool_t	shadow_error=FALSE;
   uintn	nerrors=0;
   static int	ncalls=0;

   if (0==ncalls++) {
      fprintf (stderr, "HDF5-DIAG: debugging group shadows (expensive)\n");
   }

   for (idx=0; idx<f->shared->nshadows; idx++) {
      for (hash=f->shared->shadow[idx]; hash; hash=hash->next) {
	 for (shadow=hash->head,prev_shadow=NULL;
	      shadow;
	      shadow=shadow->next) {
	    shadow_error = FALSE;
	    
	    /* Each shadow has a name and the names are in order */
	    if (!shadow->name) {
	       fprintf (stderr, "name=NULL, ");
	       shadow_error = TRUE;
	    }
	    if (prev_shadow && strcmp (prev_shadow->name, shadow->name)>=0) {
	       fprintf (stderr, "names not sorted, ");
	       shadow_error = TRUE;
	    }
	    
	    /*
	     * Valid group addresses.  The root (which is hashed to entry
	     * zero) always has an undefined group address.
	     */
	    if (idx==0 &&
		!H5F_addr_defined (&(shadow->grp_addr)) &&
		!H5F_addr_defined (&(hash->grp_addr))) {
	       /*
	        * The shadow for the root object always has an undefined
	        * group address.
	        */
	       
	    } else if (!H5F_addr_defined (&(shadow->grp_addr)) ||
		       !H5F_addr_defined (&(hash->grp_addr))) {
	       /*
	        * Non-root objects must have defined group addresses.
	        */
	       fprintf (stderr, "grp_addr=");
	       H5F_addr_print (stderr, &(shadow->grp_addr));
	       fprintf (stderr, ", hash_addr=");
	       H5F_addr_print (stderr, &(hash->grp_addr));
	       fprintf (stderr, ", ");
	       shadow_error = TRUE;
	       
	    } else if (H5F_addr_ne (&(shadow->grp_addr), &(hash->grp_addr))) {
	       /*
	        * Something's wrong with the data structure.  The hash
	        * address should always be the same as the shadow group
	        * address.
	        */
	       fprintf (stderr, "grp_addr=");
	       H5F_addr_print (stderr, &(shadow->grp_addr));
	       fprintf (stderr, " (should be ");
	       H5F_addr_print (stderr, &(hash->grp_addr));
	       fprintf (stderr, "), ");
	       shadow_error = TRUE;
	    }

	    /* Linked to symbol table entry */
	    if (shadow->main && shadow!=shadow->main->shadow) {
	       fprintf (stderr, "entry linkage problem, ");
	       shadow_error = TRUE;
	    }

	    /* Shadow linked list is consistent */
	    if (shadow->prev && prev_shadow!=shadow->prev) {
	       fprintf (stderr, "shadow linked list problem, ");
	       shadow_error = TRUE;
	    }
	    prev_shadow = shadow;

	    /* If an error occurred then print other info */
	    if (shadow_error) {
	       fprintf (stderr, "idx=%u, shadow=0x%08lx, grp_addr=",
			idx, (unsigned long)shadow);
	       H5F_addr_print (stderr, &(shadow->grp_addr));
	       fprintf (stderr, "\n");
	       nerrors++;
	    }
	 }
      }
   }
   if (nerrors) {
      fprintf (stderr, "Error in H5G_shadow_check, call %d\n",  ncalls);
      abort ();
   }
}
#endif


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_p
 *
 * Purpose:	Determines if ENT is a shadow or a real symbol table entry.
 *
 * Return:	Success:	Non-zero if ENT is a shadow; zero otherwise.
 *
 *		Failure:	FALSE
 *
 * Programmer:	Robb Matzke
 *              Thursday, September 18, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hbool_t
H5G_shadow_p (H5G_entry_t *ent)
{
   H5G_shadow_t		tmp;
   size_t 		delta = (char*)&(tmp.entry) - (char*)&tmp;
   hbool_t		ret_value = FALSE;
   
   FUNC_ENTER (H5G_shadow_p, FALSE);

   if (!ent || !ent->shadow) return FALSE;
   ret_value = ((char*)ent - (char*)(ent->shadow) == delta);

   FUNC_LEAVE (ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_dissociate
 *
 * Purpose:	Removes the association between a shadow and its entry or an
 *		entry and its shadow.  The ENT argument can be a shadow or a
 *		cached entry.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, September 18, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_dissociate (H5G_entry_t *ent)
{
   FUNC_ENTER (H5G_shadow_dissociate, FAIL);

   if (H5G_shadow_p (ent)) {
      if (ent->shadow->main) {
	 ent->shadow->main->shadow = NULL;
	 ent->shadow->main = NULL;
      }
   } else if (ent && ent->shadow) {
      ent->shadow->main = NULL;
      ent->shadow = NULL;
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_sync
 *
 * Purpose:	Synchronizes a shadow with an entry by copying the
 *		shadow contents to the entry if the shadow is dirty,
 *		and then clearing the shadow dirty bit.  You may call this
 *		function with either a shadow or a real entry.
 *
 *		If ENT is a shadow, then the shadow is synchronized only if
 *		the entry is currently cached.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, September 18, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_sync (H5G_entry_t *ent)
{
   H5G_shadow_t	*shadow = NULL;
   FUNC_ENTER (H5G_shadow_sync, FAIL);

   /*
    * If the caller supplied us with a shadow instead of the main entry, then
    * adjust the arguments.
    */
   if (H5G_shadow_p (ent)) {
      shadow = ent->shadow;
      ent = shadow->main;
   } else {
      shadow = ent->shadow;
   }

   if (shadow && shadow->entry.dirty) {
      if (!ent) {
	 HRETURN_ERROR (H5E_SYM, H5E_NOTCACHED, FAIL,
			"main entry is not cached");
      }
      *ent = shadow->entry;
      shadow->entry.dirty = FALSE;
   }
   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_list
 *
 * Purpose:	Returns a doubly linked list of shadows for the symbol
 *		table whose header address is GRP_ADDR.
 *
 * Return:	Success:	Ptr shadow list or null.
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Wednesday, September 17, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5G_shadow_t *
H5G_shadow_list (H5F_t *f, const haddr_t *grp_addr)
{
   uintn	idx = H5F_addr_hash (grp_addr, f->shared->nshadows);
   H5G_hash_t	*bucket = NULL;

   FUNC_ENTER (H5G_shadows, NULL);

   for (bucket=f->shared->shadow[idx]; bucket; bucket=bucket->next) {
      if (0==idx &&
	  !H5F_addr_defined (&(bucket->grp_addr)) &&
	  !H5F_addr_defined (grp_addr)) {
	 HRETURN (bucket->head);/*shadow list for root object*/
      } else if (H5F_addr_eq (&(bucket->grp_addr), grp_addr)) {
	 HRETURN (bucket->head);/*shadow list for other objects*/
      }
   }
   FUNC_LEAVE (NULL);
}
   

/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_assoc_node
 *
 * Purpose:	Given a new symbol table node and a symbol table header
 *		address, associate entries in that node with their shadow if
 *		they have one.
 *
 * 		SYM must be an uncached or protected symbol table node.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL, if an error occurs then none of the
 *				entries are associated with shadows.
 *
 * Programmer:	Robb Matzke
 *              Wednesday, September 17, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_assoc_node (H5F_t *f, H5G_node_t *sym, const H5G_ac_ud1_t *ac_udata)
{
   H5G_shadow_t *shadow = NULL;
   const char	*s = NULL;
   intn		i = 0;
   haddr_t	heap_addr;

   FUNC_ENTER (H5G_shadow_assoc_node, FAIL);

   /* Check arguments */
   assert (f);			/* The file			*/
   assert (sym);		/* The symbol table node	*/
   assert (ac_udata);		/* The symbol table header info	*/

#ifdef H5G_DEBUG
   H5G_shadow_check (f);
#endif

   if ((shadow=H5G_shadow_list (f, &(ac_udata->grp_addr)))) {
      heap_addr = ac_udata->heap_addr;

      while (i<sym->nsyms && shadow) {
	 /* Advance the Entry ptr until it gets to the next shadow. */
	 while (i<sym->nsyms &&
		(s=H5H_peek (f, &heap_addr, sym->entry[i].name_off)) &&
		strcmp (s, shadow->name)<0) i++;

	 /* Advance the Shadow ptr until it gets to the next entry. */
	 while (i<sym->nsyms && s && shadow &&
		strcmp (s,  shadow->name)>0) shadow = shadow->next;

	 /* Did we find a match? */
	 if (i<sym->nsyms && s && shadow && !strcmp (s,  shadow->name)) {
	    shadow->main = sym->entry + i;
	    sym->entry[i].shadow = shadow;
	    i++;
	    shadow = shadow->next;
	 }
      }
   }

   FUNC_LEAVE (SUCCEED);
}


/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_open
 *
 * Purpose:	Given a handle to an already open object, or given a
 *		pointer to the cached symbol table entry for that
 *		object, open the object (again) and return a handle
 *		to it.
 *
 *		If ENT refers to the root object, then GRP can be a null
 *		pointer or a pointer to an entry with an invalid header
 *		address.
 *
 * Return:	Success:	Handle to open object
 *
 *		Failure:	NULL
 *
 * Programmer:	Robb Matzke
 *              Thursday, September 18, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
H5G_entry_t *
H5G_shadow_open (H5F_t *f, H5G_entry_t *grp, H5G_entry_t *ent)
{
   H5G_shadow_t	*shadow = NULL;
   H5O_stab_t 	stab;
   const char 	*s = NULL;
   H5G_hash_t	*hash = NULL;
   H5G_shadow_t	*hash_ent = NULL, *prev_ent = NULL;
   uintn	idx;
   H5O_name_t	name_mesg = {NULL};
   H5G_entry_t	*ret_value = NULL;
   haddr_t	grp_header;
   
   FUNC_ENTER (H5G_shadow_open, NULL);

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

   if ((shadow = ent->shadow)) {
      /* Object is already open.  Open it again */
      shadow->nrefs += 1;
      HRETURN (&(shadow->entry));
   }

   /*
    * If the root object is being opened then the GRP argument is optional.
    * If it's supplied then it had better have an undefined header address.
    * For all other objects the GRP argument is required to have a valid
    * header address.
    */
   if (ent==f->shared->root_sym) {
      assert (!grp || !H5F_addr_defined (&(grp->header)));
      H5F_addr_undef (&grp_header);
      idx = 0;
   } else {
      assert (grp && H5F_addr_defined (&(grp->header)));
      grp_header = grp->header;
      idx = H5F_addr_hash (&grp_header, f->shared->nshadows);
   }
   
   /*
    * Build the new shadow.
    */
   shadow = H5MM_xcalloc (1, sizeof(H5G_shadow_t));
   ent->shadow = shadow;
   shadow->main = ent;
   shadow->nrefs = 1;
   shadow->entry = *ent;
   shadow->entry.dirty = FALSE;
   shadow->grp_addr = grp_header;
   
   /*
    * Give the shadow a name.  Obtaining the name might remove ENT from the
    * cache, so we're careful not to reference it again.
    */
   if (ent==f->shared->root_sym) {
      /*
       * We're opening the root entry.  Get the name from the name message or
       * use a generic default.
       */
      if (H5O_read (f, NO_ADDR, ent, H5O_NAME, 0, &name_mesg)) {
	 shadow->name = H5MM_xstrdup (name_mesg.s);
	 H5O_reset (H5O_NAME, &name_mesg);
      } else {
	 shadow->name = H5MM_xstrdup ("Root Object");
      }

   } else {
      /*
       * Some entry other than the root.
       */
      if (NULL==H5O_read (f, NO_ADDR, grp, H5O_STAB, 0, &stab)) {
	 HGOTO_ERROR (H5E_SYM, H5E_NOTFOUND, NULL,
		      "unable to read symbol table object header message");
      }
      if (NULL==(s=H5H_peek (f, &(stab.heap_addr), ent->name_off))) {
	 HGOTO_ERROR (H5E_SYM, H5E_NOTFOUND, NULL,
		      "unable to read symbol name");
      }
      shadow->name = H5MM_xstrdup (s);
   }
   ent = NULL; /*previous ops might have invalidated it*/
   

   /*
    * Link it into the shadow heap
    */
   for (hash=f->shared->shadow[idx]; hash; hash=hash->next) {
      if (0==idx &&
	  !H5F_addr_defined (&(hash->grp_addr)) &&
	  !H5F_addr_defined (&grp_header)) {
	 break; /*shadow list for root object*/
      } else if (H5F_addr_eq (&(hash->grp_addr), &grp_header)) {
	 break; /*shadow list for other objects*/
      }
   }
   if (!hash) {
      hash = H5MM_xcalloc (1, sizeof(H5G_hash_t));
      hash->grp_addr = grp_header;
      hash->next = f->shared->shadow[idx];
      f->shared->shadow[idx] = hash;
      if (hash->next) hash->next->prev = hash;
   }
   if (hash->head) {
      for (hash_ent=hash->head,prev_ent=NULL;
	   hash_ent;
	   hash_ent=hash_ent->next) {
	 if (strcmp (shadow->name, hash_ent->name)<0) break;
	 prev_ent = hash_ent;
      }
      if (hash_ent) {
	 /* Insert SHADOW before HASH_ENT */
	 if (hash_ent->prev) hash_ent->prev->next = shadow;
	 else hash->head = shadow;
	 shadow->prev = hash_ent->prev;
	 shadow->next = hash_ent;
	 hash_ent->prev = shadow;
      } else {
	 /* Append SHADOW to list */
	 assert (prev_ent && NULL==prev_ent->next);
	 prev_ent->next = shadow;
	 shadow->prev = prev_ent;
      }
   } else {
      /* Insert shadow at head of list */
      shadow->next = hash->head;
      if (hash->head) hash->head->prev = shadow;
      hash->head = shadow;
   }

#ifdef H5G_DEBUG
   H5G_shadow_check (f);
#endif

   ret_value = &(shadow->entry);

 done:
   if (!ret_value) {
      if (shadow) {
	 H5MM_xfree (shadow->name);
	 H5MM_xfree (shadow);
      }
   }
   
   FUNC_LEAVE (ret_value);
}



/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_close
 *
 * Purpose:	Closes an open object.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Thursday, September 18, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_close (H5F_t *f, H5G_entry_t *ent)
{
   uintn	idx;
   H5G_hash_t	*hash=NULL;
   H5G_shadow_t	*hash_ent=NULL, *shadow=NULL;
   
   FUNC_ENTER (H5G_shadow_close, FAIL);

   /* check args */
   assert (ent);
   assert (H5G_shadow_p (ent));
   assert (ent->shadow->nrefs>0);
   shadow = ent->shadow;

   /* clean the shadow */
   if (1==shadow->nrefs && ent->dirty) {
      if (!shadow->main &&
	  NULL==H5G_stab_find (f, &(shadow->grp_addr), NULL, shadow->name)) {
	 HRETURN_ERROR (H5E_SYM, H5E_NOTFOUND, FAIL,
			"unable to find shadow name in symbol table");
      }
      assert (shadow->main);
      *(shadow->main) = *ent;
      ent->dirty = FALSE;
   }

   /* close */
   shadow->nrefs -= 1;

   if (0==shadow->nrefs) {
      /* dissociate shadow and entry */
      H5G_shadow_dissociate (ent);

      /*
       * find symtabs shadow list -- if this is a shadow of the root object
       * then use zero for the hash value.  The root object always has an
       * undefined group address.
       */
      if (H5F_addr_defined (&(shadow->grp_addr))) {
	 idx = H5F_addr_hash (&(shadow->grp_addr), f->shared->nshadows);
      } else {
	 idx = 0;
      }
      for (hash=f->shared->shadow[idx]; hash; hash=hash->next) {
	 if (0==idx &&
	     !H5F_addr_defined (&(hash->grp_addr)) &&
	     !H5F_addr_defined (&(shadow->grp_addr))) {
	    break; /*shadow list for root object*/
	 } else if (H5F_addr_eq (&(hash->grp_addr), &(shadow->grp_addr))) {
	    break; /*shadow list for other objects*/
	 }
      }
      assert (hash);

      /* find shadow in shadow list */
      for (hash_ent=hash->head; hash_ent; hash_ent=hash_ent->next) {
	 if (hash_ent==shadow) break;
      }
      assert (hash_ent);

      /* remove shadow from shadow list */
      if (hash_ent->prev) hash_ent->prev->next = hash_ent->next;
      else hash->head = hash_ent->next;
      if (hash_ent->next) hash_ent->next->prev = hash_ent->prev;
      H5MM_xfree (shadow->name);
      H5MM_xfree (shadow);

      /* remove symtab's shadow list if empty */
      if (!hash->head) {
	 if (hash->prev) hash->prev->next = hash->next;
	 else f->shared->shadow[idx] = hash->next;
	 if (hash->next) hash->next->prev = hash->prev;
	 H5MM_xfree (hash);
      }
   }

   FUNC_LEAVE (SUCCEED);
}



/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_move
 *
 * Purpose:	Moves the SHADOW for some entry to correspond to a
 *		NEW_ENTRY. The GRP_ADDR is the address for the group
 *		which contains NEW_ENTRY.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL
 *
 * Programmer:	Robb Matzke
 *              Friday, September 19, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_move (H5F_t *f, H5G_shadow_t *shadow, const char *new_name,
		 H5G_entry_t *new_entry, const haddr_t *grp_addr)
{
   H5G_hash_t	*hash;
   uintn	idx;
   
   FUNC_ENTER (H5G_shadow_move, FAIL);

   assert (shadow);
   assert (new_entry);
   assert (grp_addr && H5F_addr_defined (grp_addr));

   if (!H5F_addr_defined (&(shadow->grp_addr))) {
      /*
       * We're moving the shadow for the root object.  This simplifies things
       * greatly since it implies that this is the only shadow currently
       * defined for the entire file.
       */
      idx = H5F_addr_hash (grp_addr, f->shared->nshadows);
      assert (NULL==f->shared->shadow[idx]); /*Nothing at new idx...	*/
      hash = f->shared->shadow[0];
      assert (hash);			/*..but root idx has something. */
      assert (!H5F_addr_defined (&(hash->grp_addr)));/*..it's the root obj*/
      assert (NULL==hash->next);	/*..and just that		*/
      assert (hash->head==shadow);	/*..and exactly that		*/

      /* Move root entry to new hash bucket */
      f->shared->shadow[idx] = hash;
      f->shared->shadow[0] = NULL;
      hash->grp_addr = *grp_addr;

      /* Associate SHADOW with NEW_ENTRY */
      shadow->grp_addr = *grp_addr;
      shadow->main = new_entry;
      new_entry->shadow = shadow;

      /* Give the shadow a new name */
      H5MM_xfree (shadow->name);
      shadow->name = H5MM_xstrdup (new_name);
      
   } else {
      /*
       * Other shadows never move.
       */
      assert (H5F_addr_eq (&(shadow->grp_addr), grp_addr));
      shadow->main = new_entry;
      new_entry->shadow = shadow;
   }

   FUNC_LEAVE (SUCCEED);
}



/*-------------------------------------------------------------------------
 * Function:	H5G_shadow_flush
 *
 * Purpose:	Flush all open object information to the main cache.
 *
 * Return:	Success:	SUCCEED
 *
 *		Failure:	FAIL if INVALIDATE is non-zero and there are
 *				open objects.
 *
 * Programmer:	Robb Matzke
 *              Friday, September 19, 1997
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5G_shadow_flush (H5F_t *f, hbool_t invalidate)
{
   uintn	idx;
   H5G_hash_t	*hash = NULL;
   H5G_shadow_t	*shadow = NULL;
   intn		nfound=0;

   FUNC_ENTER (H5G_shadow_flush, FAIL);

   for (idx=0; idx<f->shared->nshadows; idx++) {
      for (hash=f->shared->shadow[idx]; hash; hash=hash->next) {
	 for (shadow=hash->head; shadow; shadow=shadow->next) {
	    /*
	     * If the shadow is dirty, then transfer the shadow info to the
	     * symbol table node.
	     */
	    if (shadow->entry.dirty) {
	       if (0==idx && !H5F_addr_defined (&(shadow->grp_addr)) &&
		   shadow->main==f->shared->root_sym) {
		  /*
		   * The shadow for the root entry gets copied back into the
		   * root symbol
		   */
		  *f->shared->root_sym = shadow->entry;
	       } else {
		  /*
		   * Other shadows get copied back into the symbol table.
		   */
		  if (!shadow->main &&
		      NULL==H5G_stab_find (f, &(shadow->grp_addr), NULL,
					   shadow->name)) {
		     HRETURN_ERROR (H5E_SYM, H5E_NOTFOUND, FAIL,
				    "unable to find shadow name in symbol "
				    "table");
		  }
		  assert (shadow->main);
		  *(shadow->main) = shadow->entry;
		  shadow->entry.dirty = FALSE;
		  nfound++;
	       }
	    }
#ifndef NDEBUG
	    /*
	     * This is usually a bad thing--an hdf5 programmer forgot to close
	     * some object before closing the file.  Since this is hard to
	     * debug, we'll be nice and print the names here.  We don't know
	     * the full name, but we'll print the relative file address
	     * of the object header for the group that contains the open
	     * object.
	     */
	    if (invalidate) {
	       fprintf (stderr, "HDF5-DIAG: warning: open object <");
	       H5F_addr_print (stderr, &(shadow->grp_addr));
	       fprintf (stderr, ">/%s", shadow->name);
	       if (shadow->nrefs>1) {
		  fprintf (stderr,  " (%d times)", shadow->nrefs);
	       }
	       fputc ('\n', stderr);
	    }
#endif
	 }
      }
   }

   if (invalidate && nfound) {
      /*
       * No clean easy way to do this, just leak the memory.  If we free a
       * shadow and then something else tries to access it (perhaps to close
       * it) then they trample on freed memory.
       */
      HRETURN_ERROR (H5E_SYM, H5E_UNSUPPORTED, FAIL,
		     "leaking memory due to shadow errors");
   }

   FUNC_LEAVE (SUCCEED);
}