forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc4memcb.c
1043 lines (913 loc) · 33.5 KB
/
nc4memcb.c
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 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* ********************************************************************/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* 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 COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* @internal Inmemory support
*
* This code is derived from H5LT.c#H5LTopen_file_image.
* In order to make the netcdf inmemory code work, it is necessary
* to modify some of the callback functions; specifically
* image_malloc, image_realloc, and image_memcpy.
* <p>
* The changes are directed at allowing the caller to
* specify two things.
* <ol>
* <li> To specify (indirectly) the H5LT_FILE_IMAGE_DONT_COPY flag.
* This means that no attempt to realloc the caller provided memory
* should be made. This also means that the memory block pointer
* provided by the caller will be thesame returned by <em>nc_close_memio()</em>.
* <li> The caller overallocates the memory so that there is space
* to allow the file to be modified in place.
* </ol>
* <p>
* The existing implementation of H5LTopen_file_image has two flaws
* with respect to these properties.
* <ol>
* <li> The image_realloc callback fails if
* H5LT_FILE_IMAGE_DONT_COPY flag is set even if there is room
* to allow the memory block to pretend to expand (because
* of overallocation).
* <li> When the caller attempts to get the final memory block,
* the HDF5 library makes a copy, unless theH5LT_FILE_IMAGE_DONT_COPY
* flag is set. This is unnecessary. Note that in this situation,
* the HDF5 library will use
* <em>image_malloc()</em>
* followed by
* <em>image_memcpy()</em>
* </ol>
* <p>
* So, the callback changes to support this properly are as follows.
* <dl>
* <dt>image_realloc</dt>
* <dd>If there is sufficient space (because of overallocation),
* the pretend to realloc and return the incoming memory block
* instead of taking the chance of doing a real realloc.</dd>
* <dt>image_malloc</dt>
* <dd>If the operation being performed is to obtain
* the space to copy the final memory, then just return
* the original memory block. Note that this case is detectable
* because the callback is given the value
* H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET.</dd>
* <dt>image_memcpy</dt>
* <dd>Similar to the image_malloc change.
* Namely, if the operation being performed is to copy
* out the final memory contents, and the final memory block
* is the same as that provided by the caller originally, then
* just do nothing. Again, this case can be detected
* by the occurrence of H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET.</dd>
* </dl>
* @author Dennis Heimbigner
*/
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <hdf5.h>
#include <hdf5_hl.h>
#include "nc4internal.h"
#ifndef HDrealloc
#define HDrealloc(x,y) realloc(x,y)
#endif
#ifndef SUCCEED
#define SUCCEED 0
#define FAIL -1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#undef TRACE
#undef CATCH
#undef TRACE_UDATA
#ifdef TRACE
#define CATCH
#endif
#ifdef TRACE
#include <stdarg.h>
static void trace(const char* fcn, H5FD_file_image_op_t op, void* _udata, ...);
static void traceend(const char* fcn, void* _udata, uintptr_t retval);
static const char* traceop(H5FD_file_image_op_t op);
static char* traceflags(int flags);
/* In case we do not have variadic macros */
#define TRACE0(fcn,op,udata) trace(fcn,op,udata)
#define TRACE1(fcn,op,udata,x1) trace(fcn,op,udata,x1)
#define TRACE2(fcn,op,udata,x1,x2) trace(fcn,op,udata,x1,x2)
#define TRACE3(fcn,op,udata,x1,x2,x3) trace(fcn,op,udata,x1,x2,x3)
#define TRACEEND(fcn,udata,retval) traceend(fcn,udata,(uintptr_t)retval);
#else /*!TRACE*/
#define TRACE0(fcn,op,udata)
#define TRACE1(fcn,op,udata,x1)
#define TRACE2(fcn,op,udata,x1,x2)
#define TRACE3(fcn,op,udata,x1,x2,x3)
#define TRACEEND(fcn,udata,retval)
#endif
#ifdef CATCH
static void tracefail(const char* fcn);
#define TRACEFAIL(fcn) tracefail(fcn)
#else
#define TRACEFAIL(fcn)
#endif
#define DEFAULT_CREATE_MEMSIZE ((size_t)1<<16)
#ifndef H5LT_FILE_IMAGE_DONT_COPY
/* Flag definitions for H5LTopen_file_image() */
#define H5LT_FILE_IMAGE_OPEN_RW 0x0001 /* Open image for read-write */
#define H5LT_FILE_IMAGE_DONT_COPY 0x0002 /* The HDF5 lib won't copy */
/* user supplied image buffer. The same image is open with the core driver. */
#define H5LT_FILE_IMAGE_DONT_RELEASE 0x0004 /* The HDF5 lib won't */
/* deallocate user supplied image buffer. The user application is responsible */
/* for doing so. */
#define H5LT_FILE_IMAGE_ALL 0x0007
#endif /*H5LT_FILE_IMAGE_DONT_COPY*/
#if 0
/* For Lex and Yacc */
#define COL 3
#define LIMIT 512
#define INCREMENT 1024
#define TMP_LEN 256
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
size_t input_len;
char *myinput;
size_t indent = 0;
#endif /*0*/
/* File Image operations
A file image is a representation of an HDF5 file in a memory
buffer. In order to perform operations on an image in a similar way
to a file, the application buffer is copied to a FAPL buffer, which
in turn is copied to a VFD buffer. Buffer copying can decrease
performance, especially when using large file images. A solution to
this issue is to simulate the copying of the application buffer,
when actually the same buffer is used for the FAPL and the VFD.
This is implemented by using callbacks that simulate the standard
functions for memory management (additional callbacks are used for
the management of associated data structures). From the application
standpoint, a file handle can be obtained from a file image by using
the API routine H5LTopen_file_image(). This function takes a flag
argument that indicates the HDF5 library how to handle the given image;
several flag values can be combined by using the bitwise OR operator.
Valid flag values include:
H5LT_FILE_IMAGE_OPEN_RW indicates the HDF5 library to open the file
image in read/write mode. Default is read-only mode.
H5LT_FILE_IMAGE_DONT_COPY indicates the HDF5 library to not copy the
supplied user buffer; the same buffer will be handled by the FAPL and
the VFD driver. Default operation copies the user buffer to the FAPL and
VFD driver.
H5LT_FILE_IMAGE_DONT_RELEASE indicates the HDF5 library to not release
the buffer handled by the FAPL and the VFD upon closing. This flag value
is only applicable when the flag value H5LT_FILE_IMAGE_DONT_COPY is set as
well. The application is responsible to release the image buffer.
*/
/* Data structure to pass application data to callbacks. */
/* Modified to add NC_FILE_INFO_T ptr */
typedef struct {
void *app_image_ptr; /* Pointer to application buffer */
size_t app_image_size; /* Size of application buffer */
void *fapl_image_ptr; /* Pointer to FAPL buffer */
size_t fapl_image_size; /* Size of FAPL buffer */
int fapl_ref_count; /* Reference counter for FAPL buffer */
void *vfd_image_ptr; /* Pointer to VFD buffer */
size_t vfd_image_size; /* Size of VFD buffer */
int vfd_ref_count; /* Reference counter for VFD buffer */
unsigned flags; /* Flags indicate how the file image will */
/* be open */
int ref_count; /* Reference counter on udata struct */
NC_FILE_INFO_T* h5;
} H5LT_file_image_ud_t;
/* Unique id for file name */
static long file_name_counter;
/* callbacks prototypes for file image ops */
static void *local_image_malloc(size_t size, H5FD_file_image_op_t file_image_op, void *udata);
static void *local_image_memcpy(void *dest, const void *src, size_t size, H5FD_file_image_op_t file_image_op, void *udata);
static herr_t local_image_free(void *ptr, H5FD_file_image_op_t file_image_op, void *udata);
static void *local_udata_copy(void *udata);
static herr_t local_udata_free(void *udata);
/* Definition of callbacks for file image operations. */
/*-------------------------------------------------------------------------
* Function: image_malloc
*
* Purpose: Simulates malloc() function to avoid copying file images.
* The application buffer is set to the buffer on only one FAPL.
* Then the FAPL buffer can be copied to other FAPL buffers or
* to only one VFD buffer.
*
* Return: Address of "allocated" buffer, if successful. Otherwise, it returns
* NULL.
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
static void *
local_image_malloc(size_t size, H5FD_file_image_op_t file_image_op, void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
void * return_value = NULL;
TRACE1("malloc", file_image_op, _udata, size);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
switch ( file_image_op ) {
/* the app buffer is "copied" to only one FAPL. Afterwards, FAPLs can be "copied" */
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
/* It appears that the fapl memory is never created as we use it, so
we expect the udata ptr to be either null or same as the app buffer.*/
assert(udata->fapl_image_ptr == NULL || udata->fapl_image_ptr == udata->app_image_ptr);
if (udata->app_image_ptr == NULL)
goto out;
if (udata->app_image_size != size)
goto out;
if (udata->fapl_image_ptr != NULL)
goto out;
if (udata->fapl_image_size != 0)
goto out;
if (udata->fapl_ref_count != 0)
goto out;
udata->fapl_image_ptr = udata->app_image_ptr;
udata->fapl_image_size = udata->app_image_size;
return_value = udata->fapl_image_ptr;
udata->fapl_ref_count++;
return_value = udata->fapl_image_ptr;
break;
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
if (udata->fapl_image_ptr == NULL)
goto out;
if (udata->fapl_image_size != size)
goto out;
if (udata->fapl_ref_count == 0)
goto out;
return_value = udata->fapl_image_ptr;
udata->fapl_ref_count++;
break;
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
if (udata->fapl_image_ptr == NULL)
goto out;
/* fake the malloc by returning the current memory */
return_value = udata->fapl_image_ptr;
break;
case H5FD_FILE_IMAGE_OP_FILE_OPEN:
/* FAPL buffer is "copied" to only one VFD buffer */
if (udata->vfd_image_ptr != NULL)
goto out;
if (udata->vfd_image_size != 0)
goto out;
if (udata->vfd_ref_count != 0)
goto out;
if (udata->fapl_image_ptr == NULL)
goto out;
if (udata->fapl_image_size != size)
goto out;
if (udata->fapl_ref_count == 0)
goto out;
udata->vfd_image_ptr = udata->fapl_image_ptr;
udata->vfd_image_size = size;
udata->vfd_ref_count++;
return_value = udata->vfd_image_ptr;
break;
/* added unused labels to shut the compiler up */
case H5FD_FILE_IMAGE_OP_NO_OP:
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
default:
goto out;
} /* end switch */
TRACEEND("malloc",_udata,return_value);
return(return_value);
out:
TRACEFAIL("malloc");
return NULL;
} /* end image_malloc() */
/*-------------------------------------------------------------------------
* Function: image_memcpy
*
* Purpose: Simulates memcpy() function to avoid copying file images.
* The image buffer can be set to only one FAPL buffer, and
* "copied" to only one VFD buffer. The FAPL buffer can be
* "copied" to other FAPLs buffers.
*
* Return: The address of the destination buffer, if successful. Otherwise, it
* returns NULL.
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
static void *
local_image_memcpy(void *dest, const void *src, size_t size, H5FD_file_image_op_t file_image_op,
void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
TRACE3("memcpy", file_image_op, _udata,dest,src,size);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
switch(file_image_op) {
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
if (dest != udata->fapl_image_ptr)
goto out;
if (src != udata->app_image_ptr)
goto out;
if (size != udata->fapl_image_size)
goto out;
if (size != udata->app_image_size)
goto out;
if (udata->fapl_ref_count == 0)
goto out;
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY)) {
if(src != dest) {
memcpy(dest,src,size);
#ifdef TRACE
fprintf(stderr,"\t>>>> memcpy(%p,%p,%ld)\n",
dest,src,(unsigned long)size);
#endif
}
}
break;
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
if (dest != udata->fapl_image_ptr)
goto out;
if (src != udata->fapl_image_ptr)
goto out;
if (size != udata->fapl_image_size)
goto out;
if (udata->fapl_ref_count < 2)
goto out;
break;
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
/* test: src == dest == original */
if(src != dest || src != udata->fapl_image_ptr)
goto out;
break;
case H5FD_FILE_IMAGE_OP_FILE_OPEN:
if (dest != udata->vfd_image_ptr)
goto out;
if (src != udata->fapl_image_ptr)
goto out;
if (size != udata->vfd_image_size)
goto out;
if (size != udata->fapl_image_size)
goto out;
if (udata->fapl_ref_count == 0)
goto out;
if (udata->vfd_ref_count != 1)
goto out;
break;
/* added unused labels to shut the compiler up */
case H5FD_FILE_IMAGE_OP_NO_OP:
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
default:
goto out;
} /* end switch */
TRACEEND("memcpy",_udata,dest);
return(dest);
out:
TRACEFAIL("memcpy");
return NULL;
} /* end image_memcpy() */
/*-------------------------------------------------------------------------
* Function: image_realloc
*
* Purpose: Reallocates the shared application image buffer and updates data
* structures that manage buffer "copying".
*
* Return: Address of reallocated buffer, if successful. Otherwise, it returns
* NULL.
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
/* This warning is from H5FDcore.c"
Be careful of non-Posix realloc() that doesn't understand
what to do when the first argument is null.
*/
/* Modified:
1. If the realloc new size is <= existing size,
then pretend we did a realloc and return success.
This avoids unnecessary heap operations.
2. If the H5LT_FILE_IMAGE_DONT_COPY or
H5LT_FILE_IMAGE_DONT_RELEASE flag is set and the
realloc new size is > existing size, then fail
because the realloc() call may change the address
of the buffer. The new address cannot be
communicated to the application to release it.
3. Otherwise, use realloc(). Note that this may have the
side effect of freeing the previous memory chunk.
*/
static void *
local_image_realloc(void *ptr, size_t size, H5FD_file_image_op_t file_image_op, void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
void * return_value = NULL;
TRACE2("realloc", file_image_op, _udata, ptr, size);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
/* realloc() is not allowed if the image is open in read-only mode */
if (!(udata->flags & H5LT_FILE_IMAGE_OPEN_RW))
goto out;
/* DONT_COPY => DONT_RELEASE */
assert(((udata->flags & H5LT_FILE_IMAGE_DONT_COPY)?(udata->flags & H5LT_FILE_IMAGE_DONT_RELEASE):1));
/* Note that the fapl pointer is never realloc'd */
if (file_image_op == H5FD_FILE_IMAGE_OP_FILE_RESIZE) {
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY)) { /* buffer modification is allowed */
/* Divide code based on whether ptr == NULL or not */
if(ptr == NULL) {
/* From realloc man page: If ptr is NULL, then the call is equivalent to malloc(size),
for all values of size; if size is equal to zero, and ptr is not NULL, then the call
is equivalent to free(ptr). */
/* if the app_image != NULL then free it to simulate effect of realloc */
if(udata->app_image_ptr != NULL)
free(udata->app_image_ptr);
udata->vfd_image_ptr = malloc(size);
udata->vfd_ref_count++;
} else { /* ptr != NULL */
if(udata->vfd_image_ptr != ptr)
goto out;
if (udata->vfd_ref_count != 1)
goto out;
udata->vfd_image_ptr = realloc(ptr, size);
if(NULL == udata->vfd_image_ptr) {
LOG((0,"image_realloc: unable to allocate memory block of size: %lu bytes",(unsigned long)size));
goto out;
}
#ifdef TRACE
fprintf(stderr,"\t>>>> realloc(%p,%ld)=>%p\n",ptr,(unsigned long)size,udata->vfd_image_ptr);
#endif
}
udata->vfd_image_size = size;
/* Make sure other pointers are consistent */
udata->app_image_ptr = udata->vfd_image_ptr;
udata->fapl_image_ptr = udata->vfd_image_ptr;
} else { /* Cannot realloc, so fake it */
if(size <= udata->vfd_image_size) {
/* Ok, pretend we did a realloc but just change size*/
udata->vfd_image_size = size;
} else
goto out;
}
return_value = udata->vfd_image_ptr;
} /* end if */
else
goto out;
TRACEEND("realloc",_udata,return_value);
return(return_value);
out:
TRACEFAIL("realloc");
return NULL;
} /* end local_image_realloc() */
/*-------------------------------------------------------------------------
* Function: image_free
*
* Purpose: Simulates deallocation of FAPL and VFD buffers by decreasing
* reference counters. Shared application buffer is actually
* deallocated if there are no outstanding references.
*
* Return: SUCCEED or FAIL
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
static herr_t
local_image_free(void *ptr, H5FD_file_image_op_t file_image_op, void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
TRACE1("free", file_image_op, _udata, ptr);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
switch(file_image_op) {
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
if (udata->fapl_image_ptr != ptr)
goto out;
if (udata->fapl_ref_count == 0)
goto out;
udata->fapl_ref_count--;
/* For the way we use it, it should still be the case that
the fapl pointer is same as image_ptr, so we do not need
to do anything */
assert(udata->fapl_image_ptr == udata->app_image_ptr);
/* clean up */
#if 0
udata->app_image_ptr = NULL;
udata->fapl_image_ptr = NULL;
#endif
break;
case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
if (udata->vfd_image_ptr != ptr)
goto out;
if (udata->vfd_ref_count != 1)
goto out;
udata->vfd_ref_count--;
break;
/* added unused labels to keep the compiler quiet */
case H5FD_FILE_IMAGE_OP_NO_OP:
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
case H5FD_FILE_IMAGE_OP_FILE_OPEN:
case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
default:
goto out;
} /* end switch */
TRACEEND("free",_udata,1);
return(SUCCEED);
out:
TRACEFAIL("free");
return(FAIL);
} /* end image_free() */
/*-------------------------------------------------------------------------
* Function: udata_copy
*
* Purpose: Simulates the copying of the user data structure utilized in the
* management of the "copying" of file images.
*
* Return: Address of "newly allocated" structure, if successful. Otherwise, it
* returns NULL.
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
static void *
local_udata_copy(void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
TRACE0("udata_copy", 0, _udata);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
/* never copy so we only have one instance */
if (udata->ref_count == 0)
goto out;
udata->ref_count++;
TRACEEND("udata_copy",udata,1);
return(udata);
out:
TRACEFAIL("udata_copy");
return NULL;
} /* end udata_copy */
/*-------------------------------------------------------------------------
* Function: udata_free
*
* Purpose: Simulates deallocation of the user data structure utilized in the
* management of the "copying" of file images. The data structure is
* actually deallocated when there are no outstanding references.
*
* Return: SUCCEED or FAIL
*
* Programmer: Christian Chilan
*
* Date: October 3, 2011
*
*-------------------------------------------------------------------------
*/
static herr_t
local_udata_free(void *_udata)
{
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
TRACE0("udata_free", 0, _udata);
#if 0
/* callback is only used if the application buffer is not actually copied */
if (!(udata->flags & H5LT_FILE_IMAGE_DONT_COPY))
goto out;
#endif
if (udata->ref_count == 0)
goto out;
udata->ref_count--;
TRACEEND("udata_free",udata,1);
return(SUCCEED);
out:
TRACEFAIL("udata_free");
return(FAIL);
} /* end udata_free */
/* End of callbacks definitions for file image operations */
hid_t
NC4_image_init(NC_FILE_INFO_T* h5)
{
hid_t fapl = -1, file_id = -1; /* HDF5 identifiers */
unsigned file_open_flags = 0;/* Flags for hdf5 open */
char file_name[64]; /* Filename buffer */
size_t alloc_incr; /* Buffer allocation increment */
size_t min_incr = 65536; /* Minimum buffer increment */
double buf_prcnt = 0.1f; /* Percentage of buffer size to set
as increment */
unsigned imageflags;
int create = 0;
H5LT_file_image_ud_t *udata = NULL; /* Pointer to udata structure */
H5FD_file_image_callbacks_t callbacks = {&local_image_malloc, &local_image_memcpy,
&local_image_realloc, &local_image_free,
&local_udata_copy, &local_udata_free,
(void *)NULL};
imageflags = h5->mem.imageflags;
create = h5->mem.created;
/* check arguments */
if (h5->mem.memio.memory == NULL) {
if(create) {
if(h5->mem.memio.size == 0) h5->mem.memio.size = DEFAULT_CREATE_MEMSIZE;
h5->mem.memio.memory = malloc(h5->mem.memio.size);
} else
goto out; /* open requires an input buffer */
} else if(h5->mem.memio.size == 0)
goto out;
/* Create FAPL to transmit file image */
if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0)
goto out;
/* set allocation increment to a percentage of the supplied buffer size, or
* a pre-defined minimum increment value, whichever is larger
*/
if ((buf_prcnt * h5->mem.memio.size) > min_incr)
alloc_incr = (size_t)(buf_prcnt * h5->mem.memio.size);
else
alloc_incr = min_incr;
/* Configure FAPL to use the core file driver */
if (H5Pset_fapl_core(fapl, alloc_incr, FALSE) < 0)
goto out;
/* Set callbacks for file image ops always */
{
/* Allocate buffer to communicate user data to callbacks */
if (NULL == (udata = (H5LT_file_image_ud_t *)calloc(1,sizeof(H5LT_file_image_ud_t))))
goto out;
/* Initialize udata with info about app buffer containing file image and flags */
udata->app_image_ptr = h5->mem.memio.memory;
udata->app_image_size = h5->mem.memio.size;
h5->mem.memio.memory = NULL; /* move control */
h5->mem.memio.size = 0;
udata->fapl_image_ptr = NULL;
udata->fapl_image_size = 0;
udata->fapl_ref_count = 0;
udata->vfd_image_ptr = NULL;
udata->vfd_image_size = 0;
udata->vfd_ref_count = 0;
udata->flags = imageflags;
udata->ref_count = 1; /* corresponding to the first FAPL */
udata->h5 = h5;
/* copy address of udata into callbacks */
callbacks.udata = (void *)udata;
/* Set file image callbacks */
if (H5Pset_file_image_callbacks(fapl, &callbacks) < 0)
goto out;
}
/* Assign file image in user buffer to FAPL */
if (H5Pset_file_image(fapl, udata->app_image_ptr, udata->app_image_size) < 0)
goto out;
/* define a unique file name */
snprintf(file_name, (sizeof(file_name) - 1), "file_image_%ld", file_name_counter++);
/* set file open/create flags */
if(create)
file_open_flags = H5F_ACC_TRUNC; /* H5Fcreate does not like H5F_ACC_RDWR */
else if (imageflags & H5LT_FILE_IMAGE_OPEN_RW)
file_open_flags = H5F_ACC_RDWR;
else
file_open_flags = H5F_ACC_RDONLY;
/* Assign file image in FAPL to the core file driver */
if(create) {
if ((file_id = H5Fcreate(file_name, file_open_flags, H5P_DEFAULT, fapl)) < 0)
goto out;
} else {
if ((file_id = H5Fopen(file_name, file_open_flags, fapl)) < 0)
goto out;
}
/* Maintain a backward link */
h5->mem.udata = (void*)udata;
udata = NULL;
done:
/* Reclaim the fapl object */
H5E_BEGIN_TRY {
if(fapl >= 0)
H5Pclose(fapl);
} H5E_END_TRY;
/* Return file identifier */
return file_id;
out:
/* free up udata only on error */
if(udata != NULL) free(udata);
file_id = -1;
goto done;
} /* end H5LTopen_file_image() */
void
NC4_image_finalize(void* _udata)
{
if(_udata != NULL) {
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t*)_udata;
/* checks reference counts before deallocating udata */
#if 0
assert(udata->ref_count == 1 && udata->fapl_ref_count == 0 && udata->vfd_ref_count == 0);
if(udata->app_image_ptr != NULL) free(udata->app_image_ptr);
if(udata->fapl_image_ptr != NULL) free(udata->fapl_image_ptr);
if(udata->vfd_image_ptr != NULL) free(udata->vfd_image_ptr);
#endif
free(udata);
#ifdef TRACE_UDATA
fprintf(stderr,"\t>>>> freed: udata=%p\n",udata);
#endif
}
}
int
NC4_extract_file_image(NC_FILE_INFO_T* h5)
{
int stat = NC_NOERR;
H5LT_file_image_ud_t *udata;
udata = (H5LT_file_image_ud_t *)h5->mem.udata;
assert(udata != NULL);
/* Fill in h5->mem.memio from udata */
h5->mem.memio.memory = udata->vfd_image_ptr;
h5->mem.memio.size = udata->vfd_image_size;
/* Move control */
udata->vfd_image_ptr = NULL;
udata->vfd_image_size = 0;
return stat;
}
#ifdef TRACE
static char*
printudata(H5LT_file_image_ud_t* udata)
{
char buf[8192];
char tmp[8192];
char* flags = "";
buf[0] = '\0';
if(udata == NULL) return strdup("");
strlcat(buf,"flags=",sizeof(buf));
flags = traceflags(udata->flags);
strlcat(buf,flags,sizeof(buf));
if(flags != NULL) free(flags);
snprintf(tmp,sizeof(tmp)," ref_count=%d",udata->ref_count);
strlcat(buf,tmp,sizeof(tmp));
snprintf(tmp,sizeof(tmp)," app=(%p,%lld)",udata->app_image_ptr,(long long)udata->app_image_size);
strlcat(buf,tmp,sizeof(tmp));
snprintf(tmp,sizeof(tmp)," fapl=(%p,%lld)[%d]",udata->fapl_image_ptr,(long long)udata->fapl_image_size,udata->fapl_ref_count);
strlcat(buf,tmp,sizeof(tmp));
snprintf(tmp,sizeof(tmp)," vfd=(%p,%lld)[%d]",udata->vfd_image_ptr,(long long)udata->vfd_image_size,udata->vfd_ref_count);
strlcat(buf,tmp,sizeof(tmp));
return strdup(buf);
}
static void
trace(const char* fcn, H5FD_file_image_op_t op, void* _udata, ...)
{
H5LT_file_image_ud_t *udata = NULL;
va_list ap;
char buf[16000];
char tmp[8192];
char* ud;
va_start(ap, _udata); /* Requires the last fixed parameter (to get the address) */
udata = (H5LT_file_image_ud_t *)_udata;
buf[0] = '\0';
snprintf(tmp,sizeof(tmp),"trace [ %s: op=%s: ",fcn,traceop(op));
strlcat(buf,tmp,sizeof(tmp));
if(strcmp("malloc",fcn)==0) {
size_t size = va_arg(ap,size_t);
snprintf(tmp,sizeof(tmp),"size=%lld",(long long)size);
} else if(strcmp("realloc",fcn)==0) {
void* ptr = va_arg(ap,void*);
size_t size = va_arg(ap,size_t);
snprintf(tmp,sizeof(tmp),"ptr=%p, size=%lld",ptr,(long long)size);
} else if(strcmp("free",fcn)==0) {
void* ptr = va_arg(ap,void*);
snprintf(tmp,sizeof(tmp),"ptr=%p",ptr);
} else if(strcmp("memcpy",fcn)==0) {
void* dest = va_arg(ap,void*);
void* src = va_arg(ap,void*);
size_t size = va_arg(ap,size_t);
snprintf(tmp,sizeof(tmp),"dest=%p, src=%p, size=%lld",dest,src,(long long)size);
} else if(strcmp("udata_copy",fcn)==0) {
#ifdef TRACE_UDATA
snprintf(tmp,sizeof(tmp),"udata=%p",udata);
#endif
} else if(strcmp("udata_free",fcn)==0) {
#ifdef TRACE_UDATA
snprintf(tmp,sizeof(tmp),"udata=%p",udata);
#endif
} else {
snprintf(tmp,sizeof(tmp),"unknown fcn: %s",fcn);
}
strlcat(buf,tmp,sizeof(buf));
ud = printudata(udata);
strlcat(buf,"\n\tudata=",sizeof(buf));
strlcat(buf,ud,sizeof(tmp));
free(ud);
strlcat(buf,"\n",sizeof(buf));
va_end(ap);
fprintf(stderr,"%s",buf);
fflush(stderr);
}
static void
traceend(const char* fcn, void* _udata, uintptr_t retval)
{
char buf[16000];
char tmp[8192];
char* ud;
const char* tab = " ";
buf[0] = '\0';
H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
snprintf(tmp,sizeof(tmp),"%s]: retval=%p",tab,(void*)retval);
strlcat(buf,tmp,sizeof(buf));
strlcat(buf," udata=",sizeof(buf));
ud = printudata(udata);
strlcat(buf,ud,sizeof(tmp));
free(ud);
strlcat(buf,"\n",sizeof(buf));
fprintf(stderr,"%s",buf);
fflush(stderr);
}
#endif /*TRACE*/
#ifdef CATCH
static void
tracefail(const char* fcn)
{
fprintf(stderr,"fail: %s\n",fcn);
fflush(stderr);
}
#endif /*CATCH*/
#ifdef TRACE
static char*
traceflags(int flags)
{
int i;
char buf[8192];
char tmp[8192];
buf[0] = '\0';
for(i=0;i<16;i++) {
tmp[0] = '\0';
if((flags & 1<<i) == 0) continue;
if(i > 0) strlcat(tmp,"|",sizeof(tmp));
switch(1<<i) {
case H5LT_FILE_IMAGE_OPEN_RW: /* 0x0001 Open image for read-write */
strlcat(tmp,"OPEN_RW",sizeof(tmp));
break;
case H5LT_FILE_IMAGE_DONT_COPY: /*0x0002 the HDF5 lib won't copy */