/*------------------------------------------------------------------------- * Function: main * * Purpose: Create a file, create a local heap, write data into the local * heap, close the file, open the file, read data out of the * local heap, close the file. * * Return: Success: zero * * Failure: non-zero * * Programmer: Robb Matzke * Tuesday, November 24, 1998 * * Modifications: * *------------------------------------------------------------------------- */ int main(void) { hid_t fapl=H5P_DEFAULT; /*file access properties */ hid_t file=-1; /*hdf5 file */ H5F_t *f=NULL; /*hdf5 file pointer */ char filename[1024]; /*file name */ haddr_t heap_addr; /*local heap address */ H5HL_t *heap = NULL; /*local heap */ size_t obj[NOBJS]; /*offsets within the heap */ int i, j; /*miscellaneous counters */ char buf[1024]; /*the value to store */ const char *s; /*value to read */ /* Reset library */ h5_reset(); fapl = h5_fileaccess(); /* * Test writing to the heap... */ TESTING("local heap write"); h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl))<0) goto error; if(NULL == (f = H5I_object(file))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } if(H5HL_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)0, &heap_addr/*out*/) < 0) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } if (NULL == (heap = H5HL_protect(f, H5P_DATASET_XFER_DEFAULT, heap_addr, H5AC_WRITE))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } for(i = 0; i < NOBJS; i++) { sprintf(buf, "%03d-", i); for (j=4; j<i; j++) buf[j] = '0' + j%10; if (j>4) buf[j] = '\0'; if ((size_t)(-1)==(obj[i]=H5HL_insert(f, H5P_DATASET_XFER_DEFAULT, heap, strlen(buf)+1, buf))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } } if (H5HL_unprotect(f, H5P_DATASET_XFER_DEFAULT, heap, heap_addr) < 0) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } if (H5Fclose(file)<0) goto error; PASSED(); /* * Test reading from the heap... */ TESTING("local heap read"); h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if ((file=H5Fopen(filename, H5F_ACC_RDONLY, fapl))<0) goto error; if (NULL==(f=H5I_object(file))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } for (i=0; i<NOBJS; i++) { sprintf(buf, "%03d-", i); for (j=4; j<i; j++) buf[j] = '0' + j%10; if (j>4) buf[j] = '\0'; if (NULL == (heap = H5HL_protect(f, H5P_DATASET_XFER_DEFAULT, heap_addr, H5AC_READ))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } if (NULL == (s = H5HL_offset_into(f, heap, obj[i]))) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } if (strcmp(s, buf)) { H5_FAILED(); printf(" i=%d, heap offset=%lu\n", i, (unsigned long)(obj[i])); printf(" got: \"%s\"\n", s); printf(" ans: \"%s\"\n", buf); goto error; } if (H5HL_unprotect(f, H5P_DATASET_XFER_DEFAULT, heap, heap_addr) < 0) { H5_FAILED(); H5Eprint2(H5E_DEFAULT, stdout); goto error; } } if (H5Fclose(file)<0) goto error; PASSED(); puts("All local heap tests passed."); h5_cleanup(FILENAME, fapl); return 0; error: puts("*** TESTS FAILED ***"); H5E_BEGIN_TRY { H5Fclose(file); } H5E_END_TRY; return 1; }
/*------------------------------------------------------------------------- * Function: H5O_efl_copy_file * * Purpose: Copies an efl message from _MESG to _DEST in file * * Return: Success: Ptr to _DEST * * Failure: NULL * * Programmer: Peter Cao * September 29, 2005 * *------------------------------------------------------------------------- */ static void * H5O_efl_copy_file(H5F_t H5_ATTR_UNUSED *file_src, void *mesg_src, H5F_t *file_dst, hbool_t H5_ATTR_UNUSED *recompute_size, unsigned H5_ATTR_UNUSED *mesg_flags, H5O_copy_t H5_ATTR_UNUSED *cpy_info, void H5_ATTR_UNUSED *_udata, hid_t dxpl_id) { H5O_efl_t *efl_src = (H5O_efl_t *) mesg_src; H5O_efl_t *efl_dst = NULL; H5HL_t *heap = NULL; /* Pointer to local heap for EFL file names */ size_t idx, size, name_offset, heap_size; void *ret_value = NULL; /* Return value */ FUNC_ENTER_NOAPI_NOINIT_TAG(dxpl_id, H5AC__COPIED_TAG, NULL) /* check args */ HDassert(efl_src); HDassert(file_dst); /* Allocate space for the destination efl */ if(NULL == (efl_dst = (H5O_efl_t *)H5MM_calloc(sizeof(H5O_efl_t)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") /* Copy the "top level" information */ HDmemcpy(efl_dst, efl_src, sizeof(H5O_efl_t)); /* Determine size needed for destination heap */ heap_size = H5HL_ALIGN(1); /* "empty" name */ for(idx = 0; idx < efl_src->nused; idx++) heap_size += H5HL_ALIGN(HDstrlen(efl_src->slot[idx].name) + 1); /* Create name heap */ if(H5HL_create(file_dst, dxpl_id, heap_size, &efl_dst->heap_addr/*out*/) < 0) HGOTO_ERROR(H5E_EFL, H5E_CANTINIT, NULL, "can't create heap") /* Pin the heap down in memory */ if(NULL == (heap = H5HL_protect(file_dst, dxpl_id, efl_dst->heap_addr, H5AC__NO_FLAGS_SET))) HGOTO_ERROR(H5E_EFL, H5E_PROTECT, NULL, "unable to protect EFL file name heap") /* Insert "empty" name first */ if((size_t)(-1) == (name_offset = H5HL_insert(file_dst, dxpl_id, heap, (size_t)1, ""))) HGOTO_ERROR(H5E_EFL, H5E_CANTINSERT, NULL, "can't insert file name into heap") HDassert(0 == name_offset); /* allocate array of external file entries */ if(efl_src->nalloc > 0) { size = efl_src->nalloc * sizeof(H5O_efl_entry_t); if((efl_dst->slot = (H5O_efl_entry_t *)H5MM_calloc(size)) == NULL) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") /* copy content from the source. Need to update later */ HDmemcpy(efl_dst->slot, efl_src->slot, size); } /* end if */ /* copy the name from the source */ for(idx = 0; idx < efl_src->nused; idx++) { efl_dst->slot[idx].name = H5MM_xstrdup(efl_src->slot[idx].name); if((size_t)(-1) == (efl_dst->slot[idx].name_offset = H5HL_insert(file_dst, dxpl_id, heap, HDstrlen(efl_dst->slot[idx].name) + 1, efl_dst->slot[idx].name))) HGOTO_ERROR(H5E_EFL, H5E_CANTINSERT, NULL, "can't insert file name into heap") } /* end for */ /* Set return value */ ret_value = efl_dst; done: /* Release resources */ if(heap && H5HL_unprotect(heap) < 0) HDONE_ERROR(H5E_EFL, H5E_PROTECT, NULL, "unable to unprotect EFL file name heap") if(!ret_value) if(efl_dst) H5MM_xfree(efl_dst); FUNC_LEAVE_NOAPI_TAG(ret_value, NULL) } /* end H5O_efl_copy_file() */