/** * 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 * @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 * 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. If @report is true, * OOM errors are reported automatically. * * * Returns -1 on failure to allocate, zero on success */ int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count, size_t add, bool report, int domcode, const char *filename, const char *funcname, size_t linenr) { size_t delta; if (count + add < count) { if (report) virReportOOMErrorFull(domcode, filename, funcname, linenr); errno = ENOMEM; return -1; } if (count + add <= *allocptr) return 0; delta = count + add - *allocptr; if (delta < *allocptr / 2) delta = *allocptr / 2; return virExpandN(ptrptr, size, allocptr, delta, report, domcode, filename, funcname, linenr); }
/** * 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 * @add: number of elements to add * @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. * @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 * long, to be *countptr+add elements long, then appropriately move * the elements starting at ptrptr[at] up by add 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. If @report is true, * OOM errors are reported automatically. * * Returns -1 on failure, 0 on success */ int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr, size_t add, void *newelems, bool clearOriginal, bool inPlace, bool report, int domcode, const char *filename, const char *funcname, size_t linenr) { if (at == -1) { at = *countptr; } else if (at > *countptr) { virReportError(VIR_ERR_INTERNAL_ERROR, _("out of bounds index - count %zu at %zu add %zu"), *countptr, at, add); return -1; } if (inPlace) { *countptr += add; } else if (virExpandN(ptrptr, size, countptr, add, report, domcode, filename, funcname, linenr) < 0) { return -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 - add) { memmove(*(char**)ptrptr + (size * (at + add)), *(char**)ptrptr + (size * at), size * (*countptr - add - at)); } if (newelems) { memcpy(*(char**)ptrptr + (size * at), newelems, size * add); if (clearOriginal) memset((char*)newelems, 0, size * add); } else if (inPlace || (at < *countptr - add)) { /* NB: if inPlace, assume memory at the end wasn't initialized */ memset(*(char**)ptrptr + (size * at), 0, size * add); } return 0; }
/** * 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. * * Returns -1 on failure to allocate, zero on success */ int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count, size_t add) { size_t delta; if (count + add < count) { errno = ENOMEM; return -1; } if (count + add <= *allocptr) return 0; delta = count + add - *allocptr; if (delta < *allocptr / 2) delta = *allocptr / 2; return virExpandN(ptrptr, size, allocptr, delta); }