summaryrefslogtreecommitdiffstats
path: root/src/H5Tbit.c
blob: f2ad5f1aa0acc6d766aaae506993e79e32a6df64 (plain)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Module Info:	Operations on bit vectors.  A bit vector is an array of bytes
 *		with the least-significant bits in the first byte.  That is,
 *		the bytes are in little-endian order.
 */

#define H5T_PACKAGE		/*suppress error about including H5Tpkg	  */

/* Pablo information */
/* (Put before include files to avoid problems with inline functions) */
#define PABLO_MASK	H5Tbit_mask

#include "H5private.h"		/*generic functions			  */
#include "H5Eprivate.h"		/*error handling			  */
#include "H5Iprivate.h"		/*ID functions		   		  */
#include "H5Tpkg.h"		/*data-type functions			  */

/* Interface initialization */
static int interface_initialize_g = 0;
#define INTERFACE_INIT NULL


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_copy
 *
 * Purpose:	Copies bits from one vector to another.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Wednesday, June 10, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void
H5T_bit_copy (uint8_t *dst, size_t dst_offset, const uint8_t *src,
	      size_t src_offset, size_t size)
{
    int	shift;
    unsigned	mask_lo, mask_hi;
    int	s_idx, d_idx;

    /*
     * Normalize the offset to be a byte number and a bit offset within that
     * byte.
     */
    s_idx = (int)src_offset / 8;
    d_idx = (int)dst_offset / 8;
    src_offset %= 8;
    dst_offset %= 8;
    
    /*
     * Get things rolling. This means copying bits until we're aligned on a
     * source byte.  This the following example, five bits are copied to the
     * destination.
     *
     *                      src[s_idx]
     *   +---------------+---------------+
     *   |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
     *   +---------------+---------------+
     *      ... : : : : : | | | | |
     *      ... v v v v v V V V V V
     *      ...+---------------+---------------+
     *      ...|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
     *      ...+---------------+---------------+
     *           dst[d_idx+1]      dst[d_idx]
     */
    while (src_offset && size>0) {
	unsigned nbits = (unsigned)MIN3 (size, 8-dst_offset, 8-src_offset);
	unsigned mask = (1<<nbits) - 1;

	dst[d_idx] &= ~(mask<<dst_offset);
	dst[d_idx] |= ((src[s_idx]>>src_offset)&mask) << dst_offset;

	src_offset += nbits;
	if (src_offset>=8) {
	    s_idx++;
	    src_offset %= 8;
	}
	dst_offset += nbits;
	if (dst_offset>=8) {
	    d_idx++;
	    dst_offset %= 8;
	}
	size -= nbits;
    }
	
    /*
     * The middle bits. We are aligned on a source byte which needs to be
     * copied to two (or one in the degenerate case) destination bytes.
     *
     * 		      src[s_idx]
     * 		   +---------------+
     *  	   |7 6 5 4 3 2 1 0|
     * 		   +---------------+
     *              | | | | | | | |
     * 		    V V V V V V V V
     *   +---------------+---------------+
     *   |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
     *   +---------------+---------------+
     *     dst[d_idx+1]      dst[d_idx]
     *
     *		 
     * Calculate shifts and masks.  See diagrams below.  MASK_LO in this
     * example is 0x1f (the low five bits) and MASK_HI is 0xe0 (the high three
     * bits). SHIFT is three since the source must be shifted right three bits
     * to line up with the destination.
     */
    shift = (int)dst_offset;
    mask_lo = (1<<(8-shift))-1;
    mask_hi = (~mask_lo) & 0xff;
    
    for (/*void*/; size>8; size-=8, d_idx++, s_idx++) {
	if (shift) {
	    dst[d_idx+0] &= ~(mask_lo<<shift);
	    dst[d_idx+0] |= (src[s_idx] & mask_lo) << shift;
	    dst[d_idx+1] &= ~(mask_hi>>(8-shift));
	    dst[d_idx+1] |= (src[s_idx] & mask_hi) >> (8-shift);
	} else {
	    dst[d_idx] = src[s_idx];
	}
    }

    /* Finish up */
    while (size>0) {
	unsigned nbits = (unsigned)MIN3 (size, 8-dst_offset, 8-src_offset);
	unsigned mask = (1<<nbits) - 1;

	dst[d_idx] &= ~(mask<<dst_offset);
	dst[d_idx] |= ((src[s_idx]>>src_offset)&mask) << dst_offset;

	src_offset += nbits;
	if (src_offset>=8) {
	    s_idx++;
	    src_offset %= 8;
	}
	dst_offset += nbits;
	if (dst_offset>=8) {
	    d_idx++;
	    dst_offset %= 8;
	}
	size -= nbits;
    }
}


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_get_d
 *
 * Purpose:	Return a small bit sequence as a number.
 *
 * Return:	Success:	The bit sequence interpretted as an unsigned
 *				integer.
 *
 *		Failure:	0
 *
 * Programmer:	Robb Matzke
 *              Tuesday, June 23, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hsize_t
