Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Allocation schema w.r.t. padding and alignment...

Reply
Thread Tools

Allocation schema w.r.t. padding and alignment...

 
 
SenderX
Guest
Posts: n/a
 
      05-13-2004
I am thinking of porting a library from C to C++, and was tinkering with
ways to ensure proper struct padding and alignment. The library is very
hardware specific, and will simply crash and/or perform bad if the memory
some of its algos use is not properly padded and aligned. So I had to come
up with a way to create several objects of the same class that have
different alignment and padding...

Consider my temporary solution, using placement new and templates:


Example.cpp
( compiles with GCC and MSVC++6.0 )
-----------------

// C++ code created by C programmer!

#include <iostream>
#include <cassert>
#include <cstddef>
#include <new>


#define AC_CACHE_LINE 128
#define AC_ALGO1_ALIGN ( AC_CACHE_LINE / 2 )
#define AC_ALGO2_ALIGN ( AC_CACHE_LINE * 2 )


template< size_t T_Align >
struct TExampleImpl_Padded
{
// snip

int m_iExampleState;

// Is this legal!?! Works for GCC... humm
char PAD1[T_Align - sizeof( int )];
};


struct SExampleImpl
{
int m_iExampleState;
};


template< typename T_Impl, size_t T_Align /* snip */ >
class TExample
{

public:

TExample() : m_pImpl( 0 ), m_pImplMem( 0 )
{
// Assert
if ( ! T_Align ||
T_Align != 1 && ( ((ptrdiff_t)m_pImpl) % sizeof( size_t ) ) )
{
throw;
}

// Alloc and align
m_pImplMem = :perator new( sizeof( T_Impl ) + ( T_Align - 1 ) );

m_pImpl = new ( (void*)(( (((ptrdiff_t)m_pImplMem) + (T_Align - 1))
& -(T_Align) )) ) T_Impl;

// Assert
if ( T_Align != 1 && ( ((ptrdiff_t)m_pImpl) % T_Align ) )
{
m_pImpl->~T_Impl();
:perator delete( m_pImplMem );

throw;
}

std::cout << m_pImpl << "::sizeof - " << sizeof( *m_pImpl ) <<
std::endl;

// Init impl
m_pImpl->m_iExampleState = 0;
}

~TExample()
{
assert( m_pImpl && m_pImplMem );

// Dtor, placement new...
m_pImpl->~T_Impl();
:perator delete( m_pImplMem );
}


public:


// snip


private:

T_Impl *m_pImpl;
void *m_pImplMem;
};


// Now I can create a number of different padding and alignments

typedef TExample< TExampleImpl_Padded< AC_CACHE_LINE >, AC_CACHE_LINE >
CExample;
typedef TExample< TExampleImpl_Padded< AC_ALGO1_ALIGN >, AC_ALGO1_ALIGN >
CExampleAlgo1;
typedef TExample< TExampleImpl_Padded< AC_ALGO2_ALIGN >, AC_ALGO2_ALIGN >
CExampleAlgo2;
typedef TExample< SExampleImpl, 1 > CLwExample;




int main()
{
CExample s;
CExampleAlgo1 sa1;
CExampleAlgo2 sa2;
CLwExample lws;

return 0;
}


As you can see, this provides the solution I want, and passes all of the
library's tests, but...

Is this a non-portable crappy solution? If so, got anything better? I'm not
that good at C++, I'm a C guy...



Thank you.


 
Reply With Quote
 
 
 
 
SenderX
Guest
Posts: n/a
 
      05-13-2004
