summaryrefslogtreecommitdiffstats
path: root/src/H5WB.c
blob: b9e11d9232be60eb47efe364d9f741ee50eaa02c (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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:		H5WB.c
 *			Jun 26 2007
 *			Quincey Koziol
 *
 * Purpose:		Implements the "wrapped buffer" code for wrapping
 *                      an existing [statically sized] buffer, in order to
 *                      avoid lots of memory allocation calls.
 *
 *-------------------------------------------------------------------------
 */

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

/***********/
/* Headers */
/***********/
#include "H5private.h"   /* Generic Functions			*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5FLprivate.h" /* Free Lists                           */
#include "H5WBprivate.h" /* Wrapped Buffers                      */

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

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

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

/* Typedef for buffer wrapper */
struct H5WB_t {
    void  *wrapped_buf;  /* Pointer to wrapped buffer */
    size_t wrapped_size; /* Size of wrapped buffer */
    void  *actual_buf;   /* Pointer to actual buffer */
    size_t actual_size;  /* Size of actual buffer used */
    size_t alloc_size;   /* Size of actual buffer allocated */
};

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

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

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

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

/* Declare a free list to manage the H5WB_t struct */
H5FL_DEFINE_STATIC(H5WB_t);

/* Declare a free list to manage the extra buffer information */
H5FL_BLK_DEFINE_STATIC(extra_buf);

/*-------------------------------------------------------------------------
 * Function:	H5WB_wrap
 *
 * Purpose:	Wraps an existing [possibly static] buffer
 *
 * Return:	Pointer to buffer wrapper info on success
 *              NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		Jun 26 2007
 *
 *-------------------------------------------------------------------------
 */
H5WB_t *
H5WB_wrap(void *buf, size_t buf_size)
{
    H5WB_t *wb = NULL; /* Wrapped buffer info */
    H5WB_t *ret_value; /* Return value */

    FUNC_ENTER_NOAPI(NULL)

    /*
     * Check arguments.
     */
    assert(buf);
    assert(buf_size);

    /* Create wrapped buffer info */
    if (NULL == (wb = H5FL_MALLOC(H5WB_t)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for wrapped buffer info")

    /* Wrap buffer given */
    wb->wrapped_buf  = buf;
    wb->wrapped_size = buf_size;

    /* No actual buffer yet */
    wb->actual_buf  = NULL;
    wb->actual_size = 0;
    wb->alloc_size  = 0;

    /* Set the return value */
    ret_value = wb;

done:
    /* Release resources on error */
    if (!ret_value && wb)
        wb = H5FL_FREE(H5WB_t, wb);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5WB_wrap() */

/*-------------------------------------------------------------------------
 * Function:	H5WB_actual
 *
 * Purpose:	Get the pointer to an "actual" buffer, of at least a certain
 *              size.
 *
 * Return:	Pointer to buffer pointer on success
 *              NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		Jun 26 2007
 *
 *-------------------------------------------------------------------------
 */
void *
H5WB_actual(H5WB_t *wb, size_t need)
{
    void *ret_value; /* Return value */

    FUNC_ENTER_NOAPI(NULL)

    /*
     * Check arguments.
     */
    assert(wb);
    assert(wb->wrapped_buf);

    /* Check for previously allocated buffer */
    if (wb->actual_buf && wb->actual_buf != wb->wrapped_buf) {
        /* Sanity check */
        assert(wb->actual_size > wb->wrapped_size);

        /* Check if we can re-use existing buffer */
        if (need <= wb->alloc_size)
            HGOTO_DONE(wb->actual_buf)
        /* Can't re-use existing buffer, free it and proceed */
        else
            wb->actual_buf = H5FL_BLK_FREE(extra_buf, wb->actual_buf);
    } /* end if */

    /* Check if size needed can be fulfilled with wrapped buffer */
    if (need > wb->wrapped_size) {
        /* Need to allocate new buffer */
        if (NULL == (wb->actual_buf = H5FL_BLK_MALLOC(extra_buf, need)))
            HGOTO_ERROR(H5E_ATTR, H5E_NOSPACE, NULL, "memory allocation failed")

        /* Remember size of buffer allocated */
        wb->alloc_size = need;
    } /* end if */
    else {
        /* Don't have to allocate a new buffer, use the wrapped one */
        wb->actual_buf = wb->wrapped_buf;
        wb->alloc_size = 0;
    } /* end else */

    /* Set the return value */
    ret_value = wb->actual_buf;

done:
    /* Remember size of buffer used, if we were successful */
    if (ret_value)
        wb->actual_size = need;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5WB_actual() */

/*-------------------------------------------------------------------------
 * Function:	H5WB_actual_clear
 *
 * Purpose:	Get the pointer to an "actual" buffer, of at least a certain
 *              size.  Also, clear actual buffer to zeros.
 *
 * Return:	Pointer to buffer pointer on success
 *              NULL on failure
 *
 * Programmer:	Quincey Koziol
 *		Jun 26 2007
 *
 *-------------------------------------------------------------------------
 */
void *
H5WB_actual_clear(H5WB_t *wb, size_t need)
{
    void *ret_value; /* Return value */

    FUNC_ENTER_NOAPI(NULL)

    /*
     * Check arguments.
     */
    assert(wb);
    assert(wb->wrapped_buf);

    /* Get a pointer to an actual buffer */
    if (NULL == (ret_value = H5WB_actual(wb, need)))
        HGOTO_ERROR(H5E_ATTR, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Clear the buffer */
    HDmemset(ret_value, 0, need);

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

/*-------------------------------------------------------------------------
 * Function:	H5WB_unwrap
 *
 * Purpose:	"unwrap" a wrapped buffer, releasing all resources used
 *
 * Return:	SUCCEED/FAIL
 *
 * Programmer:	Quincey Koziol
 *		Jun 26 2007
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5WB_unwrap(H5WB_t *wb)
{
    FUNC_ENTER_NOAPI_NOINIT_NOERR

    /*
     * Check arguments.
     */
    assert(wb);
    assert(wb->wrapped_buf);

    /* Release any extra buffers allocated */
    if (wb->actual_buf && wb->actual_buf != wb->wrapped_buf) {
        /* Sanity check */
        assert(wb->actual_size > wb->wrapped_size);

        wb->actual_buf = H5FL_BLK_FREE(extra_buf, wb->actual_buf);
    } /* end if */

    /* Release the buffer wrapper info */
    wb = H5FL_FREE(H5WB_t, wb);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5WB_unwrap() */