summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Unangst <tedu@cvs.openbsd.org>2010-05-18 22:24:56 +0000
committerTed Unangst <tedu@cvs.openbsd.org>2010-05-18 22:24:56 +0000
commit0eebcf7d241b97d6a763d98b8cd5ee2dd7fe9ab6 (patch)
tree7991b5d4f89c176f4a5dba6e39883fe012b931e8
parent2e943e3e1fd3e074c743818e07c658d1a41a0020 (diff)
add posix_madvise, posix_memalign, strndup, and strnlen. mostly from
brad and millert, with hints from guenther, jmc, and otto I think. ok previous.
-rw-r--r--include/stdlib.h3
-rw-r--r--include/string.h7
-rw-r--r--lib/libc/gen/valloc.39
-rw-r--r--lib/libc/shlib_version2
-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
-rw-r--r--lib/libc/string/Makefile.inc8
-rw-r--r--lib/libc/string/strdup.331
-rw-r--r--lib/libc/string/strlen.339
-rw-r--r--lib/libc/string/strndup.c39
-rw-r--r--lib/libc/string/strnlen.c34
-rw-r--r--lib/libc/sys/Makefile.inc6
-rw-r--r--lib/libc/sys/madvise.237
-rw-r--r--lib/libc/sys/posix_madvise.c11
-rw-r--r--sys/sys/mman.h49
17 files changed, 372 insertions, 39 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 840d381aa1f..affc6b18010 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: stdlib.h,v 1.46 2009/06/03 15:52:16 millert Exp $ */
+/* $OpenBSD: stdlib.h,v 1.47 2010/05/18 22:24:55 tedu Exp $ */
/* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */
/*-
@@ -125,6 +125,7 @@ char *getenv(const char *);
long labs(long);
ldiv_t ldiv(long, long);
void *malloc(size_t);
+int posix_memalign(void **, size_t, size_t);
void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
int rand(void);
void *realloc(void *, size_t);
diff --git a/include/string.h b/include/string.h
index 52f3f718f7c..e33204d923b 100644
--- a/include/string.h
+++ b/include/string.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: string.h,v 1.18 2007/09/03 14:36:40 millert Exp $ */
+/* $OpenBSD: string.h,v 1.19 2010/05/18 22:24:55 tedu Exp $ */
/* $NetBSD: string.h,v 1.6 1994/10/26 00:56:30 cgd Exp $ */
/*-
@@ -110,6 +110,11 @@ int strerror_r(int, char *, size_t)
__attribute__ ((__bounded__(__string__,2,3)));
#endif
+#if __POSIX_VISIBLE >= 200809
+char *strndup(const char *, size_t);
+size_t strnlen(const char *, size_t);
+#endif
+
#if __BSD_VISIBLE
char *strcasestr(const char *, const char *);
size_t strlcat(char *, const char *, size_t)
diff --git a/lib/libc/gen/valloc.3 b/lib/libc/gen/valloc.3
index adb4948d052..cea59620988 100644
--- a/lib/libc/gen/valloc.3
+++ b/lib/libc/gen/valloc.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: valloc.3,v 1.12 2007/05/31 19:19:29 jmc Exp $
+.\" $OpenBSD: valloc.3,v 1.13 2010/05/18 22:24:55 tedu Exp $
.\"
.\" Copyright (c) 1980, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -27,7 +27,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: May 31 2007 $
+.Dd $Mdocdate: May 18 2010 $
.Dt VALLOC 3
.Os
.Sh NAME
@@ -43,7 +43,10 @@ The
.Fn valloc
function is obsoleted by the current version of
.Xr malloc 3 ,
-which aligns page-sized and larger allocations.
+which aligns page-sized and larger allocations,
+and
+.Xr posix_memalign 3 ,
+which allocates memory with a given alignment.
.Ef
.Pp
The
diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version
index b6d2a137e28..81a5aca30f3 100644
--- a/lib/libc/shlib_version
+++ b/lib/libc/shlib_version
@@ -1,4 +1,4 @@
major=53
-minor=1
+minor=2
# note: If changes were made to include/thread_private.h or if system
# calls were added/changed then libpthread must also be updated.
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.
diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc
index 8aa072a2893..3264b799f17 100644
--- a/lib/libc/string/Makefile.inc
+++ b/lib/libc/string/Makefile.inc
@@ -1,11 +1,11 @@
-# $OpenBSD: Makefile.inc,v 1.21 2010/02/03 20:49:00 miod Exp $
+# $OpenBSD: Makefile.inc,v 1.22 2010/05/18 22:24:55 tedu Exp $
# string sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
- strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \
- strxfrm.c \
+ strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \
+ strsignal.c strtok.c strxfrm.c \
wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \
wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \
@@ -153,7 +153,9 @@ MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strcmp.3 strncmp.3
MLINKS+=strcpy.3 strncpy.3
+MLINKS+=strdup.3 strndup.3
MLINKS+=strlcpy.3 strlcat.3
+MLINKS+=strlen.3 strnlen.3
MLINKS+=strstr.3 strcasestr.3
MLINKS+=strtok.3 strtok_r.3
MLINKS+=strerror.3 strerror_r.3
diff --git a/lib/libc/string/strdup.3 b/lib/libc/string/strdup.3
index 60a74627771..05dcb794f04 100644
--- a/lib/libc/string/strdup.3
+++ b/lib/libc/string/strdup.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: strdup.3,v 1.15 2010/03/24 14:47:46 kettenis Exp $
+.\" $OpenBSD: strdup.3,v 1.16 2010/05/18 22:24:55 tedu Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,16 +29,19 @@
.\"
.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
.\"
-.Dd $Mdocdate: March 24 2010 $
+.Dd $Mdocdate: May 18 2010 $
.Dt STRDUP 3
.Os
.Sh NAME
-.Nm strdup
+.Nm strdup ,
+.Nm strndup
.Nd save a copy of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft char *
.Fn strdup "const char *s"
+.Ft char *
+.Fn strndup "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strdup
@@ -48,6 +51,16 @@ does the copy, and returns a pointer to it.
The pointer may subsequently be used as an argument to the function
.Xr free 3 .
.Pp
+The
+.Fn strndup
+function behaves similarly to
+.Nm strdup
+but only copies up to
+.Fa maxlen
+characters from
+.Fa s .
+The resulting string is always NUL-terminated.
+.Pp
If insufficient memory is available,
.Dv NULL
is returned.
@@ -83,3 +96,15 @@ The
.Fn strdup
function first appeared in
.Bx 4.4 .
+.Pp
+The
+.Fn strndup
+function first appeared in
+.Ox 4.8 .
+.Sh STANDARDS
+The
+.Fn strdup
+and
+.Fn strndup
+functions conform to
+.St -p1003.1-2008 .
diff --git a/lib/libc/string/strlen.3 b/lib/libc/string/strlen.3
index f8a4efe9d24..3b5f9a10c12 100644
--- a/lib/libc/string/strlen.3
+++ b/lib/libc/string/strlen.3
@@ -29,29 +29,55 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: strlen.3,v 1.6 2007/05/31 19:19:32 jmc Exp $
+.\" $OpenBSD: strlen.3,v 1.7 2010/05/18 22:24:55 tedu Exp $
.\"
-.Dd $Mdocdate: May 31 2007 $
+.Dd $Mdocdate: May 18 2010 $
.Dt STRLEN 3
.Os
.Sh NAME
-.Nm strlen
+.Nm strlen ,
+.Nm strnlen
.Nd find length of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft size_t
.Fn strlen "const char *s"
+.Ft size_t
+.Fn strnlen "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strlen
function computes the length of the string
.Fa s .
+.Pp
+The
+.Fn strnlen
+function computes the length of the string
+.Fa s ,
+up to
+.Fa maxlen
+characters.
+The
+.Fn strnlen
+function will never attempt to address more than
+.Fa maxlen
+characters, making it suitable for use with character arrays that are
+not guaranteed to be NUL-terminated.
+.Pp
.Sh RETURN VALUES
The
.Fn strlen
function returns the number of characters that precede the terminating
.Tn NUL
character.
+.Pp
+The
+.Fn strnlen
+function returns the number of characters that precede the terminating
+.Tn NUL
+or
+.Fa maxlen ,
+whichever is smaller.
.Sh SEE ALSO
.Xr string 3
.Sh STANDARDS
@@ -59,3 +85,10 @@ The
.Fn strlen
function conforms to
.St -ansiC .
+.Pp
+The
+.Fn strlen
+and
+.Fn strnlen
+functions conform to
+.St -p1003.1-2008 .
diff --git a/lib/libc/string/strndup.c b/lib/libc/string/strndup.c
new file mode 100644
index 00000000000..27701ac555a
--- /dev/null
+++ b/lib/libc/string/strndup.c
@@ -0,0 +1,39 @@
+/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *
+strndup(const char *str, size_t maxlen)
+{
+ char *copy;
+ size_t len;
+
+ len = strnlen(str, maxlen);
+ copy = malloc(len + 1);
+ if (copy != NULL) {
+ (void)memcpy(copy, str, len);
+ copy[len] = '\0';
+ }
+
+ return copy;
+}
diff --git a/lib/libc/string/strnlen.c b/lib/libc/string/strnlen.c
new file mode 100644
index 00000000000..5c999947444
--- /dev/null
+++ b/lib/libc/string/strnlen.c
@@ -0,0 +1,34 @@
+/* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+
+/*
+ * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <string.h>
+
+size_t
+strnlen(const char *str, size_t maxlen)
+{
+ const char *cp, *ep;
+ size_t len;
+
+ ep = str + maxlen;
+ for (cp = str; cp < ep && *cp != '\0'; cp++)
+ ;
+
+ return (size_t)(cp - str);
+}
diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
index 6e440ac40ce..4d31ad1411c 100644
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.89 2010/02/03 20:49:00 miod Exp $
+# $OpenBSD: Makefile.inc,v 1.90 2010/05/18 22:24:55 tedu Exp $
# $NetBSD: Makefile.inc,v 1.35 1995/10/16 23:49:07 jtc Exp $
# @(#)Makefile.inc 8.1 (Berkeley) 6/17/93
@@ -23,6 +23,9 @@ DPSRCS+= Lint_Ovfork.c Lint_brk.c Lint_exect.c Lint_fork.c \
Lint_setjmp.c Lint_longjmp.c \
Lint_sigsetjmp.c Lint_siglongjmp.c
+# glue to offer userland wrappers for some syscalls
+SRCS+= posix_madvise.c
+
# glue to provide compatibility between GCC 1.X and 2.X and for compat
# with old syscall interfaces.
SRCS+= ftruncate.c lseek.c mquery.c mmap.c ptrace.c semctl.c truncate.c \
@@ -264,6 +267,7 @@ MLINKS+=gettimeofday.2 settimeofday.2
MLINKS+=getuid.2 geteuid.2
MLINKS+=kqueue.2 kevent.2 kqueue.2 EV_SET.2
MLINKS+=intro.2 errno.2
+MLINKS+=madvise.2 posix_madvise.2
MLINKS+=mlock.2 munlock.2
MLINKS+=mlockall.2 munlockall.2
MLINKS+=mount.2 unmount.2
diff --git a/lib/libc/sys/madvise.2 b/lib/libc/sys/madvise.2
index 7ac374636d0..0bd769da626 100644
--- a/lib/libc/sys/madvise.2
+++ b/lib/libc/sys/madvise.2
@@ -1,4 +1,4 @@
-.\" $OpenBSD: madvise.2,v 1.14 2007/05/31 19:19:32 jmc Exp $
+.\" $OpenBSD: madvise.2,v 1.15 2010/05/18 22:24:55 tedu Exp $
.\" $NetBSD: madvise.2,v 1.7 1995/12/27 21:17:02 jtc Exp $
.\"
.\" Copyright (c) 1991, 1993
@@ -30,23 +30,29 @@
.\"
.\" @(#)madvise.2 8.1 (Berkeley) 6/9/93
.\"
-.Dd $Mdocdate: May 31 2007 $
+.Dd $Mdocdate: May 18 2010 $
.Dt MADVISE 2
.Os
.Sh NAME
-.Nm madvise
+.Nm madvise ,
+.Nm posix_madvise
.Nd give advice about use of memory
.Sh SYNOPSIS
-.Fd #include <sys/types.h>
.Fd #include <sys/mman.h>
.Ft int
.Fn madvise "void *addr" "size_t len" "int behav"
+.Ft int
+.Fn posix_madvise "void *addr" "size_t len" "int behav"
.Sh DESCRIPTION
The
.Fn madvise
system call
allows a process that has knowledge of its memory behavior
to describe it to the system.
+The
+.Fn posix_madvise
+interface is identical and is provided for standards conformance.
+.Pp
The possible behaviors are:
.Bl -tag -width MADV_SEQUENTIAL
.It Dv MADV_NORMAL
@@ -64,6 +70,15 @@ Ensure that resources are reserved.
.It Dv MADV_FREE
The pages don't contain any useful data and can be recycled.
.El
+.Pp
+Portable programs that call the
+.Fn posix_madvise
+interface should use the aliases
+.Dv POSIX_MADV_NORMAL , POSIX_MADV_RANDOM ,
+.Dv POSIX_MADV_SEQUENTIAL , POSIX_MADV_WILLNEED ,
+and
+.Dv POSIX_MADV_DONTNEED
+rather than the flags described above.
.Sh RETURN VALUES
Upon successful completion,
a value of 0 is returned.
@@ -75,9 +90,21 @@ is set to indicate the error.
.Xr minherit 2 ,
.Xr mprotect 2 ,
.Xr msync 2 ,
-.Xr munmap 2
+.Xr munmap 2 ,
+.Xr posix_madvise 2
+.Sh STANDARDS
+The
+.Fn posix_madvise
+system call is expected to conform to the
+.St -p1003.1-2001
+standard.
.Sh HISTORY
The
.Nm madvise
function first appeared in
.Bx 4.4 .
+The
+.Nm posix_madvise
+function first appeared in
+.Ox 4.8 .
+
diff --git a/lib/libc/sys/posix_madvise.c b/lib/libc/sys/posix_madvise.c
new file mode 100644
index 00000000000..4952c5cc77a
--- /dev/null
+++ b/lib/libc/sys/posix_madvise.c
@@ -0,0 +1,11 @@
+/* $OpenBSD: posix_madvise.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
+/*
+ * Ted Unangst wrote this file and placed it into the public domain.
+ */
+#include <sys/mman.h>
+
+int
+posix_madvise(void *addr, size_t len, int behav)
+{
+ return (_thread_sys_madvise(addr, len, behav));
+}
diff --git a/sys/sys/mman.h b/sys/sys/mman.h
index ffd4de00264..ebebdc68851 100644
--- a/sys/sys/mman.h
+++ b/sys/sys/mman.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: mman.h,v 1.18 2003/07/21 22:52:19 tedu Exp $ */
+/* $OpenBSD: mman.h,v 1.19 2010/05/18 22:24:54 tedu Exp $ */
/* $NetBSD: mman.h,v 1.11 1995/03/26 20:24:23 jtc Exp $ */
/*-
@@ -32,6 +32,10 @@
* @(#)mman.h 8.1 (Berkeley) 6/2/93
*/
+#ifndef _KERNEL
+#include <sys/cdefs.h>
+#endif
+
/*
* Protections are chosen from these bits, or-ed together
*/
@@ -72,15 +76,28 @@
#define MAP_FLAGMASK 0x17f7
/*
- * Advice to madvise
+ * POSIX memory advisory values.
+ * Note: keep consistent with the original defintions below.
+ */
+#define POSIX_MADV_NORMAL 0 /* no further special treatment */
+#define POSIX_MADV_RANDOM 1 /* expect random page references */
+#define POSIX_MADV_SEQUENTIAL 2 /* expect sequential page references */
+#define POSIX_MADV_WILLNEED 3 /* will need these pages */
+#define POSIX_MADV_DONTNEED 4 /* don't need these pages */
+
+#if __BSD_VISIBLE
+/*
+ * Original advice values, equivalent to POSIX defintions,
+ * and few implementation-specific ones.
*/
-#define MADV_NORMAL 0 /* no further special treatment */
-#define MADV_RANDOM 1 /* expect random page references */
-#define MADV_SEQUENTIAL 2 /* expect sequential page references */
-#define MADV_WILLNEED 3 /* will need these pages */
-#define MADV_DONTNEED 4 /* dont need these pages */
-#define MADV_SPACEAVAIL 5 /* insure that resources are reserved */
-#define MADV_FREE 6 /* pages are empty, free them */
+#define MADV_NORMAL POSIX_MADV_NORMAL
+#define MADV_RANDOM POSIX_MADV_RANDOM
+#define MADV_SEQUENTIAL POSIX_MADV_SEQUENTIAL
+#define MADV_WILLNEED POSIX_MADV_WILLNEED
+#define MADV_DONTNEED POSIX_MADV_DONTNEED
+#define MADV_SPACEAVAIL 5 /* insure that resources are reserved */
+#define MADV_FREE 6 /* pages are empty, free them */
+#endif
/*
* Flags to minherit
@@ -105,8 +122,17 @@
#define MCL_FUTURE 0x02 /* lock all pages mapped in the future */
#ifndef _KERNEL
+#include <sys/_types.h>
-#include <sys/cdefs.h>
+#ifndef _SIZE_T_DEFINED_
+#define _SIZE_T_DEFINED_
+typedef __size_t size_t;
+#endif
+
+#ifndef _OFF_T_DEFINED_
+#define _OFF_T_DEFINED_
+typedef __off_t off_t;
+#endif
__BEGIN_DECLS
/* Some of these int's should probably be size_t's */
@@ -118,10 +144,13 @@ int mlock(const void *, size_t);
int munlock(const void *, size_t);
int mlockall(int);
int munlockall(void);
+#if __BSD_VISIBLE
int madvise(void *, size_t, int);
int mincore(void *, size_t, char *);
int minherit(void *, size_t, int);
void * mquery(void *, size_t, int, int, int, off_t);
+#endif
+int posix_madvise(void *, size_t, int);
__END_DECLS
#endif /* !_KERNEL */