> public:
>
> TExample() : m_pImpl( 0 ), m_pImplMem( 0 )
> {




DOH!

***************
> // Assert
> if ( ! T_Align ||
> T_Align != 1 && ( ((ptrdiff_t)m_pImpl) % sizeof( size_t ) ) )
> {
> throw;
> }

**************

This was not suppose to be pasted in the example!

Sorry.

:O



>
> // Alloc and align
> m_pImplMem = :perator new( sizeof( T_Impl ) + ( T_Align - 1 ) );
>
> m_pImpl = new ( (void*)(( (((ptrdiff_t)m_pImplMem) + (T_Align -

1))
> & -(T_Align) )) ) T_Impl;



 
Reply With Quote
 
 
 
 
perry
Guest
Posts: n/a
 
      05-13-2004
you know, i use to be such a fan of c++

i think it's the templates that get me down the most...

and to think they've actually brought these over to Java now...

- perry

SenderX wrote:
> I am thinking of porting a library from C to C++, and was tinkering with
> ways to ensure proper struct padding and alignment. The library is very
> hardware specific, and will simply crash and/or perform bad if the memory
> some of its algos use is not properly padded and aligned. So I had to come
> up with a way to create several objects of the same class that have
> different alignment and padding...
>
> Consider my temporary solution, using placement new and templates:
>
>
> Example.cpp
> ( compiles with GCC and MSVC++6.0 )
> -----------------
>
> // C++ code created by C programmer!
>
> #include <iostream>
> #include <cassert>
> #include <cstddef>
> #include <new>
>
>
> #define AC_CACHE_LINE 128
> #define AC_ALGO1_ALIGN ( AC_CACHE_LINE / 2 )
> #define AC_ALGO2_ALIGN ( AC_CACHE_LINE * 2 )
>
>
> template< size_t T_Align >
> struct TExampleImpl_Padded
> {
> // snip
>
> int m_iExampleState;
>
> // Is this legal!?! Works for GCC... humm
> char PAD1[T_Align - sizeof( int )];
> };
>
>
> struct SExampleImpl
> {
> int m_iExampleState;
> };
>
>
> template< typename T_Impl, size_t T_Align /* snip */ >
> class TExample
> {
>
> public:
>
> TExample() : m_pImpl( 0 ), m_pImplMem( 0 )
> {
> // Assert
> if ( ! T_Align ||
> T_Align != 1 && ( ((ptrdiff_t)m_pImpl) % sizeof( size_t ) ) )
> {
> throw;
> }
>
> // Alloc and align
> m_pImplMem = :perator new( sizeof( T_Impl ) + ( T_Align - 1 ) );
>
> m_pImpl = new ( (void*)(( (((ptrdiff_t)m_pImplMem) + (T_Align - 1))
> & -(T_Align) )) ) T_Impl;
>
> // Assert
> if ( T_Align != 1 && ( ((ptrdiff_t)m_pImpl) % T_Align ) )
> {
> m_pImpl->~T_Impl();
> :perator delete( m_pImplMem );
>
> throw;
> }
>
> std::cout << m_pImpl << "::sizeof - " << sizeof( *m_pImpl ) <<
> std::endl;
>
> // Init impl
> m_pImpl->m_iExampleState = 0;
> }
>
> ~TExample()
> {
> assert( m_pImpl && m_pImplMem );
>
> // Dtor, placement new...
> m_pImpl->~T_Impl();
> :perator delete( m_pImplMem );
> }
>
>
> public:
>
>
> // snip
>
>
> private:
>
> T_Impl *m_pImpl;
> void *m_pImplMem;
> };
>
>
> // Now I can create a number of different padding and alignments
>
> typedef TExample< TExampleImpl_Padded< AC_CACHE_LINE >, AC_CACHE_LINE >
> CExample;
> typedef TExample< TExampleImpl_Padded< AC_ALGO1_ALIGN >, AC_ALGO1_ALIGN >
> CExampleAlgo1;
> typedef TExample< TExampleImpl_Padded< AC_ALGO2_ALIGN >, AC_ALGO2_ALIGN >
> CExampleAlgo2;
> typedef TExample< SExampleImpl, 1 > CLwExample;
>
>
>
>
> int main()
> {
> CExample s;
> CExampleAlgo1 sa1;
> CExampleAlgo2 sa2;
> CLwExample lws;
>
> return 0;
> }
>
>
> As you can see, this provides the solution I want, and passes all of the
> library's tests, but...
>
> Is this a non-portable crappy solution? If so, got anything better? I'm not
> that good at C++, I'm a C guy...
>
>
>
> Thank you.
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Problem with schema-validation and property "http://apache.org/xml/properties/schema/external-schemaLocation" Markus Java 1 11-23-2005 02:41 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM



Advertisments