viralloc.h 25.1 KB
Newer Older
1
/*
2
 * viralloc.h: safer memory allocation
3
 *
4
 * Copyright (C) 2010-2013 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */


#ifndef __VIR_MEMORY_H_
25
# define __VIR_MEMORY_H_
26

27
# include "internal.h"
28

29 30 31 32 33 34 35 36 37
/* Return 1 if an array of N objects, each of size S, cannot exist due
   to size arithmetic overflow.  S must be positive and N must be
   nonnegative.  This is a macro, not an inline function, so that it
   works correctly even when SIZE_MAX < N.

   By gnulib convention, SIZE_MAX represents overflow in size
   calculations, so the conservative dividend to use here is
   SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
   However, malloc (SIZE_MAX) fails on all known hosts where
38
   sizeof(ptrdiff_t) <= sizeof(size_t), so do not bother to test for
39 40
   exactly-SIZE_MAX allocations on such hosts; this avoids a test and
   branch when S is known to be 1.  */
41 42
# ifndef xalloc_oversized
#  define xalloc_oversized(n, s) \
43
    ((size_t) (sizeof(ptrdiff_t) <= sizeof(size_t) ? -1 : -2) / (s) < (n))
44
# endif
45 46 47



48
/* Don't call these directly - use the macros below */
49 50 51 52 53 54 55 56 57 58 59
int virAlloc(void *ptrptr, size_t size, bool report, int domcode,
             const char *filename, const char *funcname, size_t linenr)
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virAllocN(void *ptrptr, size_t size, size_t count, bool report, int domcode,
              const char *filename, const char *funcname, size_t linenr)
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virReallocN(void *ptrptr, size_t size, size_t count, bool report, int domcode,
                const char *filename, const char *funcname, size_t linenr)
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add, bool report,
               int domcode, const char *filename, const char *funcname, size_t linenr)
60
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
61 62 63
int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, size_t desired,
               bool report, int domcode, const char *filename,
               const char *funcname, size_t linenr)
64
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
65
void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove)
66
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
67 68
int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
                       size_t add, void *newelem,
69 70
                       bool clearOriginal, bool inPlace, bool report, int domcode,
                       const char *filename, const char *funcname, size_t linenr)
71 72
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
73
                       size_t toremove, bool inPlace)
74
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
75 76 77 78
int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count,
                bool report, int domcode, const char *filename,
                const char *funcname, size_t linenr)
    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
79
void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
80 81 82 83 84 85 86 87 88

/**
 * VIR_ALLOC:
 * @ptr: pointer to hold address of allocated memory
 *
 * Allocate sizeof(*ptr) bytes of memory and store
 * the address of allocated memory in 'ptr'. Fill the
 * newly allocated memory with zeros.
 *
89 90
 * This macro is safe to use on arguments with side effects.
 *
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
 * Returns -1 on failure (with OOM error reported), 0 on success
 */
# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)), true, VIR_FROM_THIS, \
                                 __FILE__, __FUNCTION__, __LINE__)

/**
 * VIR_ALLOC_QUIET:
 * @ptr: pointer to hold address of allocated memory
 *
 * Allocate sizeof(*ptr) bytes of memory and store
 * the address of allocated memory in 'ptr'. Fill the
 * newly allocated memory with zeros.
 *
 * This macro is safe to use on arguments with side effects.
 *
106 107
 * Returns -1 on failure, 0 on success
 */
108
# define VIR_ALLOC_QUIET(ptr) virAlloc(&(ptr), sizeof(*(ptr)), false, 0, NULL, NULL, 0)
109 110 111 112 113 114 115 116 117 118

/**
 * VIR_ALLOC_N:
 * @ptr: pointer to hold address of allocated memory
 * @count: number of elements to allocate
 *
 * Allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long and store the address of allocated memory in
 * 'ptr'. Fill the newly allocated memory with zeros.
 *
119 120
 * This macro is safe to use on arguments with side effects.
 *
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
 * Returns -1 on failure (with OOM error reported), 0 on success
 */
# define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), true, \
                                           VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)

/**
 * VIR_ALLOC_N_QUIET:
 * @ptr: pointer to hold address of allocated memory
 * @count: number of elements to allocate
 *
 * Allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long and store the address of allocated memory in
 * 'ptr'. Fill the newly allocated memory with zeros.
 *
 * This macro is safe to use on arguments with side effects.
 *
137 138
 * Returns -1 on failure, 0 on success
 */
