Re: Unaligned memory access

"'Sainan' via lua-l" <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <2Quv-tCLMaSCL3TdV6KJtQW3nZZOfVNznv0VkSukcXSfPDZQwZz9eTjDOPy0pjmW8yogjAl-ImHGV8Dsu1T9aidUWnNWwjQ228d375na7hg=@calamity.inc>
lua_newuserdata does not provide alignment guarantees, but practically, you will see that it is only 8-byte aligned (on 64-bit). Of course, your workaround of using 'new' works, but you can also simply allocate 15 more bytes so you can safely offset the pointer:

char *data = reinterpret_cast<char*>(lua_newuserdata(L, data_len + 15)); 
if (reinterpret_cast<uintptr_t>(data) % 16) {  /* data is not 16-byte aligned? */
  data = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(data) + (16 - (reinterpret_cast<uintptr_t>(data) % 16)));  /* align data to 16-byte boundary */
}

-- Sainan

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/2Quv-tCLMaSCL3TdV6KJtQW3nZZOfVNznv0VkSukcXSfPDZQwZz9eTjDOPy0pjmW8yogjAl-ImHGV8Dsu1T9aidUWnNWwjQ228d375na7hg%3D%40calamity.inc.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.