summaryrefslogtreecommitdiff
path: root/src/util/viralloc.c
blob: 7aa14cccb28d1e6a4c5cd6a9dd7f56d4235ec71f (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
/*
 * viralloc.c: safer memory allocation
 *
 * Copyright (C) 2010-2014 Red Hat, Inc.
 * Copyright (C) 2008 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "viralloc.h"
#include "virlog.h"
#include "virerror.h"

#define VIR_FROM_THIS VIR_FROM_NONE

VIR_LOG_INIT("util.alloc");


/**
 * virReallocN:
 * @ptrptr: pointer to pointer for address of allocated memory
 * @size: number of bytes to allocate
 * @count: number of elements in array
 *
 * Resize the block of memory in 'ptrptr' to be an array of
 * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
 * with the address of the newly allocated memory. On failure,
 * 'ptrptr' is not changed and still points to the original memory
 * block. Any newly allocated memory in 'ptrptr' is uninitialized.
 *
 * Returns zero on success, aborts on OOM
 */
void virReallocN(void *ptrptr,
                 size_t size,
                 size_t count)
{
    *(void **)ptrptr = g_realloc_n(*(void**)ptrptr, size, count);
}

/**
 * virExpandN:
 * @ptrptr: pointer to pointer for address of allocated memory
 * @size: number of bytes per element
 * @countptr: pointer to number of elements in array
 * @add: number of elements to add
 *
 * Resize the block of memory in 'ptrptr' to be an array of
 * '*countptr' + 'add' elements, each 'size' bytes in length.
 * Update 'ptrptr' and 'countptr'  with the details of the newly
 * allocated memory. On failure, 'ptrptr' and 'countptr' are not
 * changed. Any newly allocated memory in 'ptrptr' is zero-filled.
 *
 * Aborts on OOM
 */
void virExpandN(void *ptrptr,
                size_t size,
                size_t *countptr,
                size_t add)
{
    if (*countptr + add < *countptr)
        abort();

    virReallocN(ptrptr, size, *countptr + add);
    memset(*(char **)ptrptr + (size * *countptr), 0, size * add);
    *countptr += add;
}

/**
 * virResizeN:
 * @ptrptr: pointer to pointer for address of allocated memory
 * @size: number of bytes per element
 * @allocptr: pointer to number of elements allocated in array
 * @count: number of elements currently used in array
 * @add: minimum number of additional elements to support in array
 *
 * If 'count' + 'add' is larger than '*allocptr', then resize the
 * block of memory in 'ptrptr' to be an array of at least 'count' +
 * 'add' elements, each 'size' bytes in length. Update 'ptrptr' and
 * 'allocptr' with the details of the newly allocated memory. On
 * failure, 'ptrptr' and 'allocptr' are not changed. Any newly
 * allocated memory in 'ptrptr' is zero-filled.
 *
 * Aborts on OOM
 */
void virResizeN(void *ptrptr,
                size_t size,
                size_t *allocptr,
                size_t count,
                size_t add)
{
    size_t delta;

    if (count + add < count)
        abort();

    if (count + add <= *allocptr)
        return;

    delta = count + add - *allocptr;
    if (delta < *allocptr / 2)
        delta = *allocptr / 2;

    virExpandN(ptrptr, size, allocptr, delta);
}

/**
 * virShrinkN:
 * @ptrptr: pointer to pointer for address of allocated memory
 * @size: number of bytes per element
 * @countptr: pointer to number of elements in array
 * @toremove: number of elements to remove
 *
 * Resize the block of memory in 'ptrptr' to be an array of
 * '*countptr' - 'toremove' elements, each 'size' bytes in length.
 * Update 'ptrptr' and 'countptr'  with the details of the newly
 * allocated memory. If 'toremove' is larger than 'countptr', free
 * the entire array.
 */
void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
{
    if (toremove < *countptr) {
        virReallocN(ptrptr, size, *countptr -= toremove);
    } else {
        g_clear_pointer(((void **)ptrptr), g_free);
        *countptr = 0;
    }
}

/**
 * virInsertElementsN:
 * @ptrptr:   pointer to hold address of allocated memory
 * @size:     the size of one element in bytes
 * @at:       index within array where new elements should be added, -1 for end
 * @countptr: variable tracking number of elements currently allocated
 * @newelems: pointer to array of one or more new elements to move into
 *            place (the originals will be zeroed out if successful
 *            and if clearOriginal is true)
 * @clearOriginal: false if the new item in the array should be copied
 *            from the original, and the original left intact.
 *            true if the original should be 0'd out on success.
 * @inPlace:  false if we should expand the allocated memory before
 *            moving, true if we should assume someone else *has
 *            already* done that.
 *
 * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes
 * long, to be *countptr elements long, then appropriately move
 * the elements starting at ptrptr[at] up by 1 elements, copy the
 * items from newelems into ptrptr[at], then store the address of
 * allocated memory in *ptrptr and the new size in *countptr.  If
 * newelems is NULL, the new elements at ptrptr[at] are instead filled
 * with zero.  at must be between [0,*countptr], except that -1 is
 * treated the same as *countptr for convenience.
 *
 * Aborts on OOM failure.
 */
static void
virInsertElementInternal(void *ptrptr,
                         size_t size,
                         size_t at,
                         size_t *countptr,
                         void *newelems,
                         bool clearOriginal,
                         bool inPlace)
{
    if (inPlace) {
        *countptr += 1;
    } else {
        virExpandN(ptrptr, size, countptr, 1);
    }

    /* memory was successfully re-allocated. Move up all elements from
     * ptrptr[at] to the end (if we're not "inserting" at the end
     * already), memcpy in the new elements, and clear the elements
     * from their original location. Remember that *countptr has
     * already been updated with new element count!
     */
    if (at < *countptr - 1) {
        memmove(*(char**)ptrptr + (size * (at + 1)),
                *(char**)ptrptr + (size * at),
                size * (*countptr - 1 - at));
    }

    if (newelems) {
        memcpy(*(char**)ptrptr + (size * at), newelems, size);
        if (clearOriginal)
           memset((char*)newelems, 0, size);
    } else if (inPlace || (at < *countptr - 1)) {
        /* NB: if inPlace, assume memory at the end wasn't initialized */
        memset(*(char**)ptrptr + (size * at), 0, size);
    }
}


/**
 * virInsertElementsN:
 * @ptrptr:   pointer to hold address of allocated memory
 * @size:     the size of one element in bytes
 * @at:       index within array where new elements should be added, -1 for end
 * @countptr: variable tracking number of elements currently allocated
 * @typematchDummy: helper variable to consume results of compile time checks
 * @newelems: pointer to array of one or more new elements to move into
 *            place (the originals will be zeroed out if successful
 *            and if clearOriginal is true)
 * @clearOriginal: false if the new item in the array should be copied
 *            from the original, and the original left intact.
 *            true if the original should be 0'd out on success.
 * @inPlace:  false if we should expand the allocated memory before
 *            moving, true if we should assume someone else *has
 *            already* done that.
 *
 * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes
 * long, to be *countptr elements long, then appropriately move
 * the elements starting at ptrptr[at] up by 1 elements, copy the
 * items from newelems into ptrptr[at], then store the address of
 * allocated memory in *ptrptr and the new size in *countptr.  If
 * newelems is NULL, the new elements at ptrptr[at] are instead filled
 * with zero.  at must be between [0,*countptr], except that -1 is
 * treated the same as *countptr for convenience.
 *
 * Returns -1 on failure, 0 on success
 */
int
virInsertElementsN(void *ptrptr,
                   size_t size,
                   size_t at,
                   size_t *countptr,
                   size_t typematchDummy G_GNUC_UNUSED,
                   void *newelems,
                   bool clearOriginal,
                   bool inPlace)
{
    if (at == -1) {
        at = *countptr;
    } else if (at > *countptr) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("out of bounds index - count %1$zu at %2$zu"),
                       *countptr, at);
        return -1;
    }

    virInsertElementInternal(ptrptr, size, at, countptr, newelems, clearOriginal, inPlace);

    return 0;
}


