/** * Vir_Alloc_Var: * @ptrptr: pointer to hold address of allocated memory * @struct_size: size of initial struct * @element_size: size of array elements * @count: number of array elements to allocate * * Allocate struct_size bytes plus an array of 'count' elements, each * of size element_size. 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. * * Returns -1 on failure, 0 on success */ int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count) { size_t alloc_size = 0; #if TEST_OOM if (virAllocTestFail()) return -1; #endif if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) { errno = ENOMEM; return -1; } alloc_size = struct_size + (element_size * count); *(void **)ptrptr = calloc(1, alloc_size); if (*(void **)ptrptr == NULL) return -1; return 0; }
/** * virAllocVar: * @ptrptr: pointer to hold address of allocated memory * @struct_size: size of initial struct * @element_size: size of array elements * @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 * of size element_size. 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. If @report * is true, OOM errors are reported automatically. * * Returns -1 on failure, 0 on success */ 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) { size_t alloc_size = 0; #if TEST_OOM if (virAllocTestFail()) { if (report) virReportOOMErrorFull(domcode, filename, funcname, linenr); errno = ENOMEM; return -1; } #endif if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) { if (report) virReportOOMErrorFull(domcode, filename, funcname, linenr); errno = ENOMEM; return -1; } alloc_size = struct_size + (element_size * count); *(void **)ptrptr = calloc(1, alloc_size); if (*(void **)ptrptr == NULL) { if (report) virReportOOMErrorFull(domcode, filename, funcname, linenr); return -1; } return 0; }