Object-oriented design patterns in the kernel, part 2
Posted Jun 7, 2011 22:29 UTC (Tue) by
cmccabe (guest, #60281)
In reply to:
Object-oriented design patterns in the kernel, part 2 by nix
Parent article:
Object-oriented design patterns in the kernel, part 2
I don't think any macros are really needed, just something like this:
struct base {
int a;
int b;
char end[0] __attribute__((aligned(4)));
};
struct derived1 {
int a;
};
struct derived2 {
char b;
};
Then you have stuff like this:
void foo(struct base *b) {
if (type == DERIVED1) {
struct derived1 *d = (struct derived1*)b->end;
...
}
}
It isn't as nice as C++, but it's ok.
Flexible array members were standardized in C99. Every compiler can do it, but I don't know if the syntax for forcing alignment is standardized yet.
(
Log in to post comments)