139 140
# define VIR_ALLOC_N_QUIET(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), \
                                                 false, 0, NULL, NULL, 0)
141 142 143 144 145 146 147 148

/**
 * VIR_REALLOC_N:
 * @ptr: pointer to hold address of allocated memory
 * @count: number of elements to allocate
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long and store the address of allocated memory in
149
 * 'ptr'. If 'ptr' grew, the added memory is uninitialized.
150
 *
151 152
 * This macro is safe to use on arguments with side effects.
 *
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
 * Returns -1 on failure (with OOM error reported), 0 on success
 */
# define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \
                                               true, VIR_FROM_THIS, __FILE__, \
                                               __FUNCTION__, __LINE__)

/**
 * VIR_REALLOC_N_QUIET:
 * @ptr: pointer to hold address of allocated memory
 * @count: number of elements to allocate
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long and store the address of allocated memory in
 * 'ptr'. If 'ptr' grew, the added memory is uninitialized.
 *
 * This macro is safe to use on arguments with side effects.
 *
170 171
 * Returns -1 on failure, 0 on success
 */
172 173
# define VIR_REALLOC_N_QUIET(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \
                                                     false, 0, NULL, NULL, 0)
174

175 176 177 178 179 180 181 182 183 184 185
/**
 * VIR_EXPAND_N:
 * @ptr: pointer to hold address of allocated memory
 * @count: variable tracking number of elements currently allocated
 * @add: number of elements to add
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long, to be 'count' + 'add' elements long, then store the
 * address of allocated memory in 'ptr' and the new size in 'count'.
 * The new elements are filled with zero.
 *
186 187
 * This macro is safe to use on arguments with side effects.
 *
188
 * Returns -1 on failure (with OOM error reported), 0 on success
189 190
 */
# define VIR_EXPAND_N(ptr, count, add) \
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, true, VIR_FROM_THIS, \
               __FILE__, __FUNCTION__, __LINE__)

/**
 * VIR_EXPAND_N_QUIET:
 * @ptr: pointer to hold address of allocated memory
 * @count: variable tracking number of elements currently allocated
 * @add: number of elements to add
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long, to be 'count' + 'add' elements long, then store the
 * address of allocated memory in 'ptr' and the new size in 'count'.
 * The new elements are filled with zero.
 *
 * This macro is safe to use on arguments with side effects.
 *
 * Returns -1 on failure, 0 on success
 */
# define VIR_EXPAND_N_QUIET(ptr, count, add) \
    virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, false, 0, NULL, NULL, 0)
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/**
 * VIR_RESIZE_N:
 * @ptr: pointer to hold address of allocated memory
 * @alloc: variable tracking number of elements currently allocated
 * @count: number of elements currently in use
 * @add: minimum number of elements to additionally support
 *
 * Blindly using VIR_EXPAND_N(array, alloc, 1) in a loop scales
 * quadratically, because every iteration must copy contents from
 * all prior iterations.  But amortized linear scaling can be achieved
 * by tracking allocation size separately from the number of used
 * elements, and growing geometrically only as needed.
 *
 * If 'count' + 'add' is larger than 'alloc', then geometrically reallocate
 * the array of 'alloc' elements, each sizeof(*ptr) bytes long, and store
 * the address of allocated memory in 'ptr' and the new size in 'alloc'.
 * The new elements are filled with zero.
 *
230 231
 * This macro is safe to use on arguments with side effects.
 *
232
 * Returns -1 on failure (with OOM error reported), 0 on success
233 234
 */
# define VIR_RESIZE_N(ptr, alloc, count, add) \
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
    virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, true, \
               VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)

/**
 * VIR_RESIZE_N_QUIET:
 * @ptr: pointer to hold address of allocated memory
 * @alloc: variable tracking number of elements currently allocated
 * @count: number of elements currently in use
 * @add: minimum number of elements to additionally support
 *
 * Blindly using VIR_EXPAND_N(array, alloc, 1) in a loop scales
 * quadratically, because every iteration must copy contents from
 * all prior iterations.  But amortized linear scaling can be achieved
 * by tracking allocation size separately from the number of used
 * elements, and growing geometrically only as needed.
 *
 * If 'count' + 'add' is larger than 'alloc', then geometrically reallocate
 * the array of 'alloc' elements, each sizeof(*ptr) bytes long, and store
 * the address of allocated memory in 'ptr' and the new size in 'alloc'.
 * The new elements are filled with zero.
 *
 * This macro is safe to use on arguments with side effects.
 *
 * Returns -1 on failure, 0 on success
 */
