473,387 Members | 1,549 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,387 software developers and data experts.

possible solutions to alignment issues...

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

http://appcore.home.comcast.net/vzoo...oc/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!
Mar 13 '07 #1
2 1567
Oops! I posted the wrong version! Sorry!!

http://appcore.home.comcast.net/vzoo...oc/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<foofp1(new (buf_foo + offset) foo(1));
offset++;

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

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

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

} return prompt_exit("\n\n--- press <ENTERto exit ---\n");
}
// prompt util
char prompt_getchar(char const *buf) {
printf("%s", buf);
return getchar();
}

________________
Mar 13 '07 #2
Son of a bitch I need coffee! Man, I accidentally posted another wrong
version.

http://appcore.home.comcast.net/vzoo...oc/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<foofp1(new (buf_foo + offset) foo(1));
offset++;

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

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

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

} return prompt_exit("\n\n--- press <ENTERto exit ---\n");
}
// prompt util
char prompt_getchar(char const *buf) {
printf("%s", buf);
return getchar();
}
_____________

Mar 13 '07 #3

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

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
10
by: j0mbolar | last post by:
for any pointer to T, does a pointer to T have different or can have different alignment requirement than a pointer to pointer to T? if so, where is the exact wording in the standard that would...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
14
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
1
by: Chris Thomasson | last post by:
I found some time to work a little more on my C++ allocator project. Here is some of the basic alignment code that I am thinking about using: ---------------- #include <cstdio> #include...
2
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Sep 22, 10:45 am, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote: Only problem is that then entire frame in memory might have strange alignment.
8
by: Stephen Horne | last post by:
I understand that the next C++ standard will have features to handle the alignment of data types. This is good, but a bit late for me! I've been using some template trickery to handle alignment...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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...

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.