Re: [libvirt] [PATCH v2 2/4] util: make allocation functions abort on OOM

2019-09-12 Thread Michal Privoznik

On 9/12/19 1:31 PM, Daniel P. Berrangé wrote:

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: Ján Tomko 
Signed-off-by: Daniel P. Berrangé 
---
  src/util/viralloc.c | 203 +++-
  src/util/viralloc.h | 133 ++---
  2 files changed, 93 insertions(+), 243 deletions(-)



Reviewed-by: Michal Privoznik 

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

[libvirt] [PATCH v2 2/4] util: make allocation functions abort on OOM

2019-09-12 Thread Daniel P . Berrangé
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: Ján Tomko 
Signed-off-by: Daniel P. Berrangé 
---
 src/util/viralloc.c | 203 +++-
 src/util/viralloc.h | 133 ++---
 2 files changed, 93 insertions(+), 243 deletions(-)

diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index 5a0adcc706..10a8d0fb73 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -35,33 +35,20 @@ VIR_LOG_INIT("util.alloc");
  * virAlloc:
  * @ptrptr: pointer to pointer for address of allocated memory
  * @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
  * allocated memory in 'ptrptr'. The newly allocated memory is
- * filled with zeros. If @report is true, OOM errors are
- * reported automatically.
+ * filled with zeros.
  *
- * Returns -1 on failure to allocate, zero on success
+ * Returns zero on success, aborts on OOM
  */
 int virAlloc(void *ptrptr,
- size_t size,
- bool report,
- int domcode,
- const char *filename,
- const char *funcname,
- size_t linenr)
+ size_t size)
 {
 *(void **)ptrptr = calloc(1, size);
-if (*(void **)ptrptr == NULL) {
-if (report)
-virReportOOMErrorFull(domcode, filename, funcname, linenr);
-return -1;
-}
+if (*(void **)ptrptr == NULL)
+abort();
+
 return 0;
 }
 
@@ -70,35 +57,22 @@ int virAlloc(void *ptrptr,
  * @ptrptr: pointer to pointer for address of allocated memory
  * @size: number of bytes 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,
  * each with 'size' bytes. Return the address of the
  * allocated memory in 'ptrptr'.  The newly allocated
- * memory is filled with zeros. If @report is true,
- * OOM errors are reported automatically.
+ * memory is filled with zeros.
  *
- * Returns -1 on failure to allocate, zero on success
+ * Returns zero on success, aborts on OOM
  */
 int virAllocN(void *ptrptr,
   size_t size,
-  size_t count,
-  bool report,
-  int domcode,
-  const char *filename,
-  const char *funcname,
-  size_t linenr)
+  size_t count)
 {
 *(void**)ptrptr = calloc(count, size);
-if (*(void**)ptrptr == NULL) {
-if (report)
-virReportOOMErrorFull(domcode, filename, funcname, linenr);
-return -1;
-}
+if (*(void**)ptrptr == NULL)
+abort();
+
 return 0;
 }
 
@@ -107,44 +81,28 @@ int virAllocN(void *ptrptr,
  * @ptrptr: pointer to pointer for address of allocated memory
  * @size: number of bytes to allocate
  * @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
  * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
  * with the address of the newly allocated memory. On failure,
  * 'ptrptr' is not changed and still points to the original memory
  * 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,
 size_t size,
-size_t count,
-bool report,
-int domcode,
-const char *filename,
-const char *funcname,
-size_t linenr)
+size_t count)
 {
 void *tmp;
 
-if (xalloc_oversized(count, size)) {
-if (report)
-virReportOOMErrorFull(domcode, filename, funcname, linenr);
-errno = ENOMEM;
-return -1;
-}
+if (xalloc_oversized(count, size))
+abort();
+
 tmp = realloc(*(void**)ptrptr, size * count);
-if (!tmp && ((size * count) != 0)) {
-if (report)
-virReportOOMErrorFull(domcode, filename, funcname, linenr);
-return -1;
-}
+if (!tmp && ((size * count) != 0))
+abort();
+