1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * Copyright by The HDF Group. * 3 * Copyright by the Board of Trustees of the University of Illinois. * 4 * All rights reserved. * 5 * * 6 * This file is part of HDF5. The full HDF5 copyright notice, including * 7 * terms governing use, modification, and redistribution, is contained in * 8 * the COPYING file, which can be found at the root of the source code * 9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. * 10 * If you do not have access to either file, you may request a copy from * 11 * help@hdfgroup.org. * 12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 13 14 /*------------------------------------------------------------------------- 15 * 16 * Created: H5EAdblkpage.c 17 * Nov 20 2008 18 * Quincey Koziol <koziol@hdfgroup.org> 19 * 20 * Purpose: Data block page routines for extensible arrays. 21 * 22 *------------------------------------------------------------------------- 23 */ 24 25 /**********************/ 26 /* Module Declaration */ 27 /**********************/ 28 29 #include "H5EAmodule.h" /* This source code file is part of the H5EA module */ 30 31 32 /***********************/ 33 /* Other Packages Used */ 34 /***********************/ 35 36 37 /***********/ 38 /* Headers */ 39 /***********/ 40 #include "H5private.h" /* Generic Functions */ 41 #include "H5Eprivate.h" /* Error handling */ 42 #include "H5EApkg.h" /* Extensible Arrays */ 43 #include "H5FLprivate.h" /* Free Lists */ 44 #include "H5VMprivate.h" /* Vectors and arrays */ 45 46 47 /****************/ 48 /* Local Macros */ 49 /****************/ 50 51 52 /******************/ 53 /* Local Typedefs */ 54 /******************/ 55 56 57 /********************/ 58 /* Package Typedefs */ 59 /********************/ 60 61 62 /********************/ 63 /* Local Prototypes */ 64 /********************/ 65 66 67 /*********************/ 68 /* Package Variables */ 69 /*********************/ 70 71 72 /*****************************/ 73 /* Library Private Variables */ 74 /*****************************/ 75 76 77 /*******************/ 78 /* Local Variables */ 79 /*******************/ 80 81 /* Declare a free list to manage the H5EA_dblk_page_t struct */ 82 H5FL_DEFINE_STATIC(H5EA_dblk_page_t); 83 84 85 86 /*------------------------------------------------------------------------- 87 * Function: H5EA__dblk_page_alloc 88 * 89 * Purpose: Allocate extensible array data block page 90 * 91 * Return: Non-NULL pointer to data block on success/NULL on failure 92 * 93 * Programmer: Quincey Koziol 94 * koziol@hdfgroup.org 95 * Nov 20 2008 96 * 97 *------------------------------------------------------------------------- 98 */ 99 BEGIN_FUNC(PKG, ERR, 100 H5EA_dblk_page_t *, NULL, NULL, 101 H5EA__dblk_page_alloc(H5EA_hdr_t *hdr, H5EA_sblock_t *parent)) 102 103 /* Local variables */ 104 H5EA_dblk_page_t *dblk_page = NULL; /* Extensible array data block page */ 105 106 /* Check arguments */ 107 HDassert(hdr); 108 109 /* Allocate memory for the data block */ 110 if(NULL == (dblk_page = H5FL_CALLOC(H5EA_dblk_page_t))) 111 H5E_THROW(H5E_CANTALLOC, "memory allocation failed for extensible array data block page") 112 113 /* Share common array information */ 114 if(H5EA__hdr_incr(hdr) < 0) 115 H5E_THROW(H5E_CANTINC, "can't increment reference count on shared array header") 116 dblk_page->hdr = hdr; 117 118 /* Set non-zero internal fields */ 119 dblk_page->parent = parent; 120 121 /* Allocate buffer for elements in data block page */ 122 if(NULL == (dblk_page->elmts = H5EA__hdr_alloc_elmts(hdr, hdr->dblk_page_nelmts))) 123 H5E_THROW(H5E_CANTALLOC, "memory allocation failed for data block page element buffer") 124 125 /* Set the return value */ 126 ret_value = dblk_page; 127 128 CATCH 129 if(!ret_value) 130 if(dblk_page && H5EA__dblk_page_dest(dblk_page) < 0) 131 H5E_THROW(H5E_CANTFREE, "unable to destroy extensible array data block page") 132 133 END_FUNC(PKG) /* end H5EA__dblk_page_alloc() */ 134 135 136 /*------------------------------------------------------------------------- 137 * Function: H5EA__dblk_page_create 138 * 139 * Purpose: Creates a new extensible array data block page in the file 140 * 141 * Return: Valid file address on success/HADDR_UNDEF on failure 142 * 143 * Programmer: Quincey Koziol 144 * koziol@hdfgroup.org 145 * Nov 20 2008 146 * 147 *------------------------------------------------------------------------- 148 */ 149 BEGIN_FUNC(PKG, ERR, 150 herr_t, SUCCEED, FAIL, 151 H5EA__dblk_page_create(H5EA_hdr_t *hdr, hid_t dxpl_id, H5EA_sblock_t *parent, 152 haddr_t addr)) 153 154 /* Local variables */ 155 H5EA_dblk_page_t *dblk_page = NULL; /* Extensible array data block page */ 156 hbool_t inserted = FALSE; /* Whether the header was inserted into cache */ 157 158 /* Sanity check */ 159 HDassert(hdr); 160 161 /* Allocate the data block page */ 162 if(NULL == (dblk_page = H5EA__dblk_page_alloc(hdr, parent))) 163 H5E_THROW(H5E_CANTALLOC, "memory allocation failed for extensible array data block page") 164 165 /* Set info about data block page on disk */ 166 dblk_page->addr = addr; 167 dblk_page->size = H5EA_DBLK_PAGE_SIZE(hdr); 168 169 /* Clear any elements in data block page to fill value */ 170 if((hdr->cparam.cls->fill)(dblk_page->elmts, (size_t)hdr->dblk_page_nelmts) < 0) 171 H5E_THROW(H5E_CANTSET, "can't set extensible array data block page elements to class's fill value") 172 173 /* Cache the new extensible array data block page */ 174 if(H5AC_insert_entry(hdr->f, dxpl_id, H5AC_EARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, H5AC__NO_FLAGS_SET) < 0) 175 H5E_THROW(H5E_CANTINSERT, "can't add extensible array data block page to cache") 176 inserted = TRUE; 177 178 /* Add data block page as child of 'top' proxy */ 179 if(hdr->top_proxy) { 180 if(H5AC_proxy_entry_add_child(hdr->top_proxy, hdr->f, dxpl_id, dblk_page) < 0) 181 H5E_THROW(H5E_CANTSET, "unable to add extensible array entry as child of array proxy") 182 dblk_page->top_proxy = hdr->top_proxy; 183 } /* end if */ 184 185 CATCH 186 if(ret_value < 0) 187 if(dblk_page) { 188 /* Remove from cache, if inserted */ 189 if(inserted) 190 if(H5AC_remove_entry(dblk_page) < 0) 191 H5E_THROW(H5E_CANTREMOVE, "unable to remove extensible array data block page from cache") 192 193 /* Destroy data block page */ 194 if(H5EA__dblk_page_dest(dblk_page) < 0) 195 H5E_THROW(H5E_CANTFREE, "unable to destroy extensible array data block page") 196 } /* end if */ 197 198 END_FUNC(PKG) /* end H5EA__dblk_page_create() */ 199 200 201 /*------------------------------------------------------------------------- 202 * Function: H5EA__dblk_page_protect 203 * 204 * Purpose: Convenience wrapper around protecting extensible array data 205 * block page 206 * 207 * Return: Non-NULL pointer to data block page on success/NULL on failure 208 * 209 * Programmer: Quincey Koziol 210 * koziol@hdfgroup.org 211 * Nov 20 2008 212 * 213 *------------------------------------------------------------------------- 214 */ 215 BEGIN_FUNC(PKG, ERR, 216 H5EA_dblk_page_t *, NULL, NULL, 217 H5EA__dblk_page_protect(H5EA_hdr_t *hdr, hid_t dxpl_id, H5EA_sblock_t *parent, 218 haddr_t dblk_page_addr, unsigned flags)) 219 220 /* Local variables */ 221 H5EA_dblk_page_t *dblk_page = NULL; /* Extensible array data block page */ 222 H5EA_dblk_page_cache_ud_t udata; /* Information needed for loading data block page */ 223 224 /* Sanity check */ 225 HDassert(hdr); 226 HDassert(H5F_addr_defined(dblk_page_addr)); 227 228 /* only the H5AC__READ_ONLY_FLAG may be set */ 229 HDassert((flags & (unsigned)(~H5AC__READ_ONLY_FLAG)) == 0); 230 231 /* Set up user data */ 232 udata.hdr = hdr; 233 udata.parent = parent; 234 udata.dblk_page_addr = dblk_page_addr; 235 236 /* Protect the data block page */ 237 if(NULL == (dblk_page = (H5EA_dblk_page_t *)H5AC_protect(hdr->f, dxpl_id, H5AC_EARRAY_DBLK_PAGE, dblk_page_addr, &udata, flags))) 238 H5E_THROW(H5E_CANTPROTECT, "unable to protect extensible array data block page, address = %llu", (unsigned long long)dblk_page_addr) 239 240 /* Create top proxy, if it doesn't exist */ 241 if(hdr->top_proxy && NULL == dblk_page->top_proxy) { 242 /* Add data block page as child of 'top' proxy */ 243 if(H5AC_proxy_entry_add_child(hdr->top_proxy, hdr->f, dxpl_id, dblk_page) < 0) 244 H5E_THROW(H5E_CANTSET, "unable to add extensible array entry as child of array proxy") 245 dblk_page->top_proxy = hdr->top_proxy; 246 } /* end if */ 247 248 /* Set return value */ 249 ret_value = dblk_page; 250 251 CATCH 252 /* Clean up on error */ 253 if(!ret_value) { 254 /* Release the data block page, if it was protected */ 255 if(dblk_page && H5AC_unprotect(hdr->f, dxpl_id, H5AC_EARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, H5AC__NO_FLAGS_SET) < 0) 256 H5E_THROW(H5E_CANTUNPROTECT, "unable to unprotect extensible array data block page, address = %llu", (unsigned long long)dblk_page->addr) 257 } /* end if */ 258 259 END_FUNC(PKG) /* end H5EA__dblk_page_protect() */ 260 261 262 /*------------------------------------------------------------------------- 263 * Function: H5EA__dblk_page_unprotect 264 * 265 * Purpose: Convenience wrapper around unprotecting extensible array 266 * data block page 267 * 268 * Return: Non-negative on success/Negative on failure 269 * 270 * Programmer: Quincey Koziol 271 * koziol@hdfgroup.org 272 * Nov 20 2008 273 * 274 *------------------------------------------------------------------------- 275 */ 276 BEGIN_FUNC(PKG, ERR, 277 herr_t, SUCCEED, FAIL, 278 H5EA__dblk_page_unprotect(H5EA_dblk_page_t *dblk_page, hid_t dxpl_id, 279 unsigned cache_flags)) 280 281 /* Local variables */ 282 283 /* Sanity check */ 284 HDassert(dblk_page); 285 286 /* Unprotect the data block page */ 287 if(H5AC_unprotect(dblk_page->hdr->f, dxpl_id, H5AC_EARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, cache_flags) < 0) 288 H5E_THROW(H5E_CANTUNPROTECT, "unable to unprotect extensible array data block page, address = %llu", (unsigned long long)dblk_page->addr) 289 290 CATCH 291 292 END_FUNC(PKG) /* end H5EA__dblk_page_unprotect() */ 293 294 295 /*------------------------------------------------------------------------- 296 * Function: H5EA__dblk_page_dest 297 * 298 * Purpose: Destroys an extensible array data block page in memory. 299 * 300 * Return: Non-negative on success/Negative on failure 301 * 302 * Programmer: Quincey Koziol 303 * koziol@hdfgroup.org 304 * Nov 20 2008 305 * 306 *------------------------------------------------------------------------- 307 */ 308 BEGIN_FUNC(PKG, ERR, 309 herr_t, SUCCEED, FAIL, 310 H5EA__dblk_page_dest(H5EA_dblk_page_t *dblk_page)) 311 312 /* Sanity check */ 313 HDassert(dblk_page); 314 HDassert(!dblk_page->has_hdr_depend); 315 316 /* Check if header field has been initialized */ 317 if(dblk_page->hdr) { 318 /* Check if buffer for data block page elements has been initialized */ 319 if(dblk_page->elmts) { 320 /* Free buffer for data block page elements */ 321 if(H5EA__hdr_free_elmts(dblk_page->hdr, dblk_page->hdr->dblk_page_nelmts, dblk_page->elmts) < 0) 322 H5E_THROW(H5E_CANTFREE, "unable to free extensible array data block element buffer") 323 dblk_page->elmts = NULL; 324 } /* end if */ 325 326 /* Decrement reference count on shared info */ 327 if(H5EA__hdr_decr(dblk_page->hdr) < 0) 328 H5E_THROW(H5E_CANTDEC, "can't decrement reference count on shared array header") 329 dblk_page->hdr = NULL; 330 } /* end if */ 331 332 /* Sanity check */ 333 HDassert(NULL == dblk_page->top_proxy); 334 335 /* Free the data block page itself */ 336 dblk_page = H5FL_FREE(H5EA_dblk_page_t, dblk_page); 337 338 CATCH 339 340 END_FUNC(PKG) /* end H5EA__dblk_page_dest() */ 341 342