Re: C++ in kernel (was Re: exception in a device driver)

Khimenko Victor (khim@sch57.msk.ru)
Sat, 16 Jan 1999 23:13:07 +0300 (MSK)


In <19990116134458.F4232@perlsupport.com> Chip Salzenberg (chip@perlsupport.com) wrote:
CS> According to Khimenko Victor:
>> Now about speed issue:

>> -- C++ code --
>> struct Base {
>> int x;
>> virtual foo() { }
>> virtual ~Base() { }
>> };

CS> This C++ code is not equivalent to the C code below, because the C
CS> code does not include any equivalent to a virtual destructor.

Big deal.

-- cut --
struct Base;

struct BaseVptr {
int (*foo)(struct Base* this);
void (*destructor)(struct Base* this);
};

struct Base {
int x;
struct BaseVptr *Vptr;
};

int Base_foo(struct Base* this) {
}

void Base_destructor(struct Base* this) {
}

struct BaseVptr Base_Vptr = { Base_foo, Base_destructor };

int Derived1_foo(struct Base* this) {
}

void Derived1_destructor(struct Base* this) {
}

struct BaseVptr Derived1_Vptr = { Derived1_foo, Derived1_destructor };

int Derived2_foo(struct Base* this) {
}

void Derived2_destructor(struct Base* this) {
}

struct BaseVptr Derived2_Vptr = { Derived2_foo, Derived2_destructor };

main() {
struct Base * volatile target = (struct Base*)malloc(sizeof(struct Base));
int i;
target->x=0;
target->Vptr=&Derived1_Vptr;
for (i=0;i<1000000000;i++) {
// Cast to Derived2
target->Vptr=&Derived2_Vptr;
// Cast to Derived1
target->Vptr=&Derived1_Vptr;
}
}
-- cut --

The same 77s.

P.S. You could say that it's dangerous and such but this is portable at least!

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/