Skip to content

Commit

Permalink
Fix alignment with custom memory allocator in tre.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@87512 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed Jan 2, 2025
1 parent f165c3c commit ab69f59
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/extra/tre/tre-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/extra/tre/tre-match-approx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
8 changes: 4 additions & 4 deletions src/extra/tre/tre-match-parallel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/extra/tre/tre-mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ab69f59

Please sign in to comment.