Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory alignment fix for VS2013 x64 #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions ext/ffi_c/AbstractMemory.h
Expand Up @@ -165,6 +165,13 @@ get_memory_op(Type* type)
#define MEMORY_PTR(obj) MEMORY((obj))->address
#define MEMORY_LEN(obj) MEMORY((obj))->size

/* ensure the memory is aligned on at least a 8 byte boundary */
#if __x86_64__ || _WIN64 || __ppc64__
#define MEMORY_ALIGN_MASK 0x7ULL
#else
#define MEMORY_ALIGN_MASK 0x7UL
#endif
#define MEMORY_ALIGN(p) (((uintptr_t) p + MEMORY_ALIGN_MASK) & (uintptr_t) ~MEMORY_ALIGN_MASK)


#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions ext/ffi_c/Buffer.c
Expand Up @@ -114,7 +114,7 @@ buffer_initialize(int argc, VALUE* argv, VALUE self)
}

/* ensure the memory is aligned on at least a 8 byte boundary */
p->memory.address = (void *) (((uintptr_t) p->data.storage + 0x7) & (uintptr_t) ~0x7UL);
p->memory.address = (void *) MEMORY_ALIGN(p->data.storage);

if (p->memory.size > 0 && (nargs < 3 || RTEST(rbClear))) {
memset(p->memory.address, 0, p->memory.size);
Expand Down Expand Up @@ -154,7 +154,7 @@ buffer_initialize_copy(VALUE self, VALUE other)
return Qnil;
}

dst->memory.address = (void *) (((uintptr_t) dst->data.storage + 0x7) & (uintptr_t) ~0x7UL);
dst->memory.address = (void *) MEMORY_ALIGN(dst->data.storage);
dst->memory.size = src->size;
dst->memory.typeSize = src->typeSize;

Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/MemoryPointer.c
Expand Up @@ -112,7 +112,7 @@ memptr_malloc(VALUE self, long size, long count, bool clear)
p->memory.typeSize = (int) size;
p->memory.size = msize;
/* ensure the memory is aligned on at least a 8 byte boundary */
p->memory.address = (char *) (((uintptr_t) p->storage + 0x7) & (uintptr_t) ~0x7UL);;
p->memory.address = (char *) MEMORY_ALIGN(p->storage);
p->allocated = true;

if (clear && p->memory.size > 0) {
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/Pointer.c
Expand Up @@ -183,7 +183,7 @@ ptr_initialize_copy(VALUE self, VALUE other)

dst->allocated = true;
dst->autorelease = true;
dst->memory.address = (void *) (((uintptr_t) dst->storage + 0x7) & (uintptr_t) ~0x7UL);
dst->memory.address = (void *) MEMORY_ALIGN(dst->storage);
dst->memory.size = src->size;
dst->memory.typeSize = src->typeSize;

Expand Down