summaryrefslogtreecommitdiffstats
path: root/src/H5HFspace.c
blob: 3519e91b9d1a70e7975c5aca8ad13edc48adfe5b (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5HFspace.c
 *			May  2 2006
 *			Quincey Koziol <koziol@ncsa.uiuc.edu>
 *
 * Purpose:		Space allocation 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 */
/****************/

#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 */

/******************/
/* 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
 *		koziol@ncsa.uiuc.edu
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_space_start(H5HF_hdr_t *hdr, hid_t dxpl_id)
{
    const H5FS_section_class_t *classes[] = { /* Free space section classes implemented for fractal heap */
        H5HF_FSPACE_SECT_CLS_SINGLE,
        H5HF_FSPACE_SECT_CLS_RANGE,
        H5HF_FSPACE_SECT_CLS_INDIRECT};
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_start)

    /*
     * 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, dxpl_id, hdr->fs_addr,
                NELMTS(classes), classes, hdr)))
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize free space info")
    } /* end if */
    else {
        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, dxpl_id, &hdr->fs_addr,
                &fs_create, NELMTS(classes), classes, hdr)))
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize free space info")
    } /* end else */

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


/*-------------------------------------------------------------------------
 * Function:	H5HF_space_find
 *
 * Purpose:	Attempt to find space in a fractal heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5HF_space_find(H5HF_hdr_t *hdr, hid_t dxpl_id, hsize_t request, H5HF_free_section_t **node)
{
    htri_t node_found;          /* Whether an existing free list node was found */
    htri_t ret_value;           /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_find)

    /*
     * 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, dxpl_id) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize heap free space")

    /* Search for free space in the heap */
    if((node_found = H5FS_find(hdr->f, dxpl_id, 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_add
 *
 * Purpose:	Add a section to the free space for the heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_space_add(H5HF_hdr_t *hdr, hid_t dxpl_id, H5HF_free_section_t *node)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_add)

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

    /* Add to the free space for the heap */
    if(H5FS_add(hdr->f, dxpl_id, hdr->fspace, (H5FS_section_info_t *)node, 0, NULL) < 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_return
 *
 * Purpose:	Return a freedsection to the free space for the heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		May 15 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_space_return(H5HF_hdr_t *hdr, hid_t dxpl_id, H5HF_free_section_t *node)
{
    H5HF_add_ud1_t udata;               /* User data for free space manager 'add' */
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_return)

    /*
     * 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, dxpl_id) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTINIT, FAIL, "can't initialize heap free space")

    /* Construct user data */
    udata.hdr = hdr;
    udata.dxpl_id = dxpl_id;
    udata.dblock = NULL;

    /* Add to the free space for the heap */
    if(H5FS_add(hdr->f, dxpl_id, hdr->fspace, (H5FS_section_info_t *)node, H5FS_ADD_RETURNED_SPACE, &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_return() */


/*-------------------------------------------------------------------------
 * Function:	H5HF_space_close
 *
 * Purpose:	Close the free space for the heap
 *
 * Return:	Success:	non-negative
 *
 *		Failure:	negative
 *
 * Programmer:	Quincey Koziol
 *		koziol@ncsa.uiuc.edu
 *		May  2 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5HF_space_close(H5HF_hdr_t *hdr, hid_t dxpl_id)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5HF_space_close)

    /*
     * 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_get_sect_count(hdr->fspace, &nsects) < 0)
            HGOTO_ERROR(H5E_HEAP, H5E_CANTCOUNT, FAIL, "can't query free space section count")
#ifdef QAK
HDfprintf(stderr, "%s: nsects = %Hu\n", FUNC, nsects);
#endif /* QAK */

        /* Close the free space for the heap */
        if(H5FS_close(hdr->f, dxpl_id, 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, dxpl_id, 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() */