[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast
From: |
Juan Quintela |
Subject: |
[Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast |
Date: |
Wed, 02 Dec 2009 20:03:22 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
"Michael S. Tsirkin" <address@hidden> wrote:
> I don't understand.
> container_of is just more generic than DO_UPCAST.
> So why *ever* use DO_UPCAST? Let's get rid of it.
functions that use a PCIDevice and you pass FooState "require" that
PCIDevice to be the 1st element in the struct.
Notice that it is "required", not "would be nice" or similar. If that
is not the way, things dont' work. In this particular case, it is also
required that VirtIODevice to be the 1st element:
VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
size_t config_size, size_t struct_size)
{
VirtIODevice *vdev;
vdev = qemu_mallocz(struct_size);
vdev->device_id = device_id;
vdev->status = 0;
vdev->isr = 0;
vdev->queue_sel = 0;
vdev->config_vector = VIRTIO_NO_VECTOR;
vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
vdev->name = name;
vdev->config_len = config_size;
if (vdev->config_len)
vdev->config = qemu_mallocz(config_size);
else
vdev->config = NULL;
return vdev;
}
See how you create a device of size struct_size, but then you access it
with vdev. If vdev is _not_ the 1st element of the struct, you have got
corruption. DO_UPCAST() prevent you for having that error.
container_of() would have leave you go around, and have a memory
corruption not easy to fix.
DO_UPCAST() macro was created just to avoid this kind of errors.
Later, Juan.
- [Qemu-devel] [PATCH 03/41] virtio: Teach virtio-console about DO_UPCAST, (continued)
- [Qemu-devel] [PATCH 03/41] virtio: Teach virtio-console about DO_UPCAST, Juan Quintela, 2009/12/02
- [Qemu-devel] [PATCH 04/41] virtio: Teach virtio-net about DO_UPCAST, Juan Quintela, 2009/12/02
- [Qemu-devel] [PATCH 05/41] virtio-console: Remove useless casts, Juan Quintela, 2009/12/02
- [Qemu-devel] [PATCH 07/41] virtio-pci: Remove duplicate test, Juan Quintela, 2009/12/02
- [Qemu-devel] [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Juan Quintela, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Michael S. Tsirkin, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Juan Quintela, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Michael S. Tsirkin, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Juan Quintela, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Michael S. Tsirkin, 2009/12/02
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast,
Juan Quintela <=
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Michael S. Tsirkin, 2009/12/03
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Juan Quintela, 2009/12/03
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Michael S. Tsirkin, 2009/12/03
- [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Juan Quintela, 2009/12/03
- Re: [Qemu-devel] Re: [PATCH 06/41] virtio: Use DO_UPCAST instead of a cast, Avi Kivity, 2009/12/03
[Qemu-devel] [PATCH 08/41] msix: Store sizes that we send/receive, Juan Quintela, 2009/12/02
[Qemu-devel] [PATCH 09/41] msix: port to vmstate, Juan Quintela, 2009/12/02
[Qemu-devel] [PATCH 10/41] qemu/pci: document msix_entries_nr field, Juan Quintela, 2009/12/02
[Qemu-devel] [PATCH 11/41] virtio: Introduce type field to distingish between PCI and Syborg, Juan Quintela, 2009/12/02