From 5080b4f0d280571ce6f89429561db390c5faa037 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Thu, 17 Jan 2008 15:06:30 +0000 Subject: [PATCH] minios: add realloc Signed-off-by: Samuel Thibault Signed-off-by: Tim Deegan --- include/xmalloc.h | 3 +++ lib/xmalloc.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/xmalloc.h b/include/xmalloc.h index f5e721f..e8052ac 100644 --- a/include/xmalloc.h +++ b/include/xmalloc.h @@ -9,12 +9,15 @@ #define malloc(size) _xmalloc(size, 4) #define free(ptr) xfree(ptr) +#define realloc(ptr, size) _realloc(ptr, size) /* Free any of the above. */ extern void xfree(const void *); /* Underlying functions */ extern void *_xmalloc(size_t size, size_t align); +extern void *_realloc(void *ptr, size_t size); + static inline void *_xmalloc_array(size_t size, size_t align, size_t num) { /* Check for overflow. */ diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 3246d57..de0001b 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -223,3 +223,24 @@ void xfree(const void *p) /* spin_unlock_irqrestore(&freelist_lock, flags); */ } +void *_realloc(void *ptr, size_t size) +{ + void *new; + struct xmalloc_hdr *hdr; + + if (ptr == NULL) + return _xmalloc(size, 4); + + hdr = (struct xmalloc_hdr *)ptr - 1; + if (hdr->size >= size) + return ptr; + + new = _xmalloc(size, 4); + if (new == NULL) + return NULL; + + memcpy(new, ptr, hdr->size); + xfree(ptr); + + return new; +} -- 2.39.5