Patchwork Tell users about out-of-memory errors

login
register
mail settings
Submitter Stefan Weil
Date Jan. 21, 2010, 9:24 p.m.
Message ID <1264109098-14373-1-git-send-email-weil@mail.berlios.de>
Download mbox | patch
Permalink /patch/43466/
State New
Headers show

Comments

Stefan Weil - Jan. 21, 2010, 9:24 p.m.
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.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 osdep.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
Anthony Liguori - Jan. 27, 2010, 12:06 a.m.
On 01/21/2010 03:24 PM, Stefan Weil wrote:
> 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.
>
> Signed-off-by: Stefan Weil<weil@mail.berlios.de>
>    

Applied.  Thanks.

Regards,

Anthony Liguori
> ---
>   osdep.c |   10 +++++++++-
>   1 files changed, 9 insertions(+), 1 deletions(-)
>
> diff --git a/osdep.c b/osdep.c
> index 1310684..9b47066 100644
> --- a/osdep.c
> +++ b/osdep.c
> @@ -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));
>

Patch

diff --git a/osdep.c b/osdep.c
index 1310684..9b47066 100644
--- a/osdep.c
+++ b/osdep.c
@@ -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));