Example #1
0
/**
 * 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
 * @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
 * '*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.
 * If @report is true, OOM errors are reported automatically.
 *
 * Returns -1 on failure to allocate, zero on success
 */
int virExpandN(void *ptrptr,
               size_t size,
               size_t *countptr,
               size_t add,
               bool report,
               int domcode,
               const char *filename,
               const char *funcname,
               size_t linenr)
{
    int ret;

    if (*countptr + add < *countptr) {
        if (report)
            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);
        *countptr += add;
    }
    return ret;
}
Example #2
0
/**
 * 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)
        ignore_value(virReallocN(ptrptr, size, *countptr -= toremove));
    else {
        virFree(ptrptr);
        *countptr = 0;
    }
}
Example #3
0
/**
 * 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.
 *
 * Returns -1 on failure to allocate, zero on success
 */
int virExpandN(void *ptrptr, size_t size, size_t *countptr, size_t add)
{
    int ret;

    if (*countptr + add < *countptr) {
        errno = ENOMEM;
        return -1;
    }
    ret = virReallocN(ptrptr, size, *countptr + add);
    if (ret == 0) {
        memset(*(char **)ptrptr + (size * *countptr), 0, size * add);
        *countptr += add;
    }
    return ret;
}