Article View: comp.lang.c
Article #2048Re: C and its peasant 'ancestors'
From: Chris M. Thomasson
Date: Tue, 01 Dec 2026 05:48
Date: Tue, 01 Dec 2026 05:48
125 lines
4757 bytes
4757 bytes
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 <11eiqbe$332f$1@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)
Technical Details
Message-ID:
<11eln3s$19p22$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dup6q$v4j9$1@dont-email.me> <11ehuri$3ps8n$1@raubtier-asyl.eternal-september.org> <11ei9tp$sps$1@reader2.panix.com> <11eiqbe$332f$1@raubtier-asyl.eternal-september.org> <11eisjs$f7p$1@reader2.panix.com> <11ej2k8$5cc4$1@raubtier-asyl.eternal-september.org> <11ellkg$196o4$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c Subject: Re: C and its peasant 'ancestors' Date: Mon, 30 Nov 2026 21:48:44 -0800 Organization: A noiseless patient Spider Lines: 125 Message-ID: <11eln3s$19p22$1@dont-email.me> References: <11dup6q$v4j9$1@dont-email.me> <11ehuri$3ps8n$1@raubtier-asyl.eternal-september.org> <11ei9tp$sps$1@reader2.panix.com> <11eiqbe$332f$1@raubtier-asyl.eternal-september.org> <11eisjs$f7p$1@reader2.panix.com> <11ej2k8$5cc4$1@raubtier-asyl.eternal-september.org> <11ellkg$196o4$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 01 Dec 2026 05:48:45 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1369154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/h5Ud7lbznT2r7XBbcbWD/qyPKxA86nUA="; posting-host="5898cded6d2676b56560081c09d52a4a" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:FHkNw2QhESqKLRvVLwJTE1BhWcg= sha256:RpoFwnqxadLY9puvU7KIReIxgRoraD4nWUSEPRrdM64= sha1:zatnKKXlRK0CgfPJzGseUAQ8+ng= sha256:pbnEdUL6A2qPvLnn1izNPgiie4vEFX6ymKBNIcdALUY= Content-Language: en-US In-Reply-To: <11ellkg$196o4$1@dont-email.me>