summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib/malloc.c
diff options
context:
space:
mode:
authortedu <tedu@openbsd.org>2010-05-18 22:24:54 +0000
committertedu <tedu@openbsd.org>2010-05-18 22:24:54 +0000
commit243f393511fee0e3dea053d89336d8b8f85b60ee (patch)
treef0de8eb1b6b3091104481b8be30b613f807883d6 /lib/libc/stdlib/malloc.c
parentAdd as yet untested support for the 82576 quad copper ET2 (diff)
downloadwireguard-openbsd-243f393511fee0e3dea053d89336d8b8f85b60ee.tar.xz
wireguard-openbsd-243f393511fee0e3dea053d89336d8b8f85b60ee.zip
add posix_madvise, posix_memalign, strndup, and strnlen. mostly from
brad and millert, with hints from guenther, jmc, and otto I think. ok previous.
Diffstat (limited to 'lib/libc/stdlib/malloc.c')
-rw-r--r--lib/libc/stdlib/malloc.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c
index 9cee3e5935f..902b69c2162 100644
--- a/lib/libc/stdlib/malloc.c
+++ b/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: malloc.c,v 1.124 2010/01/13 12:40:11 otto Exp $ */
+/* $OpenBSD: malloc.c,v 1.125 2010/05/18 22:24:55 tedu Exp $ */
/*
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
*
@@ -1488,3 +1488,28 @@ calloc(size_t nmemb, size_t size)
return r;
}
+int
+posix_memalign(void **memptr, size_t alignment, size_t size)
+{
+ void *result;
+
+ /* Make sure that alignment is a large enough power of 2. */
+ if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *) ||
+ alignment > MALLOC_PAGESIZE)
+ return EINVAL;
+
+ /*
+ * max(size, alignment) is enough to assure the requested alignment,
+ * since the allocator always allocates power-of-two blocks.
+ */
+ if (size < alignment)
+ size = alignment;
+ result = malloc(size);
+
+ if (result == NULL)
+ return ENOMEM;
+
+ *memptr = result;
+ return 0;
+}
+