473,398 Members | 2,812 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,398 software developers and data experts.

problem with alignment

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
Nov 11 '08 #1
7 2497
On Nov 11, 6:47*am, abir <abirba...@gmail.comwrote:
*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
Nov 11 '08 #2
On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 11, 6:47 am, abir <abirba...@gmail.comwrote:
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
Nov 11 '08 #3
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.comwrote:
[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
Nov 11 '08 #4
On Nov 11, 2:13*pm, abir <abirba...@gmail.comwrote:
On Nov 11, 4:59 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 11, 6:47 am, abir <abirba...@gmail.comwrote:
*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
Nov 11 '08 #5

"abir" <ab*******@gmail.comwrote in message
news:eb**********************************@t18g2000 prt.googlegroups.com...

[...]

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...)


Nov 12 '08 #6

"abir" <ab*******@gmail.comwrote in message
news:eb**********************************@t18g2000 prt.googlegroups.com...

[...]

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...)


Nov 12 '08 #7

"abir" <ab*******@gmail.comwrote in message
news:eb**********************************@t18g2000 prt.googlegroups.com...

[...]

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...)


Nov 12 '08 #8

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

Similar topics

2
by: Matt Feinstein | last post by:
Using the 'struct' module (Win32, python version 2.4.1)-- The library documentation says that 'no alignment is required for any type'. However, struct.calcsize('fd') gives 16 while...
4
by: Mimo Zus | last post by:
I'm hoping that someone can explain what's going on; better yet provide a workaround. I'm designing a centered CSS site based on a 550 pixel wide vertical background image. Onto this background...
0
by: Job Lot | last post by:
I have an Expense Data Entry form which contains a DataGrid showing various expense categories. There are three columns Description, Cash Exp, Credit Exp, where Description column is readonly. ...
2
by: John Smith | last post by:
Hi all; Putting "Due" into the column header of a datagrid. Font is a proportional fort. When the alignment is left, there is some space between the column separator bar and the D in Due....
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
1
by: Stephen Plotnick | last post by:
I have a checkbox in a data grid that does not change it's state until I actually leave the field. I've done this routine several times in other data grids without an issue. I'm using VB.2003. ...
4
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my...
1
by: stevedub | last post by:
I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.