summaryrefslogtreecommitdiffstats
path: root/src/H5HFsection.c
blob: 6cbe4cb80b892cbc0714e73db759430a35b3ecbb (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Programmer:  Quincey Koziol <koziol@ncsa.uiuc.edu>
 *              Monday, May  1, 2006
 *
 * Purpose:	Free space section routines for fractal heaps.
 *
 */

/****************/
/* Module Setup */
/****************/

#define H5HF_PACKAGE		/*suppress error about including H5HFpkg  */

/***********/
/* Headers */
/***********/
#include "H5private.h"		/* Generic Functions			*/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5HFpkg.h"		/* Fractal heaps			*/

/****************/
/* Local Macros */
/****************/


/******************/
/* Local Typedefs */
/******************/


/********************/
/* Package Typedefs */
/********************/


/********************/
/* Local Prototypes */
/********************/
static herr_t H5HF_sect_single_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect);
static herr_t H5HF_sect_range_init(H5FS_section_class_t *cls, const void *udata);
static herr_t H5HF_sect_range_serialize(const H5FS_section_info_t *sect, uint8_t *buf);
static herr_t H5HF_sect_range_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect);
static herr_t H5HF_sect_range_debug(const H5FS_section_info_t *sect,
    FILE *stream, int indent, int fwidth);
static herr_t H5HF_sect_indirect_init(H5FS_section_class_t *cls, const void *udata);
static herr_t H5HF_sect_indirect_serialize(const H5FS_section_info_t *sect, uint8_t *buf);
static herr_t H5HF_sect_indirect_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect);
static herr_t H5HF_sect_indirect_debug(const H5FS_section_info_t *sect,
    FILE *stream, int indent, int fwidth);


/*********************/
/* Package Variables */
/*********************/

/* Class info for "single" free space sections */
/* (No callbacks necessary) */
H5FS_section_class_t H5FS_SECT_CLS_FHEAP_SINGLE[1] = {{
    H5FS_SECT_FHEAP_SINGLE,
    0,
    NULL,
    NULL,
    H5HF_sect_single_deserialize,
    NULL,
}};

/* Class info for "range" free space sections */
H5FS_section_class_t H5FS_SECT_CLS_FHEAP_RANGE[1] = {{
    H5FS_SECT_FHEAP_RANGE,
    0,
    H5HF_sect_range_init,
    H5HF_sect_range_serialize,
    H5HF_sect_range_deserialize,
    H5HF_sect_range_debug,
}};

/* Class info for "indirect" free space sections */
H5FS_section_class_t H5FS_SECT_CLS_FHEAP_INDIRECT[1] = {{
    H5FS_SECT_FHEAP_INDIRECT,
    0,
    H5HF_sect_indirect_init,
    H5HF_sect_indirect_serialize,
    H5HF_sect_indirect_deserialize,
    H5HF_sect_indirect_debug,
}};

/* Declare a free list to manage the H5HF_free_section_t struct */
H5FL_DEFINE(H5HF_free_section_t);


/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/



/*-------------------------------------------------------------------------
 * Function:	H5HF_free_section_free_cb
 *
 * Purpose:	Free a free section node
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, March 13, 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_free_section_free_cb(void *_sect, void UNUSED *key, void UNUSED *op_data)
{
    H5HF_free_section_t *sect = (H5HF_free_section_t *)_sect;
    herr_t ret_value = 0;               /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_free_section_free_cb)

    HDassert(sect);

    /* Check for live reference to an indirect block */
    if(sect->sect_info.state == H5FS_SECT_LIVE) {
        H5HF_indirect_t *iblock;        /* Indirect block referenced */

        /* Find indirect block that free section references */
        switch(sect->sect_info.cls->type) {
            case H5FS_SECT_FHEAP_SINGLE:
                iblock = sect->u.single.parent;
                break;

            case H5FS_SECT_FHEAP_RANGE:
                iblock = sect->u.range.iblock;
                break;

             case H5FS_SECT_FHEAP_INDIRECT:
                iblock = sect->u.indirect.iblock;
                break;
        } /* end switch */

        /* Release indirect block, if there was one */
        if(iblock)
            if(H5HF_iblock_decr(iblock) < 0)
                HGOTO_ERROR(H5E_HEAP, H5E_CANTDEC, FAIL, "can't decrement reference count on shared indirect block")
    } /* end if */

    /* Release the sections */
    H5FL_FREE(H5HF_free_section_t, sect);

