Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem with alignment

Reply
Thread Tools

problem with alignment

 
 
abir
Guest
Posts: n/a
 
      11-11-2008
In my program i wan to use a certain type as both type T1 (usually
non-pod)
and also T2 (usually size_t)

so an union like this doesn't work
template<typename T>
union utype{
T v_;
std::size_t sz_;
};
utype* memory_;
where T is
struct nonpod{
int x;
int y;
nonpod(){}
};

I am not familiar to aligned_storage or its usage.
Can i make something like this?
template<typename T,std::size_t N>
struct storage{
typedef std::size_t size_type;
typedef T value_type;
typedef T* pointer;
typedef typename std::tr1::aligned_storage<sizeof(value_type)
,std::tr1::alignment_of<size_type>::value>::type aligned_type;
aligned_type* bos_; ///begin of storage pointer
void init(){
bos_ =
static_cast<aligned_type*>(std::malloc(sizeof(alig ned_type)*N));
if(0 == bos_){
throw std::bad_alloc("storage initialization failed");
}
///is it ok? can i treat the memory as sie_type* ?
size_type* p = reinterpret_cast<size_type*>(bos_);
for (size_type i = 0; i != N; ++p){
///is it ok ? ++p goes to next memory location which is
multiple
//of both sizeof(size_type) & sizeof(value_type) ?
*p = ++i;
}
}
};

NOTE: it is ok for me to loose some memory if sizeof(T) <
sizeof(size_type)
or my bookkeeping takes more memory than value_type

Thanks
abir
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      11-11-2008
On Nov 11, 6:47*am, abir <abirba...@gmail.com> wrote:
> *In my program i wan to use a certain type as both type T1 (usually
> non-pod)
> * * and also T2 (usually size_t)
>
> * * so an union like this doesn't work
> * * template<typename T>
> union utype{
> * * T v_;
> * * std::size_t sz_;};
>
> utype* memory_;
> where T is
> struct nonpod{
> * * int x;
> * * int y;
> * * nonpod(){}
>
> };
>
> I am not familiar to aligned_storage or its usage.
> Can i make something like this?


[]

You could.

Or you can take a ready-made solution: boost::variant<>.

--
Max
 
Reply With Quote
 
 
 
 
abir
Guest
Posts: n/a
 
      11-11-2008
On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
> On Nov 11, 6:47 am, abir <abirba...@gmail.com> wrote:
>
>
>
> > In my program i wan to use a certain type as both type T1 (usually
> > non-pod)
> > and also T2 (usually size_t)

>
> > so an union like this doesn't work
> > template<typename T>
> > union utype{
> > T v_;
> > std::size_t sz_;};

>
> > utype* memory_;
> > where T is
> > struct nonpod{
> > int x;
> > int y;
> > nonpod(){}

>
> > };

>
> > I am not familiar to aligned_storage or its usage.
> > Can i make something like this?

>
> []
>
> You could.
>
> Or you can take a ready-made solution: boost::variant<>.
>

Probably i could, but i am more interested to know how that ready-
made
solution is made.

And i am rather interested to know the usage of aligned_storage &
alignment_of
Not finding enough documentation on them.
so whether the alignment_type below going to be aligned for both
value_type & size_type
typedef typename std::tr1::aligned_storage<sizeof(value_type)
,std::tr1::alignment_of<size_type>::value>::type
aligned_type;
and i am interested to have a loki style not-so-small-object allocator
(at least not smaller than size_type)
and each storage can hold more than 255 objects & guided by size_type.

Also the code had a problem
it should be
aligned_type* p = bos_;
for (size_type i = 0; i != N; ++p){
*(reinterpret_cast<size_type*>(p)) = ++i;
}

And thanks for reply.
abir
> --
> Max


 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      11-11-2008
On Nov 11, 2:13*pm, abir <abirba...@gmail.com> wrote:
> On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> wrote:
>
> > On Nov 11, 6:47 am, abir <abirba...@gmail.com> wrote:

>
> > > *In my program i wan to use a certain type as both type T1 (usually
> > > non-pod)
> > > * * and also T2 (usually size_t)

