Repository navigation
Expand file tree
/
Copy pathH5Dvirtual.c
More file actions
4390 lines (3755 loc) · 198 KB
/
Copy pathH5Dvirtual.c
File metadata and controls
4390 lines (3755 loc) · 198 KB
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the LICENSE file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Purpose:
* Virtual Dataset (VDS) functions. Creates a layout type which allows
* definition of a virtual dataset, where the actual dataset is stored in
* other datasets (called source datasets). The mappings between the
* virtual and source datasets are specified by hyperslab or "all"
* dataspace selections. Point selections are not currently supported.
* Overlaps in the mappings in the virtual dataset result in undefined
* behaviour.
*
* Mapping selections may be unlimited, in which case the size of the
* virtual dataset is determined by the size of the source dataset(s).
* Names for the source datasets may also be generated procedurally, in
* which case the virtual selection should be unlimited with an unlimited
* count and the source selection should be limited with a size equal to
* that of the virtual selection with the unlimited count set to 1.
*
* Source datasets are opened lazily (only when needed for I/O or to
* determine the size of the virtual dataset), and are currently held open
* until the virtual dataset is closed.
*/
/*
* Note: H5S_select_project_intersection has been updated to no longer require
* that the source and source intersect spaces have the same extent. This file
* should therefore be updated to remove code that ensures this condition, which
* should improve both maintainability and performance.
*/
/****************/
/* Module Setup */
/****************/
#include "H5Dmodule.h" /* This source code file is part of the H5D module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5CXprivate.h" /* API Contexts */
#include "H5Dpkg.h" /* Dataset functions */
#include "H5Eprivate.h" /* Error handling */
#include "H5Fprivate.h" /* Files */
#include "H5FLprivate.h" /* Free Lists */
#include "H5Gprivate.h" /* Groups */
#include "H5HGprivate.h" /* Global Heaps */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
#include "H5Oprivate.h" /* Object headers */
#include "H5Pprivate.h" /* Property Lists */
#include "H5RTprivate.h" /* R-trees */
#include "H5Sprivate.h" /* Dataspaces */
#include "H5VLprivate.h" /* Virtual Object Layer */
/****************/
/* Local Macros */
/****************/
/* Default size for sub_dset array */
#define H5D_VIRTUAL_DEF_SUB_DSET_SIZE 128
/* Default size for not_in_tree list allocation */
#define H5D_VIRTUAL_NOT_IN_TREE_INIT_SIZE 64
/*
* Determines whether a virtual dataset mapping entry should be inserted
* into the R-tree spatial index.
*
* The following mappings cannot be added to the tree:
* - Mappings with an unlimited dimension, since this would invalidate the spatial search process
* - Mappings with zero dimensions, since the tree relies on each entry having at least one dimension
*
*/
/* Mappings with an unlimited dimension */
#define H5D_RTREE_SHOULD_INSERT(entry) \
!(((entry)->unlim_dim_virtual >= 0) || ((entry)->source_dset.virtual_select && \
H5S_GET_EXTENT_NDIMS((entry)->source_dset.virtual_select) < 1))
/******************/
/* Local Typedefs */
/******************/
/********************/
/* Local Prototypes */
/********************/
/* Layout operation callbacks */
static herr_t H5D__virtual_construct(H5F_t *f, H5D_t *dset);
static herr_t H5D__virtual_init(H5F_t *f, H5D_t *dset, hid_t dapl_id, bool open_op);
static bool H5D__virtual_is_space_alloc(const H5O_storage_t *storage);
static bool H5D__virtual_is_data_cached(const H5D_shared_t *shared_dset);
static herr_t H5D__virtual_io_init(H5D_io_info_t *io_info, H5D_dset_io_info_t *dinfo);
static herr_t H5D__virtual_read(H5D_io_info_t *io_info, H5D_dset_io_info_t *dinfo);
static herr_t H5D__virtual_write(H5D_io_info_t *io_info, H5D_dset_io_info_t *dinfo);
static herr_t H5D__virtual_flush(H5D_t *dset);
/* Other functions */
static herr_t H5D__virtual_free_layout_mappings(H5O_storage_virtual_t *virt);
static herr_t H5D__virtual_open_source_dset(const H5D_t *vdset, H5O_storage_virtual_ent_t *virtual_ent,
H5O_storage_virtual_srcdset_t *source_dset);
static herr_t H5D__virtual_reset_source_dset(H5O_storage_virtual_ent_t *virtual_ent,
H5O_storage_virtual_srcdset_t *source_dset);
static herr_t H5D__virtual_str_append(const char *src, size_t src_len, char **p, char **buf,
size_t *buf_size);
static herr_t H5D__virtual_copy_parsed_name(H5O_storage_virtual_name_seg_t **dst,
H5O_storage_virtual_name_seg_t *src);
static herr_t H5D__virtual_build_source_name(char *source_name,
const H5O_storage_virtual_name_seg_t *parsed_name,
size_t static_strlen, size_t nsubs, hsize_t blockno,
char **built_name);
static herr_t H5D__virtual_init_all(const H5D_t *dset);
static herr_t H5D__virtual_pre_io(H5D_dset_io_info_t *dset_info, H5O_storage_virtual_t *storage,
H5S_t *file_space, H5S_t *mem_space, hsize_t *tot_nelmts,
H5RT_result_set_t *mappings);
static herr_t H5D__virtual_post_io(H5O_storage_virtual_t *storage, H5RT_result_set_t *mappings);
static herr_t H5D__virtual_close_mapping(H5O_storage_virtual_ent_t *mapping);
static herr_t H5D__virtual_read_one_mapping(H5D_dset_io_info_t *dset_info,
H5O_storage_virtual_ent_t *mapping);
static herr_t H5D__virtual_read_one_src(H5D_dset_io_info_t *dset_info,
H5O_storage_virtual_srcdset_t *source_dset);
static herr_t H5D__virtual_write_one_mapping(H5D_dset_io_info_t *dset_info,
H5O_storage_virtual_ent_t *mapping);
static herr_t H5D__virtual_write_one_src(H5D_dset_io_info_t *dset_info,
H5O_storage_virtual_srcdset_t *source_dset);
/* R-tree helper functions */
static herr_t H5D__virtual_build_tree(H5O_storage_virtual_t *virt, int rank);
static herr_t H5D__mappings_to_leaves(H5O_storage_virtual_ent_t *mappings, size_t num_mappings,
H5RT_leaf_t **leaves_out, H5O_storage_virtual_ent_t ***not_in_tree_out,
size_t *leaf_count, size_t *not_in_tree_count,
size_t *not_in_tree_nalloc);
static herr_t H5D__should_build_tree(H5O_storage_virtual_t *storage, hid_t dapl_id, bool *should_build_tree);
static herr_t H5D__virtual_not_in_tree_grow(H5O_storage_virtual_ent_t ***list, size_t *nalloc);
static herr_t H5D__virtual_not_in_tree_add(H5O_storage_virtual_ent_t ***list, size_t *nused, size_t *nalloc,
H5O_storage_virtual_ent_t *mapping);
/*********************/
/* Package Variables */
/*********************/
/* Contiguous storage layout I/O ops */
const H5D_layout_ops_t H5D_LOPS_VIRTUAL[1] = {{
H5D__virtual_construct, /* construct */
H5D__virtual_init, /* init */
H5D__virtual_is_space_alloc, /* is_space_alloc */
H5D__virtual_is_data_cached, /* is_data_cached */
H5D__virtual_io_init, /* io_init */
NULL, /* mdio_init */
H5D__virtual_read, /* ser_read */
H5D__virtual_write, /* ser_write */
NULL, /* readvv */
NULL, /* writevv */
H5D__virtual_flush, /* flush */
NULL, /* io_term */
NULL /* dest */
}};
/*******************/
/* Local Variables */
/*******************/
/* Declare a free list to manage the H5O_storage_virtual_name_seg_t struct */
H5FL_DEFINE(H5O_storage_virtual_name_seg_t);
/* Declare a static free list to manage H5D_virtual_file_list_t structs */
H5FL_DEFINE_STATIC(H5D_virtual_held_file_t);
/*-------------------------------------------------------------------------
* Function: H5D_virtual_check_mapping_pre
*
* Purpose: Checks that the provided virtual and source selections are
* legal for use as a VDS mapping, prior to creating the rest
* of the mapping entry.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5D_virtual_check_mapping_pre(const H5S_t *vspace, const H5S_t *src_space,
H5O_virtual_space_status_t space_status)
{
H5S_sel_type select_type; /* Selection type */
hsize_t nelmts_vs; /* Number of elements in virtual selection */
hsize_t nelmts_ss; /* Number of elements in source selection */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
/* Check for point selections (currently unsupported) */
if (H5S_SEL_ERROR == (select_type = H5S_GET_SELECT_TYPE(vspace)))
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get selection type");
if (select_type == H5S_SEL_POINTS)
HGOTO_ERROR(H5E_DATASET, H5E_UNSUPPORTED, FAIL,
"point selections not currently supported with virtual datasets");
if (H5S_SEL_ERROR == (select_type = H5S_GET_SELECT_TYPE(src_space)))
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get selection type");
if (select_type == H5S_SEL_POINTS)
HGOTO_ERROR(H5E_DATASET, H5E_UNSUPPORTED, FAIL,
"point selections not currently supported with virtual datasets");
/* Get number of elements in spaces */
nelmts_vs = (hsize_t)H5S_GET_SELECT_NPOINTS(vspace);
nelmts_ss = (hsize_t)H5S_GET_SELECT_NPOINTS(src_space);
/* Check for unlimited vspace */
if (nelmts_vs == H5S_UNLIMITED) {
/* Check for unlimited src_space */
if (nelmts_ss == H5S_UNLIMITED) {
hsize_t nenu_vs; /* Number of elements in the non-unlimited dimensions of vspace */
hsize_t nenu_ss; /* Number of elements in the non-unlimited dimensions of src_space */
/* Non-printf unlimited selection. Make sure both selections have
* the same number of elements in the non-unlimited dimension. Note
* we can always check this even if the space status is invalid
* because unlimited selections are never dependent on the extent.
*/
if (H5S_get_select_num_elem_non_unlim(vspace, &nenu_vs) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTCOUNT, FAIL,
"can't get number of elements in non-unlimited dimension");
if (H5S_get_select_num_elem_non_unlim(src_space, &nenu_ss) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTCOUNT, FAIL,
"can't get number of elements in non-unlimited dimension");
if (nenu_vs != nenu_ss)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
"numbers of elements in the non-unlimited dimensions is different for source and "
"virtual spaces");
} /* end if */
/* We will handle the printf case after parsing the source names */
} /* end if */
else if (space_status != H5O_VIRTUAL_STATUS_INVALID)
/* Limited selections. Check number of points is the same. */
if (nelmts_vs != nelmts_ss)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
"virtual and source space selections have different numbers of elements");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D_virtual_check_mapping_pre() */
/*-------------------------------------------------------------------------
* Function: H5D_virtual_check_mapping_post
*
* Purpose: Checks that the provided virtual dataset mapping entry is
* legal, after the mapping is otherwise complete.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5D_virtual_check_mapping_post(const H5O_storage_virtual_ent_t *ent)
{
hsize_t nelmts_vs; /* Number of elements in virtual selection */
hsize_t nelmts_ss; /* Number of elements in source selection */
H5S_t *tmp_space = NULL; /* Temporary dataspace */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
/* Get number of elements in spaces */
nelmts_vs = (hsize_t)H5S_GET_SELECT_NPOINTS(ent->source_dset.virtual_select);
nelmts_ss = (hsize_t)H5S_GET_SELECT_NPOINTS(ent->source_select);
/* Check for printf selection */
if ((nelmts_vs == H5S_UNLIMITED) && (nelmts_ss != H5S_UNLIMITED)) {
/* Make sure there at least one %b substitution in the source file or
* dataset name */
if ((ent->psfn_nsubs == 0) && (ent->psdn_nsubs == 0))
HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL,
"unlimited virtual selection, limited source selection, and no printf specifiers in "
"source names");
/* Make sure virtual space uses hyperslab selection */
if (H5S_GET_SELECT_TYPE(ent->source_dset.virtual_select) != H5S_SEL_HYPERSLABS)
HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL,
"virtual selection with printf mapping must be hyperslab");
/* Check that the number of elements in one block in the virtual
* selection matches the total number of elements in the source
* selection, if the source space status is not invalid (virtual space
* status does not matter here because it is unlimited) */
if (ent->source_space_status != H5O_VIRTUAL_STATUS_INVALID) {
/* Get first block in virtual selection */
if (NULL == (tmp_space = H5S_hyper_get_unlim_block(ent->source_dset.virtual_select, (hsize_t)0)))
HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't get first block in virtual selection");
/* Check number of points */
nelmts_vs = (hsize_t)H5S_GET_SELECT_NPOINTS(tmp_space);
if (nelmts_vs != nelmts_ss)
HGOTO_ERROR(
H5E_ARGS, H5E_BADVALUE, FAIL,
"virtual (single block) and source space selections have different numbers of elements");
} /* end if */
} /* end if */
else
/* Make sure there are no printf substitutions */
if ((ent->psfn_nsubs > 0) || (ent->psdn_nsubs > 0))
HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL,
"printf specifier(s) in source name(s) without an unlimited virtual selection and "
"limited source selection");
done:
/* Free temporary space */
if (tmp_space)
if (H5S_close(tmp_space) < 0)
HDONE_ERROR(H5E_PLIST, H5E_CLOSEERROR, FAIL, "can't close dataspace");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D_virtual_check_mapping_post() */
/*-------------------------------------------------------------------------
* Function: H5D_virtual_update_min_dims
*
* Purpose: Updates the virtual layout's "min_dims" field to take into
* account the "idx"th entry in the mapping list. The entry
* must be complete, though top level field list_nused (and
* of course min_dims) does not need to take it into account.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5D_virtual_update_min_dims(H5O_layout_t *layout, size_t idx)
{
H5O_storage_virtual_t *virt = &layout->storage.u.virt;
H5O_storage_virtual_ent_t *ent = &virt->list[idx];
H5S_sel_type sel_type;
int rank;
hsize_t bounds_start[H5S_MAX_RANK];
hsize_t bounds_end[H5S_MAX_RANK];
int i;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI(FAIL)
assert(layout);
assert(layout->type == H5D_VIRTUAL);
assert(idx < virt->list_nalloc);
/* Get type of selection */
if (H5S_SEL_ERROR == (sel_type = H5S_GET_SELECT_TYPE(ent->source_dset.virtual_select)))
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "unable to get selection type");
/* Do not update min_dims for "all" or "none" selections */
if ((sel_type == H5S_SEL_ALL) || (sel_type == H5S_SEL_NONE))
HGOTO_DONE(SUCCEED);
/* Get rank of vspace */
if ((rank = H5S_GET_EXTENT_NDIMS(ent->source_dset.virtual_select)) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "unable to get number of dimensions");
/* Get selection bounds */
if (H5S_SELECT_BOUNDS(ent->source_dset.virtual_select, bounds_start, bounds_end) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "unable to get selection bounds");
/* Update min_dims */
for (i = 0; i < rank; i++)
/* Don't check unlimited dimensions in the selection */
if ((i != ent->unlim_dim_virtual) && (bounds_end[i] >= virt->min_dims[i]))
virt->min_dims[i] = bounds_end[i] + (hsize_t)1;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D_virtual_update_min_dims() */
/*-------------------------------------------------------------------------
* Function: H5D_virtual_check_min_dims
*
* Purpose: Checks if the dataset's dimensions are at least the
* calculated minimum dimensions from the mappings.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5D_virtual_check_min_dims(const H5D_t *dset)
{
int rank;
hsize_t dims[H5S_MAX_RANK];
int i;
herr_t ret_value = SUCCEED;
FUNC_ENTER_NOAPI(FAIL)
assert(dset);
assert(dset->shared);
assert(dset->shared->layout.type == H5D_VIRTUAL);
/* Get rank of dataspace */
if ((rank = H5S_GET_EXTENT_NDIMS(dset->shared->space)) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "unable to get number of dimensions");
/* Get VDS dimensions */
if (H5S_get_simple_extent_dims(dset->shared->space, dims, NULL) < 0)
HGOTO_ERROR(H5E_DATASET, H5E_CANTGET, FAIL, "can't get VDS dimensions");
/* Verify that dimensions are larger than min_dims */
for (i = 0; i < rank; i++)
if (dims[i] < dset->shared->layout.storage.u.virt.min_dims[i])
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL,
"virtual dataset dimensions not large enough to contain all limited dimensions in "
"all selections");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D_virtual_check_min_dims() */
/*-------------------------------------------------------------------------
* Function: H5D__virtual_store_layout
*
* Purpose: Store virtual dataset layout information, for new dataset
*
* Note: We assume here that the contents of the heap block cannot
* change! If this ever stops being the case we must change
* this code to allow overwrites of the heap block. -NAF
*
* Return: Success: SUCCEED
* Failure: FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5D__virtual_store_layout(H5F_t *f, H5O_layout_t *layout)
{
H5O_storage_virtual_t *virt = &layout->storage.u.virt;
uint8_t *heap_block = NULL; /* Block to add to heap */
size_t *str_size = NULL; /* Array for VDS entry string lengths */
uint8_t *heap_block_p; /* Pointer into the heap block, while encoding */
size_t block_size; /* Total size of block needed */
hsize_t tmp_hsize; /* Temp. variable for encoding hsize_t */
uint32_t chksum; /* Checksum for heap data */
uint8_t max_version; /* Maximum encoding version allowed by version bounds */
uint8_t version = H5O_LAYOUT_VDS_GH_ENC_VERS_0; /* Encoding version */
size_t i; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity checking */
assert(f);
assert(layout);
assert(virt->serial_list_hobjid.addr == HADDR_UNDEF);
/* Create block if # of used entries > 0 */
if (virt->list_nused > 0) {
/* Set the low/high bounds according to 'f' for the API context */
H5CX_set_libver_bounds(f);
/* Calculate maximum encoding version. Currently there are no features that require a later version,
* so we only upgrade if the lower bound is high enough that we don't worry about backward
* compatibility, and if there is a benefit (will calculate the benefit later). */
max_version =
H5F_LOW_BOUND(f) >= H5F_LIBVER_V200 ? H5O_LAYOUT_VDS_GH_ENC_VERS_1 : H5O_LAYOUT_VDS_GH_ENC_VERS_0;
/* Allocate array for caching results of strlen */
if (NULL == (str_size = (size_t *)H5MM_malloc(2 * virt->list_nused * sizeof(size_t))))
HGOTO_ERROR(H5E_OHDR, H5E_RESOURCE, FAIL, "unable to allocate string length array");
/*
* Calculate heap block size
*/
/* Version and number of entries */
block_size = (size_t)1 + H5F_SIZEOF_SIZE(f);
/* Calculate size of each entry */
for (i = 0; i < virt->list_nused; i++) {
H5O_storage_virtual_ent_t *ent = &virt->list[i];
hssize_t select_serial_size; /* Size of serialized selection */
assert(ent->source_file_name);
assert(ent->source_dset_name);
assert(ent->source_select);
assert(ent->source_dset.virtual_select);
/* Source file name */
str_size[2 * i] = strlen(ent->source_file_name) + (size_t)1;
block_size += str_size[2 * i];
/* Source dset name */
str_size[(2 * i) + 1] = strlen(ent->source_dset_name) + (size_t)1;
block_size += str_size[(2 * i) + 1];
/* Source selection */
if ((select_serial_size = H5S_SELECT_SERIAL_SIZE(ent->source_select)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTENCODE, FAIL, "unable to check dataspace selection size");
block_size += (size_t)select_serial_size;
/* Virtual dataset selection */
if ((select_serial_size = H5S_SELECT_SERIAL_SIZE(ent->source_dset.virtual_select)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTENCODE, FAIL, "unable to check dataspace selection size");
block_size += (size_t)select_serial_size;
}
/* Checksum */
block_size += 4;
/*
* Calculate_heap_block_size for version 1, if available
*/
if (max_version >= H5O_LAYOUT_VDS_GH_ENC_VERS_1) {
size_t block_size_1; /* Block size if we use version 1 */
/* Version and number of entries */
block_size_1 = (size_t)1 + H5F_SIZEOF_SIZE(f);
/* Calculate size of each entry */
for (i = 0; i < virt->list_nused; i++) {
H5O_storage_virtual_ent_t *ent = &virt->list[i];
hssize_t select_serial_size; /* Size of serialized selection */
/* Flags */
block_size_1 += (size_t)1;
/* Source file name (no encoding necessary for ".") */
if (strcmp(ent->source_file_name, ".")) {
if (ent->source_file_orig == SIZE_MAX)
block_size_1 += str_size[2 * i];
else
block_size_1 += MIN(str_size[2 * i], H5F_SIZEOF_SIZE(f));
}
/* Source dset name */
if (ent->source_dset_orig == SIZE_MAX)
block_size_1 += str_size[(2 * i) + 1];
else
block_size_1 += MIN(str_size[(2 * i) + 1], H5F_SIZEOF_SIZE(f));
/* Source selection */
if ((select_serial_size = H5S_SELECT_SERIAL_SIZE(ent->source_select)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTENCODE, FAIL, "unable to check dataspace selection size");
block_size_1 += (size_t)select_serial_size;
/* Virtual dataset selection */
if ((select_serial_size = H5S_SELECT_SERIAL_SIZE(ent->source_dset.virtual_select)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTENCODE, FAIL, "unable to check dataspace selection size");
block_size_1 += (size_t)select_serial_size;
}
/* Checksum */
block_size_1 += 4;
/* Determine which version to use. Only use version 1 if we save space. In the case of a tie, use
* version 1 since it will allow faster decoding since we know (some of) which strings are shared
* and won't need to do hash table lookups for those. */
if (block_size_1 <= block_size) {
version = H5O_LAYOUT_VDS_GH_ENC_VERS_1;
block_size = block_size_1;
}
}
/* Allocate heap block */
if (NULL == (heap_block = (uint8_t *)H5MM_malloc(block_size)))
HGOTO_ERROR(H5E_OHDR, H5E_RESOURCE, FAIL, "unable to allocate heap block");
/*
* Encode heap block
*/
heap_block_p = heap_block;
/* Encode heap block encoding version */
*heap_block_p++ = version;
/* Number of entries */
H5_CHECK_OVERFLOW(virt->list_nused, size_t, hsize_t);
tmp_hsize = (hsize_t)virt->list_nused;
H5F_ENCODE_LENGTH(f, heap_block_p, tmp_hsize);
/* Encode each entry */
for (i = 0; i < virt->list_nused; i++) {
H5O_storage_virtual_ent_t *ent = &virt->list[i];
uint8_t flags = 0;
/* Flags */
if (version >= H5O_LAYOUT_VDS_GH_ENC_VERS_1) {
if (!strcmp(ent->source_file_name, "."))
/* Source file in same file as VDS */
flags |= H5O_LAYOUT_VDS_SOURCE_SAME_FILE;
else if ((ent->source_file_orig != SIZE_MAX) && (str_size[2 * i] >= H5F_SIZEOF_SIZE(f)))
/* Source file name is shared (stored in another entry) */
flags |= H5O_LAYOUT_VDS_SOURCE_FILE_SHARED;
if ((ent->source_dset_orig != SIZE_MAX) && (str_size[(2 * i) + 1] >= H5F_SIZEOF_SIZE(f)))
/* Source dataset name is shared (stored in another entry) */
flags |= H5O_LAYOUT_VDS_SOURCE_DSET_SHARED;
*heap_block_p++ = flags;
}
/* Source file name */
if (!(flags & H5O_LAYOUT_VDS_SOURCE_SAME_FILE)) {
if (flags & H5O_LAYOUT_VDS_SOURCE_FILE_SHARED) {
assert(ent->source_file_orig < i);
tmp_hsize = (hsize_t)ent->source_file_orig;
H5F_ENCODE_LENGTH(f, heap_block_p, tmp_hsize);
}
else {
H5MM_memcpy((char *)heap_block_p, ent->source_file_name, str_size[2 * i]);
heap_block_p += str_size[2 * i];
}
}
/* Source dataset name */
if (flags & H5O_LAYOUT_VDS_SOURCE_DSET_SHARED) {
assert(ent->source_dset_orig < i);
tmp_hsize = (hsize_t)ent->source_dset_orig;
H5F_ENCODE_LENGTH(f, heap_block_p, tmp_hsize);
}
else {
H5MM_memcpy((char *)heap_block_p, ent->source_dset_name, str_size[(2 * i) + 1]);
heap_block_p += str_size[(2 * i) + 1];
}
/* Source selection */
if (H5S_SELECT_SERIALIZE(ent->source_select, &heap_block_p) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, FAIL, "unable to serialize source selection");
/* Virtual selection */
if (H5S_SELECT_SERIALIZE(ent->source_dset.virtual_select, &heap_block_p) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTCOPY, FAIL, "unable to serialize virtual selection");
}
/* Checksum */
chksum = H5_checksum_metadata(heap_block, block_size - (size_t)4, 0);
UINT32ENCODE(heap_block_p, chksum);
/* Insert block into global heap */
if (H5HG_insert(f, block_size, heap_block, &(virt->serial_list_hobjid)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINSERT, FAIL, "unable to insert virtual dataset heap block");
}
done:
heap_block = (uint8_t *)H5MM_xfree(heap_block);
str_size = (size_t *)H5MM_xfree(str_size);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D__virtual_store_layout() */
/*-------------------------------------------------------------------------
* Function: H5D__virtual_load_layout
*
* Purpose: Loads virtual dataset layout information from global heap
*
* Return: Success: SUCCEED
* Failure: FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5D__virtual_load_layout(H5F_t *f, H5O_layout_t *layout)
{
uint8_t *heap_block = NULL;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Decode heap block if it exists and we don't already have the list of mappings */
if (!layout->storage.u.virt.list && layout->storage.u.virt.serial_list_hobjid.addr != HADDR_UNDEF) {
const uint8_t *heap_block_p;
const uint8_t *heap_block_p_end;
uint8_t heap_vers;
size_t block_size = 0;
size_t tmp_size;
hsize_t tmp_hsize = 0;
uint32_t stored_chksum;
uint32_t computed_chksum;
size_t first_same_file = SIZE_MAX;
bool clear_file_hash_table = false;
/* Read heap */
if (NULL == (heap_block = (uint8_t *)H5HG_read(f, &(layout->storage.u.virt.serial_list_hobjid), NULL,
&block_size)))
HGOTO_ERROR(H5E_OHDR, H5E_READERROR, FAIL, "Unable to read global heap block");
heap_block_p = (const uint8_t *)heap_block;
heap_block_p_end = heap_block_p + block_size - 1;
/* Decode the version number of the heap block encoding */
if (H5_IS_BUFFER_OVERFLOW(heap_block_p, 1, heap_block_p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
heap_vers = (uint8_t)*heap_block_p++;
assert(H5O_LAYOUT_VDS_GH_ENC_VERS_0 == 0);
if (heap_vers > (uint8_t)H5O_LAYOUT_VDS_GH_ENC_VERS_1)
HGOTO_ERROR(H5E_OHDR, H5E_VERSION, FAIL,
"bad version # of encoded VDS heap information, expected %u or lower, got %u",
(unsigned)H5O_LAYOUT_VDS_GH_ENC_VERS_1, (unsigned)heap_vers);
/* Number of entries */
if (H5_IS_BUFFER_OVERFLOW(heap_block_p, H5F_sizeof_size(f), heap_block_p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
H5F_DECODE_LENGTH(f, heap_block_p, tmp_hsize);
/* Allocate entry list */
if (tmp_hsize > 0) {
if (NULL == (layout->storage.u.virt.list = (H5O_storage_virtual_ent_t *)H5MM_calloc(
(size_t)tmp_hsize * sizeof(H5O_storage_virtual_ent_t))))
HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL, "unable to allocate heap block");
}
else {
/* Avoid zero-size allocation */
layout->storage.u.virt.list = NULL;
}
layout->storage.u.virt.list_nalloc = (size_t)tmp_hsize;
layout->storage.u.virt.list_nused = (size_t)tmp_hsize;
/* Decode each entry */
for (size_t i = 0; i < layout->storage.u.virt.list_nused; i++) {
H5O_storage_virtual_ent_t *ent = &layout->storage.u.virt.list[i]; /* Convenience pointer */
ptrdiff_t avail_buffer_space;
uint8_t flags = 0;
avail_buffer_space = heap_block_p_end - heap_block_p + 1;
if (avail_buffer_space <= 0)
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
/* Flags */
if (heap_vers >= H5O_LAYOUT_VDS_GH_ENC_VERS_1) {
flags = *heap_block_p++;
if (flags & ~H5O_LAYOUT_ALL_VDS_FLAGS)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "bad flag value for VDS mapping");
}
avail_buffer_space = heap_block_p_end - heap_block_p + 1;
/* Source file name */
if (flags & H5O_LAYOUT_VDS_SOURCE_SAME_FILE) {
/* Source file in same file as VDS, use "." */
if (first_same_file == SIZE_MAX) {
/* No previous instance of ".", copy "." to entry and record this instance */
if (NULL == (ent->source_file_name = (char *)H5MM_malloc(2)))
HGOTO_ERROR(H5E_OHDR, H5E_CANTALLOC, FAIL,
"memory allocation failed for source file string");
ent->source_file_name[0] = '.';
ent->source_file_name[1] = '\0';
ent->source_file_orig = SIZE_MAX;
first_same_file = i;
/* Invalidate hash table for use after decoding since it is missing this "."
*/
clear_file_hash_table = true;
}
else {
/* Reference previous instance of "." */
assert(first_same_file < i);
ent->source_file_name = layout->storage.u.virt.list[first_same_file].source_file_name;
ent->source_file_orig = first_same_file;
}
}
else {
if (flags & H5O_LAYOUT_VDS_SOURCE_FILE_SHARED) {
if (avail_buffer_space < H5F_SIZEOF_SIZE(f))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding");
/* Source file is shared (stored in another entry), decode origin entry number
*/
H5F_DECODE_LENGTH(f, heap_block_p, tmp_hsize);
H5_CHECK_OVERFLOW(tmp_hsize, hsize_t, size_t);
if ((size_t)tmp_hsize >= i)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL,
"origin source file entry has higher index than current entry");
ent->source_file_orig = (size_t)tmp_hsize;
/* Use source file name from origin entry */
ent->source_file_name = layout->storage.u.virt.list[tmp_hsize].source_file_name;
}
else {
tmp_size = strnlen((const char *)heap_block_p, (size_t)avail_buffer_space);
if (tmp_size == (size_t)avail_buffer_space)
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding - unterminated source file "
"name string");
else
tmp_size += 1; /* Add space for NUL terminator */
/* Check for source dataset name in hash table and add it if not found */
H5D_VIRTUAL_FIND_OR_ADD_NAME(file, layout, heap_block_p, tmp_size - 1, ent, FAIL);
/* Advance pointer */
heap_block_p += tmp_size;
}
}
avail_buffer_space = heap_block_p_end - heap_block_p + 1;
/* Source dataset name */
if (flags & H5O_LAYOUT_VDS_SOURCE_DSET_SHARED) {
if (avail_buffer_space < H5F_SIZEOF_SIZE(f))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
/* Source dataset is shared (stored in another entry), decode origin entry number
*/
H5F_DECODE_LENGTH(f, heap_block_p, tmp_hsize);
H5_CHECK_OVERFLOW(tmp_hsize, hsize_t, size_t);
if ((size_t)tmp_hsize >= i)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL,
"origin source dataset entry has higher index than current entry");
ent->source_dset_orig = (size_t)tmp_hsize;
/* Use source dataset name from origin entry */
ent->source_dset_name = layout->storage.u.virt.list[tmp_hsize].source_dset_name;
}
else {
tmp_size = strnlen((const char *)heap_block_p, (size_t)avail_buffer_space);
if (tmp_size == (size_t)avail_buffer_space)
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL,
"ran off end of input buffer while decoding - unterminated source dataset "
"name string");
else
tmp_size += 1; /* Add space for NUL terminator */
/* Check for source dataset name in hash table and add it if not found */
H5D_VIRTUAL_FIND_OR_ADD_NAME(dset, layout, heap_block_p, tmp_size - 1, ent, FAIL);
/* Advance pointer */
heap_block_p += tmp_size;
}
/* Source selection */
avail_buffer_space = heap_block_p_end - heap_block_p + 1;
if (avail_buffer_space <= 0)
HGOTO_ERROR(H5E_DATASPACE, H5E_OVERFLOW, FAIL, "buffer overflow while decoding layout");
if (H5S_SELECT_DESERIALIZE(&ent->source_select, &heap_block_p, (size_t)(avail_buffer_space)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't decode source space selection");
/* Virtual selection */
/* Buffer space must be updated after previous deserialization */
avail_buffer_space = heap_block_p_end - heap_block_p + 1;
if (avail_buffer_space <= 0)
HGOTO_ERROR(H5E_DATASPACE, H5E_OVERFLOW, FAIL, "buffer overflow while decoding layout");
if (H5S_SELECT_DESERIALIZE(&ent->source_dset.virtual_select, &heap_block_p,
(size_t)(avail_buffer_space)) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTDECODE, FAIL, "can't decode virtual space selection");
/* Parse source file and dataset names for "printf"
* style format specifiers */
if (H5D_virtual_parse_source_name(ent->source_file_name, &ent->parsed_source_file_name,
&ent->psfn_static_strlen, &ent->psfn_nsubs) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "can't parse source file name");
if (H5D_virtual_parse_source_name(ent->source_dset_name, &ent->parsed_source_dset_name,
&ent->psdn_static_strlen, &ent->psdn_nsubs) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL, "can't parse source dataset name");
/* Set source names in source_dset struct */
if ((ent->psfn_nsubs == 0) && (ent->psdn_nsubs == 0)) {
if (ent->parsed_source_file_name)
ent->source_dset.file_name = ent->parsed_source_file_name->name_segment;
else
ent->source_dset.file_name = ent->source_file_name;
if (ent->parsed_source_dset_name)
ent->source_dset.dset_name = ent->parsed_source_dset_name->name_segment;
else
ent->source_dset.dset_name = ent->source_dset_name;
}
/* Unlim_dim fields */
ent->unlim_dim_source = H5S_get_select_unlim_dim(ent->source_select);
ent->unlim_dim_virtual = H5S_get_select_unlim_dim(ent->source_dset.virtual_select);
ent->unlim_extent_source = HSIZE_UNDEF;
ent->unlim_extent_virtual = HSIZE_UNDEF;
ent->clip_size_source = HSIZE_UNDEF;
ent->clip_size_virtual = HSIZE_UNDEF;
/* Clipped selections */
if (ent->unlim_dim_virtual < 0) {
ent->source_dset.clipped_source_select = ent->source_select;
ent->source_dset.clipped_virtual_select = ent->source_dset.virtual_select;
}
/* Check mapping for validity (do both pre and post
* checks here, since we had to allocate the entry list
* before decoding the selections anyways) */
if (H5D_virtual_check_mapping_pre(ent->source_dset.virtual_select, ent->source_select,
H5O_VIRTUAL_STATUS_INVALID) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "invalid mapping selections");
if (H5D_virtual_check_mapping_post(ent) < 0)
HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid mapping entry");
/* Update min_dims */
if (H5D_virtual_update_min_dims(layout, i) < 0)
HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, FAIL,
"unable to update virtual dataset minimum dimensions");
}
/* Read stored checksum */
if (H5_IS_BUFFER_OVERFLOW(heap_block_p, 4, heap_block_p_end))
HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, FAIL, "ran off end of input buffer while decoding");
UINT32DECODE(heap_block_p, stored_chksum);
/* Compute checksum */
computed_chksum = H5_checksum_metadata(heap_block, block_size - (size_t)4, 0);
/* Verify checksum */
if (stored_chksum != computed_chksum)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "incorrect metadata checksum for global heap block");
/* Verify that the heap block size is correct */
if ((size_t)(heap_block_p - heap_block) != block_size)
HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "incorrect heap block size");
/* Clear hash tables if requested */
if (clear_file_hash_table)
HASH_CLEAR(hh_source_file, layout->storage.u.virt.source_file_hash_table);
} /* end if */
done:
heap_block = (uint8_t *)H5MM_xfree(heap_block);
/* Free mappings on failure */
if (ret_value < 0)
if (H5D__virtual_free_layout_mappings(&layout->storage.u.virt) < 0)
HDONE_ERROR(H5E_DATASET, H5E_CLOSEERROR, FAIL, "unable to release VDS mappings");
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5D__virtual_load_layout() */
/*-------------------------------------------------------------------------
* Function: H5D__virtual_copy_layout
*
* Purpose: Deep copies virtual storage layout message in memory.
* This function assumes that the top-level struct has
* already been copied (so the source struct retains
* ownership of the fields passed to this function).
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5D__virtual_copy_layout(H5O_layout_t *layout)
{
H5O_storage_virtual_ent_t *orig_list = NULL;
H5O_storage_virtual_ent_t **orig_not_in_tree_list = NULL;
H5O_storage_virtual_t *virt = &layout->storage.u.virt;
hid_t orig_source_fapl;
hid_t orig_source_dapl;
H5P_genplist_t *plist;
size_t i;
herr_t ret_value = SUCCEED;
H5RT_t *new_tree = NULL;
FUNC_ENTER_PACKAGE
assert(layout);
assert(layout->type == H5D_VIRTUAL);
/* Reset hash tables (they are owned by the original list). No need to recreate here - they are only
* needed when adding mappings, and if we add a new mapping the code in H5Pset_virtual() will rebuild
* them). */
virt->source_file_hash_table = NULL;
virt->source_dset_hash_table = NULL;
/* Save original entry list and top-level property lists and reset in layout
* so the originals aren't closed on error */
orig_source_fapl = virt->source_fapl;
virt->source_fapl = -1;
orig_source_dapl = virt->source_dapl;
virt->source_dapl = -1;
orig_list = virt->list;
virt->list = NULL;
orig_not_in_tree_list = virt->not_in_tree_list;
virt->not_in_tree_list = NULL;
/* Copy entry list */
if (virt->list_nused > 0) {
assert(orig_list);
/* Allocate memory for the list */
if (NULL == (virt->list = H5MM_calloc(virt->list_nused * sizeof(virt->list[0]))))
HGOTO_ERROR(H5E_DATASET, H5E_CANTALLOC, FAIL,
"unable to allocate memory for virtual dataset entry list");
virt->list_nalloc = virt->list_nused;
/* Copy the list entries, though set source_dset.dset and sub_dset to
* NULL */
for (i = 0; i < virt->list_nused; i++) {
H5O_storage_virtual_ent_t *ent = &virt->list[i];