Skip to content

Commit ed2ccda

Browse files
committed
Use _aligned_malloc on Windows.
On 32-bit Windows, malloc doesn't give us memory with sufficiently strong alignment for AVX vectors. Note that Windows also has a family of allocators that provide guarantee exact misalignment. Using these would simplify the ivar layout logic slightly, because we wouldn't need to account for the refcount pointer. It's not clear that it's actually worth doing though, because that logic already exists.
1 parent 6e52cbb commit ed2ccda

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

gc_none.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88

99
static id allocate_class(Class cls, size_t extraBytes)
1010
{
11-
intptr_t *addr = calloc(cls->instance_size + extraBytes + sizeof(intptr_t), 1);
11+
size_t size = cls->instance_size + extraBytes + sizeof(intptr_t);
12+
intptr_t *addr =
13+
#ifdef _WIN32
14+
// Malloc on Windows doesn't guarantee 32-byte alignment, but we
15+
// require this for any class that may contain vectors
16+
_aligned_malloc(size, 32);
17+
memset(addr, 0, size);
18+
#else
19+
calloc(size, 1);
20+
#endif
1221
return (id)(addr + 1);
1322
}
1423

1524
static void free_object(id obj)
1625
{
26+
#ifdef _WIN32
27+
_aligned_free((void*)(((intptr_t*)obj) - 1));
28+
#else
1729
free((void*)(((intptr_t*)obj) - 1));
30+
#endif
1831
}
1932

2033
static void *alloc(size_t size)

0 commit comments

Comments
 (0)