summaryrefslogtreecommitdiff
path: root/mem_malloc.c
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2011-06-28 09:56:33 +0200
committerRichard Braun <rbraun@sceen.net>2011-06-28 09:56:33 +0200
commitca10664c442b660e9231ee357f8979888be3e185 (patch)
tree23ad4e5d752f0803558c30d465853fcd4acbdcfc /mem_malloc.c
Initial commit.
Diffstat (limited to 'mem_malloc.c')
-rw-r--r--mem_malloc.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/mem_malloc.c b/mem_malloc.c
new file mode 100644
index 0000000..8df6814
--- /dev/null
+++ b/mem_malloc.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2010 Richard Braun.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * Libc-compatible malloc() functions implementation.
+ *
+ * Keep in mind this code is mainly used for tests. Also, initialization is
+ * not thread-safe.
+ */
+
+#include <errno.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "mem.h"
+#include "macros.h"
+#include "mem_malloc.h"
+
+struct btag {
+ void *addr;
+ size_t size;
+} __aligned(8);
+
+void * malloc(size_t size)
+{
+ struct btag *btag;
+
+ mem_init();
+
+ size += sizeof(*btag);
+ btag = mem_alloc(size);
+
+ if (btag == NULL) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ btag->addr = btag;
+ btag->size = size;
+
+ return btag + 1;
+}
+
+void * calloc(size_t nmemb, size_t size)
+{
+ size_t bytes;
+ void *buf;
+
+ mem_init();
+
+ bytes = nmemb * size;
+ buf = malloc(bytes);
+
+ if (buf == NULL)
+ return NULL;
+
+ memset(buf, 0, bytes);
+
+ return buf;
+}
+
+void * realloc(void *ptr, size_t size)
+{
+ struct btag *btag;
+ size_t old_size;
+ char *buf;
+
+ mem_init();
+
+ if (ptr == NULL)
+ return malloc(size);
+ else if (size == 0) {
+ free(ptr);
+ return NULL;
+ }
+
+ buf = malloc(size);
+
+ if (buf == NULL)
+ return NULL;
+
+ btag = (struct btag *)ptr - 1;
+ old_size = btag->size - sizeof(*btag);
+ memcpy(buf, ptr, MIN(old_size, size));
+ mem_free(btag->addr, btag->size);
+
+ return buf;
+}
+
+int posix_memalign(void **ptr, size_t align, size_t size)
+{
+ struct btag *btag;
+ char *buf;
+
+ mem_init();
+
+ if (!ISP2(align))
+ return EINVAL;
+
+ size += sizeof(*btag);
+ size = P2ROUND(size, align * 2);
+ buf = mem_alloc(size);
+
+ if (buf == NULL)
+ return ENOMEM;
+
+ btag = (struct btag *)P2ROUND((unsigned long)buf + sizeof(*btag), align)
+ - 1;
+ btag->addr = buf;
+ btag->size = size;
+
+ *ptr = btag + 1;
+ return 0;
+}
+
+void free(void *ptr)
+{
+ struct btag *btag;
+
+ mem_init();
+
+ if (ptr == NULL)
+ return;
+
+ btag = (struct btag *)ptr - 1;
+ mem_free(btag->addr, btag->size);
+}