1 # malloc-align.m4 serial 1
2 dnl Copyright (C) 2020 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 dnl Defines a C macro MALLOC_ALIGNMENT, whose value is a numeric constant,
8 dnl a power of 2, with the property that
9 dnl (uintptr_t) malloc (N)
10 dnl is always guaranteed to be a multiple of MALLOC_ALIGNMENT.
12 AC_DEFUN([gl_MALLOC_ALIGNMENT],
14 AC_REQUIRE([AC_CANONICAL_HOST])
15 AC_CACHE_CHECK([for the malloc() alignment],
16 [gl_cv_malloc_alignment],
17 [gl_cv_malloc_alignment=
22 #if defined _WIN32 && !defined __CYGWIN__
23 # include <inttypes.h>
24 /* uintptr_t is equivalent to 'unsigned long long' if _WIN64,
25 or to 'unsigned long' otherwise. */
28 # define uintptr_t unsigned long
31 [[FILE *fp = fopen ("conftest.out", "w");
36 bits |= (uintptr_t) malloc (1);
37 bits |= (uintptr_t) malloc (1);
38 bits |= (uintptr_t) malloc (1);
39 bits |= (uintptr_t) malloc (2);
40 bits |= (uintptr_t) malloc (2);
41 bits |= (uintptr_t) malloc (2);
42 bits |= (uintptr_t) malloc (3);
43 bits |= (uintptr_t) malloc (3);
44 bits |= (uintptr_t) malloc (3);
45 bits |= (uintptr_t) malloc (5);
46 bits |= (uintptr_t) malloc (8);
47 bits |= (uintptr_t) malloc (8);
48 bits |= (uintptr_t) malloc (13);
49 bits |= (uintptr_t) malloc (13);
50 bits |= (uintptr_t) malloc (19);
51 bits |= (uintptr_t) malloc (19);
52 bits |= (uintptr_t) malloc (28);
53 bits |= (uintptr_t) malloc (28);
54 bits |= (uintptr_t) malloc (37);
55 bits |= (uintptr_t) malloc (37);
56 bits |= (uintptr_t) malloc (73);
57 bits |= (uintptr_t) malloc (73);
58 bits |= (uintptr_t) malloc (117);
59 bits |= (uintptr_t) malloc (117);
60 bits |= (uintptr_t) malloc (351);
61 bits |= (uintptr_t) malloc (351);
62 bits |= (uintptr_t) malloc (914);
63 bits |= (uintptr_t) malloc (914);
64 bits |= (uintptr_t) malloc (1712);
65 bits |= (uintptr_t) malloc (1712);
66 bits |= (uintptr_t) malloc (4021);
67 bits |= (uintptr_t) malloc (4021);
68 bits |= (uintptr_t) malloc (7641);
69 bits |= (uintptr_t) malloc (7641);
70 bits |= (uintptr_t) malloc (17027);
71 bits |= (uintptr_t) malloc (17027);
72 bits |= (uintptr_t) malloc (81231);
73 bits |= (uintptr_t) malloc (81231);
74 fprintf (fp, "%u\n", (unsigned int) (((bits ^ (bits - 1)) + 1) >> 1));
81 [gl_cv_malloc_alignment=`cat conftest.out`],
82 [gl_cv_malloc_alignment="unknown"],
83 [dnl When cross-compiling, guess a value. Note that it's OK to return
84 dnl a smaller value (e.g. 4 instead of 8 or 16).
85 gl_cv_malloc_alignment="unknown"
89 dnl - It's 8 on most 32-bit platforms, except 16 on x86_64-x32 and
90 dnl (with newer versions of glibc) on i386 and powerpc. 8 is a
92 dnl - It's 16 on all 64-bit platforms.
93 dnl On Windows: It's 8 on 32-bit Windows, 16 on 64-bit Windows.
94 for nn in 4 8 16 32; do
97 #define MALLOC_ALIGN (2 * sizeof (void *))
98 int test [MALLOC_ALIGN <= $nn ? 1 : -1];
101 [gl_cv_malloc_alignment="guessing $nn"
108 dnl If we don't know, assume the worst.
109 dnl This minimum is e.g. reached on NetBSD/i386 and NetBSD/sparc.
113 #define MALLOC_ALIGN (sizeof (void *))
114 int test [MALLOC_ALIGN <= $nn ? 1 : -1];
117 [gl_cv_malloc_alignment="guessing $nn"
126 case "$gl_cv_malloc_alignment" in
128 dnl Assume the worst.
132 value=`echo "$gl_cv_malloc_alignment" | sed -e 's/guessing //'`
135 value="$gl_cv_malloc_alignment"
138 AC_DEFINE_UNQUOTED([MALLOC_ALIGNMENT], [$value],
139 [Define to the guaranteed alignment of malloc() return values.])