standards/85090: [patch] add memalign() and posix_memalign()
functions
Sergey Matveychuk
sem at FreeBSD.org
Sun Oct 16 09:30:23 PDT 2005
The following reply was made to PR standards/85090; it has been noted by GNATS.
From: Sergey Matveychuk <sem at FreeBSD.org>
To: bug-followup at FreeBSD.org, sem at FreeBSD.org, stefanf at FreeBSD.org
Cc:
Subject: Re: standards/85090: [patch] add memalign() and posix_memalign()
functions
Date: Sun, 16 Oct 2005 20:22:12 +0400
This is a multi-part message in MIME format.
--------------020009040200090906070000
Content-Type: text/plain; charset=KOI8-R; format=flowed
Content-Transfer-Encoding: 7bit
I've removed memalign() and made some fixes on a man page.
--
Sem.
--------------020009040200090906070000
Content-Type: text/plain;
name="posix_memalign.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="posix_memalign.patch"
Index: include/stdlib.h
===================================================================
RCS file: /home/ncvs/src/include/stdlib.h,v
retrieving revision 1.58
diff -u -r1.58 stdlib.h
--- include/stdlib.h 12 Sep 2005 15:58:15 -0000 1.58
+++ include/stdlib.h 16 Oct 2005 12:43:30 -0000
@@ -157,7 +157,7 @@
* research can be done.
*/
#if __POSIX_VISIBLE /* >= ??? */
-/* int posix_memalign(void **, size_t, size_t); (ADV) */
+int posix_memalign(void **, size_t, size_t); /* (ADV) */
int rand_r(unsigned *); /* (TSF) */
int setenv(const char *, const char *, int);
void unsetenv(const char *);
Index: lib/libc/stdlib/Makefile.inc
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/Makefile.inc,v
retrieving revision 1.48
diff -u -r1.48 Makefile.inc
--- lib/libc/stdlib/Makefile.inc 12 May 2004 08:13:40 -0000 1.48
+++ lib/libc/stdlib/Makefile.inc 16 Oct 2005 12:43:30 -0000
@@ -41,5 +41,5 @@
MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
MLINKS+=malloc.3 calloc.3 malloc.3 free.3 malloc.3 malloc.conf.5 \
- malloc.3 realloc.3 malloc.3 reallocf.3
+ malloc.3 realloc.3 malloc.3 reallocf.3 malloc.3 posix_memalign.3
MLINKS+=tsearch.3 tdelete.3 tsearch.3 tfind.3 tsearch.3 twalk.3
Index: lib/libc/stdlib/malloc.3
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/malloc.3,v
retrieving revision 1.63
diff -u -r1.63 malloc.3
--- lib/libc/stdlib/malloc.3 20 Jan 2005 09:17:04 -0000 1.63
+++ lib/libc/stdlib/malloc.3 16 Oct 2005 12:43:30 -0000
@@ -36,11 +36,11 @@
.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.63 2005/01/20 09:17:04 ru Exp $
.\"
-.Dd August 19, 2004
+.Dd October 16, 2005
.Dt MALLOC 3
.Os
.Sh NAME
-.Nm malloc , calloc , realloc , free , reallocf
+.Nm malloc , calloc , realloc , free , reallocf , posix_memalign
.Nd general purpose memory allocation functions
.Sh LIBRARY
.Lb libc
@@ -54,6 +54,8 @@
.Fn realloc "void *ptr" "size_t size"
.Ft void *
.Fn reallocf "void *ptr" "size_t size"
+.Ft int
+.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
.Ft void
.Fn free "void *ptr"
.Ft const char *
@@ -148,6 +150,19 @@
for realloc causing memory leaks in libraries.
.Pp
The
+.Fn posix_memalign
+function allocates
+.Fa size
+bytes aligned on a boundary specified by
+.Fa alignment
+and places the address of the allocated memory in
+.Fa ptr .
+The value of
+.Fa alignment
+must be a power of two and a multiple of
+.Fn sizeof "void *" .
+.Pp
+The
.Fn free
function causes the allocated memory referenced by
.Fa ptr
@@ -276,6 +291,18 @@
.Er ENOMEM .
.Pp
The
+.Fn posix_memalign
+function returns zero on success,
+.Er EINVAL
+if the
+.Fa alignment
+parameter was not a power of two, or was not a multiple of
+.Fn sizeof "void *" .
+Note that
+.Va errno
+is not set.
+.Pp
+The
.Fn realloc
and
.Fn reallocf
@@ -363,7 +390,8 @@
If
.Fn malloc ,
.Fn calloc ,
-.Fn realloc
+.Fn realloc ,
+.Fn posix_memalign
or
.Fn free
detect an error or warning condition,
@@ -480,6 +508,11 @@
.Fn free
functions conform to
.St -isoC .
+.Pp
+The
+.Fn posix_memalign
+function conforms to
+.St -p1003.1-2001 .
.Sh HISTORY
The present allocation implementation started out as a file system for a
drum attached to a 20bit binary challenged computer which was built
@@ -493,8 +526,18 @@
.Fn reallocf
function first appeared in
.Fx 3.0 .
+.Pp
+The
+.Fn posix_memalign
+function first appeared in
+.Fx 7.0 .
.Sh AUTHORS
.An Poul-Henning Kamp Aq phk at FreeBSD.org
+.Pp
+The
+.Fn posix_memalign
+function was added by
+.An Sergey Matveychuk Aq sem at FreeBSD.org
.Sh BUGS
The messages printed in case of problems provide no detail about the
actual values.
Index: lib/libc/stdlib/malloc.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.90
diff -u -r1.90 malloc.c
--- lib/libc/stdlib/malloc.c 27 Feb 2005 17:16:16 -0000 1.90
+++ lib/libc/stdlib/malloc.c 16 Oct 2005 12:43:30 -0000
@@ -1164,3 +1164,25 @@
return (pubrealloc(ptr, size, " in realloc():"));
}
+#define POWEROF2(x) ((((x)-1)&(x))==0)
+
+int
+posix_memalign(void **ptr, size_t alignment, size_t size)
+{
+ void *p1;
+ int err;
+
+ if (alignment % sizeof(void *) || !POWEROF2(alignment))
+ return(EINVAL);
+
+ p1 = pubrealloc(NULL, (size+alignment-1) & ~(alignment-1),
+ " in posix_memalign():");
+ if(p1) {
+ *ptr = p1;
+ return (0);
+ } else {
+ err = errno;
+ errno = 0;
+ return (err);
+ }
+}
--------------020009040200090906070000--
More information about the freebsd-standards
mailing list