/src/hdf5/src/H5FAdblkpage.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: H5FAdblkpage.c |
16 | | * |
17 | | * Purpose: Data block page routines for fixed array. |
18 | | * |
19 | | *------------------------------------------------------------------------- |
20 | | */ |
21 | | |
22 | | /**********************/ |
23 | | /* Module Declaration */ |
24 | | /**********************/ |
25 | | |
26 | | #include "H5FAmodule.h" /* This source code file is part of the H5FA 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 "H5FApkg.h" /* Fixed 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 H5FA_dblk_page_t struct */ |
69 | | H5FL_DEFINE_STATIC(H5FA_dblk_page_t); |
70 | | |
71 | | /* Declare a free list to manage the page elements */ |
72 | | H5FL_BLK_DEFINE(page_elmts); |
73 | | |
74 | | /*------------------------------------------------------------------------- |
75 | | * Function: H5FA__dblk_page_alloc |
76 | | * |
77 | | * Purpose: Allocate fixed array data block page |
78 | | * |
79 | | * Return: Non-NULL pointer to data block on success/NULL on failure |
80 | | * |
81 | | *------------------------------------------------------------------------- |
82 | | */ |
83 | | H5FA_dblk_page_t * |
84 | | H5FA__dblk_page_alloc(H5FA_hdr_t *hdr, size_t nelmts) |
85 | 0 | { |
86 | 0 | H5FA_dblk_page_t *dblk_page = NULL; /* Fixed array data block page */ |
87 | 0 | H5FA_dblk_page_t *ret_value = NULL; |
88 | |
|
89 | 0 | FUNC_ENTER_PACKAGE |
90 | | |
91 | | /* Check arguments */ |
92 | 0 | assert(hdr); |
93 | | |
94 | | /* Allocate memory for the data block */ |
95 | 0 | if (NULL == (dblk_page = H5FL_CALLOC(H5FA_dblk_page_t))) |
96 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTALLOC, NULL, |
97 | 0 | "memory allocation failed for fixed array data block page"); |
98 | | |
99 | | /* Share common array information */ |
100 | 0 | if (H5FA__hdr_incr(hdr) < 0) |
101 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTINC, NULL, "can't increment reference count on shared array header"); |
102 | 0 | dblk_page->hdr = hdr; |
103 | | |
104 | | /* Set non-zero internal fields */ |
105 | 0 | dblk_page->nelmts = nelmts; |
106 | | |
107 | | /* Allocate buffer for elements in data block page */ |
108 | 0 | if (NULL == (dblk_page->elmts = H5FL_BLK_MALLOC(page_elmts, nelmts * hdr->cparam.cls->nat_elmt_size))) |
109 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTALLOC, NULL, |
110 | 0 | "memory allocation failed for data block page element buffer"); |
111 | | |
112 | | /* Set the return value */ |
113 | 0 | ret_value = dblk_page; |
114 | |
|
115 | 0 | done: |
116 | |
|
117 | 0 | if (!ret_value) |
118 | 0 | if (dblk_page && H5FA__dblk_page_dest(dblk_page) < 0) |
119 | 0 | HDONE_ERROR(H5E_FARRAY, H5E_CANTFREE, NULL, "unable to destroy fixed array data block page"); |
120 | |
|
121 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
122 | 0 | } /* end H5FA__dblk_page_alloc() */ |
123 | | |
124 | | /*------------------------------------------------------------------------- |
125 | | * Function: H5FA__dblk_page_create |
126 | | * |
127 | | * Purpose: Creates a new fixed array data block page in the file |
128 | | * |
129 | | * Return: SUCCEED/FAIL |
130 | | * |
131 | | *------------------------------------------------------------------------- |
132 | | */ |
133 | | herr_t |
134 | | H5FA__dblk_page_create(H5FA_hdr_t *hdr, haddr_t addr, size_t nelmts) |
135 | 0 | { |
136 | 0 | H5FA_dblk_page_t *dblk_page = NULL; /* Fixed array data block page */ |
137 | 0 | bool inserted = false; /* Whether the header was inserted into cache */ |
138 | 0 | herr_t ret_value = SUCCEED; |
139 | |
|
140 | 0 | FUNC_ENTER_PACKAGE |
141 | |
|
142 | | #ifdef H5FA_DEBUG |
143 | | fprintf(stderr, "%s: Called, addr = %" PRIuHADDR "\n", __func__, addr); |
144 | | #endif /* H5FA_DEBUG */ |
145 | | |
146 | | /* Sanity check */ |
147 | 0 | assert(hdr); |
148 | | |
149 | | /* Allocate the data block page */ |
150 | 0 | if (NULL == (dblk_page = H5FA__dblk_page_alloc(hdr, nelmts))) |
151 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTALLOC, FAIL, |
152 | 0 | "memory allocation failed for fixed array data block page"); |
153 | | |
154 | | /* Set info about data block page on disk */ |
155 | 0 | dblk_page->addr = addr; |
156 | 0 | dblk_page->size = H5FA_DBLK_PAGE_SIZE(hdr, nelmts); |
157 | | #ifdef H5FA_DEBUG |
158 | | fprintf(stderr, "%s: dblk_page->size = %Zu\n", __func__, dblk_page->size); |
159 | | #endif /* H5FA_DEBUG */ |
160 | | |
161 | | /* Clear any elements in data block page to fill value */ |
162 | 0 | if ((hdr->cparam.cls->fill)(dblk_page->elmts, nelmts) < 0) |
163 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTSET, FAIL, |
164 | 0 | "can't set fixed array data block page elements to class's fill value"); |
165 | | |
166 | | /* Cache the new fixed array data block page */ |
167 | 0 | if (H5AC_insert_entry(hdr->f, H5AC_FARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, H5AC__NO_FLAGS_SET) < 0) |
168 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTINSERT, FAIL, "can't add fixed array data block page to cache"); |
169 | 0 | inserted = true; |
170 | | |
171 | | /* Add data block page as child of 'top' proxy */ |
172 | 0 | if (hdr->top_proxy) { |
173 | 0 | if (H5AC_proxy_entry_add_child(hdr->top_proxy, hdr->f, dblk_page) < 0) |
174 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTSET, FAIL, |
175 | 0 | "unable to add fixed array entry as child of array proxy"); |
176 | 0 | dblk_page->top_proxy = hdr->top_proxy; |
177 | 0 | } /* end if */ |
178 | | |
179 | 0 | done: |
180 | 0 | if (ret_value < 0) |
181 | 0 | if (dblk_page) { |
182 | | /* Remove from cache, if inserted */ |
183 | 0 | if (inserted) |
184 | 0 | if (H5AC_remove_entry(dblk_page) < 0) |
185 | 0 | HDONE_ERROR(H5E_FARRAY, H5E_CANTREMOVE, FAIL, |
186 | 0 | "unable to remove fixed array data block page from cache"); |
187 | | |
188 | | /* Destroy data block page */ |
189 | 0 | if (H5FA__dblk_page_dest(dblk_page) < 0) |
190 | 0 | HDONE_ERROR(H5E_FARRAY, H5E_CANTFREE, FAIL, "unable to destroy fixed array data block page"); |
191 | 0 | } /* end if */ |
192 | |
|
193 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
194 | 0 | } /* end H5FA__dblk_page_create() */ |
195 | | |
196 | | /*------------------------------------------------------------------------- |
197 | | * Function: H5FA__dblk_page_protect |
198 | | * |
199 | | * Purpose: Convenience wrapper around protecting fixed array data |
200 | | * block page |
201 | | * |
202 | | * Return: Non-NULL pointer to data block page on success/NULL on failure |
203 | | * |
204 | | *------------------------------------------------------------------------- |
205 | | */ |
206 | | H5FA_dblk_page_t * |
207 | | H5FA__dblk_page_protect(H5FA_hdr_t *hdr, haddr_t dblk_page_addr, size_t dblk_page_nelmts, unsigned flags) |
208 | 0 | { |
209 | 0 | H5FA_dblk_page_t *dblk_page = NULL; /* Fixed array data block page */ |
210 | 0 | H5FA_dblk_page_cache_ud_t udata; /* Information needed for loading data block page */ |
211 | 0 | H5FA_dblk_page_t *ret_value = NULL; |
212 | |
|
213 | 0 | FUNC_ENTER_PACKAGE |
214 | |
|
215 | | #ifdef H5FA_DEBUG |
216 | | fprintf(stderr, "%s: Called\n", __func__); |
217 | | #endif /* H5FA_DEBUG */ |
218 | | |
219 | | /* Sanity check */ |
220 | 0 | assert(hdr); |
221 | 0 | assert(H5_addr_defined(dblk_page_addr)); |
222 | | |
223 | | /* only the H5AC__READ_ONLY_FLAG is permitted */ |
224 | 0 | assert((flags & (unsigned)(~H5AC__READ_ONLY_FLAG)) == 0); |
225 | | |
226 | | /* Set up user data */ |
227 | 0 | udata.hdr = hdr; |
228 | 0 | udata.nelmts = dblk_page_nelmts; |
229 | 0 | udata.dblk_page_addr = dblk_page_addr; |
230 | | |
231 | | /* Protect the data block page */ |
232 | 0 | if (NULL == (dblk_page = (H5FA_dblk_page_t *)H5AC_protect(hdr->f, H5AC_FARRAY_DBLK_PAGE, dblk_page_addr, |
233 | 0 | &udata, flags))) |
234 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTPROTECT, NULL, |
235 | 0 | "unable to protect fixed array data block page, address = %llu", |
236 | 0 | (unsigned long long)dblk_page_addr); |
237 | | |
238 | | /* Create top proxy, if it doesn't exist */ |
239 | 0 | if (hdr->top_proxy && NULL == dblk_page->top_proxy) { |
240 | | /* Add data block page as child of 'top' proxy */ |
241 | 0 | if (H5AC_proxy_entry_add_child(hdr->top_proxy, hdr->f, dblk_page) < 0) |
242 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTSET, NULL, |
243 | 0 | "unable to add fixed array entry as child of array proxy"); |
244 | 0 | dblk_page->top_proxy = hdr->top_proxy; |
245 | 0 | } /* end if */ |
246 | | |
247 | | /* Set return value */ |
248 | 0 | ret_value = dblk_page; |
249 | |
|
250 | 0 | done: |
251 | | |
252 | | /* Clean up on error */ |
253 | 0 | if (!ret_value) { |
254 | | /* Release the data block page, if it was protected */ |
255 | 0 | if (dblk_page && |
256 | 0 | H5AC_unprotect(hdr->f, H5AC_FARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, H5AC__NO_FLAGS_SET) < 0) |
257 | 0 | HDONE_ERROR(H5E_FARRAY, H5E_CANTUNPROTECT, NULL, |
258 | 0 | "unable to unprotect fixed array data block page, address = %llu", |
259 | 0 | (unsigned long long)dblk_page->addr); |
260 | 0 | } /* end if */ |
261 | |
|
262 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
263 | 0 | } /* end H5FA__dblk_page_protect() */ |
264 | | |
265 | | /*------------------------------------------------------------------------- |
266 | | * Function: H5FA__dblk_page_unprotect |
267 | | * |
268 | | * Purpose: Convenience wrapper around unprotecting fixed array |
269 | | * data block page |
270 | | * |
271 | | * Return: SUCCEED/FAIL |
272 | | * |
273 | | *------------------------------------------------------------------------- |
274 | | */ |
275 | | herr_t |
276 | | H5FA__dblk_page_unprotect(H5FA_dblk_page_t *dblk_page, unsigned cache_flags) |
277 | 0 | { |
278 | 0 | herr_t ret_value = SUCCEED; |
279 | |
|
280 | 0 | FUNC_ENTER_PACKAGE |
281 | |
|
282 | | #ifdef H5FA_DEBUG |
283 | | fprintf(stderr, "%s: Called\n", __func__); |
284 | | #endif /* H5FA_DEBUG */ |
285 | | |
286 | | /* Sanity check */ |
287 | 0 | assert(dblk_page); |
288 | | |
289 | | /* Unprotect the data block page */ |
290 | 0 | if (H5AC_unprotect(dblk_page->hdr->f, H5AC_FARRAY_DBLK_PAGE, dblk_page->addr, dblk_page, cache_flags) < 0) |
291 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTUNPROTECT, FAIL, |
292 | 0 | "unable to unprotect fixed array data block page, address = %llu", |
293 | 0 | (unsigned long long)dblk_page->addr); |
294 | | |
295 | 0 | done: |
296 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
297 | 0 | } /* end H5FA__dblk_page_unprotect() */ |
298 | | |
299 | | /*------------------------------------------------------------------------- |
300 | | * Function: H5FA__dblk_page_dest |
301 | | * |
302 | | * Purpose: Destroys a fixed array data block page in memory. |
303 | | * |
304 | | * Return: SUCCEED/FAIL |
305 | | * |
306 | | *------------------------------------------------------------------------- |
307 | | */ |
308 | | herr_t |
309 | | H5FA__dblk_page_dest(H5FA_dblk_page_t *dblk_page) |
310 | 0 | { |
311 | 0 | herr_t ret_value = SUCCEED; |
312 | |
|
313 | 0 | FUNC_ENTER_PACKAGE |
314 | | |
315 | | /* Sanity check */ |
316 | 0 | assert(dblk_page); |
317 | | |
318 | | /* Check if header field has been initialized */ |
319 | 0 | if (dblk_page->hdr) { |
320 | | /* Check if buffer for data block page elements has been initialized */ |
321 | 0 | if (dblk_page->elmts) { |
322 | | /* Free buffer for data block page elements */ |
323 | 0 | dblk_page->elmts = H5FL_BLK_FREE(page_elmts, dblk_page->elmts); |
324 | 0 | } /* end if */ |
325 | | |
326 | | /* Decrement reference count on shared info */ |
327 | 0 | if (H5FA__hdr_decr(dblk_page->hdr) < 0) |
328 | 0 | HGOTO_ERROR(H5E_FARRAY, H5E_CANTDEC, FAIL, |
329 | 0 | "can't decrement reference count on shared array header"); |
330 | 0 | dblk_page->hdr = NULL; |
331 | 0 | } /* end if */ |
332 | | |
333 | | /* Sanity check */ |
334 | 0 | assert(NULL == dblk_page->top_proxy); |
335 | | |
336 | | /* Free the data block page itself */ |
337 | 0 | dblk_page = H5FL_FREE(H5FA_dblk_page_t, dblk_page); |
338 | |
|
339 | 0 | done: |
340 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
341 | 0 | } /* end H5FA__dblk_page_dest() */ |