Skip to contentSkip to conversation

Tell users about out-of-memory errors

PATCH In review Stefan Weil 3 participants 4 messages Jan 20, 2010 – Jan 27, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Tell users about out-of-memory errors

Aborting without an error message when memory is short is not helpful, so print the reason for the abort.

Try

	qemu -m 1000000

to force an out-of-memory error.

---
 osdep.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
osdep.c +5 -1
index 1310684..5d4e810 100644
@@ -52,6 +52,7 @@
 static void *oom_check(void *ptr)
 {
     if (ptr == NULL) {
+        perror("qemu (memory allocation)");
         abort();
     }
     return ptr;
@@ -91,8 +92,11 @@ void *qemu_memalign(size_t alignment, size_t size)
     int ret;
     void *ptr;
     ret = posix_memalign(&ptr, alignment, size);
-    if (ret != 0)
+    if (ret != 0) {
+        fprintf(stderr, "Failed to allocate %zu B: %s\n",
+                size, strerror(errno));
         abort();
+    }
     return ptr;
 #elif defined(CONFIG_BSD)
     return oom_check(valloc(size));
Signature · 1 line
1.6.5
#1 lore raw
Quoted 6 lines · Stefan Weil

Aborting without an error message when memory is short is not helpful, so print the reason for the abort.

Try

	qemu -m 1000000

to force an out-of-memory error.

The patch is wrong for, at least, Windows.

Quoted 32 lines
---
 osdep.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
osdep.c +5 -1
index 1310684..5d4e810 100644
@@ -52,6 +52,7 @@
 static void *oom_check(void *ptr)
 {
     if (ptr == NULL) {
+        perror("qemu (memory allocation)");
         abort();
     }
     return ptr;
@@ -91,8 +92,11 @@ void *qemu_memalign(size_t alignment, size_t size)
     int ret;
     void *ptr;
     ret = posix_memalign(&ptr, alignment, size);
-    if (ret != 0)
+    if (ret != 0) {
+        fprintf(stderr, "Failed to allocate %zu B: %s\n",
+                size, strerror(errno));
         abort();
+    }
     return ptr;
 #elif defined(CONFIG_BSD)
     return oom_check(valloc(size));
Signature · 1 line
mailto:[email protected]
#2 lore raw

PATCH 1/1 Tell users about out-of-memory errors

Aborting without an error message when memory is short is not helpful, so print the reason for the abort.

Try

	qemu -m 1000000
or
	qemu -m 2000 (win32)

to force an out-of-memory error.

v2:
* Fix error message for win32.
* Fix error message for posix_memalign.

Thanks to malc for the hints.

---
 osdep.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
osdep.c +9 -1
index 1310684..9b47066 100644
@@ -52,6 +52,11 @@
 static void *oom_check(void *ptr)
 {
     if (ptr == NULL) {
+#if defined(_WIN32)
+        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
+#else
+        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
+#endif
         abort();
     }
     return ptr;
@@ -91,8 +96,11 @@ void *qemu_memalign(size_t alignment, size_t size)
     int ret;
     void *ptr;
     ret = posix_memalign(&ptr, alignment, size);
-    if (ret != 0)
+    if (ret != 0) {
+        fprintf(stderr, "Failed to allocate %zu B: %s\n",
+                size, strerror(ret));
         abort();
+    }
     return ptr;
 #elif defined(CONFIG_BSD)
     return oom_check(valloc(size));
Signature · 1 line
1.6.5
#3 lore raw
Quoted 10 lines · Stefan Weil

Aborting without an error message when memory is short is not helpful, so print the reason for the abort.

Try

	qemu -m 1000000
or
	qemu -m 2000 (win32)

to force an out-of-memory error.

Quoted 8 lines

v2:
* Fix error message for win32.
* Fix error message for posix_memalign.

Thanks to malc for the hints.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 34 lines
---
  osdep.c |   10 +++++++++-
  1 files changed, 9 insertions(+), 1 deletions(-)
osdep.c +9 -1
index 1310684..9b47066 100644
@@ -52,6 +52,11 @@
  static void *oom_check(void *ptr)
  {
      if (ptr == NULL) {
+#if defined(_WIN32)
+        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
+#else
+        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
+#endif
          abort();
      }
      return ptr;
@@ -91,8 +96,11 @@ void *qemu_memalign(size_t alignment, size_t size)
      int ret;
      void *ptr;
      ret = posix_memalign(&ptr, alignment, size);
-    if (ret != 0)
+    if (ret != 0) {
+        fprintf(stderr, "Failed to allocate %zu B: %s\n",
+                size, strerror(ret));
          abort();
+    }
      return ptr;
  #elif defined(CONFIG_BSD)
      return oom_check(valloc(size));