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

buffer allignment #1

Open
odinthenerd opened this issue Sep 22, 2017 · 4 comments
Open

buffer allignment #1

odinthenerd opened this issue Sep 22, 2017 · 4 comments

Comments

@odinthenerd
Copy link

although I am no expert on alignment I believe the char buffer should be some kind of aligned storage

@nikhedonia
Copy link
Contributor

@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

@jcelerier
Copy link

jcelerier commented Dec 31, 2017

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

@nikhedonia
Copy link
Contributor

@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?

@jcelerier
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants