提交 52117fa9 编写于 作者: D Daniel P. Berrangé

util: make allocation functions abort on OOM

The functions are left returning an "int" to avoid an immediate
big-bang cleanup. They'll simply never return anything other
than 0, except for virInsertN which can still return an error
if the requested insertion index is out of range. Interestingly
in that case, the _QUIET function would none the less report
an error.
Reviewed-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 2c52ecd9
...@@ -35,33 +35,20 @@ VIR_LOG_INIT("util.alloc"); ...@@ -35,33 +35,20 @@ VIR_LOG_INIT("util.alloc");
* virAlloc: * virAlloc:
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate 'size' bytes of memory. Return the address of the * Allocate 'size' bytes of memory. Return the address of the
* allocated memory in 'ptrptr'. The newly allocated memory is * allocated memory in 'ptrptr'. The newly allocated memory is
* filled with zeros. If @report is true, OOM errors are * filled with zeros.
* reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns zero on success, aborts on OOM
*/ */
int virAlloc(void *ptrptr, int virAlloc(void *ptrptr,
size_t size, size_t size)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
*(void **)ptrptr = calloc(1, size); *(void **)ptrptr = calloc(1, size);
if (*(void **)ptrptr == NULL) { if (*(void **)ptrptr == NULL)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
return 0; return 0;
} }
...@@ -70,35 +57,22 @@ int virAlloc(void *ptrptr, ...@@ -70,35 +57,22 @@ int virAlloc(void *ptrptr,
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @count: number of elements to allocate * @count: number of elements to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate an array of memory 'count' elements long, * Allocate an array of memory 'count' elements long,
* each with 'size' bytes. Return the address of the * each with 'size' bytes. Return the address of the
* allocated memory in 'ptrptr'. The newly allocated * allocated memory in 'ptrptr'. The newly allocated
* memory is filled with zeros. If @report is true, * memory is filled with zeros.
* OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns zero on success, aborts on OOM
*/ */
int virAllocN(void *ptrptr, int virAllocN(void *ptrptr,
size_t size, size_t size,
size_t count, size_t count)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
*(void**)ptrptr = calloc(count, size); *(void**)ptrptr = calloc(count, size);
if (*(void**)ptrptr == NULL) { if (*(void**)ptrptr == NULL)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
return 0; return 0;
} }
...@@ -107,44 +81,28 @@ int virAllocN(void *ptrptr, ...@@ -107,44 +81,28 @@ int virAllocN(void *ptrptr,
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @count: number of elements in array * @count: number of elements in array
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Resize the block of memory in 'ptrptr' to be an array of * Resize the block of memory in 'ptrptr' to be an array of
* 'count' elements, each 'size' bytes in length. Update 'ptrptr' * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
* with the address of the newly allocated memory. On failure, * with the address of the newly allocated memory. On failure,
* 'ptrptr' is not changed and still points to the original memory * 'ptrptr' is not changed and still points to the original memory
* block. Any newly allocated memory in 'ptrptr' is uninitialized. * block. Any newly allocated memory in 'ptrptr' is uninitialized.
* If @report is true, OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns zero on success, aborts on OOM
*/ */
int virReallocN(void *ptrptr, int virReallocN(void *ptrptr,
size_t size, size_t size,
size_t count, size_t count)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
void *tmp; void *tmp;
if (xalloc_oversized(count, size)) { if (xalloc_oversized(count, size))
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
tmp = realloc(*(void**)ptrptr, size * count); tmp = realloc(*(void**)ptrptr, size * count);
if (!tmp && ((size * count) != 0)) { if (!tmp && ((size * count) != 0))
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
*(void**)ptrptr = tmp; *(void**)ptrptr = tmp;
return 0; return 0;
} }
...@@ -155,46 +113,28 @@ int virReallocN(void *ptrptr, ...@@ -155,46 +113,28 @@ int virReallocN(void *ptrptr,
* @size: number of bytes per element * @size: number of bytes per element
* @countptr: pointer to number of elements in array * @countptr: pointer to number of elements in array
* @add: number of elements to add * @add: number of elements to add
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Resize the block of memory in 'ptrptr' to be an array of * Resize the block of memory in 'ptrptr' to be an array of
* '*countptr' + 'add' elements, each 'size' bytes in length. * '*countptr' + 'add' elements, each 'size' bytes in length.
* Update 'ptrptr' and 'countptr' with the details of the newly * Update 'ptrptr' and 'countptr' with the details of the newly
* allocated memory. On failure, 'ptrptr' and 'countptr' are not * allocated memory. On failure, 'ptrptr' and 'countptr' are not
* changed. Any newly allocated memory in 'ptrptr' is zero-filled. * changed. Any newly allocated memory in 'ptrptr' is zero-filled.
* If @report is true, OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns zero on success, aborts on OOM
*/ */
int virExpandN(void *ptrptr, int virExpandN(void *ptrptr,
size_t size, size_t size,
size_t *countptr, size_t *countptr,
size_t add, size_t add)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
int ret; if (*countptr + add < *countptr)
abort();
if (*countptr + add < *countptr) { if (virReallocN(ptrptr, size, *countptr + add) < 0)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
ret = virReallocN(ptrptr, size, *countptr + add, report,
domcode, filename, funcname, linenr);
if (ret == 0) {
memset(*(char **)ptrptr + (size * *countptr), 0, size * add); memset(*(char **)ptrptr + (size * *countptr), 0, size * add);
*countptr += add; *countptr += add;
} return 0;
return ret;
} }
/** /**
...@@ -204,50 +144,34 @@ int virExpandN(void *ptrptr, ...@@ -204,50 +144,34 @@ int virExpandN(void *ptrptr,
* @allocptr: pointer to number of elements allocated in array * @allocptr: pointer to number of elements allocated in array
* @count: number of elements currently used in array * @count: number of elements currently used in array
* @add: minimum number of additional elements to support in array * @add: minimum number of additional elements to support in array
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* If 'count' + 'add' is larger than '*allocptr', then resize the * If 'count' + 'add' is larger than '*allocptr', then resize the
* block of memory in 'ptrptr' to be an array of at least 'count' + * block of memory in 'ptrptr' to be an array of at least 'count' +
* 'add' elements, each 'size' bytes in length. Update 'ptrptr' and * 'add' elements, each 'size' bytes in length. Update 'ptrptr' and
* 'allocptr' with the details of the newly allocated memory. On * 'allocptr' with the details of the newly allocated memory. On
* failure, 'ptrptr' and 'allocptr' are not changed. Any newly * failure, 'ptrptr' and 'allocptr' are not changed. Any newly
* allocated memory in 'ptrptr' is zero-filled. If @report is true, * allocated memory in 'ptrptr' is zero-filled.
* OOM errors are reported automatically.
* *
* * Returns zero on success, aborts on OOM
* Returns -1 on failure to allocate, zero on success
*/ */
int virResizeN(void *ptrptr, int virResizeN(void *ptrptr,
size_t size, size_t size,
size_t *allocptr, size_t *allocptr,
size_t count, size_t count,
size_t add, size_t add)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
size_t delta; size_t delta;
if (count + add < count) { if (count + add < count)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
if (count + add <= *allocptr) if (count + add <= *allocptr)
return 0; return 0;
delta = count + add - *allocptr; delta = count + add - *allocptr;
if (delta < *allocptr / 2) if (delta < *allocptr / 2)
delta = *allocptr / 2; delta = *allocptr / 2;
return virExpandN(ptrptr, size, allocptr, delta, report, return virExpandN(ptrptr, size, allocptr, delta);
domcode, filename, funcname, linenr);
} }
/** /**
...@@ -266,8 +190,8 @@ int virResizeN(void *ptrptr, ...@@ -266,8 +190,8 @@ int virResizeN(void *ptrptr,
void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove) void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
{ {
if (toremove < *countptr) { if (toremove < *countptr) {
ignore_value(virReallocN(ptrptr, size, *countptr -= toremove, if (virReallocN(ptrptr, size, *countptr -= toremove) < 0)
false, 0, NULL, NULL, 0)); abort();
} else { } else {
virFree(ptrptr); virFree(ptrptr);
*countptr = 0; *countptr = 0;
...@@ -290,11 +214,6 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove) ...@@ -290,11 +214,6 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
* @inPlace: false if we should expand the allocated memory before * @inPlace: false if we should expand the allocated memory before
* moving, true if we should assume someone else *has * moving, true if we should assume someone else *has
* already* done that. * already* done that.
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes
* long, to be *countptr+add elements long, then appropriately move * long, to be *countptr+add elements long, then appropriately move
...@@ -303,8 +222,7 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove) ...@@ -303,8 +222,7 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
* allocated memory in *ptrptr and the new size in *countptr. If * allocated memory in *ptrptr and the new size in *countptr. If
* newelems is NULL, the new elements at ptrptr[at] are instead filled * newelems is NULL, the new elements at ptrptr[at] are instead filled
* with zero. at must be between [0,*countptr], except that -1 is * with zero. at must be between [0,*countptr], except that -1 is
* treated the same as *countptr for convenience. If @report is true, * treated the same as *countptr for convenience.
* OOM errors are reported automatically.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
...@@ -312,12 +230,7 @@ int ...@@ -312,12 +230,7 @@ int
virInsertElementsN(void *ptrptr, size_t size, size_t at, virInsertElementsN(void *ptrptr, size_t size, size_t at,
size_t *countptr, size_t *countptr,
size_t add, void *newelems, size_t add, void *newelems,
bool clearOriginal, bool inPlace, bool clearOriginal, bool inPlace)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
if (at == -1) { if (at == -1) {
at = *countptr; at = *countptr;
...@@ -330,9 +243,9 @@ virInsertElementsN(void *ptrptr, size_t size, size_t at, ...@@ -330,9 +243,9 @@ virInsertElementsN(void *ptrptr, size_t size, size_t at,
if (inPlace) { if (inPlace) {
*countptr += add; *countptr += add;
} else if (virExpandN(ptrptr, size, countptr, add, report, } else {
domcode, filename, funcname, linenr) < 0) { if (virExpandN(ptrptr, size, countptr, add) < 0)
return -1; abort();
} }
/* memory was successfully re-allocated. Move up all elements from /* memory was successfully re-allocated. Move up all elements from
...@@ -407,11 +320,6 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at, ...@@ -407,11 +320,6 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at,
* @struct_size: size of initial struct * @struct_size: size of initial struct
* @element_size: size of array elements * @element_size: size of array elements
* @count: number of array elements to allocate * @count: number of array elements to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate struct_size bytes plus an array of 'count' elements, each * Allocate struct_size bytes plus an array of 'count' elements, each
* of size element_size. This sort of allocation is useful for * of size element_size. This sort of allocation is useful for
...@@ -420,37 +328,24 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at, ...@@ -420,37 +328,24 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at,
* The caller of this type of API is expected to know the length of * 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 * the array that will be returned and allocate a suitable buffer to
* contain the returned data. C99 refers to these variable length * contain the returned data. C99 refers to these variable length
* objects as structs containing flexible array members. If @report * objects as structs containing flexible array members.
* is true, OOM errors are reported automatically.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
int virAllocVar(void *ptrptr, int virAllocVar(void *ptrptr,
size_t struct_size, size_t struct_size,
size_t element_size, size_t element_size,
size_t count, size_t count)
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
size_t alloc_size = 0; size_t alloc_size = 0;
if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) { if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size))
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
alloc_size = struct_size + (element_size * count); alloc_size = struct_size + (element_size * count);
*(void **)ptrptr = calloc(1, alloc_size); *(void **)ptrptr = calloc(1, alloc_size);
if (*(void **)ptrptr == NULL) { if (*(void **)ptrptr == NULL)
if (report) abort();
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
}
return 0; return 0;
} }
......
...@@ -44,35 +44,26 @@ ...@@ -44,35 +44,26 @@
/* Don't call these directly - use the macros below */ /* Don't call these directly - use the macros below */
int virAlloc(void *ptrptr, size_t size, bool report, int domcode, int virAlloc(void *ptrptr, size_t size)
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virAllocN(void *ptrptr, size_t size, size_t count, bool report, int domcode, int virAllocN(void *ptrptr, size_t size, size_t count)
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virReallocN(void *ptrptr, size_t size, size_t count, bool report, int domcode, int virReallocN(void *ptrptr, size_t size, size_t count)
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add, bool report, int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add)
int domcode, const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, size_t desired, 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)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove) void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr, int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
size_t add, void *newelem, size_t add, void *newelem,
bool clearOriginal, bool inPlace, bool report, int domcode, bool clearOriginal, bool inPlace)
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr, int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
size_t toremove, bool inPlace) size_t toremove, bool inPlace)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count, 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); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1); void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
...@@ -91,10 +82,9 @@ void virDisposeString(char **strptr) ...@@ -91,10 +82,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)), true, VIR_FROM_THIS, \ #define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)))
__FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_ALLOC_QUIET: * VIR_ALLOC_QUIET:
...@@ -106,9 +96,9 @@ void virDisposeString(char **strptr) ...@@ -106,9 +96,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_ALLOC_QUIET(ptr) virAlloc(&(ptr), sizeof(*(ptr)), false, 0, NULL, NULL, 0) #define VIR_ALLOC_QUIET(ptr) VIR_ALLOC(ptr)
/** /**
* VIR_ALLOC_N: * VIR_ALLOC_N:
...@@ -121,10 +111,9 @@ void virDisposeString(char **strptr) ...@@ -121,10 +111,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), true, \ #define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count))
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_ALLOC_N_QUIET: * VIR_ALLOC_N_QUIET:
...@@ -137,10 +126,9 @@ void virDisposeString(char **strptr) ...@@ -137,10 +126,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_ALLOC_N_QUIET(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), \ #define VIR_ALLOC_N_QUIET(ptr, count) VIR_ALLOC_N(ptr, count)
false, 0, NULL, NULL, 0)
/** /**
* VIR_REALLOC_N: * VIR_REALLOC_N:
...@@ -153,11 +141,9 @@ void virDisposeString(char **strptr) ...@@ -153,11 +141,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \ #define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count))
true, VIR_FROM_THIS, __FILE__, \
__FUNCTION__, __LINE__)
/** /**
* VIR_REALLOC_N_QUIET: * VIR_REALLOC_N_QUIET:
...@@ -170,10 +156,9 @@ void virDisposeString(char **strptr) ...@@ -170,10 +156,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_REALLOC_N_QUIET(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \ #define VIR_REALLOC_N_QUIET(ptr, count) VIR_REALLOC_N(ptr, count)
false, 0, NULL, NULL, 0)
/** /**
* VIR_EXPAND_N: * VIR_EXPAND_N:
...@@ -188,11 +173,9 @@ void virDisposeString(char **strptr) ...@@ -188,11 +173,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_EXPAND_N(ptr, count, add) \ #define VIR_EXPAND_N(ptr, count, add) virExpandN(&(ptr), sizeof(*(ptr)), &(count), add)
virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, true, VIR_FROM_THIS, \
__FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_EXPAND_N_QUIET: * VIR_EXPAND_N_QUIET:
...@@ -207,10 +190,9 @@ void virDisposeString(char **strptr) ...@@ -207,10 +190,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_EXPAND_N_QUIET(ptr, count, add) \ #define VIR_EXPAND_N_QUIET(ptr, count, add) VIR_EXPAND_N(ptr, count, add)
virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, false, 0, NULL, NULL, 0)
/** /**
* VIR_RESIZE_N: * VIR_RESIZE_N:
...@@ -232,11 +214,10 @@ void virDisposeString(char **strptr) ...@@ -232,11 +214,10 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_RESIZE_N(ptr, alloc, count, add) \ #define VIR_RESIZE_N(ptr, alloc, count, add) \
virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, true, \ virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add)
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_RESIZE_N_QUIET: * VIR_RESIZE_N_QUIET:
...@@ -258,11 +239,9 @@ void virDisposeString(char **strptr) ...@@ -258,11 +239,9 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_RESIZE_N_QUIET(ptr, alloc, count, add) \ #define VIR_RESIZE_N_QUIET(ptr, alloc, count, add) VIR_RESIZE_N(ptr, alloc, count, add)
virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_SHRINK_N: * VIR_SHRINK_N:
...@@ -359,38 +338,26 @@ void virDisposeString(char **strptr) ...@@ -359,38 +338,26 @@ void virDisposeString(char **strptr)
*/ */
#define VIR_INSERT_ELEMENT(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/* Quiet version of macros above */ /* Quiet version of macros above */
#define VIR_INSERT_ELEMENT_QUIET(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ VIR_INSERT_ELEMENT(ptr, at, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
false, 0, NULL, NULL, 0)
#define VIR_INSERT_ELEMENT_COPY_QUIET(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_COPY_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
false, 0, NULL, NULL, 0)
#define VIR_INSERT_ELEMENT_INPLACE_QUIET(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_INPLACE_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
false, 0, NULL, NULL, 0)
#define VIR_INSERT_ELEMENT_COPY_INPLACE_QUIET(ptr, at, count, newelem) \ #define VIR_INSERT_ELEMENT_COPY_INPLACE_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_APPEND_ELEMENT: * VIR_APPEND_ELEMENT:
...@@ -429,34 +396,24 @@ void virDisposeString(char **strptr) ...@@ -429,34 +396,24 @@ void virDisposeString(char **strptr)
*/ */
#define VIR_APPEND_ELEMENT(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \ VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false)
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
#define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \
ignore_value(virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ ignore_value(virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), \ VIR_TYPEMATCH(ptr, &(newelem)), \
&(newelem), true, true, false, \ &(newelem), true, true))
VIR_FROM_THIS, __FILE__, \
__FUNCTION__, __LINE__))
#define VIR_APPEND_ELEMENT_COPY_INPLACE(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT_COPY_INPLACE(ptr, count, newelem) \
ignore_value(virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ ignore_value(virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), \ VIR_TYPEMATCH(ptr, &(newelem)), \
&(newelem), false, true, false, \ &(newelem), false, true))
VIR_FROM_THIS, __FILE__, \
__FUNCTION__, __LINE__))
/* Quiet version of macros above */ /* Quiet version of macros above */
#define VIR_APPEND_ELEMENT_QUIET(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT_QUIET(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ VIR_APPEND_ELEMENT(ptr, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
false, 0, NULL, NULL, 0)
#define VIR_APPEND_ELEMENT_COPY_QUIET(ptr, count, newelem) \ #define VIR_APPEND_ELEMENT_COPY_QUIET(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ VIR_APPEND_ELEMENT_COPY(ptr, count, newelem)
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_DELETE_ELEMENT: * VIR_DELETE_ELEMENT:
...@@ -510,11 +467,10 @@ void virDisposeString(char **strptr) ...@@ -510,11 +467,10 @@ void virDisposeString(char **strptr)
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success * Returns 0 on success, aborts on OOM
*/ */
#define VIR_ALLOC_VAR(ptr, type, count) \ #define VIR_ALLOC_VAR(ptr, type, count) \
virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), true, \ virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count))
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/** /**
* VIR_ALLOC_VAR_QUIET: * VIR_ALLOC_VAR_QUIET:
...@@ -535,8 +491,7 @@ void virDisposeString(char **strptr) ...@@ -535,8 +491,7 @@ void virDisposeString(char **strptr)
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
#define VIR_ALLOC_VAR_QUIET(ptr, type, count) \ #define VIR_ALLOC_VAR_QUIET(ptr, type, count) VIR_ALLOC_VAR(ptr, type, count)
virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), false, 0, NULL, NULL, 0)
/** /**
* VIR_FREE: * VIR_FREE:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册