H5T_bit_get_d (uint8_t *buf, size_t offset, size_t size)
{
    hsize_t	val=0;
    size_t	i, hs;
    hsize_t	ret_value;      /* Return value */
    
    FUNC_ENTER_NOAPI(H5T_bit_get_d, 0);

    assert (8*sizeof(val)>=size);

    H5T_bit_copy ((uint8_t*)&val, 0, buf, offset, size);
    switch (((H5T_t*)(H5I_object(H5T_NATIVE_INT_g)))->u.atomic.order) {
        case H5T_ORDER_LE:
            break;

        case H5T_ORDER_BE:
            for (i=0, hs=sizeof(val)/2; i<hs; i++) {
                uint8_t tmp = ((uint8_t*)&val)[i];
                ((uint8_t*)&val)[i] = ((uint8_t*)&val)[sizeof(val)-(i+1)];
                ((uint8_t*)&val)[sizeof(val)-(i+1)] = tmp;
            }
            break;

        default:
            HDabort ();
    }

    /* Set return value */
    ret_value=val;

done:
    FUNC_LEAVE_NOAPI(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_set_d
 *
 * Purpose:	Sets part of a bit vector to the specified unsigned value.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Wednesday, June 24, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void
H5T_bit_set_d (uint8_t *buf, size_t offset, size_t size, hsize_t val)
{
    size_t	i, hs;
    
    assert (8*sizeof(val)>=size);

    switch (((H5T_t*)(H5I_object(H5T_NATIVE_INT_g)))->u.atomic.order) {
        case H5T_ORDER_LE:
            break;

        case H5T_ORDER_BE:
            for (i=0, hs=sizeof(val)/2; i<hs; i++) {
                uint8_t tmp = ((uint8_t*)&val)[i];
                ((uint8_t*)&val)[i] = ((uint8_t*)&val)[sizeof(val)-(i+1)];
                ((uint8_t*)&val)[sizeof(val)-(i+1)] = tmp;
            }
            break;

        default:
            HDabort ();
    }

    H5T_bit_copy (buf, offset, (uint8_t*)&val, 0, size);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_set
 *
 * Purpose:	Sets or clears bits in a contiguous region of a vector
 *		beginning at bit OFFSET and continuing for SIZE bits.
 *
 * Return:	void
 *
 * Programmer:	Robb Matzke
 *              Wednesday, June 10, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
void
H5T_bit_set (uint8_t *buf, size_t offset, size_t size, hbool_t value)
{
    int	idx;

    /* Normalize */
    idx = (int)offset / 8;
    offset %= 8;

    /* The first partial byte */
    if (size && offset%8) {
	size_t nbits = MIN (size, 8-offset);
	unsigned mask = (1<<nbits)-1;
	if (value) {
	    buf[idx++] |= mask << offset;
	} else {
	    buf[idx++] &= ~(mask << offset);
	}
	size -= nbits;
    }
    
    /* The middle bytes */
    while (size>=8) {
	buf[idx++] = value ? 0xff : 0x00;
	size -= 8;
    }

    /* The last partial byte */
    if (size) {
	if (value) {
	    buf[idx] |= (1<<size)-1;
	} else {
	    buf[idx] &= ~((1<<size)-1);
	}
    }
}


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_find
 *
 * Purpose:	Finds the first bit with the specified VALUE within a region
 *		of a bit vector.  The region begins at OFFSET and continues
 *		for SIZE bits, but the region can be searched from the least
 *		significat end toward the most significant end with 
 *
 * Return:	Success:	The position of the bit found, relative to
 *				the offset.
 *
 *		Failure:	-1
 *
 * Programmer:	Robb Matzke
 *              Wednesday, June 10, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
ssize_t
H5T_bit_find (uint8_t *buf, size_t offset, size_t size, H5T_sdir_t direction,
	      hbool_t value)
{
    ssize_t	base=(ssize_t)offset;
    ssize_t	idx, i;
    size_t	iu;
    ssize_t     ret_value=(-1);         /* Return value */

    /* Use FUNC_ENTER_NOINIT here to avoid performance issues */
    FUNC_ENTER_NOINIT(H5T_bit_find);

    /* Some functions call this with value=TRUE */
    assert (TRUE==1);

    switch (direction) {
    case H5T_BIT_LSB:
	/* Calculate index */
	idx = (ssize_t)(offset / 8);
	offset %= 8;
	
	/* Beginning */
	if (offset) {
	    for (iu=offset; iu<8 && size>0; iu++, size--) {
		if (value==(hbool_t)((buf[idx]>>iu) & 0x01))
		    HGOTO_DONE(8*idx+(ssize_t)iu - base);
	    }
	    offset = 0;
	    idx++;
	}
	/* Middle */
	while (size>=8) {
	    if ((value?0x00:0xff)!=buf[idx]) {
		for (i=0; i<8; i++) {
		    if (value==(hbool_t)((buf[idx]>>i) & 0x01))
			HGOTO_DONE(8*idx+i - base);
		}
	    }
	    size -= 8;
	    idx++;
	}
	/* End */
	for (i=0; i<(ssize_t)size; i++) {
	    if (value==(hbool_t)((buf[idx]>>i) & 0x01))
		HGOTO_DONE(8*idx+i - base);
	}
	break;

    case H5T_BIT_MSB:
	/* Calculate index */
	idx = (ssize_t)((offset+size-1) / 8);
	offset %= 8;
	
	/* Beginning */
	if (size>8-offset && (offset+size)%8) {
	    for (iu=(offset+size)%8; iu>0; --iu, --size) {
		if (value==(hbool_t)((buf[idx]>>(iu-1)) & 0x01))
		    HGOTO_DONE(8*idx+(ssize_t)(iu-1) - base);
	    }
	    --idx;
	}
	/* Middle */
	while (size>=8) {
	    if ((value?0x00:0xff)!=buf[idx]) {
		for (i=7; i>=0; --i) {
		    if (value==(hbool_t)((buf[idx]>>i) & 0x01))
			HGOTO_DONE(8*idx+i - base);
		}
	    }
	    size -= 8;
	    --idx;
	}
	/* End */
	if (size>0) {
	    for (iu=offset+size; iu>offset; --iu) {
		if (value==(hbool_t)((buf[idx]>>(iu-1)) & 0x01))
		    HGOTO_DONE(8*idx+(ssize_t)(iu-1) - base);
	    }
	}
	break;
    }
    
done:
    FUNC_LEAVE_NOAPI(ret_value);
}


/*-------------------------------------------------------------------------
 * Function:	H5T_bit_inc
 *
 * Purpose:	Increment part of a bit field by adding 1.
 *
 * Return:	Success:        The carry-out value, one if overflow zero
 *				otherwise.
 *
 *		Failure:	Negative
 *
 * Programmer:	Robb Matzke
 *              Friday, June 26, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5T_bit_inc(uint8_t *buf, size_t start, size_t size)
{
    size_t	idx = start / 8;
    unsigned	carry = 1;
    unsigned	acc, mask;

    /* Use FUNC_ENTER_NOINIT here to avoid performance issues */
    FUNC_ENTER_NOINIT(H5T_bit_find);

    assert(buf);
    start %= 8;

    /* The first partial byte */
    if (start) {
	if (size+start<8) mask = (1<<size)-1;
	else mask = (1<<(8-start))-1;
	acc = (buf[idx]>>start) & mask;
	acc += 1;
	carry = acc & (1<<MIN(size, 8-start));
	buf[idx] &= ~(mask<<start);
	buf[idx] |= (acc & mask) << start;
	size -= MIN(size, 8-start);
	start=0;
	idx++;
    }

    /* The middle */
    while (carry && size>=8) {
	acc = buf[idx];
	acc += 1;
	carry = acc & 0x100;
	buf[idx] = acc & 0xff;
	idx++;
	size -= 8;
    }

    /* The last bits */
    if (carry && size>0) {
	mask = (1<<size)-1;
	acc = buf[idx] & mask;
	acc += 1;
	carry = acc & (1<<size);
	buf[idx] &= ~mask;
	buf[idx] |= acc & mask;
    }

    FUNC_LEAVE_NOAPI(carry ? TRUE : FALSE);
}
* Changed the file access list to the new generic property * list. * *------------------------------------------------------------------------- */ herr_t H5Pset_fapl_family(hid_t fapl_id, hsize_t memb_size, hid_t memb_fapl_id) { herr_t ret_value; H5FD_family_fapl_t fa; H5P_genplist_t *plist; /* Property list pointer */ FUNC_ENTER_API(H5Pset_fapl_family, FAIL) H5TRACE3("e","ihi",fapl_id,memb_size,memb_fapl_id); /* Check arguments */ if(TRUE != H5P_isa_class(fapl_id, H5P_FILE_ACCESS)) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list") if(H5P_DEFAULT == memb_fapl_id) memb_fapl_id = H5P_FILE_ACCESS_DEFAULT; else if(TRUE != H5P_isa_class(memb_fapl_id, H5P_FILE_ACCESS)) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access list") /* * Initialize driver specific information. No need to copy it into the FA * struct since all members will be copied by H5P_set_driver(). */ fa.memb_size = memb_size; fa.memb_fapl_id = memb_fapl_id; if(NULL == (plist = H5I_object(fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list") ret_value= H5P_set_driver(plist, H5FD_FAMILY, &fa); done: FUNC_LEAVE_API(ret_value) } /*------------------------------------------------------------------------- * Function: H5Pget_fapl_family * * Purpose: Returns information about the family file access property * list though the function arguments. * * Return: Success: Non-negative * * Failure: Negative * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * * Raymond Lu * Tuesday, Oct 23, 2001 * Changed the file access list to the new generic property * list. * *------------------------------------------------------------------------- */ herr_t H5Pget_fapl_family(hid_t fapl_id, hsize_t *memb_size/*out*/, hid_t *memb_fapl_id/*out*/) { H5FD_family_fapl_t *fa; H5P_genplist_t *plist; /* Property list pointer */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_API(H5Pget_fapl_family, FAIL) H5TRACE3("e","ixx",fapl_id,memb_size,memb_fapl_id); if(NULL == (plist = H5P_object_verify(fapl_id,H5P_FILE_ACCESS))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access list") if (H5FD_FAMILY!=H5P_get_driver(plist)) HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "incorrect VFL driver") if (NULL==(fa=H5P_get_driver_info(plist))) HGOTO_ERROR(H5E_PLIST, H5E_BADVALUE, FAIL, "bad VFL driver info") if (memb_size) *memb_size = fa->memb_size; if (memb_fapl_id) { if(NULL == (plist = H5I_object(fa->memb_fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access list") *memb_fapl_id = H5P_copy_plist(plist); } /* end if */ done: FUNC_LEAVE_API(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_fapl_get * * Purpose: Gets a file access property list which could be used to * create an identical file. * * Return: Success: Ptr to new file access property list. * * Failure: NULL * * Programmer: Robb Matzke * Friday, August 13, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static void * H5FD_family_fapl_get(H5FD_t *_file) { H5FD_family_t *file = (H5FD_family_t*)_file; H5FD_family_fapl_t *fa = NULL; H5P_genplist_t *plist; /* Property list pointer */ void *ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_fapl_get, NULL) if (NULL==(fa=H5MM_calloc(sizeof(H5FD_family_fapl_t)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") fa->memb_size = file->memb_size; if(NULL == (plist = H5I_object(file->memb_fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a file access property list") fa->memb_fapl_id = H5P_copy_plist(plist); /* Set return value */ ret_value=fa; done: if(ret_value==NULL) { if(fa!=NULL) H5MM_xfree(fa); } /* end if */ FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_fapl_copy * * Purpose: Copies the family-specific file access properties. * * Return: Success: Ptr to a new property list * * Failure: NULL * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static void * H5FD_family_fapl_copy(const void *_old_fa) { const H5FD_family_fapl_t *old_fa = (const H5FD_family_fapl_t*)_old_fa; H5FD_family_fapl_t *new_fa = NULL; H5P_genplist_t *plist; /* Property list pointer */ void *ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_fapl_copy, NULL) if (NULL==(new_fa=H5MM_malloc(sizeof(H5FD_family_fapl_t)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") /* Copy the fields of the structure */ memcpy(new_fa, old_fa, sizeof(H5FD_family_fapl_t)); /* Deep copy the property list objects in the structure */ if(old_fa->memb_fapl_id==H5P_FILE_ACCESS_DEFAULT) { if(H5I_inc_ref(new_fa->memb_fapl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTINC, NULL, "unable to increment ref count on VFL driver") } /* end if */ else { if(NULL == (plist = H5I_object(old_fa->memb_fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a file access property list") new_fa->memb_fapl_id = H5P_copy_plist(plist); } /* end else */ /* Set return value */ ret_value=new_fa; done: if(ret_value==NULL) { if(new_fa!=NULL) H5MM_xfree(new_fa); } /* end if */ FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_fapl_free * * Purpose: Frees the family-specific file access properties. * * Return: Success: 0 * * Failure: -1 * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_fapl_free(void *_fa) { H5FD_family_fapl_t *fa = (H5FD_family_fapl_t*)_fa; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_fapl_free, FAIL) if(H5I_dec_ref(fa->memb_fapl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTDEC, FAIL, "can't close driver ID") H5MM_xfree(fa); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_dxpl_copy * * Purpose: Copes the family-specific data transfer properties. * * Return: Success: Ptr to new property list * * Failure: NULL * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static void * H5FD_family_dxpl_copy(const void *_old_dx) { const H5FD_family_dxpl_t *old_dx = (const H5FD_family_dxpl_t*)_old_dx; H5FD_family_dxpl_t *new_dx = NULL; H5P_genplist_t *plist; /* Property list pointer */ void *ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_dxpl_copy, NULL) if (NULL==(new_dx=H5MM_malloc(sizeof(H5FD_family_dxpl_t)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed") memcpy(new_dx, old_dx, sizeof(H5FD_family_dxpl_t)); if(old_dx->memb_dxpl_id==H5P_DATASET_XFER_DEFAULT) { if(H5I_inc_ref(new_dx->memb_dxpl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTINC, NULL, "unable to increment ref count on VFL driver") } /* end if */ else { if(NULL == (plist = H5I_object(old_dx->memb_dxpl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a file access property list") new_dx->memb_dxpl_id = H5P_copy_plist(plist); } /* end else */ /* Set return value */ ret_value=new_dx; done: if(ret_value==NULL) { if(new_dx!=NULL) H5MM_xfree(new_dx); } /* end if */ FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_dxpl_free * * Purpose: Frees the family-specific data transfer properties. * * Return: Success: 0 * * Failure: -1 * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_dxpl_free(void *_dx) { H5FD_family_dxpl_t *dx = (H5FD_family_dxpl_t*)_dx; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_dxpl_free, FAIL) if(H5I_dec_ref(dx->memb_dxpl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTDEC, FAIL, "can't close driver ID") H5MM_xfree(dx); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_open * * Purpose: Creates and/or opens a family of files as an HDF5 file. * * Return: Success: A pointer to a new file dat structure. The * public fields will be initialized by the * caller, which is always H5FD_open(). * * Failure: NULL * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * Raymond Lu * Thursday, November 18, 2004 * When file is re-opened, member size passed in from access property * is checked to see if it's reasonable. If there is only 1 member * file, member size can't be smaller than current member size. * If there are at least 2 member files, member size can only be equal * the 1st member size. * *------------------------------------------------------------------------- */ static H5FD_t * H5FD_family_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr) { H5FD_family_t *file=NULL; H5FD_t *ret_value=NULL; char memb_name[4096], temp[4096]; hsize_t eof1=HADDR_UNDEF, eof2=HADDR_UNDEF; unsigned t_flags = flags & ~H5F_ACC_CREAT; H5P_genplist_t *plist; /* Property list pointer */ FUNC_ENTER_NOAPI(H5FD_family_open, NULL) /* Check arguments */ if (!name || !*name) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name") if (0==maxaddr || HADDR_UNDEF==maxaddr) HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr") /* Initialize file from file access properties */ if (NULL==(file=H5MM_calloc(sizeof(H5FD_family_t)))) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "unable to allocate file struct") if (H5P_FILE_ACCESS_DEFAULT==fapl_id) { file->memb_fapl_id = H5P_FILE_ACCESS_DEFAULT; if(H5I_inc_ref(file->memb_fapl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTINC, NULL, "unable to increment ref count on VFL driver") file->memb_size = 1024*1024*1024; /*1GB*/ } else { H5FD_family_fapl_t *fa; if(NULL == (plist = H5I_object(fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a file access property list") fa = H5P_get_driver_info(plist); if(fa->memb_fapl_id==H5P_FILE_ACCESS_DEFAULT) { if(H5I_inc_ref(fa->memb_fapl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTINC, NULL, "unable to increment ref count on VFL driver") file->memb_fapl_id = fa->memb_fapl_id; } /* end if */ else { if(NULL == (plist = H5I_object(fa->memb_fapl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a file access property list") file->memb_fapl_id = H5P_copy_plist(plist); } /* end else */ file->memb_size = fa->memb_size; } file->name = H5MM_strdup(name); file->flags = flags; /* Check that names are unique */ sprintf(memb_name, name, 0); sprintf(temp, name, 1); if (!strcmp(memb_name, temp)) HGOTO_ERROR(H5E_FILE, H5E_FILEEXISTS, NULL, "file names not unique") /* Open all the family members */ while (1) { sprintf(memb_name, name, file->nmembs); /* Enlarge member array */ if (file->nmembs>=file->amembs) { unsigned n = MAX(64, 2*file->amembs); H5FD_t **x = H5MM_realloc(file->memb, n*sizeof(H5FD_t*)); if (!x) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "unable to reallocate members") file->amembs = n; file->memb = x; } /* * Attempt to open file. If the first file cannot be opened then fail; * otherwise an open failure means that we've reached the last member. * Allow H5F_ACC_CREAT only on the first family member. */ H5E_BEGIN_TRY { file->memb[file->nmembs] = H5FDopen(memb_name, (0==file->nmembs ? flags : t_flags), file->memb_fapl_id, HADDR_UNDEF); } H5E_END_TRY; if (!file->memb[file->nmembs]) { if (0==file->nmembs) HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open member file") H5E_clear_stack(NULL); break; } file->nmembs++; } /* * Get file size of the first 2 member files if exist. Check if user sets * reasonable member size. */ if(HADDR_UNDEF==(eof1 = H5FD_get_eof(file->memb[0]))) HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, NULL, "file get eof1 request failed") if(file->memb[1] && (HADDR_UNDEF==(eof2 = H5FD_get_eof(file->memb[1])))) HGOTO_ERROR(H5E_VFL, H5E_CANTINIT, NULL, "file get eof2 request failed") if(eof1 && (eof2==HADDR_UNDEF || !eof2)) { /* If there is only 1 member file, new member size can't be smaller than * current member size. */ if(file->memb_size<eof1) file->memb_size = eof1; } else if(eof1 && eof2) { /* If there are at least 2 member files, new member size can only be equal * to the 1st member size */ file->memb_size = eof1; } ret_value=(H5FD_t *)file; done: /* Cleanup and fail */ if (ret_value==NULL && file!=NULL) { unsigned nerrors=0; /* Number of errors closing member files */ unsigned u; /* Local index variable */ for (u=0; u<file->nmembs; u++) if (file->memb[u]) if (H5FD_close(file->memb[u])<0) nerrors++; if (nerrors) HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, NULL, "unable to close member files") if (file->memb) H5MM_xfree(file->memb); if(H5I_dec_ref(file->memb_fapl_id)<0) HDONE_ERROR(H5E_VFL, H5E_CANTDEC, NULL, "can't close driver ID") if (file->name) H5MM_xfree(file->name); H5MM_xfree(file); } FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_close * * Purpose: Closes a family of files. * * Return: Success: Non-negative * * Failure: Negative with as many members closed as * possible. The only subsequent operation * permitted on the file is a close operation. * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_close(H5FD_t *_file) { H5FD_family_t *file = (H5FD_family_t*)_file; unsigned nerrors=0; /* Number of errors while closing member files */ unsigned u; /* Local index variable */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_close, FAIL) /* Close as many members as possible */ for (u=0; u<file->nmembs; u++) { if (file->memb[u]) { if (H5FDclose(file->memb[u])<0) nerrors++; else file->memb[u] = NULL; } } if (nerrors) HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, FAIL, "unable to close member files") /* Clean up other stuff */ if(H5I_dec_ref(file->memb_fapl_id)<0) HGOTO_ERROR(H5E_VFL, H5E_CANTDEC, FAIL, "can't close driver ID") if (file->memb) H5MM_xfree(file->memb); if (file->name) H5MM_xfree(file->name); H5MM_xfree(file); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_cmp * * Purpose: Compares two file families to see if they are the same. It * does this by comparing the first member of the two families. * * Return: Success: like strcmp() * * Failure: never fails (arguments were checked by the * caller). * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static int H5FD_family_cmp(const H5FD_t *_f1, const H5FD_t *_f2) { const H5FD_family_t *f1 = (const H5FD_family_t*)_f1; const H5FD_family_t *f2 = (const H5FD_family_t*)_f2; int ret_value=(H5FD_VFD_DEFAULT); FUNC_ENTER_NOAPI(H5FD_family_cmp, H5FD_VFD_DEFAULT) assert(f1->nmembs>=1 && f1->memb[0]); assert(f2->nmembs>=1 && f2->memb[0]); ret_value= H5FDcmp(f1->memb[0], f2->memb[0]); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_query * * Purpose: Set the flags that this VFL driver is capable of supporting. * (listed in H5FDpublic.h) * * Return: Success: non-negative * * Failure: negative * * Programmer: Quincey Koziol * Friday, August 25, 2000 * * Modifications: * *------------------------------------------------------------------------- */ /* ARGSUSED */ static herr_t H5FD_family_query(const H5FD_t UNUSED * _f, unsigned long *flags /* out */) { herr_t ret_value=SUCCEED; FUNC_ENTER_NOAPI(H5FD_family_query, FAIL) /* Set the VFL feature flags that this driver supports */ if(flags) { *flags=0; *flags|=H5FD_FEAT_AGGREGATE_METADATA; /* OK to aggregate metadata allocations */ /**flags|=H5FD_FEAT_ACCUMULATE_METADATA;*/ /* OK to accumulate metadata for faster writes. * - Turn it off temporarily because there's a bug * when trying to flush metadata during closing. */ *flags|=H5FD_FEAT_DATA_SIEVE; /* OK to perform data sieving for faster raw data reads & writes */ *flags|=H5FD_FEAT_AGGREGATE_SMALLDATA; /* OK to aggregate "small" raw data allocations */ } done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_get_eoa * * Purpose: Returns the end-of-address marker for the file. The EOA * marker is the first address past the last byte allocated in * the format address space. * * Return: Success: The end-of-address-marker * * Failure: HADDR_UNDEF * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static haddr_t H5FD_family_get_eoa(H5FD_t *_file) { H5FD_family_t *file = (H5FD_family_t*)_file; haddr_t ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_get_eoa, HADDR_UNDEF) /* Set return value */ ret_value=file->eoa; done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_set_eoa * * Purpose: Set the end-of-address marker for the file. * * Return: Success: 0 * * Failure: -1 * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_set_eoa(H5FD_t *_file, haddr_t eoa) { H5FD_family_t *file = (H5FD_family_t*)_file; haddr_t addr=eoa; char memb_name[4096]; unsigned u; /* Local index variable */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_set_eoa, FAIL) for (u=0; addr || u<file->nmembs; u++) { /* Enlarge member array */ if (u>=file->amembs) { unsigned n = MAX(64, 2*file->amembs); H5FD_t **x = H5MM_realloc(file->memb, n*sizeof(H5FD_t*)); if (!x) HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to allocate memory block") file->amembs = n; file->memb = x; file->nmembs = u; } /* Create another file if necessary */ if (u>=file->nmembs || !file->memb[u]) { file->nmembs = MAX(file->nmembs, u+1); sprintf(memb_name, file->name, u); H5E_BEGIN_TRY { H5_CHECK_OVERFLOW(file->memb_size,hsize_t,haddr_t); file->memb[u] = H5FDopen(memb_name, file->flags|H5F_ACC_CREAT, file->memb_fapl_id, (haddr_t)file->memb_size); } H5E_END_TRY; if (NULL==file->memb[u]) HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, FAIL, "unable to open member file") } /* Set the EOA marker for the member */ H5_CHECK_OVERFLOW(file->memb_size,hsize_t,haddr_t); if (addr>(haddr_t)file->memb_size) { if(H5FD_set_eoa(file->memb[u], (haddr_t)file->memb_size)<0) HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to set file eoa") addr -= file->memb_size; } else { if(H5FD_set_eoa(file->memb[u], addr)<0) HGOTO_ERROR(H5E_FILE, H5E_CANTINIT, FAIL, "unable to set file eoa") addr = 0; } } file->eoa = eoa; done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_get_eof * * Purpose: Returns the end-of-file marker, which is the greater of * either the total family size or the current EOA marker. * * Return: Success: End of file address, the first address past * the end of the family of files or the current * EOA, whichever is larger. * * Failure: HADDR_UNDEF * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static haddr_t H5FD_family_get_eof(H5FD_t *_file) { H5FD_family_t *file = (H5FD_family_t*)_file; haddr_t eof=0; int i; /* Local index variable */ haddr_t ret_value; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_get_eof, HADDR_UNDEF) /* * Find the last member that has a non-zero EOF and break out of the loop * with `i' equal to that member. If all members have zero EOF then exit * loop with i==0. */ assert(file->nmembs>0); for (i=(int)file->nmembs-1; i>=0; --i) { if ((eof=H5FD_get_eof(file->memb[i]))!=0) break; if (0==i) break; } /* * The file size is the number of members before the i'th member plus the * size of the i'th member. */ eof += ((unsigned)i)*file->memb_size; /* Set return value */ ret_value=MAX(eof, file->eoa); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_get_handle * * Purpose: Returns the file handle of FAMILY file driver. * * Returns: Non-negative if succeed or negative if fails. * * Programmer: Raymond Lu * Sept. 16, 2002 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_get_handle(H5FD_t *_file, hid_t fapl, void** file_handle) { H5FD_family_t *file = (H5FD_family_t *)_file; H5P_genplist_t *plist; hsize_t offset; int memb; herr_t ret_value; FUNC_ENTER_NOAPI(H5FD_family_get_handle, FAIL) /* Get the plist structure and family offset */ if(NULL == (plist = H5P_object_verify(fapl, H5P_FILE_ACCESS))) HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "can't find object for ID") if(H5P_get(plist, H5F_ACS_FAMILY_OFFSET_NAME, &offset) < 0) HGOTO_ERROR(H5E_PLIST, H5E_CANTGET, FAIL, "can't get offset for family driver") if(offset>(file->memb_size*file->nmembs)) HGOTO_ERROR(H5E_ATOM, H5E_BADATOM, FAIL, "offset is bigger than file size") memb = (int)(offset/file->memb_size); ret_value = H5FD_get_vfd_handle(file->memb[memb], fapl, file_handle); done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_read * * Purpose: Reads SIZE bytes of data from FILE beginning at address ADDR * into buffer BUF according to data transfer properties in * DXPL_ID. * * Return: Success: Zero. Result is stored in caller-supplied * buffer BUF. * * Failure: -1, contents of buffer BUF are undefined. * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_read(H5FD_t *_file, H5FD_mem_t type, hid_t dxpl_id, haddr_t addr, size_t size, void *_buf/*out*/) { H5FD_family_t *file = (H5FD_family_t*)_file; unsigned char *buf = (unsigned char*)_buf; hid_t memb_dxpl_id = H5P_DATASET_XFER_DEFAULT; haddr_t sub; size_t req; hsize_t tempreq; unsigned u; /* Local index variable */ H5P_genplist_t *plist; /* Property list pointer */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_read, FAIL) /* * Get the member data transfer property list. If the transfer property * list does not belong to this driver then assume defaults */ if(NULL == (plist = H5I_object(dxpl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list") if (H5P_DATASET_XFER_DEFAULT!=dxpl_id && H5FD_FAMILY==H5P_get_driver(plist)) { H5FD_family_dxpl_t *dx = H5P_get_driver_info(plist); assert(TRUE==H5P_isa_class(dxpl_id, H5P_DATASET_XFER)); assert(dx); memb_dxpl_id = dx->memb_dxpl_id; } /* Read from each member */ while (size>0) { H5_ASSIGN_OVERFLOW(u,addr /file->memb_size,hsize_t,unsigned); sub = addr % file->memb_size; /* This check is for mainly for IA32 architecture whose size_t's size * is 4 bytes, to prevent overflow when user application is trying to * write files bigger than 4GB. */ tempreq = file->memb_size-sub; if(tempreq > SIZET_MAX) tempreq = SIZET_MAX; req = MIN(size, (size_t)tempreq); assert(u<file->nmembs); if (H5FDread(file->memb[u], type, memb_dxpl_id, sub, req, buf)<0) HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "member file read failed") addr += req; buf += req; size -= req; } done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_write * * Purpose: Writes SIZE bytes of data to FILE beginning at address ADDR * from buffer BUF according to data transfer properties in * DXPL_ID. * * Return: Success: Zero * * Failure: -1 * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_write(H5FD_t *_file, H5FD_mem_t type, hid_t dxpl_id, haddr_t addr, size_t size, const void *_buf) { H5FD_family_t *file = (H5FD_family_t*)_file; const unsigned char *buf = (const unsigned char*)_buf; hid_t memb_dxpl_id = H5P_DATASET_XFER_DEFAULT; haddr_t sub; size_t req; hsize_t tempreq; unsigned u; /* Local index variable */ H5P_genplist_t *plist; /* Property list pointer */ herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_write, FAIL) /* * Get the member data transfer property list. If the transfer property * list does not belong to this driver then assume defaults. */ if(NULL == (plist = H5I_object(dxpl_id))) HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list") if (H5P_DATASET_XFER_DEFAULT!=dxpl_id && H5FD_FAMILY==H5P_get_driver(plist)) { H5FD_family_dxpl_t *dx = H5P_get_driver_info(plist); assert(TRUE==H5P_isa_class(dxpl_id, H5P_DATASET_XFER)); assert(dx); memb_dxpl_id = dx->memb_dxpl_id; } /* Write to each member */ while (size>0) { H5_ASSIGN_OVERFLOW(u,addr /file->memb_size,hsize_t,unsigned); sub = addr % file->memb_size; /* This check is for mainly for IA32 architecture whose size_t's size * is 4 bytes, to prevent overflow when user application is trying to * write files bigger than 4GB. */ tempreq = file->memb_size-sub; if(tempreq > SIZET_MAX) tempreq = SIZET_MAX; req = MIN(size, (size_t)tempreq); assert(u<file->nmembs); if (H5FDwrite(file->memb[u], type, memb_dxpl_id, sub, req, buf)<0) HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "member file write failed") addr += req; buf += req; size -= req; } done: FUNC_LEAVE_NOAPI(ret_value) } /*------------------------------------------------------------------------- * Function: H5FD_family_flush * * Purpose: Flushes all family members. * * Return: Success: 0 * * Failure: -1, as many files flushed as possible. * * Programmer: Robb Matzke * Wednesday, August 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t H5FD_family_flush(H5FD_t *_file, hid_t dxpl_id, unsigned closing) { H5FD_family_t *file = (H5FD_family_t*)_file; unsigned u, nerrors=0; herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_family_flush, FAIL) for (u=0; u<file->nmembs; u++) if (file->memb[u] && H5FD_flush(file->memb[u], dxpl_id, closing)<0) nerrors++; if (nerrors) HGOTO_ERROR(H5E_IO, H5E_BADVALUE, FAIL, "unable to flush member files") done: FUNC_LEAVE_NOAPI(ret_value) }