summaryrefslogtreecommitdiff
path: root/lib/posix_memalign.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2020-11-04 02:19:08 +0100
committerBruno Haible <bruno@clisp.org>2020-11-04 02:19:08 +0100
commit419322ec4c4c6ff1df518378d86162b7a53bf2c0 (patch)
tree9de0a3e360ff657b917881291b6ffabad0dd14bf /lib/posix_memalign.c
parent8c989f769f07699d46ba7213ded965afb0e1be35 (diff)
downloadgnulib-419322ec4c4c6ff1df518378d86162b7a53bf2c0.tar.gz
posix_memalign: New module.
* lib/stdlib.in.h (posix_memalign): New declaration. * lib/posix_memalign.c: New file. * m4/posix_memalign.m4: New file. * m4/stdlib_h.m4 (gl_STDLIB_H): Test whether posix_memalign is declared. (gl_STDLIB_H_DEFAULTS): Initialize GNULIB_POSIX_MEMALIGN, HAVE_POSIX_MEMALIGN, REPLACE_POSIX_MEMALIGN. * modules/stdlib (Makefile.am): Substitute GNULIB_POSIX_MEMALIGN, HAVE_POSIX_MEMALIGN, REPLACE_POSIX_MEMALIGN. * modules/posix_memalign: New file. * tests/test-stdlib-c++.cc (posix_memalign): Check signature. * doc/posix-functions/posix_memalign.texi: Mention the new module and the OpenBSD bug.
Diffstat (limited to 'lib/posix_memalign.c')
-rw-r--r--lib/posix_memalign.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/posix_memalign.c b/lib/posix_memalign.c
new file mode 100644
index 0000000000..ef8576820b
--- /dev/null
+++ b/lib/posix_memalign.c
@@ -0,0 +1,35 @@
+/* A posix_memalign() function that works around platform bugs.
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <stdlib.h>
+
+#include <errno.h>
+
+int
+posix_memalign (void **memptr, size_t alignment, size_t size)
+#undef posix_memalign
+{
+ /* Round up SIZE to the next multiple of ALIGNMENT, namely
+ (SIZE + ALIGNMENT - 1) & ~(ALIGNMENT - 1). */
+ size += alignment - 1;
+ if (size >= alignment - 1) /* no overflow? */
+ return posix_memalign (memptr, alignment, size & ~(size_t)(alignment - 1));
+ else
+ return ENOMEM;
+}