From 43326ef72b468806b612b12fcff91e0dad3bbecf Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Thu, 12 Jan 2006 09:29:38 +0000 Subject: [PATCH] Use posix_memalign() in valloc() rather than making assumptions about the alignment of malloc()ed memory. Approved by: markm (mentor) --- lib/libc/gen/valloc.3 | 28 ++++++++++++++-------------- lib/libc/gen/valloc.c | 12 ++++++------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/libc/gen/valloc.3 b/lib/libc/gen/valloc.3 index fceec478ced0..d9d60c1ba43a 100644 --- a/lib/libc/gen/valloc.3 +++ b/lib/libc/gen/valloc.3 @@ -32,7 +32,7 @@ .\" @(#)valloc.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd June 4, 1993 +.Dd September 13, 2005 .Dt VALLOC 3 .Os .Sh NAME @@ -48,9 +48,9 @@ .Bf -symbolic The .Fn valloc -function is obsoleted by the current version of -.Xr malloc 3 , -which aligns page-sized and larger allocations. +function is obsoleted by +.Xr posix_memalign 3 , +which can be used to request page-aligned allocations. .Ef .Pp The @@ -59,23 +59,23 @@ function allocates .Fa size bytes aligned on a page boundary. -It is implemented by calling -.Xr malloc 3 -with a slightly larger request, saving the true beginning of the block -allocated, and returning a properly aligned pointer. .Sh RETURN VALUES The .Fn valloc function returns a pointer to the allocated space if successful; otherwise -a null pointer is returned +a null pointer is returned. +.Sh SEE ALSO +.Xr posix_memalign 3 .Sh HISTORY The .Fn valloc function appeared in .Bx 3.0 . -.Sh BUGS -A -.Fn vfree -function -has not been implemented. +.Pp +The +.Fn valloc +function correctly allocated memory that could be deallocated via +.Fn free +in +.Bx 7.0 . diff --git a/lib/libc/gen/valloc.c b/lib/libc/gen/valloc.c index 456f31d21cfd..9d888d3a2442 100644 --- a/lib/libc/gen/valloc.c +++ b/lib/libc/gen/valloc.c @@ -41,12 +41,12 @@ __FBSDID("$FreeBSD$"); #include void * -valloc(i) - size_t i; +valloc(size_t i) { - long valsiz = getpagesize(), j; - void *cp = malloc(i + (valsiz-1)); + void *ret; - j = ((long)cp + (valsiz-1)) &~ (valsiz-1); - return ((void *)j); + if (posix_memalign(&ret, getpagesize(), i) != 0) + ret = NULL; + + return ret; }