From ab69f5944561b3f7e287e62e5aa65e5eabfb7436 Mon Sep 17 00:00:00 2001 From: kalibera Date: Thu, 2 Jan 2025 16:03:29 +0000 Subject: [PATCH] Fix alignment with custom memory allocator in tre. git-svn-id: https://svn.r-project.org/R/trunk@87512 00db46b3-68df-0310-9c12-caf00c1e9a41 --- src/extra/tre/tre-internal.h | 9 +++++++++ src/extra/tre/tre-match-approx.c | 6 +++--- src/extra/tre/tre-match-parallel.c | 8 ++++---- src/extra/tre/tre-mem.c | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/extra/tre/tre-internal.h b/src/extra/tre/tre-internal.h index 5cb37f77ffc..407aabf927d 100644 --- a/src/extra/tre/tre-internal.h +++ b/src/extra/tre/tre-internal.h @@ -155,6 +155,15 @@ typedef enum { STR_WIDE, STR_BYTE, STR_MBS, STR_USER } tre_str_type_t; ? (sizeof(type) - (((size_t)ptr) % sizeof(type))) \ : 0) +/* R addition, only "long" has been used before for alignment of allocated + data. With C11, one could use max_align_t. */ +typedef union { + void *ptr; + void (*funptr)(void); + long long ll; + double dbl; +} anytype; + #undef MAX #undef MIN #define MAX(a, b) (((a) >= (b)) ? (a) : (b)) diff --git a/src/extra/tre/tre-match-approx.c b/src/extra/tre/tre-match-approx.c index 58858e6838b..0276509009a 100644 --- a/src/extra/tre/tre-match-approx.c +++ b/src/extra/tre/tre-match-approx.c @@ -294,17 +294,17 @@ tre_tnfa_run_approx(const tre_tnfa_t *tnfa, const void *string, int len, /* Allocate `tmp_tags' from `buf'. */ tmp_tags = (void *)buf; buf_cursor = buf + tag_bytes; - buf_cursor += ALIGN(buf_cursor, long); + buf_cursor += ALIGN(buf_cursor, anytype); /* Allocate `reach' from `buf'. */ reach = (void *)buf_cursor; buf_cursor += reach_bytes; - buf_cursor += ALIGN(buf_cursor, long); + buf_cursor += ALIGN(buf_cursor, anytype); /* Allocate `reach_next' from `buf'. */ reach_next = (void *)buf_cursor; buf_cursor += reach_bytes; - buf_cursor += ALIGN(buf_cursor, long); + buf_cursor += ALIGN(buf_cursor, anytype); /* Allocate tag arrays for `reach' and `reach_next' from `buf'. */ for (i = 0; i < tnfa->num_states; i++) diff --git a/src/extra/tre/tre-match-parallel.c b/src/extra/tre/tre-match-parallel.c index 289a837623a..acc0b6acfa8 100644 --- a/src/extra/tre/tre-match-parallel.c +++ b/src/extra/tre/tre-match-parallel.c @@ -177,16 +177,16 @@ tre_tnfa_run_parallel(const tre_tnfa_t *tnfa, const void *string, int len, /* Get the various pointers within tmp_buf (properly aligned). */ tmp_tags = (void *)buf; tmp_buf = buf + tbytes; - tmp_buf += ALIGN(tmp_buf, long); + tmp_buf += ALIGN(tmp_buf, anytype); reach_next = (void *)tmp_buf; tmp_buf += rbytes; - tmp_buf += ALIGN(tmp_buf, long); + tmp_buf += ALIGN(tmp_buf, anytype); reach = (void *)tmp_buf; tmp_buf += rbytes; - tmp_buf += ALIGN(tmp_buf, long); + tmp_buf += ALIGN(tmp_buf, anytype); reach_pos = (void *)tmp_buf; tmp_buf += pbytes; - tmp_buf += ALIGN(tmp_buf, long); + tmp_buf += ALIGN(tmp_buf, anytype); for (i = 0; i < tnfa->num_states; i++) { reach[i].tags = (void *)tmp_buf; diff --git a/src/extra/tre/tre-mem.c b/src/extra/tre/tre-mem.c index 67ba6765a61..4b037a691f8 100644 --- a/src/extra/tre/tre-mem.c +++ b/src/extra/tre/tre-mem.c @@ -138,7 +138,7 @@ tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block, } /* Make sure the next pointer will be aligned. */ - size += ALIGN(mem->ptr + size, long); + size += ALIGN(mem->ptr + size, anytype); /* Allocate from current block. */ ptr = mem->ptr;