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