buffer allignment · Issue #1 · LoopPerfect/smallfunction · GitHub
Skip to content

buffer allignment  #1

Open
Open

Description

@odinthenerd

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

Activity

  1. nikhedonia commented on Oct 11, 2017

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

  2. jcelerier commented on Dec 31, 2017

    @jcelerier

    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

  3. nikhedonia commented on Jan 4, 2018

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

  4. jcelerier commented on Jan 4, 2018

    @jcelerier

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions