BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionLibC / commit / d0db5bd73909d1f94cac363ea1dfd12674bbd3a4 / stdlib / malloc.c

// Related

OrionLibC

Barry Fixing malloc heap exhaustion bug d0db5bd (2 years, 2 months ago)
diff --git a/stdlib/malloc.c b/stdlib/malloc.c
index cd19760..2ac9f32 100644
--- a/stdlib/malloc.c
+++ b/stdlib/malloc.c
@@ -130,7 +130,8 @@ malloc(size_t size)
 		/* Attempt to use arena */
 		addr = arena_malloc(arena, size);
 		if (!addr && !arena->next)
-			arena->next = arena_malloc(arena, sizeof(Arena));
+			arena->next = arena->start + arena->size
+			            - sizeof(Arena);
 	}
 
 	return addr;
@@ -155,3 +156,20 @@ free(void *addr)
 	if (next)
 		next->prev = prev;
 }
+
+/* Re-allocate a region of memory */
+void *
+realloc(void *addr, size_t size)
+{
+	void *ptr = malloc(size);
+	if (addr) {
+		Header *old = (Header *) addr - 1;
+		if (memcmp(old->magic, "HEAP", 4)) {
+			printf("realloc(): invalid pointer\n");
+			abort();
+		}
+		memcpy(ptr, addr, old->size);
+		free(addr);
+	}
+	return ptr;
+}