>
> > > * * so an union like this doesn't work
> > > * * template<typename T>
> > > union utype{
> > > * * T v_;
> > > * * std::size_t sz_;};

>
> > > utype* memory_;
> > > where T is
> > > struct nonpod{
> > > * * int x;
> > > * * int y;
> > > * * nonpod(){}

>
> > > };

>
> > > I am not familiar to aligned_storage or its usage.
> > > Can i make something like this?

>
> > []

>
> > You could.

>
> > Or you can take a ready-made solution: boost::variant<>.

>
> Probably i could, but i am more interested to know how that ready-
> made
> solution is made.
>
> And i am rather interested to know the usage of aligned_storage &
> alignment_of
> Not finding enough documentation on them.


You use aligned_storage<>::type as a member or a stack variable, so
that the compiler aligns it properly.

Using malloc(N * sizeof(aligned_storage<>::type)) does not guarantee
alignment in the general case. malloc() returns memory suitably
aligned for a built-in type with the largest alignment requirement
(which often is double or long double). Thus, if the alignment
requirement for aligned_storage<>::type is greater than that of a
built-in type with the largest alignment requirement, malloc() is not
directly suitable (you need to allocate more memory with malloc() and
align it manually). On the other hand, if the alignment requirement
for aligned_storage<>::type is equal to or less than that of a built-
in type with the largest alignment requirement, than you don't need
aligned_storage<>::type when using malloc(), just use the largest size
malloc(N * std::max(sizeof(nonpod), sizeof(size_t))).

> so whether the alignment_type below going to be aligned for both
> value_type & size_type
> typedef typename std::tr1::aligned_storage<sizeof(value_type)
> * * * * ,std::tr1::alignment_of<size_type>::value>::type
> aligned_type;


aligned_type here is only guaranteed to have alignment size_type and
the size of value_type.

> and i am interested to have a loki style not-so-small-object allocator
> (at least not smaller than size_type)
> and each storage can hold more than 255 objects & guided by size_type.


I depends whether that allocator needs to support types with alignment
requirement greater than that of the built-in type with the largest
alignment requirement (align on processor cache size for example). If
it does not need to (like most allocators), than malloc() is fine and
you don't need to use aligned_storage<>::type. If it does, you don't
need aligned_storage<>::type either, because you are going to use
functions like mmap, posix_memalign() to allocate suitably aligned
memory, or manually align memory allocated with malloc().

--
Max
 
Reply With Quote
 
Larry Evans
Guest
Posts: n/a
 
      11-11-2008
On 11/11/08 08:13, abir wrote:
> On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
> wrote:
>> On Nov 11, 6:47 am, abir <abirba...@gmail.com> wrote:

[snip]
>> You could.
>>
>> Or you can take a ready-made solution: boost::variant<>.
>>

> Probably i could, but i am more interested to know how that ready-
> made
> solution is made.
>
> And i am rather interested to know the usage of aligned_storage &
> alignment_of
> Not finding enough documentation on them.
> so whether the alignment_type below going to be aligned for both
> value_type & size_type
> typedef typename std::tr1::aligned_storage<sizeof(value_type)
> ,std::tr1::alignment_of<size_type>::value>::type
> aligned_type;
> and i am interested to have a loki style not-so-small-object allocator
> (at least not smaller than size_type)
> and each storage can hold more than 255 objects & guided by size_type.


Hi abir,

More than a year ago, I had a similar problem, only it involved
finding how to aligne either a tuple or a variant. I read some
references and found a solution, but I'm not sure the solution
is correct. I got no feedback either way

I've included a reference to the references in a boost post.
Maybe you could find them useful:

http://article.gmane.org/gmane.comp....=aligned_types
 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      11-12-2008

"abir" <> wrote in message
news:ebe0739d-fee8-4eac-8703-...

[...]

You can usually get the alignment of a type like:
__________________________________________________ ______________________
#include <iostream>
#include <cstddef>


template<typename T>
static std::size_t
align_of() {
struct align_of_aligner {
char m_pad;
T m_obj;
};

return offsetof(align_of_aligner, m_obj);
}


