473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 = ::operator 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();
::operator 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();
::operator 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.
Jul 22 '05 #1
2 1199
> 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 = ::operator new( sizeof( T_Impl ) + ( T_Align - 1 ) );

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

Jul 22 '05 #2
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 = ::operator 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();
::operator 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();
::operator 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.


Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
4
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
20
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables...
37
by: Egbert Nierop \(MVP for IIS\) | last post by:
In win32 mode, a BSTR was UINT length prefixed and terminated exactly by a zero char. So if you allocated "Hello World" that would allocate 28 bytes. In x64 and (IA64 as well) it would become...
10
by: Timothee Groleau | last post by:
Hi, Is it defined in C++ that 2 arrays of the same type defined one after the other are contiguous in memory? like saying: int a1; int a2;
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
1
by: fdmfdmfdm | last post by:
code like: struct pid_tag{ unsigned int inactive : 1; unsigned int : 1; /* 1 bit of padding */ unsigned int refcount : 6; unsigned int : 0; /* pad to next word boundary...
43
by: bharath539 | last post by:
how much memory is allocated for following structure struct bharath { int b; char c; float d; } and how?
8
by: rahul | last post by:
How is the memory allocated for structures? I need to optimize the memory usage and bit fields are not doing the trick. Any details about the memory allocation for the structures would be a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.