* Concatenation of dynamic structs
@ 2003-02-04 1:04 Thomas Heinz
0 siblings, 0 replies; only message in thread
From: Thomas Heinz @ 2003-02-04 1:04 UTC (permalink / raw)
To: linux-kernel
Hi
Consider the following structs:
struct a
{
char len;
char c[0];
};
struct b
{
char len;
void *p[0];
};
Now, we have struct a *x; and we want to store n characters starting
from x->c. After the n characters we want a struct b to begin
which contains m pointers. Everything should be contained in one
contiguous memory block.
Now, several problems arise. First of all, if one wants to allocate
memory for x we cannot simply call
x = kmalloc(sizeof(*x) + n + sizeof(struct b) + m * sizeof(void *));
Instead we need something like this:
#define ALIGN(size, align) (((size) + ((align) - 1)) & (~ ((align) - 1)))
#define MAXALIGN (__alignof__(struct a) > __alignof__(struct b) ? \
__alignof__(struct a) : __alignof__(struct b))
x = kmalloc(ALIGN(sizeof(*x) + n, MAXALIGN) + m * sizeof(void *));
A second problem is how to define a macro (or inline function) that
returns a pointer to struct b within the concatenated struct.
Something like:
#define GETB(x) ((struct b *) ((char *) (x) + \
ALIGN(sizeof(*(x)) + (x)->len, MAXALIGN)))
should work.
Anyway, this is all very ugly. Is there any recommended, portable
solution for concatenating dynamic structs with different alignment
requirements?
Would it work in the general case if we define ALIGN the following way:
#define ALIGN(size) (((size) + ((__alignof__(u64)) - 1)) & \
(~ ((__alignof__(u64)) - 1)))
I.e. is __alignof__(u64) always the "strictest alignment requirement"
for all architectures?
Thanks for your help.
BTW, please cc your reply to my private e-mail since I'm currently
not subscribed.
Regards,
Thomas
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2003-02-04 0:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-04 1:04 Concatenation of dynamic structs Thomas Heinz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox