summaryrefslogtreecommitdiff
path: root/src/stdlib/realloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/realloc.c')
-rw-r--r--src/stdlib/realloc.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/stdlib/realloc.c b/src/stdlib/realloc.c
new file mode 100644
index 00000000..f16ba8a5
--- /dev/null
+++ b/src/stdlib/realloc.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#if defined _POSIX_C_SOURCE && 199305L <= _POSIX_C_SOURCE
+#include "sys/types.h"
+#include "fcntl.h"
+#include "sys/mman.h"
+#endif
+
+/** change the amount of memory allocated **/
+
+void * realloc(void * ptr, size_t size)
+{
+#if defined _POSIX_C_SOURCE && 199305L <= _POSIX_C_SOURCE
+ /* FIXME: forward dependency on POSIX.1b-1993, non-std /dev/zero */
+ static int backing = -1;
+
+ if (backing == -1) {
+ backing = open("/dev/zero", O_RDWR /* | O_CLOEXEC */, 0);
+ }
+
+ if (ptr == NULL) {
+ /* malloc() */
+ return (void*)(long)mmap(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE, backing, 0);
+ } else if (size == 0) {
+ /* free() */
+ }
+
+ /* TODO: reallocate */
+#else
+ (void)size;
+#endif
+
+ return ptr;
+}
+
+/***
+changes the amount of memory allocated to ARGUMENT(ptr) to ARGUMENT(size)
+bytes.
+
+If ARGUMENT(ptr) is CONSTANT(NULL), a new allocation is made.
+
+If ARGUMENT(size) is 0 and ARGUMENT(ptr) is not CONSTANT(NULL), the memory at ARGUMENT(ptr) is
+deallocated.
+
+Otherwise, the memory allocated to ARGUMENT(ptr) is resized. If enough memory cannot
+be allocated, ARGUMENT(ptr) will not change. If enough memory is available, the
+address of ARGUMENT(ptr) may change, but the contents will be the same up to the
+minimum of the old and new sizes.
+***/
+
+/*
+UNSPECIFIED(The order and contiguity of space allocated by success calls)
+UNDEFINED(ARGUMENT(ptr) is not CONSTANT(NULL) or a pointer returned by a previous call to FUNCTION(calloc), FUNCTION(malloc), or FUNCTION(realloc))
+UNDEFINED(ARGUMENT(ptr) has been previously deallocated by FUNCTION(free) or FUNCTION(realloc))
+RETURN_FAILURE(CONSTANT(NULL))
+RETURN_SUCCESS(a pointer to the reallocate space)
+STDC(1)
+*/