One thing to be careful of is that these functions can fail and return 0,
regardless of your kmalloc flags.

Signed-off-by: Barret Rhoden <[email protected]>
---
 kern/include/kmalloc.h   | 5 +++--
 kern/src/kreallocarray.c | 9 +++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/kern/include/kmalloc.h b/kern/include/kmalloc.h
index 268cf2985ddb..26f23c1754c0 100644
--- a/kern/include/kmalloc.h
+++ b/kern/include/kmalloc.h
@@ -16,8 +16,9 @@
 #define KMALLOC_LARGEST KMALLOC_SMALLEST << NUM_KMALLOC_CACHES
 
 void kmalloc_init(void);
-void* kmalloc(size_t size, int flags);
-void* kzmalloc(size_t size, int flags);
+void *kmalloc(size_t size, int flags);
+void *kmalloc_array(size_t nmemb, size_t size, int flags);
+void *kzmalloc(size_t size, int flags);
 void *kmalloc_align(size_t size, int flags, size_t align);
 void *kzmalloc_align(size_t size, int flags, size_t align);
 void *krealloc(void *buf, size_t size, int flags);
diff --git a/kern/src/kreallocarray.c b/kern/src/kreallocarray.c
index d099721e0406..505159917317 100644
--- a/kern/src/kreallocarray.c
+++ b/kern/src/kreallocarray.c
@@ -34,3 +34,12 @@ void *kreallocarray(void *optr, size_t nmemb, size_t size, 
int flags)
        }
        return krealloc(optr, size * nmemb, flags);
 }
+
+void *kmalloc_array(size_t nmemb, size_t size, int flags)
+{
+       if (((nmemb >= MUL_NO_OVERFLOW) || (size >= MUL_NO_OVERFLOW)) &&
+           (nmemb > 0) && ((SIZE_MAX / nmemb) < size)) {
+               return NULL;
+       }
+       return kmalloc(size * nmemb, flags);
+}
-- 
2.6.0.rc2.230.g3dd15c0

-- 
You received this message because you are subscribed to the Google Groups 
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to