possible solutions to alignment issues...

Discussion in 'C++' started by Chris Thomasson, Mar 13, 2007.

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

    http://appcore.home.comcast.net/vzoom/malloc/example.cpp


    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<unsigned char const*>(0) -
    static_cast<unsigned char const*>(ptr)) + offset;

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

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

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


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


    // aligns a type ptr from a raw ptr
    template<typename 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(alignsztype<T>(offset) < maxsz);
    return 0;
    }

    return alignptrraw(ptr, offset, sz);
    }

    ______________


    Any thoughts? Thanks for all your help!
    Chris Thomasson, Mar 13, 2007
    #1
    1. Advertising

  2. Oops! I posted the wrong version! Sorry!!

    http://appcore.home.comcast.net/vzoom/malloc/example.cpp
    (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<unsigned char const*>(0) -
    (static_cast<unsigned char const*>(ptr)) + offset;

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

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

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


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


    // aligns a type ptr from a raw ptr
    template<typename 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(alignsztype<T>(offset) < maxsz);
    return 0;
    }

    return alignptrraw(ptr, offset, sz);
    }


    // autoptr_dtor class
    template<typename 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(vzmp_buf)\
    ((int)prompt_getchar((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_l2cache);

    // setup base foo buffer
    foo* const buf_foo = static_cast<foo*
    const>(alignptrtype<foo>(buf_l2cache, BASEBUF_SZ(), 0));

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

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

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

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

    } return prompt_exit("\n\n--- press <ENTER> to exit ---\n");
    }


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

    ________________
    Chris Thomasson, Mar 13, 2007
    #2
    1. Advertising

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

    http://appcore.home.comcast.net/vzoom/malloc/example.cpp
    (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_cast<ptrdiff_t>(ptr) + offset;
    ptrdiff_t const align = (base + sz - 1) & -sz;
    if (align % sz || align < base) {
    assert(! (align % sz));
    return 0;
    }
    return reinterpret_cast<void*>(align);
    }


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


    // aligns a type ptr from a raw ptr
    template<typename 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(alignsztype<T>(offset) < maxsz);
    return 0;
    }

    return alignptrraw(ptr, offset, sz);
    }


    // autoptr_dtor class
    template<typename 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(vzmp_buf)\
    ((int)prompt_getchar((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_l2cache);

    // setup base foo buffer
    foo* const buf_foo = static_cast<foo*
    const>(alignptrtype<foo>(buf_l2cache, BASEBUF_SZ(), 0));

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

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

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

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

    } return prompt_exit("\n\n--- press <ENTER> to exit ---\n");
    }


    // prompt util
    char prompt_getchar(char const *buf) {
    printf("%s", buf);
    return getchar();
    }
    _____________
    Chris Thomasson, Mar 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Swathika
    Replies:
    2
    Views:
    450
  2. Swathika
    Replies:
    1
    Views:
    518
    red floyd
    Jul 10, 2008
  3. Martjack Ecommerce
    Replies:
    0
    Views:
    520
    Martjack Ecommerce
    Dec 22, 2009
  4. Cal Who
    Replies:
    0
    Views:
    456
    Cal Who
    Jun 9, 2010
  5. Dinis Cruz
    Replies:
    2
    Views:
    305
    Dinis Cruz
    Oct 20, 2003
Loading...

Share This Page