Skip to contentSkip to conversation

ide: Get rid of IDEDrive struct

PATCH In review Eduardo Habkost 5 participants 7 messages Aug 5, 2020 – Aug 8, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ide: Get rid of IDEDrive struct

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

---
 hw/ide/qdev.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
hw/ide/qdev.c +9 -16
index 27ff1f7f66..dd3867d8b3 100644
@@ -157,10 +157,6 @@ int ide_get_bios_chs_trans(BusState *bus, int unit)
 
 /* --------------------------------- */
 
-typedef struct IDEDrive {
-    IDEDevice dev;
-} IDEDrive;
-
 static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
 {
     IDEBus *bus = DO_UPCAST(IDEBus, qbus, dev->qdev.parent_bus);
@@ -297,19 +293,19 @@ static void ide_drive_realize(IDEDevice *dev, Error **errp)
 }
 
 #define DEFINE_IDE_DEV_PROPERTIES()                     \
-    DEFINE_BLOCK_PROPERTIES(IDEDrive, dev.conf),        \
-    DEFINE_BLOCK_ERROR_PROPERTIES(IDEDrive, dev.conf),  \
-    DEFINE_PROP_STRING("ver",  IDEDrive, dev.version),  \
-    DEFINE_PROP_UINT64("wwn",  IDEDrive, dev.wwn, 0),   \
-    DEFINE_PROP_STRING("serial",  IDEDrive, dev.serial),\
-    DEFINE_PROP_STRING("model", IDEDrive, dev.model)
+    DEFINE_BLOCK_PROPERTIES(IDEDevice, conf),        \
+    DEFINE_BLOCK_ERROR_PROPERTIES(IDEDevice, conf),  \
+    DEFINE_PROP_STRING("ver",  IDEDevice, version),  \
+    DEFINE_PROP_UINT64("wwn",  IDEDevice, wwn, 0),   \
+    DEFINE_PROP_STRING("serial",  IDEDevice, serial),\
+    DEFINE_PROP_STRING("model", IDEDevice, model)
 
 static Property ide_hd_properties[] = {
     DEFINE_IDE_DEV_PROPERTIES(),
-    DEFINE_BLOCK_CHS_PROPERTIES(IDEDrive, dev.conf),
+    DEFINE_BLOCK_CHS_PROPERTIES(IDEDevice, conf),
     DEFINE_PROP_BIOS_CHS_TRANS("bios-chs-trans",
-                IDEDrive, dev.chs_trans, BIOS_ATA_TRANSLATION_AUTO),
-    DEFINE_PROP_UINT16("rotation_rate", IDEDrive, dev.rotation_rate, 0),
+                IDEDevice, chs_trans, BIOS_ATA_TRANSLATION_AUTO),
+    DEFINE_PROP_UINT16("rotation_rate", IDEDevice, rotation_rate, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_hd_info = {
     .name          = "ide-hd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
 };
 
@@ -350,7 +345,6 @@ static void ide_cd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_cd_info = {
     .name          = "ide-cd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_cd_class_init,
 };
 
@@ -373,7 +367,6 @@ static void ide_drive_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_drive_info = {
     .name          = "ide-drive",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_drive_class_init,
 };
 
Signature · 1 line
2.26.2
#1 lore raw

hw/ide/qdev.c static void ide_hd_class_init(ObjectClass *klass, void *data) · line 327

Quoted 13 lines · Eduardo Habkost

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

hw/ide/qdev.c +0 -1
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_hd_info = {
     .name          = "ide-hd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
 };

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

We define in https://wiki.qemu.org/Documentation/QOMConventions
(in the 'When to create class types and macros' bit at the bottom) what we expect for whether to provide class cast macros/a class struct/class_size in the TypeInfo, essentially recommending that types follow one of two patterns (simple leaf class with no methods or class members, vs everything else) even if in a particular case you could take a short-cut and not define everything. We haven't really defined similar "this is the standard pattern, provide it all even if you don't strictly need it" rules for the instance struct/macros. Maybe we should?

Just a thought, not a nak; I know we have quite a number of types that take this kind of "we don't really need to provide all the standard QOM macros/structs/etc" approach (some of which I wrote!).

thanks
-- PMM

#2 lore raw
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Eduardo Habkost

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

Quoted 16 lines

hw/ide/qdev.c static void ide_hd_class_init(ObjectClass *klass, void *data) · line 327

Quoted 8 lines
hw/ide/qdev.c +0 -1
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_hd_info = {
     .name          = "ide-hd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
 };

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

Is this really a pattern that exists and we want to follow? I don't see why that pattern would be useful for simple leaf types.

Also, in this case the code wasn't even following that pattern:
it was using the same IDEDrive struct for all TYPE_IDE_DEVICE subtypes.

Quoted 11 lines

We define in https://wiki.qemu.org/Documentation/QOMConventions
(in the 'When to create class types and macros' bit at the bottom) what we expect for whether to provide class cast macros/a class struct/class_size in the TypeInfo, essentially recommending that types follow one of two patterns (simple leaf class with no methods or class members, vs everything else) even if in a particular case you could take a short-cut and not define everything. We haven't really defined similar "this is the standard pattern, provide it all even if you don't strictly need it" rules for the instance struct/macros. Maybe we should?

I think we should include the instance struct/macros in the recommendations there, but I would expect those recommendations to apply only to non-leaf types.

Quoted 9 lines

Just a thought, not a nak; I know we have quite a number of types that take this kind of "we don't really need to provide all the standard QOM macros/structs/etc" approach (some of which I wrote!).

thanks
-- PMM

Signature · 1 line
Eduardo
#3 lore raw
Quoted 24 lines · Eduardo Habkost
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Eduardo Habkost

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

Quoted 16 lines

hw/ide/qdev.c static void ide_hd_class_init(ObjectClass *klass, void *data) · line 327

Quoted 8 lines
hw/ide/qdev.c +0 -1
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_hd_info = {
     .name          = "ide-hd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
 };

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

Quoted 3 lines

Is this really a pattern that exists and we want to follow? I don't see why that pattern would be useful for simple leaf types.

I think the pattern exists, but we deviate from it in quite a few places, probably just because it's so much boilerplate.

Related: Daniel's "[PATCH 0/4] qom: reduce boilerplate required for declaring and defining objects". Perhaps Daniel has an opinion on taking shortcuts with leaf types.

Quoted 3 lines

Also, in this case the code wasn't even following that pattern:
it was using the same IDEDrive struct for all TYPE_IDE_DEVICE subtypes.

Rule of thumb: hw/ide/ is a bad example. I don't mean to belittle the efforts of quite a few people over the years. It used to be worse.

Quoted 11 lines
Quoted 10 lines

We define in https://wiki.qemu.org/Documentation/QOMConventions
(in the 'When to create class types and macros' bit at the bottom) what we expect for whether to provide class cast macros/a class struct/class_size in the TypeInfo, essentially recommending that types follow one of two patterns (simple leaf class with no methods or class members, vs everything else) even if in a particular case you could take a short-cut and not define everything. We haven't really defined similar "this is the standard pattern, provide it all even if you don't strictly need it" rules for the instance struct/macros. Maybe we should?

Quoted 3 lines

I think we should include the instance struct/macros in the recommendations there, but I would expect those recommendations to apply only to non-leaf types.

I'm fine with having a separate convention for leaf types if that helps, but please let's have a convention. I like my QOM boilerplate uncreative.

Quoted 8 lines
Quoted 8 lines

Just a thought, not a nak; I know we have quite a number of types that take this kind of "we don't really need to provide all the standard QOM macros/structs/etc" approach (some of which I wrote!).

thanks
-- PMM

#4 lore raw
Quoted 30 lines · Markus Armbruster
Quoted 24 lines · Eduardo Habkost
Quoted 6 lines · Peter Maydell
Quoted 5 lines · Eduardo Habkost

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

Quoted 16 lines

hw/ide/qdev.c static void ide_hd_class_init(ObjectClass *klass, void *data) · line 327

Quoted 8 lines
hw/ide/qdev.c +0 -1
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
 static const TypeInfo ide_hd_info = {
     .name          = "ide-hd",
     .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
     .class_init    = ide_hd_class_init,
 };

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

Quoted 3 lines

Is this really a pattern that exists and we want to follow? I don't see why that pattern would be useful for simple leaf types.

Quoted 6 lines

I think the pattern exists, but we deviate from it in quite a few places, probably just because it's so much boilerplate.

Related: Daniel's "[PATCH 0/4] qom: reduce boilerplate required for declaring and defining objects". Perhaps Daniel has an opinion on taking shortcuts with leaf types.

I think following a consistent pattern everywhere is important, because people look at existing code to guide their new code. The boilerplate pain is very real, but I think my patch series you point to will reduce the burden sufficiently that the kind of optimization proposed here is not required.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw
In reply to Eduardo Habkost
Quoted 9 lines · Eduardo Habkost
Quoted 7 lines · Peter Maydell

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

Quoted 3 lines

Is this really a pattern that exists and we want to follow? I don't see why that pattern would be useful for simple leaf types.

Most leaf types need this. Consider a simple device type like TYPE_CMSDK_APB_UART. It has a TYPE_* name so that users of it can instantiate it; it has a CMSDKAPBUART struct that holds all the device state; it has the CMSDK_APB_UART() cast macro so that code that gets a Device* or Object* can get at the struct. Leaf types like ide-hd which have no actual state of their own are I think the less common case:
most leaf types do have at least some member variables.

As Markus says, we can have a couple of standard patterns if we want to (as we do for the class-macro conventions); I just wanted to explain that lots of leaf types work the way I outline above.

thanks
-- PMM

#6 lore raw
In reply to Peter Maydell
Quoted 34 lines · Peter Maydell

hw/ide/qdev.c static void ide_hd_class_init(ObjectClass *klass, void *data) · line 327

Quoted 13 lines · Eduardo Habkost

The struct had a single field (IDEDevice dev), and is only used in the QOM type declarations and property lists. We can simply use the IDEDevice struct directly instead.

hw/ide/qdev.c +0 -1
@@ -327,7 +323,6 @@ static void ide_hd_class_init(ObjectClass *klass, void *data)
  static const TypeInfo ide_hd_info = {
      .name          = "ide-hd",
      .parent        = TYPE_IDE_DEVICE,
-    .instance_size = sizeof(IDEDrive),
      .class_init    = ide_hd_class_init,
  };

This is one of those areas where this change works and reduces amount of code, but on the other hand it means the QOM type doesn't follow the common pattern for a leaf type of:
* it has a struct
* it has cast macros that cast to that struct
* the typeinfo instance_size is the size of that struct
(it wasn't exactly following this pattern before, of course).

We define in https://wiki.qemu.org/Documentation/QOMConventions
(in the 'When to create class types and macros' bit at the bottom) what we expect for whether to provide class cast macros/a class struct/class_size in the TypeInfo, essentially recommending that types follow one of two patterns (simple leaf class with no methods or class members, vs everything else) even if in a particular case you could take a short-cut and not define everything. We haven't really defined similar "this is the standard pattern, provide it all even if you don't strictly need it" rules for the instance struct/macros. Maybe we should?

Quoted 5 lines

Just a thought, not a nak; I know we have quite a number of types that take this kind of "we don't really need to provide all the standard QOM macros/structs/etc" approach (some of which I wrote!).

I'll defer to your judgment here. The IDE stuff is very confusing, but I don't know the best way to wrangle it to make it less confusing.

I assume at some point migration compatibility gets in the way of any REAL refactoring that might start to make this code make more sense.

Open to suggestions.

--js