473,626 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

standard memory allocator alignment issue...

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;
union aligner_types_u {
char char_;
short s_l;
int i_;
long l_;
double d_;
long double ld_;
float f_;
void *p_;
char (*fp0_) (char);
long double (*fp1_) (char, long double);
union aligner_types* uap_;
/* long long ll_; */
/* [...] */
};
struct aligner_offset_ s {
char offset;
aligner_types types;
};
#define ALIGN_MAX ( \
sizeof(aligner_ offset) sizeof(aligner_ types) \
? sizeof(aligner_ offset) - sizeof(aligner_ types) \
: sizeof(aligner_ types) \
)
int main() {
printf("ALIGN_M AX == %d\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}

_______________ _______________ _______________ _______________ ________

Thanks...

BTW, the ALIGN_MAX macro is needed because using a sizeof(aligner_ types)
alone is not sufficient... How many people are running platforms where
(ALIGN_MAX == 8) is true?

Jun 27 '08 #1
31 1667
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:5u******** *************** *******@comcast .com...
How many C compilers provide extensions which allow for a standard
implementation of the following hack?
[...]

Standard in the sense of being portable within the various versions of a
given vendors C compiler...

:^o

Jun 27 '08 #2
Chris Thomasson wrote:
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;
union aligner_types_u {
char char_;
short s_l;
int i_;
long l_;
double d_;
long double ld_;
float f_;
void *p_;
char (*fp0_) (char);
long double (*fp1_) (char, long double);
union aligner_types* uap_;
/* long long ll_; */
/* [...] */
};
struct aligner_offset_ s {
char offset;
aligner_types types;
};
#define ALIGN_MAX ( \
sizeof(aligner_ offset) sizeof(aligner_ types) \
? sizeof(aligner_ offset) - sizeof(aligner_ types) \
: sizeof(aligner_ types) \
)
int main() {
printf("ALIGN_M AX == %d\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}

_______________ _______________ _______________ _______________ ________

Thanks...

BTW, the ALIGN_MAX macro is needed because using a sizeof(aligner_ types)
alone is not sufficient... How many people are running platforms where
(ALIGN_MAX == 8) is true?
Observation #1: It is impossible that the "else" branch
of ALIGN_MAX' expansion will be evaluated, so you might as
well just put `42' there.

Observation #2: ALIGN_MAX computes the number of bytes
in the struct, minus one for the `char' element, minus the
number of padding bytes before the union, minus the number
of padding bytes *after* the union. I've never seen a
compiler where that final term would be non-zero, but ...

Observation #3: On some platforms the program will say
"ALIGN_MAX == 0", because that's one of the likely outcomes
of the undefined behavior in the printf() call.

Observation #4: I'm not entirely sure, but I think the
"extension" you seek is the offsetof macro in <stddef.h>.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:0f******** *************** *******@comcast .com...
Chris Thomasson wrote:
>How many C compilers provide extensions which allow for a standard
implementati on of the following hack?
______________ _______________ _______________ _______________ _________
[...]
>______________ _______________ _______________ _______________ _________
[...]
>BTW, the ALIGN_MAX macro is needed because using a sizeof(aligner_ types)
alone is not sufficient... How many people are running platforms where
(ALIGN_MAX == 8) is true?

Observation #1: It is impossible that the "else" branch
of ALIGN_MAX' expansion will be evaluated, so you might as
well just put `42' there.
Observation #2: ALIGN_MAX computes the number of bytes
in the struct, minus one for the `char' element, minus the
number of padding bytes before the union, minus the number
of padding bytes *after* the union. I've never seen a
compiler where that final term would be non-zero, but ...
Observation #3: On some platforms the program will say
"ALIGN_MAX == 0", because that's one of the likely outcomes
of the undefined behavior in the printf() call.
Totally agree with everything you said. As for printf, at least I should
have it formatted for an unsigned integer: %u. ;^(
Observation #4: I'm not entirely sure, but I think the
"extension" you seek is the offsetof macro in <stddef.h>.
You got it:
_______________ _______________ _______________ _______________ _______
#include <stdio.h>
#include <stddef.h>
typedef union aligner_types_u aligner_types;
typedef struct aligner_offset_ s aligner_offset;
union aligner_types_u {
char char_; short s_l; int i_; long l_;
double d_; long double ld_; float f_;
union aligner_types* uap_;
void *p_; char (*fp0_) (char);
long double (*fp1_) (char, long double);
/* long long ll_; */
/* [...] */
};
struct aligner_offset_ s {
char offset;
aligner_types types;
};
#define ALIGN_MAX offsetof(aligne r_offset, types)
int main() {
printf("ALIGN_M AX == %u\n\nhit enter to exit...\n", ALIGN_MAX);
getchar();
return 0;
}
_______________ _______________ _______________ _______________ _______

I am trying to come up with somewhat "portable" hack that can attempt to
determine maximum alignment for integral types across a number of different
compilers.

Jun 27 '08 #4
"Chris Thomasson" <cr*****@comcas t.netwrote in message
news:tf******** *************** *******@comcast .com...
"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:0f******** *************** *******@comcast .com...
>Chris Thomasson wrote:
>>How many C compilers provide extensions which allow for a standard
implementatio n of the following hack?
_____________ _______________ _______________ _______________ __________
[...]
>>_____________ _______________ _______________ _______________ __________
[...]
> Observation #4: I'm not entirely sure, but I think the
"extension" you seek is the offsetof macro in <stddef.h>.

You got it:
_______________ _______________ _______________ _______________ _______
[...]
_______________ _______________ _______________ _______________ _______
I am trying to come up with somewhat "portable" hack that can attempt to
determine maximum alignment for integral types across a number of
different compilers.
I have been successfully using the previous method in several
general-purpose memory allocators. However, I wanted to see if there is a
better way; offsetof works fine. BTW, does anybody know why there is not
something like ALIGN_MAX in <limits.halread y?

Jun 27 '08 #5
Chris Thomasson wrote:
[... determining alignment with a struct and offsetof ...]

I have been successfully using the previous method in several
general-purpose memory allocators. However, I wanted to see if there is
a better way; offsetof works fine. BTW, does anybody know why there is
not something like ALIGN_MAX in <limits.halread y?
The Rationale doesn't say why not. My guess (and it's only
a guess) is that the Committee didn't want to get too involved
in specifying exactly how pointers convert to and from integers.
Without knowledge of which integer bits have what significance,
you can't make effective use of things like ALIGN_MAX.

--
Er*********@sun .com
Jun 27 '08 #6
"Eric Sosman" <Er*********@su n.comwrote in message
news:1210630927 .374478@news1nw k...
Chris Thomasson wrote:
>[... determining alignment with a struct and offsetof ...]

I have been successfully using the previous method in several
general-purpose memory allocators. However, I wanted to see if there is a
better way; offsetof works fine. BTW, does anybody know why there is not
something like ALIGN_MAX in <limits.halread y?

The Rationale doesn't say why not. My guess (and it's only
a guess) is that the Committee didn't want to get too involved
in specifying exactly how pointers convert to and from integers.
Without knowledge of which integer bits have what significance,
you can't make effective use of things like ALIGN_MAX.
Humm, a compiler vendor already has to supply a malloc implementation which
returns an address that is aligned on a sufficient boundary for all integral
types; right? Well, IMVHO, the standard could mention that a vendor shall
set the value of ALIGN_MAX to a sufficient boundary analogous to the
non-NULL return value of malloc which can accompany the alignment of any
integral type. The rational is that a vendor can likely extract ALIGN_MAX
from their existing malloc implementation. ..

Is that total crap?

;^)

Jun 27 '08 #7
On 12 May, 23:41, "Chris Thomasson" <cris...@comcas t.netwrote:
"Eric Sosman" <Eric.Sos...@su n.comwrote in message

news:1210630927 .374478@news1nw k...
Chris Thomasson wrote:
[... determining alignment with a struct and offsetof ...]
I have been successfully using the previous method in several
general-purpose memory allocators. However, I wanted to see if there is a
better way; offsetof works fine. BTW, does anybody know why there is not
something like ALIGN_MAX in <limits.halread y?
The Rationale doesn't say why not. My guess (and it's only
a guess) is that the Committee didn't want to get too involved
in specifying exactly how pointers convert to and from integers.
Without knowledge of which integer bits have what significance,
you can't make effective use of things like ALIGN_MAX.