# define VIR_RESIZE_N_QUIET(ptr, alloc, count, add) \
    virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, \
               false, 0, NULL, NULL, 0)
263

264 265 266 267 268 269 270 271 272 273 274
/**
 * VIR_SHRINK_N:
 * @ptr: pointer to hold address of allocated memory
 * @count: variable tracking number of elements currently allocated
 * @remove: number of elements to remove
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long, to be 'count' - 'remove' elements long, then store the
 * address of allocated memory in 'ptr' and the new size in 'count'.
 * If 'count' <= 'remove', the entire array is freed.
 *
275 276
 * This macro is safe to use on arguments with side effects.
 *
277 278 279 280 281
 * No return value.
 */
# define VIR_SHRINK_N(ptr, count, remove) \
    virShrinkN(&(ptr), sizeof(*(ptr)), &(count), remove)

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
/*
 * VIR_TYPEMATCH:
 *
 * The following macro seems a bit cryptic, so it needs a thorough
 * explanation. Its purpose is to check for assignment compatibility
 * and identical size between two values without creating any side
 * effects (by doing something silly like actually assigning one to
 * the other). Note that it takes advantage of the C89-guaranteed
 * property of sizeof() - it cannot have any side effects, so anything
 * that happens inside sizeof() will not have any effect at runtime.
 *
 * VIR_TYPEMATCH evaluates to "1" if the two passed values are both
 * assignment-compatible and the same size, and otherwise generates a
 * compile-time error. It determines the result by performing the
 * following three operations:
 *
 *    * sizeof(*(a) = *(b)) assures that *a and *b are
 *      assignment-compatible (they may still have a different size
 *      though! e.g. longVar = intVar) (If not, there is a compile-time
 *      error. If so, the result of that subexpression is sizeof(*(a)),
 *      i.e. one element of the array)
 *
 *    * sizeof(*(a) = *(b)) == sizeof(*(b)) checks if *a and *b are also
 *      of the same size (so that, e.g. you don't accidentally copy an
 *      int plus the random bytes following it into an array of long). It
 *      evaluates to 1 if they are the same, and 0 otherwise.
 *
 *    * sizeof(char[2 * (result of previous step) - 1]) evaluates to 1
 *      if the previous step was successful (char [(2*1) - 1] i.e.
 *      char[1]), or generates a compile error if it wasn't successful
 *      (char[2*0 -1] i.e. char[-1], which isn't valid in C).
 *
L
Laine Stump 已提交
314
 * So VIR_TYPEMATCH(a, b) will either abort the compile with an error,
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
 * or evaluate to "1", and in the meantime check that we've actually
 * added the correct &'s and/or *'s to the arguments. (Whew!)
*/
# define VIR_TYPEMATCH(a, b) \
    sizeof(char[2 * (sizeof(*(a) = *(b)) == sizeof(*(b))) - 1])

/**
 * VIR_INSERT_ELEMENT:
 * @ptr:     pointer to array of objects (*not* ptr to ptr)
 * @at:      index within array where new elements should be added
 * @count:   variable tracking number of elements currently allocated
 * @newelem: the new element to move into place (*not* a pointer to
 *           the element, but the element itself).
 *           (the original will be zeroed out if successful)
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr) bytes
 * long, to be 'count' + 1 elements long, then appropriately move
 * the elements starting at ptr[at] up by 1 element, copy the
 * item 'newelem' into ptr[at], then store the address of
 * allocated memory in 'ptr' and the new size in 'count'.
 *
 * VIR_INSERT_ELEMENT_COPY is identical, but doesn't clear out the
 *   original element to 0 on success, so there are two copies of the
 *   element. This is useful if the "element" is actually just a
 *   pointer to the real data, and you want to maintain a reference to
 *   it for use after the insert is completed; but if the "element" is
 *   an object that points to other allocated memory, having multiple
 *   copies can cause problems (e.g. double free).
 *
 * VIR_INSERT_ELEMENT_*INPLACE are identical, but assume any necessary
 *   memory re-allocation has already been done.
 *
 * VIR_INSERT_ELEMENT_* all need to send "1" as the "add" argument to
 * virInsertElementsN (which has the currently-unused capability of
 * inserting multiple items at once). We use this to our advantage by
 * replacing it with VIR_TYPECHECK(ptr, &newelem) so that we can be
 * assured ptr and &newelem are of compatible types.
 *
353
 * These macros are safe to use on arguments with side effects.
354
 *
355
 * Returns -1 on failure (with OOM error reported), 0 on success
356 357 358
 */
