summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/realloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdlib/realloc.c')
-rw-r--r--lib/libc/stdlib/realloc.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/libc/stdlib/realloc.c b/lib/libc/stdlib/realloc.c
new file mode 100644
index 00000000..d90af70d
--- /dev/null
+++ b/lib/libc/stdlib/realloc.c
@@ -0,0 +1,47 @@
+#include <errno.h>
+#include <string.h>
+#include <atomic.h>
+#include <libc.h>
+#include <malloc.h>
+#include <linux/errno.h>
+#include <stdlib.h>
+
+void *realloc(void *ptr, size_t size)
+{
+ if (ptr == NULL) {
+ return malloc(size);
+ }
+ if (size == 0) {
+ free(ptr);
+ return NULL;
+ }
+
+ LIBC_LOCK(libc.lock.malloc);
+
+ struct page *p = __malloc_pvec;
+ while (p) {
+ if ((uintptr_t)ptr >= (uintptr_t)p->heap &&
+ (uintptr_t)ptr < (uintptr_t)(p->heap + (p->block.size *
+ p->block.count))) {
+ size_t old_size = p->block.size;
+ LIBC_UNLOCK(libc.lock.malloc);
+
+ if (size <= old_size) {
+ return ptr;
+ } else {
+ void *new_ptr = malloc(size);
+ if (new_ptr) {
+ memcpy(new_ptr, ptr, old_size);
+ free(ptr);
+ }
+ return new_ptr;
+ }
+ }
+ p = p->next;
+ }
+
+ LIBC_UNLOCK(libc.lock.malloc);
+
+ errno = EINVAL;
+ return NULL;
+}