diff mbox

[v2,06/18] mini-os: let memory allocation fail if no free page available

Message ID 1470418562-11234-7-git-send-email-jgross@suse.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jürgen Groß Aug. 5, 2016, 5:35 p.m. UTC
Instead of panicing when no page can be allocated try to fail the
memory allocation by returning NULL instead.

Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Wei Liu <wei.liu2@citrix.com>
---
V2: fixed minor style issue
---
 mm.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Samuel Thibault Aug. 10, 2016, 8:06 p.m. UTC | #1
Juergen Gross, on Fri 05 Aug 2016 19:35:50 +0200, wrote:
> Instead of panicing when no page can be allocated try to fail the
> memory allocation by returning NULL instead.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> Reviewed-by: Wei Liu <wei.liu2@citrix.com>

Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

> ---
> V2: fixed minor style issue
> ---
>  mm.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/mm.c b/mm.c
> index 263a356..8cf3210 100644
> --- a/mm.c
> +++ b/mm.c
> @@ -335,6 +335,13 @@ void *sbrk(ptrdiff_t increment)
>      
>      if (new_brk > heap_mapped) {
>          unsigned long n = (new_brk - heap_mapped + PAGE_SIZE - 1) / PAGE_SIZE;
> +
> +        if ( n > nr_free_pages )
> +        {
> +            printk("Memory exhausted: want %ld pages, but only %ld are left\n",
> +                   n, nr_free_pages);
> +            return NULL;
> +        }
>          do_map_zero(heap_mapped, n);
>          heap_mapped += n * PAGE_SIZE;
>      }
> -- 
> 2.6.6
>
diff mbox

Patch

diff --git a/mm.c b/mm.c
index 263a356..8cf3210 100644
--- a/mm.c
+++ b/mm.c
@@ -335,6 +335,13 @@  void *sbrk(ptrdiff_t increment)
     
     if (new_brk > heap_mapped) {
         unsigned long n = (new_brk - heap_mapped + PAGE_SIZE - 1) / PAGE_SIZE;
+
+        if ( n > nr_free_pages )
+        {
+            printk("Memory exhausted: want %ld pages, but only %ld are left\n",
+                   n, nr_free_pages);
+            return NULL;
+        }
         do_map_zero(heap_mapped, n);
         heap_mapped += n * PAGE_SIZE;
     }