1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Module Info: This module contains the functionality for reference
* datatypes in the H5T interface.
*/
#include "H5Tmodule.h" /* This source code file is part of the H5T module */
#include "H5private.h" /* Generic Functions */
#include "H5Dprivate.h" /* Dataset functions */
#include "H5Eprivate.h" /* Error handling */
#include "H5HGprivate.h" /* Global Heaps */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
#include "H5Pprivate.h" /* Property lists */
#include "H5Tpkg.h" /* Datatypes */
/* Local functions */
static size_t H5T__ref_reg_mem_getsize(const void *_ref);
static herr_t H5T__ref_reg_mem_read(H5F_t *f, hid_t dxpl_id, void *_ref, void *_buf, size_t buf_size);
static herr_t H5T__ref_reg_mem_write(H5F_t *f, hid_t dxpl_id, void *_ref, void *_buf, void *_bg, size_t buf_size);
static size_t H5T__ref_disk_getsize(const void *_ref);
static herr_t H5T__ref_disk_read(H5F_t *f, hid_t dxpl_id, void *_ref, void *_buf, size_t buf_size);
static herr_t H5T__ref_disk_write(H5F_t *f, hid_t dxpl_id, void *_ref, void *_buf, void *_bg, size_t buf_size);
/* Local variables */
/*-------------------------------------------------------------------------
* Function: H5T__ref_set_loc
*
* Purpose: Sets the location of a reference datatype to be either on disk
* or in memory
*
* Return:
* One of two values on success:
* TRUE - If the location of any reference types changed
* FALSE - If the location of any reference types is the same
* Negative value is returned on failure
*
*-------------------------------------------------------------------------
*/
htri_t
H5T__ref_set_loc(const H5T_t *dt, H5F_t *f, H5T_loc_t loc)
{
htri_t ret_value = FALSE; /* Indicate success, but no location change */
FUNC_ENTER_PACKAGE
/* check parameters */
HDassert(dt);
HDassert(loc >= H5T_LOC_BADLOC && loc < H5T_LOC_MAXLOC);
/* Only change the location if it's different */
if(loc != dt->shared->u.atomic.u.r.loc || f != dt->shared->u.atomic.u.r.f) {
switch(loc) {
case H5T_LOC_MEMORY: /* Memory based reference datatype */
HDassert(NULL == f);
/* Mark this type as being stored in memory */
dt->shared->u.atomic.u.r.loc = H5T_LOC_MEMORY;
if(dt->shared->u.atomic.u.r.rtype == H5R_REGION) {
/* size in memory, disk size is different */
dt->shared->size = sizeof(hreg_ref_t);
/* Set up the function pointers to access the region
* reference in memory */
dt->shared->u.atomic.u.r.getsize = H5T__ref_reg_mem_getsize;
dt->shared->u.atomic.u.r.read = H5T__ref_reg_mem_read;
dt->shared->u.atomic.u.r.write = H5T__ref_reg_mem_write;
} else {
HDassert(0 && "Invalid reference type");
}
/* Reset file ID (since this reference is in memory) */
dt->shared->u.atomic.u.r.f = NULL;
break;
case H5T_LOC_DISK: /* Disk based reference datatype */
HDassert(f);
/* Mark this type as being stored on disk */
dt->shared->u.atomic.u.r.loc = H5T_LOC_DISK;
/*
* Size of element on disk is 4 bytes for the length, plus the size
* of an address in this file, plus 4 bytes for the size of a heap
* ID. Memory size is different
*/
dt->shared->size = (size_t) (2 * H5_SIZEOF_UINT32_T) + H5F_SIZEOF_ADDR(f);
/*
* Set up the function pointers to access the information on
* disk. Region and attribute references are stored identically
* on disk, so use the same functions
*/
dt->shared->u.atomic.u.r.getsize = H5T__ref_disk_getsize;
dt->shared->u.atomic.u.r.read = H5T__ref_disk_read;
dt->shared->u.atomic.u.r.write = H5T__ref_disk_write;
/* Set file ID (since this reference is on disk) */
dt->shared->u.atomic.u.r.f = f;
break;
case H5T_LOC_BADLOC:
case H5T_LOC_MAXLOC:
default:
HGOTO_ERROR(H5E_DATATYPE, H5E_BADRANGE, FAIL, "invalid reference datatype location")
} /* end switch */
/* Indicate that the location changed */
ret_value = TRUE;
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__ref_set_loc() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_reg_mem_getsize
*
* Purpose: Retrieves the size of a memory based region reference.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static size_t
H5T__ref_reg_mem_getsize(const void *_ref)
{
#ifdef H5_NO_ALIGNMENT_RESTRICTIONS
const hreg_ref_t *ref = (const hreg_ref_t *)_ref;
#else
hreg_ref_t ref;
#endif
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* check parameters, return result */
#ifdef H5_NO_ALIGNMENT_RESTRICTIONS
HDassert(ref);
FUNC_LEAVE_NOAPI(ref->buf_size)
#else
HDassert(_ref);
HDmemcpy(&ref, _ref, sizeof(hreg_ref_t));
FUNC_LEAVE_NOAPI(ref.buf_size)
#endif
} /* end H5T__ref_reg_mem_getsize() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_reg_mem_read
*
* Purpose: "Reads" the memory based region reference into a buffer
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5T__ref_reg_mem_read(H5F_t H5_ATTR_UNUSED *f, hid_t H5_ATTR_UNUSED dxpl_id,
void *_ref, void *buf, size_t buf_size)
{
#ifdef H5_NO_ALIGNMENT_RESTRICTIONS
const hreg_ref_t *ref = (const hreg_ref_t *)_ref;
#else
hreg_ref_t ref;
#endif
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* check parameters, copy data */
HDassert(buf);
#ifdef H5_NO_ALIGNMENT_RESTRICTIONS
HDassert(ref && ref->buf);
HDmemcpy(buf, ref->buf, buf_size);
#else
HDassert(_ref);
HDmemcpy(&ref, _ref, sizeof(hreg_ref_t));
HDassert(ref.buf);
HDmemcpy(buf, ref.buf, buf_size);
#endif
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5T__ref_reg_mem_read() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_reg_mem_write
*
* Purpose: "Writes" the memory region reference from a buffer
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5T__ref_reg_mem_write(H5F_t H5_ATTR_UNUSED *f, hid_t H5_ATTR_UNUSED dxpl_id,
void *_ref, void *buf, void H5_ATTR_UNUSED *_bg, size_t buf_size)
{
hreg_ref_t ref;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* check parameters */
HDassert(_ref);
HDassert(buf);
if (buf_size != 0) {
if (NULL == (ref.buf = H5MM_malloc(buf_size)))
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for reference data")
/* Copy the data into the newly allocated buffer */
HDmemcpy(ref.buf, buf, buf_size);
} /* end if */
else
ref.buf = NULL;
/* Set the size */
ref.buf_size = buf_size;
/* Set pointer in user's buffer with memcpy, to avoid alignment issues */
HDmemcpy(_ref, &ref, sizeof(hreg_ref_t));
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__ref_reg_mem_write() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_disk_getsize
*
* Purpose: Retrieves the length of a disk based reference element.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static size_t
H5T__ref_disk_getsize(const void *_ref)
{
const uint8_t *ref = (const uint8_t *)_ref; /* Pointer to the disk reference information */
size_t buf_size = 0;
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* check parameters */
HDassert(ref);
UINT32DECODE(ref, buf_size);
FUNC_LEAVE_NOAPI(buf_size)
} /* end H5T__ref_disk_getsize() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_disk_read
*
* Purpose: Reads the disk based region element into a buffer
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5T__ref_disk_read(H5F_t *f, hid_t dxpl_id, void *_ref, void *buf,
size_t H5_ATTR_UNUSED buf_size)
{
const uint8_t *ref = (const uint8_t *)_ref;
H5HG_t hobjid;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* check parameters */
HDassert(ref);
HDassert(buf);
HDassert(f);
/* Skip the length of the sequence */
ref += 4;
/* Get the heap information */
H5F_addr_decode(f, (const uint8_t **)&ref, &(hobjid.addr));
UINT32DECODE(ref, hobjid.idx);
/* Check if this sequence actually has any data */
if(hobjid.addr > 0) {
/* Read the VL information from disk */
if(H5HG_read(f, dxpl_id, &hobjid, buf, NULL) == NULL)
HGOTO_ERROR(H5E_DATATYPE, H5E_READERROR, FAIL, "Unable to read reference information")
} /* end if */
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__ref_disk_read() */
/*-------------------------------------------------------------------------
* Function: H5T__ref_disk_write
*
* Purpose: Writes the disk based region element from a buffer
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5T__ref_disk_write(H5F_t *f, hid_t dxpl_id, void *_ref, void *buf, void *_bg,
size_t buf_size)
{
uint8_t *ref = (uint8_t *)_ref; /* Pointer to the user's information */
const uint8_t *bg = (const uint8_t *)_bg; /* Pointer to the old data */
H5HG_t hobjid; /* New reference's heap ID */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* check parameters */
HDassert(ref);
HDassert(buf_size == 0 || buf);
HDassert(f);
/* TODO not sure if we need that but oh well... */
/* Free heap object for old data. */
if(bg != NULL) {
H5HG_t bg_hobjid; /* "Background" reference's ID info */
/* Skip the length of the reference and heap object ID from background data. */
bg += 4;
/* Get heap information */
H5F_addr_decode(f, (const uint8_t **)&bg, &(bg_hobjid.addr));
UINT32DECODE(bg, bg_hobjid.idx);
/* Free heap object for old data */
if(bg_hobjid.addr > 0) {
/* Free heap object */
if(H5HG_remove(f, dxpl_id, &bg_hobjid) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_WRITEERROR, FAIL, "Unable to remove heap object")
} /* end if */
} /* end if */
/* Set the size */
UINT32ENCODE(ref, buf_size);
/* Write the reference information to disk (allocates space also) */
if(H5HG_insert(f, dxpl_id, buf_size, buf, &hobjid) < 0)
HGOTO_ERROR(H5E_DATATYPE, H5E_WRITEERROR, FAIL, "Unable to write reference information")
/* Encode the heap information */
H5F_addr_encode(f, &ref, hobjid.addr);
UINT32ENCODE(ref, hobjid.idx);
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5T__ref_disk_write() */
|