Skip to content

Commit 933aa97

Browse files
author
kalibera
committed
Fix alignment with custom memory allocator in graphapp.
git-svn-id: https://svn.r-project.org/R/trunk@87509 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 69f52e8 commit 933aa97

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/extra/graphapp/array.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
See the file COPYLIB.TXT for details.
2121
*/
2222

23+
/* R modification: mheader union (instead of simply long size) to force
24+
alignment of allocated data. With C11, one could use max_align_t.
25+
26+
Copyright (C) 2025 The R Core Team
27+
*/
28+
2329
#include <stdlib.h>
2430
#include "internal.h"
2531

@@ -46,32 +52,41 @@ char * memjoin(char *a, char *b);
4652

4753
#define TRACEAR(a)
4854

55+
typedef union {
56+
long size;
57+
58+
void *dummy_ptr;
59+
void (*dummy_funptr)(void);
60+
long long dummy_ll;
61+
double dummy_dbl;
62+
} mheader;
63+
4964
char * memalloc(long size)
5065
{
51-
long *block;
66+
char *block;
5267
char *a;
5368
long i, datasize;
5469
TRACEAR("alloc");
5570
datasize = (((size + 4) >> 2) << 2);
5671
#ifdef COMPILER
5772
#if (COMPILER <= 16)
58-
if ((sizeof(long)+datasize) >= (1<<16))
73+
if ((sizeof(mheader)+datasize) >= (1<<16))
5974
return NULL;
6075
#endif
6176
#endif
62-
block = (long *) malloc(sizeof(long) + datasize);
77+
block = (char *) malloc(sizeof(mheader) + datasize);
6378
if (block == NULL)
6479
return NULL;
65-
block[0] = size;
66-
a = (char *) & block[1];
80+
((mheader *)block)->size = size;
81+
a = block + sizeof(mheader);
6782
for (i=0; i<datasize; i++)
6883
a[i] = '\0';
6984
return a;
7085
}
7186

7287
char * memrealloc(char *a, long new_size)
7388
{
74-
long *block;
89+
char *block;
7590
long i, size, oldsize, newsize;
7691
TRACEAR("realloc");
7792
if (new_size <= 0) {
@@ -84,8 +99,8 @@ char * memrealloc(char *a, long new_size)
8499
size = 0;
85100
}
86101
else {
87-
block = ((long*)a) - 1;
88-
size = block[0];
102+
block = a - sizeof(mheader);
103+
size = ((mheader *)block)->size;
89104
}
90105

91106
oldsize = size ? (((size + 4) >> 2) << 2) : 0;
@@ -94,35 +109,35 @@ char * memrealloc(char *a, long new_size)
94109
if ( newsize != oldsize ) {
95110
#ifdef COMPILER
96111
#if (COMPILER <= 16)
97-
if ((sizeof(long)+newsize) >= (1<<16))
112+
if ((sizeof(mheader)+newsize) >= (1<<16))
98113
return NULL;
99114
#endif
100115
#endif
101-
block = (long *) realloc(block, sizeof(long) + newsize);
116+
block = (char *) realloc(block, sizeof(mheader) + newsize);
102117
if (block == NULL)
103118
return NULL;
104-
a = (char *) & block[1];
119+
a = block + sizeof(mheader);
105120
for (i=oldsize; i<newsize; i++)
106121
a[i] = '\0';
107122
}
108123

109-
block[0] = new_size;
124+
((mheader *)block)->size = new_size;
110125
return a;
111126
}
112127

113128
long memlength(char *a)
114129
{
115-
return (a) ? ((long*)(a)-1)[0] : 0;
130+
return (a) ? ((mheader *)(a - sizeof(mheader)))->size : 0;
116131
}
117132

118133
void memfree(char *a)
119134
{
120-
if (a) free((long*)(a)-1);
135+
if (a) free(a - sizeof(mheader));
121136
}
122137

123138
char * memexpand(char *a, long extra)
124139
{
125-
long *block;
140+
char *block;
126141
long i, size, oldsize, newsize;
127142
TRACEAR("exp");
128143
if (extra == 0)
@@ -133,8 +148,8 @@ char * memexpand(char *a, long extra)
133148
size = 0;
134149
}
135150
else {
136-
block = ((long*)a) - 1;
137-
size = block[0];
151+
block = a - sizeof(mheader);
152+
size = ((mheader *)block)->size;
138153
}
139154

140155
oldsize = size ? (((size + 4) >> 2) << 2) : 0;
@@ -143,19 +158,19 @@ char * memexpand(char *a, long extra)
143158
if ( newsize != oldsize ) {
144159
#ifdef COMPILER
145160
#if (COMPILER <= 16)
146-
if ((sizeof(long)+newsize) >= (1<<16))
161+
if ((sizeof(mheader)+newsize) >= (1<<16))
147162
return NULL;
148163
#endif
149164
#endif
150-
block = (long *) realloc(block, sizeof(long) + newsize);
165+
block = (char *) realloc(block, sizeof(mheader) + newsize);
151166
if (block == NULL)
152167
return NULL;
153-
a = (char *) & block[1];
168+
a = block + sizeof(mheader);
154169
for (i=oldsize; i<newsize; i++)
155170
a[i] = '\0';
156171
}
157172

158-
block[0] = size + extra;
173+
((mheader *)block)->size = size + extra;
159174
return a;
160175
}
161176

@@ -172,3 +187,4 @@ char * memjoin(char *a, char *b)
172187
}
173188
return a;
174189
}
190+

0 commit comments

Comments
 (0)