/**
 * virAppendElement:
 * @ptrptr:   pointer to hold address of allocated memory
 * @size:     the size of one element in bytes
 * @countptr: variable tracking number of elements currently allocated
 * @typematchDummy: helper variable to consume results of compile time checks
 * @newelem: pointer to a new element to append to @ptrptr
 *           (the original will be zeroed out if clearOriginal is true)
 * @clearOriginal: false if the new item in the array should be copied
 *            from the original, and the original left intact.
 *            true if the original should be 0'd out on success.
 * @inPlace:  false if we should expand the allocated memory before
 *            moving, true if we should assume someone else *has
 *            already* done that.
 *
 * Re-allocate @ptrptr to fit an extra element and place @newelem at the end.
 */
void
virAppendElement(void *ptrptr,
                 size_t size,
                 size_t *countptr,
                 size_t typematchDummy G_GNUC_UNUSED,
                 void *newelem,
                 bool clearOriginal,
                 bool inPlace)
{
    virInsertElementInternal(ptrptr, size, *countptr, countptr, newelem, clearOriginal, inPlace);
}


/**
 * virDeleteElementsN:
 * @ptrptr:   pointer to hold address of allocated memory
 * @size:     the size of one element in bytes
 * @at:       index within array where new elements should be deleted
 * @countptr: variable tracking number of elements currently allocated
 * @toremove: number of elements to remove
 * @inPlace:  false if we should shrink the allocated memory when done,
 *            true if we should assume someone else will do that.
 *
 * Re-allocate an array of *countptr elements, each sizeof(*ptrptr)
 * bytes long, to be *countptr-remove elements long, then store the
 * address of allocated memory in *ptrptr and the new size in *countptr.
 * If *countptr <= remove, the entire array is freed.
 *
 * Returns -1 on failure, 0 on success
 */
int
virDeleteElementsN(void *ptrptr, size_t size, size_t at,
                   size_t *countptr, size_t toremove,
                   bool inPlace)
{
    if (at + toremove > *countptr) {
        VIR_WARN("out of bounds index - count %zu at %zu toremove %zu",
                 *countptr, at, toremove);
        return -1;
    }

    /* First move down the elements at the end that won't be deleted,
     * then realloc. We assume that the items being deleted have
     * already been cleared.
    */
    memmove(*(char**)ptrptr + (size * at),
            *(char**)ptrptr + (size * (at + toremove)),
            size * (*countptr - toremove - at));
    if (inPlace)
        *countptr -= toremove;
    else
        virShrinkN(ptrptr, size, countptr, toremove);
    return 0;
}