buffer allignment
although I am no expert on alignment I believe the char buffer should be some kind of aligned storage
@odinthenerd good point. We probably need to align to the next power of two. Would be nice if someone could write a testcase for ARM as it does not support unaligned memory
hi, I've forked this for my own uses (and ended up rewriting most of it anyways :p ) ; for the alignment issue I've used the C++ alignas / alignof operator and passed the alignment as a template parameter ; that way, it can be checked at compile time just like the size (and can be tweaked by the user if necessary):
https://github.com/jcelerier/smallfunction/blob/master/smallfun/include/smallfun.hpp
@jcelerier could we use std::allign and std::alligment_of to accomplish proper alignment?
Do you know in which cases the alignment needs to be adjusted?
Do you know in which cases the alignment needs to be adjusted?
well, in a lot of cases :p for instance pointers have to be aligned on a pointer size at least.
eg:
int main()
{
int x = 0;
struct
{
bool some_bool;
smallfun::SmallFun<int()> fun;
} b;
b.fun = [&x] { return x; };
return b.fun();
}
compile:
$ g++ -fsanitize=undefined align_test.cpp && ./a.out
results in
align_test.cpp:95:31: runtime error: member call on misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:95:31: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:95:31: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:36:19: runtime error: member call on misaligned address 0x7ffee196a3b9 for type 'struct __lambda0', which requires 8 byte alignment
0x7ffee196a3b9: note: pointer points here
98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:123:16: runtime error: member access within misaligned address 0x7ffee196a3b9 for type 'const struct __lambda0', which requires 8 byte alignment
0x7ffee196a3b9: note: pointer points here
98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:89:7: runtime error: member call on misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:89:7: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:89:7: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct concept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:39:23: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct SFModel', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:39:23: runtime error: member call on misaligned address 0x7ffee196a3b1 for type 'struct SFConcept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:39:23: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct SFConcept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:15:25: runtime error: member access within misaligned address 0x7ffee196a3b1 for type 'struct SFConcept', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 90 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:15:25: runtime error: reference binding to misaligned address 0x7ffee196a3b1 for type 'struct <unknown>', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 f0 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00
^
align_test.cpp:39:23: runtime error: reference binding to misaligned address 0x7ffee196a3b1 for type 'struct <unknown>', which requires 8 byte alignment
0x7ffee196a3b1: note: pointer points here
00 00 00 00 f0 9c 9a 52 98 55 00 00 70 a2 96 e1 fe 7f 00 00 02 00 00 00 00 00 00 00 00 00 00 00