Juan Quintela wrote:
> vstrucut virtio_common *create_virtio_comon(...., size we really want);
> Again, this implements superclass/subclass as well as you can implemnt
> it in C.

It would be more type-safe for create_virtio_common() to be a macro
taking the subclass *type* rather than sizeof.

And it would make the calls short: No need to cast the result, because
the macro would return the desired type (doing the cast itself).

#define create_virtio_common(...., type) \
   ((type *)_create_virtio_common(...., sizeof(type)))

Once you have that, it's easy to change to add a field name and
container_of:

#define create_virtio_common(...., type, field) \
   (container_of(_create_virtio_common(...., sizeof(type)), type, field))

That gives you malloc in common init, and type-safe callers everywhere
(no possibility for mistaken sizeof).

I think it's a simpler to use API and better at protecting against
caller mistakes; you may disagree.

-- Jamie


Reply via email to