xref: /netbsd-src/lib/libc/stdlib/posix_memalign.3 (revision 9ff64676db55be87e5043c033d398be81be2e147)
1*9ff64676Swiz.\"	$NetBSD: posix_memalign.3,v 1.8 2025/01/11 11:41:08 wiz Exp $
295182011Sad.\"
395182011Sad.\" Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
495182011Sad.\" All rights reserved.
595182011Sad.\"
695182011Sad.\" Redistribution and use in source and binary forms, with or without
795182011Sad.\" modification, are permitted provided that the following conditions
895182011Sad.\" are met:
995182011Sad.\" 1. Redistributions of source code must retain the above copyright
1095182011Sad.\"    notice(s), this list of conditions and the following disclaimer as
1195182011Sad.\"    the first lines of this file unmodified other than the possible
1295182011Sad.\"    addition of one or more copyright notices.
1395182011Sad.\" 2. Redistributions in binary form must reproduce the above copyright
1495182011Sad.\"    notice(s), this list of conditions and the following disclaimer in
1595182011Sad.\"    the documentation and/or other materials provided with the
1695182011Sad.\"    distribution.
1795182011Sad.\"
1895182011Sad.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1995182011Sad.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2095182011Sad.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2195182011Sad.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
2295182011Sad.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2395182011Sad.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2495182011Sad.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
2595182011Sad.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2695182011Sad.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
2795182011Sad.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2895182011Sad.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2995182011Sad.\"
3095182011Sad.\" FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp
3195182011Sad.\"
328d702a64Smaya.Dd July 27, 2018
3395182011Sad.Dt POSIX_MEMALIGN 3
3495182011Sad.Os
3595182011Sad.Sh NAME
36b16a5c9dSnros.Nm posix_memalign , aligned_alloc
3795182011Sad.Nd aligned memory allocation
3895182011Sad.Sh LIBRARY
3995182011Sad.Lb libc
4095182011Sad.Sh SYNOPSIS
4195182011Sad.In stdlib.h
4295182011Sad.Ft int
4395182011Sad.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
44b16a5c9dSnros.Ft void *
45b16a5c9dSnros.Fn aligned_alloc "size_t alignment" "size_t size"
4695182011Sad.Sh DESCRIPTION
4795182011SadThe
4895182011Sad.Fn posix_memalign
4995182011Sadfunction allocates
5095182011Sad.Fa size
5195182011Sadbytes of memory such that the allocation's base address is an even multiple of
5295182011Sad.Fa alignment ,
5395182011Sadand returns the allocation in the value pointed to by
5495182011Sad.Fa ptr .
5595182011SadThe requested
5695182011Sad.Fa alignment
5795182011Sadmust be a power of 2 at least as large as
5895182011Sad.Fn sizeof "void *" .
5995182011Sad.Pp
60b16a5c9dSnrosThe
61b16a5c9dSnros.Fn aligned_alloc
62b16a5c9dSnrosfunction allocates
63b16a5c9dSnros.Fa size
64b16a5c9dSnrosbytes of memory such that the allocation's base address is an even multiple of
65b16a5c9dSnros.Fa alignment .
66b16a5c9dSnrosThe requested
67b16a5c9dSnros.Fa alignment
688d702a64Smayamust be a power of 2.
69b16a5c9dSnros.Pp
7095182011SadMemory that is allocated via
7195182011Sad.Fn posix_memalign
72b16a5c9dSnrosor
73b16a5c9dSnros.Fn aligned_alloc
7495182011Sadcan be used as an argument in subsequent calls to
7595182011Sad.Xr realloc 3
7695182011Sadand
7795182011Sad.Xr free 3 .
7895182011Sad.Sh RETURN VALUES
7995182011SadThe
8095182011Sad.Fn posix_memalign
8195182011Sadfunction returns the value 0 if successful; otherwise it returns an error value.
82b16a5c9dSnros.Pp
83b16a5c9dSnrosThe
84b16a5c9dSnros.Fn aligned_alloc
85b16a5c9dSnrosfunction returns a pointer to the allocated memory if successful; on failure it
86dec41c2eSwizreturns
87dec41c2eSwiz.Dv NULL
88dec41c2eSwizand sets
89b16a5c9dSnros.Fa errno
90b16a5c9dSnrosto indicate the error.
9195182011Sad.Sh ERRORS
9295182011SadThe
9395182011Sad.Fn posix_memalign
94b16a5c9dSnrosand
95b16a5c9dSnros.Fn aligned_alloc
96b16a5c9dSnrosfunctions will fail if:
9795182011Sad.Bl -tag -width Er
9895182011Sad.It Bq Er EINVAL
9995182011SadThe
10095182011Sad.Fa alignment
101b16a5c9dSnrosparameter is not a power of 2.
10295182011Sad.It Bq Er ENOMEM
10395182011SadMemory allocation error.
10495182011Sad.El
105b16a5c9dSnros.Pp
106b16a5c9dSnrosThe
107b16a5c9dSnros.Fn posix_memalign
108b16a5c9dSnrosfunction will also fail if
109b16a5c9dSnros.Bl -tag -width Er
110b16a5c9dSnros.It Bq Er EINVAL
111b16a5c9dSnrosThe
112b16a5c9dSnros.Fa alignment
113b16a5c9dSnrosparameter is not at least as large as
114b16a5c9dSnros.Fn sizeof "void *" .
115b16a5c9dSnros.El
11695182011Sad.Sh SEE ALSO
11795182011Sad.Xr free 3 ,
11895182011Sad.Xr malloc 3 ,
11995182011Sad.Xr realloc 3 ,
12095182011Sad.Xr valloc 3
12195182011Sad.Sh STANDARDS
12295182011SadThe
12395182011Sad.Fn posix_memalign
12495182011Sadfunction conforms to
12595182011Sad.St -p1003.1-2001 .
126b16a5c9dSnrosThe
127b16a5c9dSnros.Fn aligned_alloc
128b16a5c9dSnrosfunction conforms to
129*9ff64676Swiz.St -isoC-2011 .
130b91e8b8eSmaya.Sh HISTORY
131b91e8b8eSmaya.St -isoC-2011
132b91e8b8eSmayarequired size to be an integer multiple of alignment.
133b91e8b8eSmayaThis requirement was removed in later standards.
134