Humm, a compiler vendor already has to supply a malloc implementation which
returns an address that is aligned on a sufficient boundary for all integral
types; right? Well, IMVHO, the standard could mention that a vendor shall
set the value of ALIGN_MAX to a sufficient boundary analogous to the
non-NULL return value of malloc which can accompany the alignment of any
integral type. The rational is that a vendor can likely extract ALIGN_MAX
from their existing malloc implementation. ..
If you had ALIGN_MAX what would you do with it ?

Jun 27 '08 #8
"Ben Bacarisse" <be********@bsb .me.ukwrote in message
news:87******** ****@bsb.me.uk. ..
Keith Thompson <ks***@mib.orgw rites:
>Spiros Bousbouras <sp****@gmail.c omwrites:
>>On 12 May, 23:41, "Chris Thomasson" <cris...@comcas t.netwrote:
"Eric Sosman" <Eric.Sos...@su n.comwrote in message

news:1210630 927.374478@news 1nwk...

Chris Thomasson wrote:
[... determining alignment with a struct and offsetof ...]

I have been successfully using the previous method in several
general-purpose memory allocators.
[...]
>>>>
The Rationale doesn't say why not. My guess (and it's only
a guess) is that the Committee didn't want to get too involved
in specifying exactly how pointers convert to and from integers.
Without knowledge of which integer bits have what significance,
you can't make effective use of things like ALIGN_MAX.

Humm, a compiler vendor already has to supply a malloc implementation
which
returns an address that is aligned on a sufficient boundary for all
integral
types; right?
[...]
>>If you had ALIGN_MAX what would you do with it ?