struct foo {
char c;
double a;
};


int main(void) {
std::cout << "align_of<char>() == " << align_of<char>() << std::endl;
std::cout << "align_of<short>() == " << align_of<short>() << std::endl;
std::cout << "align_of<int>() == " << align_of<int>() << std::endl;
std::cout << "align_of<long>() == " << align_of<long>() << std::endl;
std::cout << "align_of<float>() == " << align_of<float>() << std::endl;
std::cout << "align_of<double>() == " << align_of<double>() << std::endl;
std::cout << "align_of<long double>() == " << align_of<long double>() <<
std::endl;
std::cout << "align_of<struct foo>() == " << align_of<foo>() << std::endl;
return 0;
}
__________________________________________________ ______________________




You can also forcefully align addresses by using the following simplistic
HACK:

http://groups.google.com/group/comp....b1cac59c8e98a3
(read all, follow links...)




 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      11-12-2008

"abir" <> wrote in message
news:ebe0739d-fee8-4eac-8703-...

[...]

You can usually get the alignment of a type like:
__________________________________________________ ______________________
#include <iostream>
#include <cstddef>


template<typename T>
static std::size_t
align_of() {
struct align_of_aligner {
char m_pad;
T m_obj;
};

return offsetof(align_of_aligner, m_obj);
}


struct foo {
char c;
double a;
};


int main(void) {
std::cout << "align_of<char>() == " << align_of<char>() << std::endl;
std::cout << "align_of<short>() == " << align_of<short>() << std::endl;
std::cout << "align_of<int>() == " << align_of<int>() << std::endl;
std::cout << "align_of<long>() == " << align_of<long>() << std::endl;
std::cout << "align_of<float>() == " << align_of<float>() << std::endl;
std::cout << "align_of<double>() == " << align_of<double>() << std::endl;
std::cout << "align_of<long double>() == " << align_of<long double>() <<
std::endl;
std::cout << "align_of<struct foo>() == " << align_of<foo>() << std::endl;
return 0;
}
__________________________________________________ ______________________




You can also forcefully align addresses by using the following simplistic
HACK:

http://groups.google.com/group/comp....b1cac59c8e98a3
(read all, follow links...)




 
Reply With Quote
 
Chris M. Thomasson
Guest
Posts: n/a
 
      11-12-2008

"abir" <> wrote in message
news:ebe0739d-fee8-4eac-8703-...

[...]

You can usually get the alignment of a type like:
__________________________________________________ ______________________
#include <iostream>
#include <cstddef>


template<typename T>
static std::size_t
align_of() {
struct align_of_aligner {
char m_pad;
T m_obj;
};

return offsetof(align_of_aligner, m_obj);
}


struct foo {
char c;
double a;
};


int main(void) {
std::cout << "align_of<char>() == " << align_of<char>() << std::endl;
std::cout << "align_of<short>() == " << align_of<short>() << std::endl;
std::cout << "align_of<int>() == " << align_of<int>() << std::endl;
std::cout << "align_of<long>() == " << align_of<long>() << std::endl;
std::cout << "align_of<float>() == " << align_of<float>() << std::endl;
std::cout << "align_of<double>() == " << align_of<double>() << std::endl;
std::cout << "align_of<long double>() == " << align_of<long double>() <<
std::endl;
std::cout << "align_of<struct foo>() == " << align_of<foo>() << std::endl;
return 0;
}
__________________________________________________ ______________________




You can also forcefully align addresses by using the following simplistic
HACK:

http://groups.google.com/group/comp....b1cac59c8e98a3
(read all, follow links...)




 
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
Problem w/ Text Alignment - IE7 Problem Only Mtmartin71 HTML 9 08-28-2007 06:31 AM
asp:panel alignment problem Gary Coutts ASP .Net 1 02-11-2006 11:24 PM
JCheckBoxMenuItem and submenu alignment problem JavaEnquirer Java 2 02-01-2006 11:19 AM
Alignment problem in the datagrid Vannela ASP .Net 2 12-16-2003 05:26 AM
Re: Help required for alignment problem Sunil Menon ASP .Net 0 06-26-2003 07:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57