done:
    FUNC_LEAVE_NOAPI(ret_value)
}   /* H5HF_free_section_free_cb() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_node_alloc
 *
 * Purpose:	Allocate a free space section node of a particular type
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Saturday, May 13, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_node_alloc(H5FS_section_class_t *sect_cls, unsigned sect_type,
    haddr_t sect_addr, hsize_t sect_size, H5FS_section_info_t **sect)
{
    H5HF_free_section_t *new_sect;      /* New section */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_sect_node_alloc)

    /* Check arguments. */
    HDassert(H5F_addr_defined(sect_addr));
    HDassert(sect_size);
    HDassert(sect);

    /* Create free list section node */
    if(NULL == (new_sect = H5FL_MALLOC(H5HF_free_section_t)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed for direct block free list section")

    /* Set the information passed in */
    new_sect->sect_info.addr = sect_addr;
    new_sect->sect_info.size = sect_size;

    /* Set the section's class & state */
    new_sect->sect_info.cls = &sect_cls[sect_type];
    new_sect->sect_info.state = H5FS_SECT_SERIALIZED;

    /* Update the return parameter */
    *sect = (H5FS_section_info_t *)new_sect;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5HF_sect_node_alloc() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_single_deserialize
 *
 * Purpose:	Deserialize a buffer into a "live" single section
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_single_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_sect_single_deserialize)

    /* Check arguments. */
    HDassert(buf);
    HDassert(H5F_addr_defined(sect_addr));
    HDassert(sect_size);
    HDassert(sect);

    /* Create free list section node */
    if(H5HF_sect_node_alloc(sect_cls, H5FS_SECT_FHEAP_SINGLE, sect_addr, sect_size, sect) < 0)
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "allocation failed for direct block free list section")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5HF_sect_single_deserialize() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_range_init
 *
 * Purpose:	Initialize the "range" class structure
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_range_init(H5FS_section_class_t *cls, const void UNUSED *udata)
{
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_range_init)

    /* Check arguments. */
    HDassert(cls);

    /* Set the size of all serialized objects of this class of sections */
    cls->serial_size = 2                        /* Row */
        + 2                                     /* Column */
        + 2;                                    /* # of entries */

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_range_init() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_range_serialize
 *
 * Purpose:	Serialize a "live" range section into a buffer
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_range_serialize(const H5FS_section_info_t *_sect, uint8_t *buf)
{
    const H5HF_free_section_t *sect = (const H5HF_free_section_t *)_sect;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_range_serialize)

    /* Check arguments. */
    HDassert(buf);
    HDassert(sect);

    /* Range's row */
    UINT16ENCODE(buf, sect->u.range.row);

    /* Range's column */
    UINT16ENCODE(buf, sect->u.range.col);

    /* Range's # of entries */
    UINT16ENCODE(buf, sect->u.range.num_entries);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_range_serialize() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_range_deserialize
 *
 * Purpose:	Deserialize a buffer into a "live" range section
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_range_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_sect_range_deserialize)

    /* Check arguments. */
    HDassert(buf);
    HDassert(H5F_addr_defined(sect_addr));
    HDassert(sect_size);
    HDassert(sect);

    /* Create free list section node */
    if(H5HF_sect_node_alloc(sect_cls, H5FS_SECT_FHEAP_RANGE, sect_addr, sect_size, sect) < 0)
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "allocation failed for direct block free list section")

    /* Range's row */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.range.row);

    /* Range's column */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.range.col);

    /* Range's # of entries */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.range.num_entries);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5HF_sect_range_deserialize() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_range_debug
 *
 * Purpose:	Dump debugging information about an range free space section
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Saturday, May 13, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_range_debug(const H5FS_section_info_t *_sect,
    FILE *stream, int indent, int fwidth)
{
    const H5HF_free_section_t *sect = (const H5HF_free_section_t *)_sect;       /* Section to dump info */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_range_debug)

    /* Check arguments. */
    HDassert(sect);

    /* Print range section information */
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Row:",
	      sect->u.range.row);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Column:",
	      sect->u.range.col);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Number of entries:",
	      sect->u.range.num_entries);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_range_debug() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_indirect_init
 *
 * Purpose:	Initialize the "indirect" class structure
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_indirect_init(H5FS_section_class_t *cls, const void UNUSED *udata)
{
    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_indirect_init)

    /* Check arguments. */
    HDassert(cls);

    /* Set the size of all serialized objects of this class of sections */
    cls->serial_size = 2                        /* Row */
        + 2                                     /* Column */
        + 2                                     /* # of entries */
        + 2                                     /* Indirect row */
        + 2;                                    /* Indirect # of rows */

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_indirect_init() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_indirect_serialize
 *
 * Purpose:	Serialize a "live" indirect section into a buffer
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_indirect_serialize(const H5FS_section_info_t *_sect, uint8_t *buf)
{
    const H5HF_free_section_t *sect = (const H5HF_free_section_t *)_sect;

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_indirect_serialize)

    /* Check arguments. */
    HDassert(buf);
    HDassert(_sect);

    /* Range's row */
    UINT16ENCODE(buf, sect->u.indirect.row);

    /* Range's column */
    UINT16ENCODE(buf, sect->u.indirect.col);

    /* Range's # of entries */
    UINT16ENCODE(buf, sect->u.indirect.num_entries);

    /* Range's indirect row */
    UINT16ENCODE(buf, sect->u.indirect.indir_row);

    /* Range's indirect # of rows */
    UINT16ENCODE(buf, sect->u.indirect.indir_nrows);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_indirect_serialize() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_indirect_deserialize
 *
 * Purpose:	Deserialize a buffer into a "live" indirect section
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Monday, May  1, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_indirect_deserialize(H5FS_section_class_t *sect_cls,
    const uint8_t *buf, haddr_t sect_addr, hsize_t sect_size,
    H5FS_section_info_t **sect)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_sect_indirect_deserialize)

    /* Check arguments. */
    HDassert(buf);
    HDassert(H5F_addr_defined(sect_addr));
    HDassert(sect_size);
    HDassert(sect);

    /* Create free list section node */
    if(H5HF_sect_node_alloc(sect_cls, H5FS_SECT_FHEAP_INDIRECT, sect_addr, sect_size, sect) < 0)
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "allocation failed for direct block free list section")

    /* Range's row */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.indirect.row);

    /* Range's column */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.indirect.col);

    /* Range's # of entries */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.indirect.num_entries);

    /* Range's indirect row */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.indirect.indir_row);

    /* Range's indirect # of rows */
    UINT16DECODE(buf, ((H5HF_free_section_t *)*sect)->u.indirect.indir_nrows);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5HF_sect_indirect_deserialize() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_sect_indirect_debug
 *
 * Purpose:	Dump debugging information about an indirect free space section
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *              Saturday, May 13, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF_sect_indirect_debug(const H5FS_section_info_t *_sect,
    FILE *stream, int indent, int fwidth)
{
    const H5HF_free_section_t *sect = (const H5HF_free_section_t *)_sect;       /* Section to dump info */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5HF_sect_indirect_debug)

    /* Check arguments. */
    HDassert(sect);

    /* Print indirect section information */
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Row:",
	      sect->u.indirect.row);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Column:",
	      sect->u.indirect.col);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Number of entries:",
	      sect->u.indirect.num_entries);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Indirect row:",
	      sect->u.indirect.indir_row);
    HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
	      "Indirect number of rows:",
	      sect->u.indirect.indir_nrows);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* H5HF_sect_indirect_debug() */