[PATCH] memory: Handle heap allocation failures without aborting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] memory: Handle heap allocation failures without aborting

Holger Freyther
From: Holger Hans Peter Freyther <[hidden email]>

_gst_heap_sbrk/heap_sbrk_internal will return NULL on allocation
failures and set errno to ENOMEM but the morecore method assumed
that MMAP_FAILED (PTR -1) would be returned. Make it consistent.

This way I can allocate up to 1.4gb of virtual address space until
I run into GC scalability issues.
---
 libgst/ChangeLog |    6 ++++++
 libgst/alloc.c   |    6 ++++--
 libgst/heap.c    |   11 +++--------
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/libgst/ChangeLog b/libgst/ChangeLog
index 2ba7b05..1923813 100644
--- a/libgst/ChangeLog
+++ b/libgst/ChangeLog
@@ -1,3 +1,9 @@
+2012-12-29  Holger Hans Peter Freyther  <[hidden email]>
+
+ * libgst/alloc.c: _gst_heap_sbrk returns NULL and not MMAP_FAILED
+ on allocation failure.
+ * libgst/heap.c: Return NULL on allocation failure.
+
 2012-09-09  Paolo Bonzini  <[hidden email]>
 
  * libgst/sysdep/posix/events.c: Register the fd with gst
diff --git a/libgst/alloc.c b/libgst/alloc.c
index 86810d2..cdead03 100644
--- a/libgst/alloc.c
+++ b/libgst/alloc.c
@@ -671,6 +671,8 @@ heap_system_alloc (heap_data *h, size_t sz)
 #endif
 
   mem = (heap_block *) morecore (sz);
+  if (!mem)
+ nomemory(1);
   mem->mmap_block = 0;
   mem->size = sz;
 
@@ -700,7 +702,7 @@ morecore (size_t size)
     {
       char *ptr = _gst_heap_sbrk (current_heap, size);
 
-      if (ptr != (PTR) -1)
+      if (ptr != NULL)
  {
           if (((intptr_t) ptr & (pagesize - 1)) > 0)
             {
@@ -710,7 +712,7 @@ morecore (size_t size)
       ptr = _gst_heap_sbrk (current_heap, size);
             }
 
-          if (ptr != (PTR) -1)
+          if (ptr != NULL)
     return (ptr);
  }
 
diff --git a/libgst/heap.c b/libgst/heap.c
index 1f64fb2..929389f 100644
--- a/libgst/heap.c
+++ b/libgst/heap.c
@@ -220,14 +220,9 @@ heap_sbrk_internal (struct heap * hdp,
     {
       if (hdp->breakval - hdp->base + size > hdp->areasize)
         {
-          if (hdp->breakval - hdp->base == hdp->areasize);
-            {
-              /* FIXME: a library should never exit!  */
-              fprintf (stderr, "gst: out of memory allocating %d bytes\n",
-                       size);
-              exit (1);
-            }
-          size = hdp->areasize - (hdp->breakval - hdp->base);
+          /* this heap is full? */
+          errno = ENOMEM;
+          return NULL;
         }
 
       moveto = PAGE_ALIGN (hdp->breakval + size);
--
1.7.10.4


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] memory: Handle heap allocation failures without aborting

Paolo Bonzini-2
Il 29/12/2012 00:42, Holger Hans Peter Freyther ha scritto:

> From: Holger Hans Peter Freyther <[hidden email]>
>
> _gst_heap_sbrk/heap_sbrk_internal will return NULL on allocation
> failures and set errno to ENOMEM but the morecore method assumed
> that MMAP_FAILED (PTR -1) would be returned. Make it consistent.
>
> This way I can allocate up to 1.4gb of virtual address space until
> I run into GC scalability issues.
> ---
>  libgst/ChangeLog |    6 ++++++
>  libgst/alloc.c   |    6 ++++--
>  libgst/heap.c    |   11 +++--------
>  3 files changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/libgst/ChangeLog b/libgst/ChangeLog
> index 2ba7b05..1923813 100644
> --- a/libgst/ChangeLog
> +++ b/libgst/ChangeLog
> @@ -1,3 +1,9 @@
> +2012-12-29  Holger Hans Peter Freyther  <[hidden email]>
> +
> + * libgst/alloc.c: _gst_heap_sbrk returns NULL and not MMAP_FAILED
> + on allocation failure.
> + * libgst/heap.c: Return NULL on allocation failure.
> +
>  2012-09-09  Paolo Bonzini  <[hidden email]>
>  
>   * libgst/sysdep/posix/events.c: Register the fd with gst
> diff --git a/libgst/alloc.c b/libgst/alloc.c
> index 86810d2..cdead03 100644
> --- a/libgst/alloc.c
> +++ b/libgst/alloc.c
> @@ -671,6 +671,8 @@ heap_system_alloc (heap_data *h, size_t sz)
>  #endif
>  
>    mem = (heap_block *) morecore (sz);
> +  if (!mem)
> + nomemory(1);
>    mem->mmap_block = 0;
>    mem->size = sz;
>  
> @@ -700,7 +702,7 @@ morecore (size_t size)
>      {
>        char *ptr = _gst_heap_sbrk (current_heap, size);
>  
> -      if (ptr != (PTR) -1)
> +      if (ptr != NULL)
>   {
>            if (((intptr_t) ptr & (pagesize - 1)) > 0)
>              {
> @@ -710,7 +712,7 @@ morecore (size_t size)
>        ptr = _gst_heap_sbrk (current_heap, size);
>              }
>  
> -          if (ptr != (PTR) -1)
> +          if (ptr != NULL)
>      return (ptr);
>   }
>  
> diff --git a/libgst/heap.c b/libgst/heap.c
> index 1f64fb2..929389f 100644
> --- a/libgst/heap.c
> +++ b/libgst/heap.c
> @@ -220,14 +220,9 @@ heap_sbrk_internal (struct heap * hdp,
>      {
>        if (hdp->breakval - hdp->base + size > hdp->areasize)
>          {
> -          if (hdp->breakval - hdp->base == hdp->areasize);
> -            {
> -              /* FIXME: a library should never exit!  */
> -              fprintf (stderr, "gst: out of memory allocating %d bytes\n",
> -                       size);
> -              exit (1);
> -            }
> -          size = hdp->areasize - (hdp->breakval - hdp->base);
> +          /* this heap is full? */
> +          errno = ENOMEM;
> +          return NULL;
>          }
>  
>        moveto = PAGE_ALIGN (hdp->breakval + size);
>

Applied, thanks!

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk