summaryrefslogtreecommitdiffstats
path: root/src/H5HFspace.c
blob: 53b4c4923f1df4b89aacaeeac7ae538cef0cedde (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * 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 COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5HFspace.c
 *			May  2 2006
 *			Quincey Koziol
 *
 * Purpose:		Space allocation routines for fractal heaps.
 *
 *-------------------------------------------------------------------------
 */

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

#include "H5HFmodule.h" /* This source code file is part of the H5HF module */

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

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

#define H5HF_FSPACE_SHRINK    80  /* Percent of "normal" size to shrink serialized free space size */
#define H5HF_FSPACE_EXPAND    120 /* Percent of "normal" size to expand serialized free space size */
#define H5HF_FSPACE_THRHD_DEF 1   /* Default: no alignment threshold */
#define H5HF_FSPACE_ALIGN_DEF 1   /* Default: no alignment */

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

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

/********************/
/* Local Prototypes */
/********************/

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

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

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

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_start
 *
 * Purpose:	"Start up" free space for heap - open existing free space
 *              structure if one exists, otherwise create a new free space
 *              structure
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_start(H5HF_hdr_t *hdr, hbool_t may_create)
{
    const H5FS_section_class_t *classes[] = {/* Free space section classes implemented for fractal heap */
                                             H5HF_FSPACE_SECT_CLS_SINGLE, H5HF_FSPACE_SECT_CLS_FIRST_ROW,
                                             H5HF_FSPACE_SECT_CLS_NORMAL_ROW, H5HF_FSPACE_SECT_CLS_INDIRECT};
    herr_t                      ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Check for creating free space info for the heap */
    if (H5F_addr_defined(hdr->fs_addr)) {
        /* Open an existing free space structure for the heap */
        if (NULL == (hdr->fspace = H5FS_open(hdr->f, hdr->fs_addr, NELMTS(classes), classes, hdr,
                                             (hsize_t)H5HF_FSPACE_THRHD_DEF, (hsize_t)H5HF_FSPACE_ALIGN_DEF)))
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize free space info")
    } /* end if */
    else {
        /* Check if we are allowed to create the free space manager */
        if (may_create) {
            H5FS_create_t fs_create; /* Free space creation parameters */

            /* Set the free space creation parameters */
            fs_create.client         = H5FS_CLIENT_FHEAP_ID;
            fs_create.shrink_percent = H5HF_FSPACE_SHRINK;
            fs_create.expand_percent = H5HF_FSPACE_EXPAND;
            fs_create.max_sect_size  = hdr->man_dtable.cparam.max_direct_size;
            fs_create.max_sect_addr  = hdr->man_dtable.cparam.max_index;

            /* Create the free space structure for the heap */
            if (NULL ==
                (hdr->fspace = H5FS_create(hdr->f, &hdr->fs_addr, &fs_create, NELMTS(classes), classes, hdr,
                                           (hsize_t)H5HF_FSPACE_THRHD_DEF, (hsize_t)H5HF_FSPACE_ALIGN_DEF)))
                HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize free space info")
            HDassert(H5F_addr_defined(hdr->fs_addr));
        } /* end if */
    }     /* end else */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_start() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_add
 *
 * Purpose:	Add a section to the free space for the heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		May 15 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_add(H5HF_hdr_t *hdr, H5HF_free_section_t *node, unsigned flags)
{
    H5HF_sect_add_ud_t udata;               /* User data for free space manager 'add' */
    herr_t             ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(node);

    /* Check if the free space for the heap has been initialized */
    if (!hdr->fspace)
        if (H5HF__space_start(hdr, TRUE) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize heap free space")

    /* Construct user data */
    udata.hdr = hdr;

    /* Add to the free space for the heap */
    if (H5FS_sect_add(hdr->f, hdr->fspace, (H5FS_section_info_t *)node, flags, &udata) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTINSERT, FAIL, "can't add section to heap free space")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_add() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_find
 *
 * Purpose:	Attempt to find space in a fractal heap
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5HF__space_find(H5HF_hdr_t *hdr, hsize_t request, H5HF_free_section_t **node)
{
    htri_t node_found = FALSE; /* Whether an existing free list node was found */
    htri_t ret_value  = FAIL;  /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(request);
    HDassert(node);

    /* Check if the free space for the heap has been initialized */
    if (!hdr->fspace)
        if (H5HF__space_start(hdr, FALSE) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize heap free space")

    /* Search for free space in the heap */
    if (hdr->fspace)
        if ((node_found = H5FS_sect_find(hdr->f, hdr->fspace, request, (H5FS_section_info_t **)node)) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, FAIL, "can't locate free space in fractal heap")

    /* Set return value */
    ret_value = node_found;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_find() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_revert_root_cb
 *
 * Purpose:	Callback routine from iterator, to reset 'parent' pointers in
 *		sections, when the heap is changing from having a root indirect
 *		block to a direct block.
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		Feb 24 2012
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF__space_revert_root_cb(H5FS_section_info_t *_sect, void H5_ATTR_UNUSED *_udata)
{
    H5HF_free_section_t *sect      = (H5HF_free_section_t *)_sect; /* Section to dump info */
    herr_t               ret_value = SUCCEED;                      /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Only modify "live" single blocks... */
    if (sect->sect_info.type == H5HF_FSPACE_SECT_SINGLE && sect->sect_info.state == H5FS_SECT_LIVE) {
        /* Release hold on previous indirect block (we must have one) */
        HDassert(sect->u.single.parent);
        if (H5HF__iblock_decr(sect->u.single.parent) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTDEC, FAIL,
                        "can't decrement reference count on section's indirect block")

        /* Reset parent information */
        sect->u.single.parent    = NULL;
        sect->u.single.par_entry = 0;
    } /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_revert_root_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_revert_root
 *
 * Purpose:	Reset 'parent' pointers in sections, when the heap is
 *		changing from having a root indirect block to a direct block.
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		Feb 23 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_revert_root(const H5HF_hdr_t *hdr)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Only need to scan the sections if the free space has been initialized */
    if (hdr->fspace)
        /* Iterate over all sections, resetting the parent pointers in 'single' sections */
        if (H5FS_sect_iterate(hdr->f, hdr->fspace, H5HF__space_revert_root_cb, NULL) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over sections to reset parent pointers")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_revert_root() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_create_root_cb
 *
 * Purpose:	Callback routine from iterator, to set 'parent' pointers in
 *		sections to newly created root indirect block, when the heap
 *		is changing from having a root direct block to an indirect block.
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		Feb 24 2012
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5HF__space_create_root_cb(H5FS_section_info_t *_sect, void *_udata)
{
    H5HF_free_section_t *sect        = (H5HF_free_section_t *)_sect; /* Section to dump info */
    H5HF_indirect_t     *root_iblock = (H5HF_indirect_t *)_udata;    /* User data for callback */
    herr_t               ret_value   = SUCCEED;                      /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Sanity check sections */
    /* (If we are switching from a direct block for the root block of the heap, */
    /*	there should only be 'single' type sections. -QAK) */
    HDassert(sect->sect_info.type == H5HF_FSPACE_SECT_SINGLE);

    /* Increment ref. count on new root indirect block */
    if (H5HF__iblock_incr(root_iblock) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTINC, FAIL,
                    "can't increment reference count on section's indirect block")

    /* Set parent info ("live" section must _NOT_ have a parent right now) */
    if (sect->sect_info.state == H5FS_SECT_SERIALIZED)
        sect->sect_info.state = H5FS_SECT_LIVE; /* Mark "live" now */
    else
        HDassert(!sect->u.single.parent);
    sect->u.single.parent    = root_iblock;
    sect->u.single.par_entry = 0;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_create_root_cb() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_create_root
 *
 * Purpose:	Set 'parent' pointers in sections to new indirect block, when
 *		the heap is changing from having a root direct block to a
 *		indirect block.
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		Feb 24 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_create_root(const H5HF_hdr_t *hdr, H5HF_indirect_t *root_iblock)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(root_iblock);

    /* Only need to scan the sections if the free space has been initialized */
    if (hdr->fspace)
        /* Iterate over all sections, setting the parent pointers in 'single' sections to the new indirect
         * block */
        if (H5FS_sect_iterate(hdr->f, hdr->fspace, H5HF__space_create_root_cb, root_iblock) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_BADITER, FAIL, "can't iterate over sections to set parent pointers")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_create_root() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_size
 *
 * Purpose:	Query the size of the heap's free space info on disk
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		August 14 2007
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_size(H5HF_hdr_t *hdr, hsize_t *fs_size)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(fs_size);

    /* Check if the free space for the heap has been initialized */
    if (!hdr->fspace)
        if (H5HF__space_start(hdr, FALSE) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize heap free space")

    /* Get free space metadata size */
    if (hdr->fspace) {
        if (H5FS_size(hdr->fspace, fs_size) < 0)
            HGOTO_ERROR(H5E_FSPACE, H5E_CANTGET, FAIL, "can't retrieve FS meta storage info")
    } /* end if */
    else
        *fs_size = 0;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_size() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_remove
 *
 * Purpose:	Remove a section from the free space for the heap
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		July 24 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_remove(H5HF_hdr_t *hdr, H5HF_free_section_t *node)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(hdr->fspace);
    HDassert(node);

    /* Remove from the free space for the heap */
    if (H5FS_sect_remove(hdr->f, hdr->fspace, (H5FS_section_info_t *)node) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTREMOVE, FAIL, "can't remove section from heap free space")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_remove() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_close
 *
 * Purpose:	Close the free space for the heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_close(H5HF_hdr_t *hdr)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Check if the free space was ever opened */
    if (hdr->fspace) {
        hsize_t nsects; /* Number of sections for this heap */

        /* Retrieve the number of sections for this heap */
        if (H5FS_sect_stats(hdr->fspace, NULL, &nsects) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTCOUNT, FAIL, "can't query free space section count")

        /* Close the free space for the heap */
        if (H5FS_close(hdr->f, hdr->fspace) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTRELEASE, FAIL, "can't release free space info")
        hdr->fspace = NULL;

        /* Check if we can delete the free space manager for this heap */
        if (!nsects) {
            if (H5FS_delete(hdr->f, hdr->fs_addr) < 0)
                HGOTO_ERROR(H5E_HEAP, H5E_CANTDELETE, FAIL, "can't delete free space info")
            hdr->fs_addr = HADDR_UNDEF;
        } /* end if */
    }     /* end if */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_close() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_delete
 *
 * Purpose:	Delete the free space manager for the heap
 *
 * Return:	Success:	non-negative
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		Aug  7 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_delete(H5HF_hdr_t *hdr)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

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

    /* Delete the free space manager */
    if (H5FS_delete(hdr->f, hdr->fs_addr) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTFREE, FAIL, "can't delete to free space manager")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_delete() */

/*-------------------------------------------------------------------------
 * Function:	H5HF__space_sect_change_class
 *
 * Purpose:	Change a section's class
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		July 10 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF__space_sect_change_class(H5HF_hdr_t *hdr, H5HF_free_section_t *sect, uint16_t new_class)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /*
     * Check arguments.
     */
    HDassert(hdr);
    HDassert(hdr->fspace);
    HDassert(sect);

    /* Notify the free space manager that a section has changed class */
    if (H5FS_sect_change_class(hdr->f, hdr->fspace, (H5FS_section_info_t *)sect, new_class) < 0)
        HGOTO_ERROR(H5E_HEAP, H5E_CANTMODIFY, FAIL, "can't modify class of free space section")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5HF__space_sect_change_class() */