Skip to content

Commit 224deca

Browse files
authored
Silence aliasing warnings with void* (#280)
1 parent 7147ed7 commit 224deca

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

vm/vm/main/memword.hh

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// POSSIBILITY OF SUCH DAMAGE.
2424

2525
// A MemWord (memory word) is a type which is defined as having the size of a
26-
// char* and behaving as the union of a number of other types. If they are
26+
// void* and behaving as the union of a number of other types. If they are
2727
// small enough, they are actually stored in the MemWord. If not, they are
2828
// stored as a pointer to external memory.
2929

@@ -33,7 +33,7 @@
3333
// The MWUAccessor provides the getters/setters for the MWUnion.
3434

3535
// The MWTest is an helper class to deal with types which are bigger than a
36-
// char*
36+
// void*
3737

3838

3939
#ifndef MOZART_MEMWORD_H
@@ -67,8 +67,8 @@ struct MWUAccessor {
6767
}
6868
};
6969

70-
// The bottom case of the MWUnion contains just a char* as it can be cast to
71-
// any other pointer without fear of aliasing problems.
70+
// The bottom case of the MWUnion contains just a void* as it can be cast to
71+
// and from any other pointer.
7272
template <>
7373
union MWUnion<> {
7474
template <typename Q>
@@ -86,7 +86,7 @@ union MWUnion<> {
8686
return MWUAccessor<MWUnion, Q, Q>::requiresExternalMemory();
8787
}
8888

89-
char* it;
89+
void* it;
9090
};
9191

9292
// The easy case of accessing what we have in the first member of the union.
@@ -129,7 +129,7 @@ struct MWUAccessor<MWUnion<>, T, T>{
129129
}
130130

131131
static void alloc(VM vm, MWUnion<>* u) {
132-
u->it = reinterpret_cast<char*>(new (vm) T);
132+
u->it = new (vm) T;
133133
}
134134

135135
static constexpr bool requiresExternalMemory() {
@@ -141,8 +141,8 @@ struct MWUAccessor<MWUnion<>, T, T>{
141141
// big, we reduce it.
142142
template <typename T, typename... Args>
143143
union MWUnion<T, Args...> {
144-
typedef typename std::conditional<sizeof(T) <= sizeof(char*),
145-
T, char*>::type Tred;
144+
typedef typename std::conditional<sizeof(T) <= sizeof(void*),
145+
T, void*>::type Tred;
146146

147147
typedef MWUnion<Args...> Next;
148148

@@ -180,12 +180,13 @@ typedef internal::MWUnion<nativeint, bool, double, unit_t, atom_t,
180180

181181
// Sanity tests
182182

183-
static_assert(sizeof(MemWord) == sizeof(char*),
183+
static_assert(sizeof(MemWord) == sizeof(void*),
184184
"MemWord has not the size of a word");
185185

186186
static_assert(!MemWord::requiresExternalMemory<nativeint>(), "MemWord bug");
187187
static_assert(!MemWord::requiresExternalMemory<unit_t>(), "MemWord bug");
188188
static_assert(!MemWord::requiresExternalMemory<char*>(), "MemWord bug");
189+
static_assert(!MemWord::requiresExternalMemory<void*>(), "MemWord bug");
189190
static_assert(!MemWord::requiresExternalMemory<StableNode*>(), "MemWord bug");
190191

191192
static_assert(MemWord::requiresExternalMemory<float>(), "MemWord bug");

0 commit comments

Comments
 (0)