[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH RFC 1/2] viralloc: Report OOM error on failure



On 03/22/2013 07:44 AM, Michal Privoznik wrote:
In nearly all cases of calling VIR_ALLOC*, VIR_REALLOC_N,
VIR_EXPAND_N, VIR_RESIZE_N, VIR_*_ELEMENT etc. we want to
report OOM error, so our source code base is full of:

   if (VIR_ALLOC(somePtr) < 0) {
       virReportOOMError();
       goto cleanup;
   }

or similar. Moreover, for those few cases where we don't want to
report OOM error (e.g. virReportOOMError() itself) a new
VIR_ALLOC_NOOOM macro is being introduced.
---
[...]
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index 7be7f82..30ffe15 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -46,7 +46,7 @@


  /* Don't call these directly - use the macros below */
-int virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK
+int virAlloc(void *ptrptr, size_t size, bool report) ATTRIBUTE_RETURN_CHECK
      ATTRIBUTE_NONNULL(1);
  int virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK
      ATTRIBUTE_NONNULL(1);
@@ -76,13 +76,16 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
   * VIR_ALLOC:
   * @ptr: pointer to hold address of allocated memory
   *
- * Allocate sizeof(*ptr) bytes of memory and store
- * the address of allocated memory in 'ptr'. Fill the
- * newly allocated memory with zeros.
+ * Allocate sizeof(*ptr) bytes of memory and store the
+ * address of allocated memory in 'ptr'. Fill the newly
+ * allocated memory with zeros. If there's a failure,
+ * OOM error is reported. The VIR_ALLOC_NOOOM macro
+ * behaves the same except the OOM error reporting.
   *
   * Returns -1 on failure, 0 on success
   */
-# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)))
+# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)), true)
+# define VIR_ALLOC_NOOOM(ptr) virAlloc(&(ptr), sizeof(*(ptr)), true)

should be false in the 2nd case...


  /**
   * VIR_ALLOC_N:
diff --git a/src/util/virerror.c b/src/util/virerror.c
index c30642a..c033129 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -204,7 +204,7 @@ virLastErrorObject(void)
      virErrorPtr err;
      err = virThreadLocalGet(&virLastErr);
      if (!err) {
-        if (VIR_ALLOC(err) < 0)
+        if (VIR_ALLOC_NOOOM(err) < 0)
              return NULL;
          if (virThreadLocalSet(&virLastErr, err) < 0)
              VIR_FREE(err);

ACK with nit fixed.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]