summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib
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
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')
-rw-r--r--lib/libc/stdlib/Makefile.inc8
-rw-r--r--lib/libc/stdlib/malloc.37
-rw-r--r--lib/libc/stdlib/malloc.c27
-rw-r--r--lib/libc/stdlib/posix_memalign.394
4 files changed, 128 insertions, 8 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index b003560291f..81d19620261 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.43 2010/02/03 20:49:00 miod Exp $
+# $OpenBSD: Makefile.inc,v 1.44 2010/05/18 22:24:55 tedu Exp $
# stdlib sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
@@ -42,9 +42,9 @@ SRCS+= insque.c remque.c
MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \
- lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \
- rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \
- system.3 tsearch.3
+ lldiv.3 lsearch.3 malloc.3 posix_memalign.3 qabs.3 qdiv.3 qsort.3 \
+ radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 strtonum.3 \
+ strtol.3 strtoul.3 system.3 tsearch.3
MLINKS+=exit.3 _Exit.3
MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3
diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3
index d30ac8c08ce..62006a7eb5a 100644
--- a/lib/libc/stdlib/malloc.3
+++ b/lib/libc/stdlib/malloc.3
@@ -30,9 +30,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: malloc.3,v 1.65 2010/01/25 20:14:11 jmc Exp $
+.\" $OpenBSD: malloc.3,v 1.66 2010/05/18 22:24:55 tedu Exp $
.\"
-.Dd $Mdocdate: January 25 2010 $
+.Dd $Mdocdate: May 18 2010 $
.Dt MALLOC 3
.Os
.Sh NAME
@@ -420,7 +420,8 @@ consult sources and/or wizards.
.Xr mmap 2 ,
.Xr munmap 2 ,
.Xr alloca 3 ,
-.Xr getpagesize 3
+.Xr getpagesize 3 ,
+.Xr posix_memalign 3
.Sh STANDARDS
The
.Fn malloc
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;
+}
+
diff --git a/lib/libc/stdlib/posix_memalign.3 b/lib/libc/stdlib/posix_memalign.3
new file mode 100644
index 00000000000..7bd39933dcd
--- /dev/null
+++ b/lib/libc/stdlib/posix_memalign.3
@@ -0,0 +1,94 @@
+.\" $OpenBSD
+.\" Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
+.\" 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(s), this list of conditions and the following disclaimer as
+.\" the first lines of this file unmodified other than the possible
+.\" addition of one or more copyright notices.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice(s), 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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.
+.\"
+.\" $FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp $
+.\"
+.Dd $Mdocdate: May 18 2010 $
+.Dt POSIX_MEMALIGN 3
+.Os
+.Sh NAME
+.Nm posix_memalign
+.Nd aligned memory allocation
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft int
+.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn posix_memalign
+function allocates
+.Fa size
+bytes of memory such that the allocation's base address is a multiple of
+.Fa alignment ,
+and returns the allocation in the value pointed to by
+.Fa ptr .
+.Pp
+The requested
+.Fa alignment
+must be a power of 2 at least as large as
+.Fn sizeof "void *" .
+.Pp
+Memory that is allocated via
+.Fn posix_memalign
+can be used as an argument in subsequent calls to
+.Xr realloc 3
+and
+.Xr free 3 .
+.Sh RETURN VALUES
+The
+.Fn posix_memalign
+function returns the value 0 if successful; otherwise it returns an error value.
+.Sh ERRORS
+The
+.Fn posix_memalign
+function will fail if:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa alignment
+parameter is not a power of 2 at least as large as
+.Fn sizeof "void *" .
+.It Bq Er ENOMEM
+Memory allocation error.
+.El
+.Sh SEE ALSO
+.Xr free 3 ,
+.Xr malloc 3 ,
+.Xr realloc 3
+.Sh STANDARDS
+The
+.Fn posix_memalign
+function conforms to
+.St -p1003.1-2001 .
+.Sh HISTORY
+The
+.Fn posix_memalign
+function first appeared in
+.Ox 4.8 .
+.Sh BUGS
+Only alignments up to the page size can be specified.