Skip to content

Commit ca3b248

Browse files
authored
Refactor ZEND_MM alignment check (#14720)
- Check added into a separate M4 macro - AC_CACHE_CHECK used for running the test program with cache variable for optional edge-case overrides - Help texts updated - If check fails for some reason, the configure step emits error as also done in the Zend C code - Cross-compilation values updated with type casts as done in the current conftest file output
1 parent bec2532 commit ca3b248

File tree

1 file changed

+65
-53
lines changed

1 file changed

+65
-53
lines changed

Zend/Zend.m4

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -197,59 +197,7 @@ if test "$ZEND_ZTS" = "yes"; then
197197
CFLAGS="$CFLAGS -DZTS"
198198
fi
199199
200-
dnl Test and set the alignment define for ZEND_MM. This also does the
201-
dnl logarithmic test for ZEND_MM.
202-
AC_MSG_CHECKING(for MM alignment and log values)
203-
204-
AC_RUN_IFELSE([AC_LANG_SOURCE([[
205-
#include <stdio.h>
206-
#include <stdlib.h>
207-
208-
typedef union _mm_align_test {
209-
void *ptr;
210-
double dbl;
211-
long lng;
212-
} mm_align_test;
213-
214-
#if (defined (__GNUC__) && __GNUC__ >= 2)
215-
#define ZEND_MM_ALIGNMENT (__alignof__ (mm_align_test))
216-
#else
217-
#define ZEND_MM_ALIGNMENT (sizeof(mm_align_test))
218-
#endif
219-
220-
int main(void)
221-
{
222-
size_t i = ZEND_MM_ALIGNMENT;
223-
int zeros = 0;
224-
FILE *fp;
225-
226-
while (i & ~0x1) {
227-
zeros++;
228-
i = i >> 1;
229-
}
230-
231-
fp = fopen("conftest.zend", "w");
232-
fprintf(fp, "(size_t)%zu (size_t)%d %d\n", ZEND_MM_ALIGNMENT, zeros, ZEND_MM_ALIGNMENT < 4);
233-
fclose(fp);
234-
235-
return 0;
236-
}
237-
]])], [
238-
LIBZEND_MM_ALIGN=`cat conftest.zend | cut -d ' ' -f 1`
239-
LIBZEND_MM_ALIGN_LOG2=`cat conftest.zend | cut -d ' ' -f 2`
240-
LIBZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT=`cat conftest.zend | cut -d ' ' -f 3`
241-
AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT, $LIBZEND_MM_ALIGN, [ ])
242-
AC_DEFINE_UNQUOTED(ZEND_MM_ALIGNMENT_LOG2, $LIBZEND_MM_ALIGN_LOG2, [ ])
243-
AC_DEFINE_UNQUOTED(ZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT, $LIBZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT, [ ])
244-
], [], [
245-
dnl Cross compilation needs something here.
246-
AC_DEFINE(ZEND_MM_ALIGNMENT, 8, [ ])
247-
AC_DEFINE(ZEND_MM_ALIGNMENT_LOG2, 3, [ ])
248-
AC_DEFINE(ZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT, 0, [ ])
249-
])
250-
251-
AC_MSG_RESULT(done)
252-
200+
ZEND_CHECK_ALIGNMENT
253201
ZEND_CHECK_SIGNALS
254202
255203
dnl Don't enable Zend Max Execution Timers by default until PHP 8.3 to not break the ABI
@@ -392,6 +340,70 @@ AS_VAR_IF([php_cv_have___cpuid_count], [yes],
392340
[Define to 1 if '__cpuid_count' is available.])])
393341
])
394342

343+
dnl
344+
dnl ZEND_CHECK_ALIGNMENT
345+
dnl
346+
dnl Test and set the alignment defines for the Zend memory manager (ZEND_MM).
347+
dnl This also does the logarithmic test.
348+
dnl
349+
AC_DEFUN([ZEND_CHECK_ALIGNMENT],
350+
[AC_CACHE_CHECK([for Zend memory manager alignment and log values],
351+
[php_cv_align_mm],
352+
[AC_RUN_IFELSE([AC_LANG_SOURCE([
353+
#include <stdio.h>
354+
#include <stdlib.h>
355+
356+
typedef union _mm_align_test {
357+
void *ptr;
358+
double dbl;
359+
long lng;
360+
} mm_align_test;
361+
362+
#if (defined (__GNUC__) && __GNUC__ >= 2)
363+
#define ZEND_MM_ALIGNMENT (__alignof__ (mm_align_test))
364+
#else
365+
#define ZEND_MM_ALIGNMENT (sizeof(mm_align_test))
366+
#endif
367+
368+
int main(void)
369+
{
370+
size_t i = ZEND_MM_ALIGNMENT;
371+
int zeros = 0;
372+
FILE *fp;
373+
374+
while (i & ~0x1) {
375+
zeros++;
376+
i = i >> 1;
377+
}
378+
379+
fp = fopen("conftest.zend", "w");
380+
fprintf(fp, "(size_t)%zu (size_t)%d %d\n",
381+
ZEND_MM_ALIGNMENT, zeros, ZEND_MM_ALIGNMENT < 4);
382+
fclose(fp);
383+
384+
return 0;
385+
}
386+
])],
387+
[php_cv_align_mm=$(cat conftest.zend)],
388+
[php_cv_align_mm=failed],
389+
[php_cv_align_mm="(size_t)8 (size_t)3 0"])])
390+
AS_VAR_IF([php_cv_align_mm], [failed],
391+
[AC_MSG_ERROR([ZEND_MM alignment defines failed. Please, check config.log])],
392+
[zend_mm_alignment=$(echo $php_cv_align_mm | cut -d ' ' -f 1)
393+
zend_mm_alignment_log2=$(echo $php_cv_align_mm | cut -d ' ' -f 2)
394+
zend_mm_8byte_realign=$(echo $php_cv_align_mm | cut -d ' ' -f 3)
395+
AC_DEFINE_UNQUOTED([ZEND_MM_ALIGNMENT],
396+
[$zend_mm_alignment],
397+
[Number of bytes for the ZEND_MM alignment.])
398+
AC_DEFINE_UNQUOTED([ZEND_MM_ALIGNMENT_LOG2],
399+
[$zend_mm_alignment_log2],
400+
[Number of bytes for the logarithmic ZEND_MM alignment.])
401+
AC_DEFINE_UNQUOTED([ZEND_MM_NEED_EIGHT_BYTE_REALIGNMENT],
402+
[$zend_mm_8byte_realign],
403+
[Define to 1 if ZEND_MM needs 8-byte realignment, and to 0 if not.])
404+
])
405+
])
406+
395407
dnl
396408
dnl ZEND_CHECK_SIGNALS
397409
dnl

0 commit comments

Comments
 (0)