bin/85090: [patch] add memalign() and posix_memalign() functions
Sergey Matveychuk
sem at FreeBSD.org
Thu Aug 18 14:30:27 GMT 2005
>Number: 85090
>Category: bin
>Synopsis: [patch] add memalign() and posix_memalign() functions
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: change-request
>Submitter-Id: current-users
>Arrival-Date: Thu Aug 18 14:30:20 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator: Sergey Matveychuk
>Release: FreeBSD 7.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD test.sem-home.ciam.ru 7.0-CURRENT FreeBSD 7.0-CURRENT #34: Sat Aug 13 16:02:55 MSD 2005 root at test.sem-home.ciam.ru:/usr/obj/usr/src/sys/CURRENT i386
>Description:
Add memalign() and posix_memalign() functions.
>How-To-Repeat:
>Fix:
--- memalign.patch begins here ---
Index: include/stdlib.h
===================================================================
RCS file: /home/ncvs/src/include/stdlib.h,v
retrieving revision 1.57
diff -u -r1.57 stdlib.h
--- include/stdlib.h 9 Jan 2005 03:55:12 -0000 1.57
+++ include/stdlib.h 18 Aug 2005 07:05:11 -0000
@@ -98,6 +98,7 @@
int mblen(const char *, size_t);
size_t mbstowcs(wchar_t * __restrict , const char * __restrict, size_t);
int mbtowc(wchar_t * __restrict, const char * __restrict, size_t);
+void *memalign(size_t, size_t);
void qsort(void *, size_t, size_t,
int (*)(const void *, const void *));
int rand(void);
@@ -157,7 +158,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 18 Aug 2005 07:05:11 -0000
@@ -41,5 +41,6 @@
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 memalign.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 18 Aug 2005 07:05:11 -0000
@@ -54,6 +54,10 @@
.Fn realloc "void *ptr" "size_t size"
.Ft void *
.Fn reallocf "void *ptr" "size_t size"
+.Ft void *
+.Fn memalign "size_t alignment" "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 +152,27 @@
for realloc causing memory leaks in libraries.
.Pp
The
+.Fn memalign
+function allocates
+.Fa size
+bytes aligned on a boundary specified by
+.Fa alignment
+which must be a power of two.
+.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
@@ -264,9 +289,10 @@
is flawed.
.Sh RETURN VALUES
The
-.Fn malloc
-and
+.Fn malloc ,
.Fn calloc
+and
+.Fn memalign
functions return a pointer to the allocated memory if successful; otherwise
a
.Dv NULL
@@ -276,6 +302,30 @@
.Er ENOMEM .
.Pp
The
+.Fn memalign
+function returns a
+.Dv NULL
+and sets
+.Va errno
+to
+.Er EINVAL
+if the value of the
+.Fa alignment
+parameter is not a power of two.
+.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 +413,9 @@
If
.Fn malloc ,
.Fn calloc ,
-.Fn realloc
+.Fn realloc ,
+.Fn memalign ,
+.Fn posix_memalign
or
.Fn free
detect an error or warning condition,
@@ -493,8 +545,22 @@
.Fn reallocf
function first appeared in
.Fx 3.0 .
+.Pp
+The
+.Fn memalign
+and
+.Fn posix_memalign
+functions first appeared in
+.Fx 7.0 .
.Sh AUTHORS
.An Poul-Henning Kamp Aq phk at FreeBSD.org
+.Pp
+The
+.Fn memalign
+and
+.Fn posix_memalign
+functions were 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 18 Aug 2005 07:05:11 -0000
@@ -1164,3 +1164,38 @@
return (pubrealloc(ptr, size, " in realloc():"));
}
+#define POWEROF2(x) ((((x)-1)&(x))==0)
+
+void *
+memalign(size_t alignment, size_t size)
+{
+
+ if(!POWEROF2(alignment))
+ {
+ errno = EINVAL;
+ return(NULL);
+ }
+ return (pubrealloc(NULL, (size+alignment-1) & ~(alignment-1),
+ " in memalign():"));
+}
+
+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);
+ }
+}
--- memalign.patch ends here ---
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the freebsd-bugs
mailing list