Skip to contentSkip to conversation

add realloc

PATCH New Samuel Thibault 1 participant 1 message Jan 17, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add realloc

add realloc

# HG changeset patch
# User Samuel Thibault <[email protected]> # Date 1200581655 0
# Node ID 75de0dc5198095fa4e845f4132e3370557986748 # Parent 92de8fa028b8e6696fa386b7430537d1cf99e429 add realloc

extras/mini-os/include/xmalloc.h +3 -0
diff -r 92de8fa028b8 -r 75de0dc51980 extras/mini-os/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. */
extras/mini-os/lib/xmalloc.c +21 -0
diff -r 92de8fa028b8 -r 75de0dc51980 extras/mini-os/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;
+}