med_memfile.hxx
Aller à la documentation de ce fichier.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef MED_MEMFILE_H
00020 #define MED_MEMFILE_H
00021
00022 #include <hdf5.h>
00023
00024 #ifdef __cplusplus
00025 extern "C" {
00026 #endif
00027
00028
00029 typedef struct {
00030 void *app_image_ptr;
00031 size_t app_image_size;
00032 void *fapl_image_ptr;
00033 size_t fapl_image_size;
00034 int fapl_ref_count;
00035 void *vfd_image_ptr;
00036 size_t vfd_image_size;
00037 int vfd_ref_count;
00038 unsigned flags;
00039
00040 int ref_count;
00041 } H5LT_file_image_ud_t;
00042
00043
00044 static void *image_malloc(size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00045 static void *image_memcpy(void *dest, const void *src, size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00046 static void *image_realloc(void *ptr, size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00047 static herr_t image_free(void *ptr, H5FD_file_image_op_t file_image_op, void *udata);
00048 static void *udata_copy(void *udata);
00049 static herr_t udata_free(void *udata);
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 typedef struct {
00072 void *app_image_ptr;
00073 size_t app_image_size;
00074 void *fapl_image_ptr;
00075 size_t fapl_image_size;
00076 int fapl_ref_count;
00077 void *vfd_image_ptr;
00078 size_t vfd_image_size;
00079 int vfd_ref_count;
00080 unsigned flags;
00081
00082 int ref_count;
00083 } H5LT_file_image_ud_t;
00084
00085
00086 static void *image_malloc(size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00087 static void *image_memcpy(void *dest, const void *src, size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00088 static void *image_realloc(void *ptr, size_t size, H5FD_file_image_op_t file_image_op, void *udata);
00089 static herr_t image_free(void *ptr, H5FD_file_image_op_t file_image_op, void *udata);
00090 static void *udata_copy(void *udata);
00091 static herr_t udata_free(void *udata);
00092
00093 static void *
00094 image_malloc(size_t size, H5FD_file_image_op_t file_image_op, void *_udata)
00095 {
00096 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00097 void * return_value = NULL;
00098
00099
00100
00101
00102
00103 switch ( file_image_op ) {
00104
00105 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
00106 if (udata->app_image_ptr == NULL)
00107 goto out;
00108 if (udata->app_image_size != size)
00109 goto out;
00110 if (udata->fapl_image_ptr != NULL)
00111 goto out;
00112 if (udata->fapl_image_size != 0)
00113 goto out;
00114 if (udata->fapl_ref_count != 0)
00115 goto out;
00116
00117 udata->fapl_image_ptr = udata->app_image_ptr;
00118 udata->fapl_image_size = udata->app_image_size;
00119 return_value = udata->fapl_image_ptr;
00120 udata->fapl_ref_count++;
00121 break;
00122
00123 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
00124 if (udata->fapl_image_ptr == NULL)
00125 goto out;
00126 if (udata->fapl_image_size != size)
00127 goto out;
00128 if (udata->fapl_ref_count == 0)
00129 goto out;
00130
00131 return_value = udata->fapl_image_ptr;
00132 udata->fapl_ref_count++;
00133 break;
00134
00135 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
00136 goto out;
00137
00138 case H5FD_FILE_IMAGE_OP_FILE_OPEN:
00139
00140 if (udata->vfd_image_ptr != NULL)
00141 goto out;
00142 if (udata->vfd_image_size != 0)
00143 goto out;
00144 if (udata->vfd_ref_count != 0)
00145 goto out;
00146 if (udata->fapl_image_ptr == NULL)
00147 goto out;
00148 if (udata->fapl_image_size != size)
00149 goto out;
00150 if (udata->fapl_ref_count == 0)
00151 goto out;
00152
00153 udata->vfd_image_ptr = udata->fapl_image_ptr;
00154 udata->vfd_image_size = size;
00155 udata->vfd_ref_count++;
00156 return_value = udata->vfd_image_ptr;
00157 break;
00158
00159
00160 case H5FD_FILE_IMAGE_OP_NO_OP:
00161 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
00162 case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
00163 case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
00164 default:
00165 goto out;
00166 }
00167
00168 return(return_value);
00169
00170 out:
00171 return NULL;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 static void *
00193 image_memcpy(void *dest, const void *src, size_t size, H5FD_file_image_op_t file_image_op,
00194 void *_udata)
00195 {
00196 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00197
00198
00199
00200
00201
00202 switch(file_image_op) {
00203 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
00204 if (dest != udata->fapl_image_ptr)
00205 goto out;
00206 if (src != udata->app_image_ptr)
00207 goto out;
00208 if (size != udata->fapl_image_size)
00209 goto out;
00210 if (size != udata->app_image_size)
00211 goto out;
00212 if (udata->fapl_ref_count == 0)
00213 goto out;
00214 break;
00215
00216 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
00217 if (dest != udata->fapl_image_ptr)
00218 goto out;
00219 if (src != udata->fapl_image_ptr)
00220 goto out;
00221 if (size != udata->fapl_image_size)
00222 goto out;
00223 if (udata->fapl_ref_count < 2)
00224 goto out;
00225 break;
00226
00227 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
00228 goto out;
00229
00230 case H5FD_FILE_IMAGE_OP_FILE_OPEN:
00231 if (dest != udata->vfd_image_ptr)
00232 goto out;
00233 if (src != udata->fapl_image_ptr)
00234 goto out;
00235 if (size != udata->vfd_image_size)
00236 goto out;
00237 if (size != udata->fapl_image_size)
00238 goto out;
00239 if (udata->fapl_ref_count == 0)
00240 goto out;
00241 if (udata->vfd_ref_count != 1)
00242 goto out;
00243 break;
00244
00245
00246 case H5FD_FILE_IMAGE_OP_NO_OP:
00247 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
00248 case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
00249 case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
00250 default:
00251 goto out;
00252 }
00253
00254 return(dest);
00255
00256 out:
00257 return NULL;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 static void *
00277 image_realloc(void *ptr, size_t size, H5FD_file_image_op_t file_image_op, void *_udata)
00278 {
00279 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00280 void * return_value = NULL;
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 if (!(udata->flags & H5LT_FILE_IMAGE_OPEN_RW))
00294 goto out;
00295
00296 if (file_image_op == H5FD_FILE_IMAGE_OP_FILE_RESIZE) {
00297 XSCRUTE(ptr);
00298 XSCRUTE(udata->vfd_image_ptr);
00299 ISCRUTE_long(size);
00300 ISCRUTE_long( udata->vfd_image_size);
00301 if (udata->vfd_image_ptr != ptr)
00302 goto out;
00303
00304 if (udata->vfd_ref_count != 1)
00305 goto out;
00306
00307 if (NULL == (udata->vfd_image_ptr = realloc(ptr, size)))
00308 goto out;
00309
00310 udata->vfd_image_size = size;
00311 return_value = udata->vfd_image_ptr;
00312 }
00313 else
00314 goto out;
00315
00316 return(return_value);
00317
00318 out:
00319 return NULL;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 static herr_t
00339 image_free(void *ptr, H5FD_file_image_op_t file_image_op, void *_udata)
00340 {
00341 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00342
00343
00344
00345
00346
00347 switch(file_image_op) {
00348 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_CLOSE:
00349 if (udata->fapl_image_ptr != ptr)
00350 goto out;
00351 if (udata->fapl_ref_count == 0)
00352 goto out;
00353
00354 udata->fapl_ref_count--;
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 break;
00365
00366 case H5FD_FILE_IMAGE_OP_FILE_CLOSE:
00367 if (udata->vfd_image_ptr != ptr)
00368 goto out;
00369 if (udata->vfd_ref_count != 1)
00370 goto out;
00371
00372 udata->vfd_ref_count--;
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 break;
00383
00384
00385 case H5FD_FILE_IMAGE_OP_NO_OP:
00386 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_SET:
00387 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_COPY:
00388 case H5FD_FILE_IMAGE_OP_PROPERTY_LIST_GET:
00389 case H5FD_FILE_IMAGE_OP_FILE_OPEN:
00390 case H5FD_FILE_IMAGE_OP_FILE_RESIZE:
00391 default:
00392 goto out;
00393 }
00394
00395 return(SUCCEED);
00396
00397 out:
00398 return(FAIL);
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 static void *
00418 udata_copy(void *_udata)
00419 {
00420 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00421
00422
00423
00424
00425 if (udata->ref_count == 0)
00426 goto out;
00427
00428 udata->ref_count++;
00429
00430 return(udata);
00431
00432 out:
00433 return NULL;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452 static herr_t
00453 udata_free(void *_udata)
00454 {
00455 H5LT_file_image_ud_t *udata = (H5LT_file_image_ud_t *)_udata;
00456
00457
00458
00459
00460 if (udata->ref_count == 0)
00461 goto out;
00462
00463 udata->ref_count--;
00464
00465
00466 if (udata->ref_count == 0 && udata->fapl_ref_count == 0 &&
00467 udata->vfd_ref_count == 0)
00468 HDfree(udata);
00469
00470 return(SUCCEED);
00471
00472 out:
00473 return(FAIL);
00474 }
00475
00476
00477
00478
00479 #ifdef __cplusplus
00480 }
00481 #endif
00482
00483 #endif