473,777 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory allocators & proper alignment...

Here is some info on a C++ allocator prototype I am working on:

http://groups.google.com/group/comp....eee1f61fdbb52c

Any tried-and-true techniques for calculating the correct alignment of any
C++ type the user can throw at it? For the initial code, I was assuming that
the alignment(T) == sizeof(T)... Now that I am so close to being able to
release this thing, I wanted to be able to stitch up this loose end.

All of the allocators I have worked on were always uses by entities which
were private to a library implementation. .. Any they only needed to align
structures on level-2 cache-line boundaries. So, I didn't need to align for
the type, I only align for the cache line.

Now I need to figure out how to get the correct alignment of any C++ type a
user can come up with...

Help!
:^)

--
Chris M. Thomasson
http://appcore.home.comcast.net

May 16 '07
13 3633
Here is the caveat!

I need to be able to create an instance of your single-threaded allocator by
using extern "C" Create/Destroy function pointers.

May 21 '07 #11
Here is some updated rough-example code:

http://appcore.home.comcast.net/vzoo...oc/example.cpp
Notice any problems right off the bat?

May 22 '07 #12
One more quick question...

wrt overloading global new and delete operators...

struct alignchar {
char m_buf;
};

void* ::operator new(size_t sz) {
// alloc and align buf on sizeof(aligncha r) boundary
char* const buf = ::mymalloc_alig ned(sz, sizeof(aligncha r));
if (! buf) { throw std::bad_alloc( ); }
return buf ;
}

void ::operator delete(void *buf) {
::myfree(buf);
}
Is that sufficient alignment? AFAICT, void* ::operator new(size_t) does not
have to align for the specific C++ type... It just has to mimic malloc
alignment requirements right?

And, the size variable (e.g., size_t sz) passed to the void* ::operator
new(size_t) function when new is called with any type should be large enough
to accommodate the alignment for those types?
Thanks.
May 22 '07 #13
On May 22, 8:14 pm, "Chris Thomasson" <cris...@comcas t.netwrote:
One more quick question...
wrt overloading global new and delete operators...
struct alignchar {
char m_buf;
};
Not sure what you're trying to achieve here, but on all of the
platforms I have access to, sizeof( alignchar ) is 1. About the
only cases where I would expect something different would be on
a word addressed machine.
void* ::operator new(size_t sz) {
// alloc and align buf on sizeof(aligncha r) boundary
char* const buf = ::mymalloc_alig ned(sz, sizeof(aligncha r));
if (! buf) { throw std::bad_alloc( ); }
return buf ;
}
void ::operator delete(void *buf) {
::myfree(buf);
}
Is that sufficient alignment? AFAICT, void* ::operator new(size_t) does not
have to align for the specific C++ type... It just has to mimic malloc
alignment requirements right?
The returned address should be sufficiently aligned for any data
type. Just like malloc, yes.
And, the size variable (e.g., size_t sz) passed to the void* ::operator
new(size_t) function when new is called with any type should be large enough
to accommodate the alignment for those types?
Run that by me again. There's no relationship between the size
argument and the alignment requirements for operator new.
Although logically... my machine requires an alignment of 8 for
double, but it doesn't make much sense to require operator
new(4) to return memory aligned on an 8 byte boundary, because
there's no way you can write anything larger than a float or an
int into the returned memory, and they only require an alignment
of 4. But the requirement in the standard (§3.7.3.1) is clear:
"The pointer returned shall be suitably aligned so that it can
be converted to a pointer of any complete object type and then
used to access the object or array in the storage allocated."
"Any complete object type", and not "Any complete object type
which will fit in the allocated memory".

The usual way to determine alignment of a type T, I believe, is
something like:

struct AlignOfT { char a ; T b ; } ;
size_t alignOfT() { return offsetof( AlignOfT, b ) ; }

(This only works for POD types, of course.)

If you want to ensure alignment for any possible type, use a
union of all the basic types, plus a few pointers (to char, to a
struct, to a function, etc.) for T in the above. It's not
formally guaranteed by the standard, but it should work on any
real platform.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #14

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

Similar topics

18
6681
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a hard drive file system. Over time, the more often use call new/delete or alloc/free, there will be gaps and fragments in the heap. This can lead to inefficient use of available memory, as well as cache-hit inefficiencies.
11
2664
by: aaaaa | last post by:
Hi all, Does anybody know if STLPort or SGI STL standard allocators do memory pooling for the list, map and set? Also I have had a look at the BOOST pool_alloc (to be used as a pooling allocator for lists), but looking into the code it doesn't seem to ever release (to the global ::free) the memory that was once allocated. I can understand that such memory can be re-used if I have another list
6
1518
by: Ares Lagae | last post by:
Hello, I am trying to create a container the stl way, and I have a couple of questions. The code of the container in question can be found at http://www.cs.kuleuven.ac.be/~ares/tmp/array_2_hpp.html It is a 2d array that dynamically allocates its storage. It cannot be resized. It offers element access methods for both sequential access and 2d element access.
13
684
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 memory alignment?
29
2835
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? Here N is a positive integer, and I am assuming that malloc() succeeds in allocating the chunk of memory specified.
3
2077
by: yasmin | last post by:
I am dealing with internal memory fragmentation issues in my program (C++ program on freeBSD). It involves a large number of fixed-size structures. For example for my records which are typically fixed at 100 bytes, the default allocator allocates 128 bytes (closest power of two) wasting 28 bytes per records and this adds up to a significant amount over several thousand records. Before jumping in and writing my own custom allocator which...
2
3800
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long Align; union header { struct {
31
1692
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;
12
3004
by: David Given | last post by:
I have a situation where I need to be able to allocate chunks on unmapped virtual memory. Because the memory I'm allocating isn't mapped, I can't use a normal memory allocator, as most of them want to store their metadata about the free list in the unused holes in the heap. Instead, I need a memory allocator that stores its metadata out-of-line (perversely enough, a simple malloc() heap would be fine for that). Does anyone know of...
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
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
7471
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
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
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.