# define VIR_INSERT_ELEMENT(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count),    \
359 360
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
361 362
# define VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
363 364
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
365 366
# define VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
367 368
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
369 370
# define VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)

/* Quiet version of macros above */
# define VIR_INSERT_ELEMENT_QUIET(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count),    \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
                       false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_COPY_QUIET(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
                       false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_INPLACE_QUIET(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
                       false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_COPY_INPLACE_QUIET(ptr, at, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
                       false, 0, NULL, NULL, 0)
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

/**
 * VIR_APPEND_ELEMENT:
 * @ptr:     pointer to array of objects (*not* ptr to ptr)
 * @count:   variable tracking number of elements currently allocated
 * @newelem: the new element to move into place (*not* a pointer to
 *           the element, but the element itself).
 *           (the original will be zeroed out if successful)
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr) bytes
 * long, to be 'count' + 1 elements long, then copy the item from
 * 'newelem' into ptr[count+1], and store the address of allocated
 * memory in 'ptr' and the new size in 'count'. If 'newelem' is NULL,
 * the new element at ptr[at] is instead filled with zero.
 *
 * VIR_APPEND_ELEMENT_COPY is identical, but doesn't clear out the
 *   original element to 0 on success, so there are two copies of the
 *   element. This is useful if the "element" is actually just a
 *   pointer to the real data, and you want to maintain a reference to
 *   it for use after the append is completed; but if the "element" is
 *   an object that points to other allocated memory, having multiple
 *   copies can cause problems (e.g. double free).
 *
 * VIR_APPEND_ELEMENT_*INPLACE are identical, but assume any
 *   necessary memory re-allocation has already been done.
 *
 * VIR_APPEND_ELEMENT_* all need to send "1" as the "add" argument to
 * virInsertElementsN (which has the currently-unused capability of
 * inserting multiple items at once). We use this to our advantage by
 * replacing it with VIR_TYPECHECK(ptr, &newelem) so that we can be
 * assured ptr and &newelem are of compatible types.
 *
423
 * These macros are safe to use on arguments with side effects.
424
 *
425
 * Returns -1 on failure (with OOM error reported), 0 on success
426 427
 */
# define VIR_APPEND_ELEMENT(ptr, count, newelem) \
428
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
429 430
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
431
# define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \
432
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
433 434
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
435
# define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \
436
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
437 438
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
439
# define VIR_APPEND_ELEMENT_COPY_INPLACE(ptr, count, newelem) \
440
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
441 442
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
                       true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
443

444 445 446 447 448
/* Quiet version of macros above */
# define VIR_APPEND_ELEMENT_QUIET(ptr, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
                       false, 0, NULL, NULL, 0)
449
# define VIR_APPEND_ELEMENT_COPY_QUIET(ptr, count, newelem) \
450 451 452 453 454 455 456 457 458 459 460
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
                       false, 0, NULL, NULL, 0)
# define VIR_APPEND_ELEMENT_INPLACE_QUIET(ptr, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
                       false. 0, NULL, NULL, 0)
# define VIR_APPEND_ELEMENT_COPY_INPLACE_QUIET(ptr, count, newelem) \
    virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count),  \
                       VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
                       false, 0, NULL, NULL, 0)
461 462 463 464 465 466 467 468 469 470 471 472 473 474
/**
 * VIR_DELETE_ELEMENT:
 * @ptr:   pointer to array of objects (*not* ptr to ptr)
 * @at:    index within array where new elements should be deleted
 * @count: variable tracking number of elements currently allocated
 *
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
 * bytes long, to be 'count' - 1 elements long, then store the
 * address of allocated memory in 'ptr' and the new size in 'count'.
 * If 'count' <= 1, the entire array is freed.
 *
 * VIR_DELETE_ELEMENT_INPLACE is identical, but assumes any
 *   necessary memory re-allocation will be done later.
 *
475 476
 * These macros are safe to use on arguments with side effects.
 *
477 478 479 480 481 482 483
 * Returns -1 on failure, 0 on success
 */
