473,809 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allocator alignment issues...

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 <cstring>
#include <cassert>
// attempts to extract the alignemnt of a type T
template<typena me T>
class align_of {
struct temp {
char m_offset;
T m_object;
};

public:
enum result_e {
c_result = sizeof(temp) sizeof(T) ?
sizeof(temp) - sizeof(T) : sizeof(T)
};
};
// attempts to extract an aligned buffer from _this
template<typena me T>
static char* align_ptr(
char const* const _this,
ptrdiff_t alignsz = align_of<T>::c_ result
) {
ptrdiff_t offsetsz =
reinterpret_cas t<ptrdiff_t>(_t his) % alignsz;

if (offsetsz) {
assert(offsetsz < alignsz);

char const* const offsetptr =
reinterpret_cas t<char const*>(_this) + alignsz - offsetsz;

if (reinterpret_ca st<ptrdiff_t>(o ffsetptr) % alignsz) {
assert(! (reinterpret_ca st<ptrdiff_t>(o ffsetptr) % alignsz));
throw;
}

printf("%p alignptr(%p, %i); // offset %i\n",
(void*)offsetpt r, (void*)_this, alignsz, offsetsz);

return const_cast<char *>(offsetptr);
}

printf("%p alignptr(%p, %i);\n",
(void*)_this, (void*)_this, alignsz);

return const_cast<char *>(_this);
}
// a buffer and an aligned pointer into it...
template<size_t T_sz, ptrdiff_t T_alignsz = T_sz>
struct aligned_buf {
enum const_e {
c_alignsz = T_alignsz,
c_alignobjsz = T_sz + c_alignsz - 1
};

char c_bufraw[c_alignobjsz];
char* const c_bufalign;

aligned_buf() :
c_bufalign(alig n_ptr<char>(c_b ufraw, c_alignsz)) {
memset(c_bufraw , 0, c_alignobjsz);
}

char* load_align_ptr( ptrdiff_t alignsz) const {
char* const ptr = align_ptr<char> (c_bufalign, alignsz);
return ptr;
}
};
/* App
_______________ _______________ _______________ _______*/
// platform specific
namespace platform {
namespace os {
enum const_e {
c_pagesz = 8192
};
}

namespace arch {
enum const_e {
c_l2cachelinesz = 128
};
}
}
// misc types...
struct temp1 {
char m_1;
short m_2;
};

struct temp2 {
char m_1;
temp1 m_2;
float m_3;
};

struct temp3 {
char m_1;
temp2 m_2;
double m_3;
};
// os page(s) w/ page-boundary alignment type
template<size_t T_pages>
struct pagebuf {
typedef aligned_buf<
platform::os::c _pagesz * T_pages,
platform::os::c _pageszpagebuf_ t;

// pagebuf is os page aligned on page-boundary
pagebuf_t m_buf;
};
int main(void) {
{
// create 4 pages on the stack.
typedef pagebuf<4pages_ t;
pages_t pages;

// l2cachebuf is aligned off of pagebuf
char* const l2cachebuf =
pages.m_buf.loa d_align_ptr(pla tform::arch::c_ l2cachelinesz);
// display page alignment info
printf("\n\n\
(%u)-pages sizeof\n\
(%p)-pages.m_buf.c_b ufraw\n\
(%p)-pages.m_buf.c_b ufalign\n\
(%p)-l2cachebuf\n",
sizeof(pages),
(void*)pages.m_ buf.c_bufraw,
(void*)pages.m_ buf.c_bufalign,
(void*)l2cacheb uf);
// display basic type alignment info
printf("\n\n\
(sz:%u\talign:% i)-char\n\
(sz:%u\talign:% i)-short\n\
(sz:%u\talign:% i)-long\n\
(sz:%u\talign:% i)-float\n\
(sz:%u\talign:% i)-double\n\
(sz:%u\talign:% i)-long double\n\
(sz:%u\talign:% i)-void* align\n\
(sz:%u\talign:% i)-void* (*) (void*)\n\
(sz:%u\talign:% i)-temp1\n\
(sz:%u\talign:% i)-temp2\n\
(sz:%u\talign:% i)-temp3\n",
sizeof(char), align_of<char>: :c_result,
sizeof(short), align_of<short> ::c_result,
sizeof(long), align_of<long>: :c_result,
sizeof(float), align_of<float> ::c_result,
sizeof(double), align_of<double >::c_result,
sizeof(long double), align_of<long double>::c_resu lt,
sizeof(void*), align_of<void*> ::c_result,
sizeof(void* (*) (void*)), align_of<void* (*) (void*)>::c_res ult,
sizeof(temp1), align_of<temp1> ::c_result,
sizeof(temp2), align_of<temp2> ::c_result,
sizeof(temp3), align_of<temp3> ::c_result);
}

puts("\n\n\n\
_______________ ________\npress <enterto exit...\n");
return getchar();
}

----------------
This alignment code will be used in a per-thread C++ allocator scheme. I am
always looking for better ways of doing the "alignment" stuff. If you have
any suggestions I would like to hear them...

Aug 21 '07 #1
1 2084
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:E6******** *************** *******@comcast .com...
>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 <cstring>
#include <cassert>
[...]

OOPS!

I forgot to add:

#include <cstddef>
darn. Sorry about that.

Aug 22 '07 #2

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

Similar topics

2
3448
by: Joshua Kolden | last post by:
STL allocators are templates so that when you write one you are obliged to make it work with any type. However, the Intel IPP library that we use has memory aligned allocators for each of 15 different types. For example an 8 bit unsigned array is allocated with ippsMalloc_8u(size). So I want to create memory aligned allocators for use with the STL (in particular the vector container) that is fast (due to alignment), and pointer...
4
4135
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if people would back me up on this. The design basically uses a pool of memory, allocated as a character array. Pointers into the array are retured by the allocated function. Isn't this very dangerous, as a char has very lenient memory alignment...
10
3827
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my application that the code be speed optimized ) I use reinterpret_cast to alias a block of chars read in from disc as a block of integer "words". My existing code (see simplified code below) appears to work well enough on the platforms available to me,...
3
1904
by: joe | last post by:
I have written a custom std allocator which follows the example in Stroustrup's book. I'm seeing a behavior I don't understand. My allocate method within the allocator looks like the following: (FooAllocator<T>) pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer hint = 0)
15
7970
by: shaanxxx | last post by:
why malloc (allocator) guarantees that address return by them will be aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?
1
2066
by: Chris Thomasson | last post by:
This region allocator is not dynamic and can be fed with a buffer residing on the stack of the calling thread. Therefore, you can use this in an environment which does not have a heap; its basically analogous to `alloca'. However, it does not align each buffer on a boundary sufficient for _any_ type. Instead, it dynamically calculates the types alignment requirements and mutates the offset accordingly. This makes it more efficient wrt...
11
1736
by: Chris Thomasson | last post by:
Here is the code: http://pastebin.com/m4a405e67 One advantage to using a region allocator is that you can usually skip most calls to free. Instead you can merge multiple free calls into a single reset call. Here is simple example: ___________________________________________________________________
31
1699
by: Chris Thomasson | last post by:
How many C compilers provide extensions which allow for a standard implementation of the following hack? ____________________________________________________________________ #include <stdio.h> typedef union aligner_types_u aligner_types; typedef struct aligner_offset_s aligner_offset;
8
2833
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 issues for some time. What I usually want is a type that takes the same amount of space and has the same alignment as some other type, but which doesn't have constructors, destructors etc. The idea is that initialisation and cleanup can be done...
0
9601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.