From dcfc312e3cc930d418f86948ef062eb7e8fd283a Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 29 Jan 2005 18:20:17 +0000 Subject: [PATCH] Add xmalloc(), xcalloc() functions. --- src/threads/malloc.c | 26 ++++++++++++++++++++++++++ src/threads/malloc.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/threads/malloc.c b/src/threads/malloc.c index 8e6b459..9c83e3d 100644 --- a/src/threads/malloc.c +++ b/src/threads/malloc.c @@ -153,6 +153,19 @@ malloc (size_t size) return b; } +/* Obtains and returns a new block of at least SIZE bytes. + Panics if memory is not available. It is unacceptable for the + kernel to panic in normal operation, so this function should + only be used during kernel initialization. */ +void * +xmalloc (size_t size) +{ + void *p = malloc (size); + if (p == NULL && size > 0) + PANIC ("memory exhausted"); + return p; +} + /* Allocates and return A times B bytes initialized to zeroes. Returns a null pointer if memory is not available. */ void * @@ -174,6 +187,19 @@ calloc (size_t a, size_t b) return p; } +/* Allocates and return A times B bytes initialized to zeroes. + Panics if memory is not available. It is unacceptable for the + kernel to panic in normal operation, so this function should + only be used during kernel initialization. */ +void * +xcalloc (size_t a, size_t b) +{ + void *p = calloc (a, b); + if (p == NULL && a > 0 && b > 0) + PANIC ("memory exhausted"); + return p; +} + /* Frees block P, which must have been previously allocated with malloc() or calloc(). */ void diff --git a/src/threads/malloc.h b/src/threads/malloc.h index 2f19155..4f0d0bf 100644 --- a/src/threads/malloc.h +++ b/src/threads/malloc.h @@ -6,7 +6,9 @@ void malloc_init (void); void *malloc (size_t) __attribute__ ((malloc)); +void *xmalloc (size_t) __attribute__ ((malloc)); void *calloc (size_t, size_t) __attribute__ ((malloc)); +void *xcalloc (size_t, size_t) __attribute__ ((malloc)); void free (void *); #endif /* threads/malloc.h */ -- 2.30.2