One thing I might do is write my own memory allocator.
[...]
The suggestion seems to be that ALIGN_MAX is some arithmetic value. I
can't see a way to adjust a pointer using it in any potable way.
[...]
I am currently using something like the following hack to align pointers on
boundaries which are powers of 2:
_______________ _______________ _______________ _______________ ______
#include <stdio.h>
#include <stddef.h>
typedef union align_detail_ty pes_u align_detail_ty pes;
typedef struct align_detail_of fset_s align_detail_of fset;
union align_detail_ty pes_u {
char char_; short s_l; int i_; long l_;
double d_; long double ld_; float f_;
union align_types* uap_;
void *p_; char (*fp0_) (char);
long double (*fp1_) (char, long double);
/* long long ll_; */
/* [...] */
};
struct align_detail_of fset_s {
char offset;
align_detail_ty pes types;
};
typedef long int align_detail_in tptr;
#define ALIGN_MAX offsetof(align_ detail_offset, types)
#define ALIGN_POW2(mp_t his, mp_type) ((mp_type)( \
(((align_detail _intptr const)(mp_this) ) + 1) & (-2) \
))
#define ALIGN(mp_this, mp_type, mp_align) ((mp_type)( \
(((align_detail _intptr const)(mp_this) ) + \
ALIGN_POW2(mp_a lign, align_detail_in tptr const) - 1) \
& (-ALIGN_POW2(mp_a lign, align_detail_in tptr const)) \
))
#define ALIGN_CHECK(mp_ this, mp_type, mp_align) ( \
(mp_this) == ALIGN(mp_this, mp_type, mp_align) \
)
typedef char ALIGN_DETAIL_SA SSERT[
(ALIGN_MAX)
&& (ALIGN_CHECK(AL IGN_MAX, size_t, 2))
&& (ALIGN_CHECK(AL IGN_MAX, size_t, sizeof(void*)))
&& (ALIGN_CHECK(AL IGN_MAX, size_t, sizeof(void* (*) (void*))))
&& (sizeof(align_d etail_intptr) >= sizeof(void*))
&& (sizeof(align_d etail_intptr) >= sizeof(void* (*) (void*)))
? 1 : -1
];

#define L2CACHE_SIZE 128
#define BLOCK_SIZE 4096
#define SUPERBLOCK_SIZE (BLOCK_SIZE * 8)
int main() {
unsigned char* rawbuf[(SUPERBLOCK_SIZ E * 2) - 1];

unsigned char* l2cachebuf =
ALIGN(rawbuf, unsigned char*, L2CACHE_SIZE);

unsigned char* pagebuf =
ALIGN(rawbuf, unsigned char*, BLOCK_SIZE);

unsigned char* superbuf =
ALIGN(rawbuf, unsigned char*, SUPERBLOCK_SIZE );

printf("(%u) == ALIGN_MAX\n(%p) == rawbuf\n\
(%p) == l2cachebuf\n(%p ) == pagebuf\n(%p) == superbuf\n",
(unsigned)ALIGN _MAX, (void*)rawbuf, (void*)l2cacheb uf,
(void*)pagebuf, (void*)superbuf );

return 0;
}

_______________ _______________ _______________ _______________ ______


Any suggestions on how to improve the abomination?

;^)

If you are assuming that the suggestion is for a function-line macro:

#define ALIGN_MAX(p)

that behaves like a function declared void *ALIGN_MAX(void *) which
adjusts the pointer it is given, then I agree it would be possible to
write an allocator. (But I would suggest the name be lower-cased to
match things like offsetof and changed to sound less like a constant
-- another parameter might also be required to indicate "direction" ).
That should be workable; I like it.

Jun 27 '08 #9
Eric Sosman <es*****@ieee-dot-org.invalidwrit es:
Ben Bacarisse wrote:
>>
The suggestion seems to be that ALIGN_MAX is some arithmetic value. I
can't see a way to adjust a pointer using it in any potable way.

Ahh! That wets the whistle proper, that does!
*sigh* At least this one was mildly amusing. Most of my typos invert
the meaning of the text.

--
Ben.
Jun 27 '08 #10

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

Similar topics

18
6666
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.
4
4124
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...
6
3840
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of the bit-field object from the size used to specify the field. Could this be considered a defacto standard now (at least for 8 bit sized bit-fields)? Any recent compilers not allowing this?
22
2282
by: Chris Thomasson | last post by:
I am thinking about using this technique for all the "local" memory pools in a paticular multi-threaded allocator algorithm I invented. Some more info on that can be found here: http://groups.google.com/group/comp.arch/browse_frm/thread/24c40d42a04ee855 Anyway, here is the code snippet: #include <cstdio> #include <cstddef>
31
1658
by: Francine.Neary | last post by:
One interesting thing to come out of the recent "Alignment" thread is that it is impossible to write a portable replacement for malloc in "user space" (not sure what the right term is - I mean an ordinary programmer, not an implementor) - even a naive method using a large array isn't guaranteed to work if there's no way of having a variable of strictest alignment. Oh, for the sake of the pedants, let's discount void *my_malloc(size_t...
0
8269
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
8203
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
8642
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...
1
8368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7203
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...
0
5576
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
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
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
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.