possible solutions to alignment issues...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Thomasson

    #1

    possible solutions to alignment issues...

    Can anybody notice any problems with the following example of an approach I
    have been tinkering around with:




    Here is a code snippet of the main alignment functions I created; I think I
    am going to end up actually using the logic...
    _______________ ___

    // aligns a ptr from a raw ptr
    static void* alignptrraw(
    void const *ptr,
    size_t offset,
    ptrdiff_t sz) {

    ptrdiff_t const base = (static_cast<un signed char const*>(0) -
    static_cast<uns igned char const*>(ptr)) + offset;

    ptrdiff_t const align = (base + sz - 1) & -(sz);

    if (align % sz) {
    assert(! (align % sz));
    return 0;
    }

    return static_cast<uns igned char*>(0) - align;
    }


    // get the size of an align
    template<typena me T>
    static size_t alignsztype(siz e_t offset) {
    return (sizeof(T) * 2) + 1 + offset;
    }


    // aligns a type ptr from a raw ptr
    template<typena me T>
    static void* alignptrtype(
    void const *ptr,
    size_t const maxsz,
    size_t offset,
    ptrdiff_t sz = sizeof(T)) {

    if (alignsztype<T> (offset) >= maxsz) {
    assert(alignszt ype<T>(offset) < maxsz);
    return 0;
    }

    return alignptrraw(ptr , offset, sz);
    }

    ______________


    Any thoughts? Thanks for all your help!


  • Chris Thomasson

    #2
    Re: possible solutions to alignment issues...

    Oops! I posted the wrong version! Sorry!!


    (updated ver)



    example.cpp
    ______________

    // sample program


    #include <new>
    #include <cstddef>
    #include <cassert>
    #include <cstdio>


    // aligns a ptr from a raw ptr
    static void* alignptrraw(
    void const *ptr,
    size_t offset,
    ptrdiff_t sz) {

    ptrdiff_t const base = static_cast<uns igned char const*>(0) -
    (static_cast<un signed char const*>(ptr)) + offset;

    ptrdiff_t const align = (base + sz - 1) & -(sz);

    if (align % sz) {
    assert(! (align % sz));
    return 0;
    }

    return static_cast<uns igned char*>(0) - align;
    }


    // get the size of an align
    template<typena me T>
    static size_t alignsztype(siz e_t offset) {
    return ((sizeof(T) * 2) - 1) + offset;
    }


    // aligns a type ptr from a raw ptr
    template<typena me T>
    static void* alignptrtype(
    void const *ptr,
    size_t const maxsz,
    size_t offset,
    ptrdiff_t sz = sizeof(T)) {

    if (alignsztype<T> (offset) >= maxsz) {
    assert(alignszt ype<T>(offset) < maxsz);
    return 0;
    }

    return alignptrraw(ptr , offset, sz);
    }


    // autoptr_dtor class
    template<typena me T>
    class autoptr_dtor {
    T * const m_ptr;

    public:
    autoptr_dtor(T * const ptr) : m_ptr(ptr) {}

    ~autoptr_dtor() {
    if (m_ptr) { m_ptr->~T(); }
    }
    };


    // foo class
    class foo {
    int m_myint;

    public:
    foo(int my) : m_myint(my) {
    printf(" (%p)- foo::foo(int my=(%d))- myint(%d)\n",
    (void*)this, my, m_myint);
    }

    ~foo() {
    printf(" (%p)- foo::~foo()- myint(%d)\n",
    (void*)this, m_myint);
    }

    };


    // prompt util
    static char prompt_getchar( char const*);
    #define prompt_exit(vzm p_buf)\
    ((int)prompt_ge tchar((vzmp_buf )))


    #define BASEBUF_SZ() 4096
    #define L2_CACHELINE_SZ () 128


    // entry
    int main(void) {
    {
    // setup base buffer and offset
    size_t offset = 0;
    unsigned char buf_base[BASEBUF_SZ() + L2_CACHELINE_SZ () - 1] = {0};

    // setup l2-cacheline aligned buffer
    void* const buf_l2cache = alignptrraw(buf _base, 0, L2_CACHELINE_SZ ());
    printf("%p - %p\n", (void*)buf_base , (void*)buf_l2ca che);

    // setup base foo buffer
    foo* const buf_foo = static_cast<foo *
    const>(alignptr type<foo>(buf_l 2cache, BASEBUF_SZ(), 0));

    // acquire a ptr, and bump the offset
    autoptr_dtor<fo ofp1(new (buf_foo + offset) foo(1));
    offset++;

    // acquire a ptr, and bump the offset
    autoptr_dtor<fo ofp2(new (buf_foo + offset) foo(2));
    offset++;

    // acquire a ptr, and bump the offset
    autoptr_dtor<fo ofp3(new (buf_foo + offset) foo(3));
    offset++;

    // acquire a ptr, and bump the offset
    autoptr_dtor<fo ofp4(new (buf_foo + offset) foo(4));
    offset++;

    } return prompt_exit("\n \n--- press <ENTERto exit ---\n");
    }


    // prompt util
    char prompt_getchar( char const *buf) {
    printf("%s", buf);
    return getchar();
    }

    _______________ _


    Comment

    • Chris Thomasson

      #3
      Re: possible solutions to alignment issues...

      Son of a bitch I need coffee! Man, I accidentally posted another wrong
      version.


      (right one this time!)


      So sorry for any inconveniences/confusions... BTW, could you please "try" to
      not flame me "too" bad!

      :^(...



      example.cpp
      ______________
      // sample program


      #include <new>
      #include <cstddef>
      #include <cassert>
      #include <cstdio>


      // aligns a ptr from a raw ptr
      static void* alignptrraw(
      void const *ptr,
      size_t const offset,
      ptrdiff_t const sz) {
      ptrdiff_t const base = reinterpret_cas t<ptrdiff_t>(pt r) + offset;
      ptrdiff_t const align = (base + sz - 1) & -sz;
      if (align % sz || align < base) {
      assert(! (align % sz));
      return 0;
      }
      return reinterpret_cas t<void*>(align) ;
      }


      // get the size of an align
      template<typena me T>
      static size_t alignsztype(siz e_t offset) {
      return ((sizeof(T) * 2) - 1) + offset;
      }


      // aligns a type ptr from a raw ptr
      template<typena me T>
      static void* alignptrtype(
      void const *ptr,
      size_t const maxsz,
      size_t offset,
      ptrdiff_t sz = sizeof(T)) {

      if (alignsztype<T> (offset) >= maxsz) {
      assert(alignszt ype<T>(offset) < maxsz);
      return 0;
      }

      return alignptrraw(ptr , offset, sz);
      }


      // autoptr_dtor class
      template<typena me T>
      class autoptr_dtor {
      T * const m_ptr;

      public:
      autoptr_dtor(T * const ptr) : m_ptr(ptr) {}

      ~autoptr_dtor() {
      if (m_ptr) { m_ptr->~T(); }
      }
      };


      // foo class
      class foo {
      int m_myint;

      public:
      foo(int my) : m_myint(my) {
      printf(" (%p)- foo::foo(int my=(%d))- myint(%d)\n",
      (void*)this, my, m_myint);
      }

      ~foo() {
      printf(" (%p)- foo::~foo()- myint(%d)\n",
      (void*)this, m_myint);
      }

      };


      // prompt util
      static char prompt_getchar( char const*);
      #define prompt_exit(vzm p_buf)\
      ((int)prompt_ge tchar((vzmp_buf )))


      #define BASEBUF_SZ() 4096
      #define L2_CACHELINE_SZ () 64


      // entry
      int main(void) {
      {
      // setup base buffer and offset
      size_t offset = 0;
      unsigned char buf_base[BASEBUF_SZ() + L2_CACHELINE_SZ () - 1] = {0};

      // setup l2-cacheline aligned buffer
      void* const buf_l2cache = alignptrraw(buf _base, 0, L2_CACHELINE_SZ ());
      printf("%p - %p\n", (void*)buf_base , (void*)buf_l2ca che);

      // setup base foo buffer
      foo* const buf_foo = static_cast<foo *
      const>(alignptr type<foo>(buf_l 2cache, BASEBUF_SZ(), 0));

      // acquire a ptr, and bump the offset
      autoptr_dtor<fo ofp1(new (buf_foo + offset) foo(1));
      offset++;

      // acquire a ptr, and bump the offset
      autoptr_dtor<fo ofp2(new (buf_foo + offset) foo(2));
      offset++;

      // acquire a ptr, and bump the offset
      autoptr_dtor<fo ofp3(new (buf_foo + offset) foo(3));
      offset++;

      // acquire a ptr, and bump the offset
      autoptr_dtor<fo ofp4(new (buf_foo + offset) foo(4));
      offset++;

      } return prompt_exit("\n \n--- press <ENTERto exit ---\n");
      }


      // prompt util
      char prompt_getchar( char const *buf) {
      printf("%s", buf);
      return getchar();
      }
      _____________



      Comment

      Working...