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