# define VIR_DELETE_ELEMENT(ptr, at, count) \
    virDeleteElementsN(&(ptr), sizeof(*(ptr)), at, &(count), 1, false)
# define VIR_DELETE_ELEMENT_INPLACE(ptr, at, count) \
    virDeleteElementsN(&(ptr), sizeof(*(ptr)), at, &(count), 1, true)

484 485 486 487 488 489 490 491 492 493
/*
 * VIR_ALLOC_VAR_OVERSIZED:
 * @M: size of base structure
 * @N: number of array elements in trailing array
 * @S: size of trailing array elements
 *
 * Check to make sure that the requested allocation will not cause
 * arithmetic overflow in the allocation size.  The check is
 * essentially the same as that in gnulib's xalloc_oversized.
 */
E
Eric Blake 已提交
494
# define VIR_ALLOC_VAR_OVERSIZED(M, N, S) ((((size_t)-1) - (M)) / (S) < (N))
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

/**
 * VIR_ALLOC_VAR:
 * @ptr: pointer to hold address of allocated memory
 * @type: element type of trailing array
 * @count: number of array elements to allocate
 *
 * Allocate sizeof(*ptr) bytes plus an array of 'count' elements, each
 * sizeof('type').  This sort of allocation is useful for receiving
 * the data of certain ioctls and other APIs which return a struct in
 * which the last element is an array of undefined length.  The caller
 * of this type of API is expected to know the length of the array
 * that will be returned and allocate a suitable buffer to contain the
 * returned data.  C99 refers to these variable length objects as
 * structs containing flexible array members.
510 511 512
 *
 * This macro is safe to use on arguments with side effects.
 *
513
 * Returns -1 on failure (with OOM error reported), 0 on success
514
 */
E
Eric Blake 已提交
515
# define VIR_ALLOC_VAR(ptr, type, count) \
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), true, \
                VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)

/**
 * VIR_ALLOC_VAR_QUIET:
 * @ptr: pointer to hold address of allocated memory
 * @type: element type of trailing array
 * @count: number of array elements to allocate
 *
 * Allocate sizeof(*ptr) bytes plus an array of 'count' elements, each
 * sizeof('type').  This sort of allocation is useful for receiving
 * the data of certain ioctls and other APIs which return a struct in
 * which the last element is an array of undefined length.  The caller
 * of this type of API is expected to know the length of the array
 * that will be returned and allocate a suitable buffer to contain the
 * returned data.  C99 refers to these variable length objects as
 * structs containing flexible array members.
 *
 * This macro is safe to use on arguments with side effects.
 *
 * Returns -1 on failure, 0 on success
 */
# define VIR_ALLOC_VAR_QUIET(ptr, type, count) \
    virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), false, 0, NULL, NULL, 0)
540

541 542 543 544 545 546
/**
 * VIR_FREE:
 * @ptr: pointer holding address to be freed
 *
 * Free the memory stored in 'ptr' and update to point
 * to NULL.
547 548
 *
 * This macro is safe to use on arguments with side effects.
549
 */
550
# if !STATIC_ANALYSIS
551
/* The ternary ensures that ptr is a pointer and not an integer type,
552 553
 * while evaluating ptr only once.  This gives us extra compiler
 * safety when compiling under gcc.  For now, we intentionally cast
554 555
 * away const, since a number of callers safely pass const char *.
 */
556 557 558 559 560 561 562 563 564
#  define VIR_FREE(ptr) virFree((void *) (1 ? (const void *) &(ptr) : (ptr)))
# else
/* The Coverity static analyzer considers the else path of the "?:" and
 * flags the VIR_FREE() of the address of the address of memory as a
 * RESOURCE_LEAK resulting in numerous false positives (eg, VIR_FREE(&ptr))
 */
#  define VIR_FREE(ptr) virFree((void *) &(ptr))
# endif

D
Daniel P. Berrange 已提交
565 566 567
void virAllocTestInit(void);
int virAllocTestCount(void);
void virAllocTestOOM(int n, int m);
568
void virAllocTestHook(void (*func)(int, void*), void *data);
569
#endif /* __VIR_MEMORY_H_ */