On 11/30/2026 9:23 PM, Chris M. Thomasson wrote:
> On 11/29/2026 9:46 PM, Bonita Montero wrote:
>> Am 30.11.2026 um 05:04 schrieb Dan Cross:
>>
>>> In article <11ei…@raubtier-asyl.eternal-september.org>,
>>
>>>> It is hard since a different class has a different object layout
>>>> with different state.
>>
>>> Hence thunks.
>> ???
>>
>
> Think of storing the original vtable pointer from something, oh, say like in my code:
>
> https://pastebin.com/raw/f52a443b1
>
> struct device {
> struct device_vtable const* vtable;
> };
>
> You make another vtable that acts as a proxy for starters, just intercepts everything then sends along to the stored original vtable:
>
>
>
> struct device_vtable* old = device->vtable;
> device->vtable = &new;
>
> Then in the new table we simply forward things to the old vtable.
>
> Now, we have a man in the middle.
>
> This can be thunked:[...]
Or even something like, going for a simple wrapper:
/* Sample Application
____________________________________________________________________*/
struct device_thunk {
struct device device;
struct device* target;
};
static int thunk_object_destroy(void* const self_) {
struct device_thunk* const self = self_;
int rc;
printf("thunk_object_destroy(%p -> %p)\n", (void*)self, (void*)self->target);
rc = object_destroy(self->target);
free(self);
return rc;
}
static int thunk_device_read(void* const self_, void* buf, size_t size) {
struct device_thunk* const self = self_;
printf("thunk_device_read(%p, %p, %lu)\n", (void*)self, buf, (unsigned long)size);
return device_read(self->target, buf, size);
}
static int thunk_device_write(void* const self_, void const* buf, size_t size) {
struct device_thunk* const self = self_;
printf("thunk_device_write(%p, %p, %lu)\n", (void*)self, buf, (unsigned long)size);
return device_write(self->target, buf, size);
}
static struct device_vtable const g_thunk_table = {
{ thunk_object_destroy },
{ thunk_device_read, thunk_device_write }
};
int device_thunk_create(struct device** const pself, struct device* const target) {
struct device_thunk* const self = malloc(sizeof(*self));
if (self) {
self->device.vtable = &g_thunk_table;
self->target = target;
*pself = &self->device;
return 0;
}
return -1;
}
void read_write(
struct device* const self
) {
char buf[100];
device_read(self, buf, 50);
device_write(self, buf, 5);
}
int main(void) {
struct device* raw_device;
struct device* a_device;
if (! usb_drive_create(&raw_device)) {
if (! device_thunk_create(&a_device, raw_device)) {
read_write(a_device);
object_destroy(a_device); /* destroys raw_device, then frees wrapper */
} else {
object_destroy(raw_device);
}
}
return 0;
}
I get:
thunk_device_read(0xca5757ebc2d0, 0xffffdff38c28, 50)
usb_drive_device_read(0xca5757ebc2b0, 0xffffdff38c28, 50)
thunk_device_write(0xca5757ebc2d0, 0xffffdff38c28, 5)
usb_drive_device_write(0xca5757ebc2b0, 0xffffdff38c28, 5)
thunk_object_destroy(0xca5757ebc2d0 -> 0xca5757ebc2b0)
usb_drive_object_destroy(0xca5757ebc2b0)