qdev: Move property code to qdev-properties.[ch]

Move everything related to Property and PropertyInfo to
qdev-properties.[ch] to make it easier to refactor that code.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20201211220529.2290218-4-ehabkost@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
This commit is contained in:
Eduardo Habkost 2020-12-11 17:05:00 -05:00
parent 1b36e4f5a5
commit d3fd6e7380
5 changed files with 159 additions and 157 deletions

View File

@ -929,3 +929,123 @@ const PropertyInfo qdev_prop_link = {
.name = "link",
.create = create_link_property,
};
void qdev_property_add_static(DeviceState *dev, Property *prop)
{
Object *obj = OBJECT(dev);
ObjectProperty *op;
assert(!prop->info->create);
op = object_property_add(obj, prop->name, prop->info->name,
prop->info->get, prop->info->set,
prop->info->release,
prop);
object_property_set_description(obj, prop->name,
prop->info->description);
if (prop->set_default) {
prop->info->set_default_value(op, prop);
if (op->init) {
op->init(obj, op);
}
}
}
static void qdev_class_add_property(DeviceClass *klass, Property *prop)
{
ObjectClass *oc = OBJECT_CLASS(klass);
if (prop->info->create) {
prop->info->create(oc, prop);
} else {
ObjectProperty *op;
op = object_class_property_add(oc,
prop->name, prop->info->name,
prop->info->get, prop->info->set,
prop->info->release,
prop);
if (prop->set_default) {
prop->info->set_default_value(op, prop);
}
}
object_class_property_set_description(oc, prop->name,
prop->info->description);
}
/**
* Legacy property handling
*/
static void qdev_get_legacy_property(Object *obj, Visitor *v,
const char *name, void *opaque,
Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
char buffer[1024];
char *ptr = buffer;
prop->info->print(dev, prop, buffer, sizeof(buffer));
visit_type_str(v, name, &ptr, errp);
}
/**
* qdev_class_add_legacy_property:
* @dev: Device to add the property to.
* @prop: The qdev property definition.
*
* Add a legacy QOM property to @dev for qdev property @prop.
*
* Legacy properties are string versions of QOM properties. The format of
* the string depends on the property type. Legacy properties are only
* needed for "info qtree".
*
* Do not use this in new code! QOM Properties added through this interface
* will be given names in the "legacy" namespace.
*/
static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
{
g_autofree char *name = NULL;
/* Register pointer properties as legacy properties */
if (!prop->info->print && prop->info->get) {
return;
}
name = g_strdup_printf("legacy-%s", prop->name);
object_class_property_add(OBJECT_CLASS(dc), name, "str",
prop->info->print ? qdev_get_legacy_property : prop->info->get,
NULL, NULL, prop);
}
void device_class_set_props(DeviceClass *dc, Property *props)
{
Property *prop;
dc->props_ = props;
for (prop = props; prop && prop->name; prop++) {
qdev_class_add_legacy_property(dc, prop);
qdev_class_add_property(dc, prop);
}
}
void qdev_alias_all_properties(DeviceState *target, Object *source)
{
ObjectClass *class;
Property *prop;
class = object_get_class(OBJECT(target));
do {
DeviceClass *dc = DEVICE_CLASS(class);
for (prop = dc->props_; prop && prop->name; prop++) {
object_property_add_alias(source, prop->name,
OBJECT(target), prop->name);
}
class = object_class_get_parent(class);
} while (class != object_class_by_name(TYPE_DEVICE));
}

View File

@ -705,115 +705,6 @@ char *qdev_get_dev_path(DeviceState *dev)
return NULL;
}
/**
* Legacy property handling
*/
static void qdev_get_legacy_property(Object *obj, Visitor *v,
const char *name, void *opaque,
Error **errp)
{
DeviceState *dev = DEVICE(obj);
Property *prop = opaque;
char buffer[1024];
char *ptr = buffer;
prop->info->print(dev, prop, buffer, sizeof(buffer));
visit_type_str(v, name, &ptr, errp);
}
/**
* qdev_class_add_legacy_property:
* @dev: Device to add the property to.
* @prop: The qdev property definition.
*
* Add a legacy QOM property to @dev for qdev property @prop.
*
* Legacy properties are string versions of QOM properties. The format of
* the string depends on the property type. Legacy properties are only
* needed for "info qtree".
*
* Do not use this in new code! QOM Properties added through this interface
* will be given names in the "legacy" namespace.
*/
static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
{
g_autofree char *name = NULL;
/* Register pointer properties as legacy properties */
if (!prop->info->print && prop->info->get) {
return;
}
name = g_strdup_printf("legacy-%s", prop->name);
object_class_property_add(OBJECT_CLASS(dc), name, "str",
prop->info->print ? qdev_get_legacy_property : prop->info->get,
NULL, NULL, prop);
}
void qdev_property_add_static(DeviceState *dev, Property *prop)
{
Object *obj = OBJECT(dev);
ObjectProperty *op;
assert(!prop->info->create);
op = object_property_add(obj, prop->name, prop->info->name,
prop->info->get, prop->info->set,
prop->info->release,
prop);
object_property_set_description(obj, prop->name,
prop->info->description);
if (prop->set_default) {
prop->info->set_default_value(op, prop);
if (op->init) {
op->init(obj, op);
}
}
}
static void qdev_class_add_property(DeviceClass *klass, Property *prop)
{
ObjectClass *oc = OBJECT_CLASS(klass);
if (prop->info->create) {
prop->info->create(oc, prop);
} else {
ObjectProperty *op;
op = object_class_property_add(oc,
prop->name, prop->info->name,
prop->info->get, prop->info->set,
prop->info->release,
prop);
if (prop->set_default) {
prop->info->set_default_value(op, prop);
}
}
object_class_property_set_description(oc, prop->name,
prop->info->description);
}
void qdev_alias_all_properties(DeviceState *target, Object *source)
{
ObjectClass *class;
Property *prop;
class = object_get_class(OBJECT(target));
do {
DeviceClass *dc = DEVICE_CLASS(class);
for (prop = dc->props_; prop && prop->name; prop++) {
object_property_add_alias(source, prop->name,
OBJECT(target), prop->name);
}
class = object_class_get_parent(class);
} while (class != object_class_by_name(TYPE_DEVICE));
}
static bool device_get_realized(Object *obj, Error **errp)
{
DeviceState *dev = DEVICE(obj);
@ -1208,17 +1099,6 @@ static void device_class_init(ObjectClass *class, void *data)
offsetof(DeviceState, parent_bus), NULL, 0);
}
void device_class_set_props(DeviceClass *dc, Property *props)
{
Property *prop;
dc->props_ = props;
for (prop = props; prop && prop->name; prop++) {
qdev_class_add_legacy_property(dc, prop);
qdev_class_add_property(dc, prop);
}
}
void device_class_set_parent_reset(DeviceClass *dc,
DeviceReset dev_reset,
DeviceReset *parent_reset)

View File

@ -276,43 +276,6 @@ struct BusState {
ResettableState reset;
};
/**
* Property:
* @set_default: true if the default value should be set from @defval,
* in which case @info->set_default_value must not be NULL
* (if false then no default value is set by the property system
* and the field retains whatever value it was given by instance_init).
* @defval: default value for the property. This is used only if @set_default
* is true.
*/
struct Property {
const char *name;
const PropertyInfo *info;
ptrdiff_t offset;
uint8_t bitnr;
bool set_default;
union {
int64_t i;
uint64_t u;
} defval;
int arrayoffset;
const PropertyInfo *arrayinfo;
int arrayfieldsize;
const char *link_type;
};
struct PropertyInfo {
const char *name;
const char *description;
const QEnumLookup *enum_table;
int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
void (*set_default_value)(ObjectProperty *op, const Property *prop);
void (*create)(ObjectClass *oc, Property *prop);
ObjectPropertyAccessor *get;
ObjectPropertyAccessor *set;
ObjectPropertyRelease *release;
};
/**
* GlobalProperty:
* @used: Set to true if property was used when initializing a device.

View File

@ -3,6 +3,44 @@
#include "hw/qdev-core.h"
/**
* Property:
* @set_default: true if the default value should be set from @defval,
* in which case @info->set_default_value must not be NULL
* (if false then no default value is set by the property system
* and the field retains whatever value it was given by instance_init).
* @defval: default value for the property. This is used only if @set_default
* is true.
*/
struct Property {
const char *name;
const PropertyInfo *info;
ptrdiff_t offset;
uint8_t bitnr;
bool set_default;
union {
int64_t i;
uint64_t u;
} defval;
int arrayoffset;
const PropertyInfo *arrayinfo;
int arrayfieldsize;
const char *link_type;
};
struct PropertyInfo {
const char *name;
const char *description;
const QEnumLookup *enum_table;
int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
void (*set_default_value)(ObjectProperty *op, const Property *prop);
void (*create)(ObjectClass *oc, Property *prop);
ObjectPropertyAccessor *get;
ObjectPropertyAccessor *set;
ObjectPropertyRelease *release;
};
/*** qdev-properties.c ***/
extern const PropertyInfo qdev_prop_bit;

View File

@ -38,6 +38,7 @@
#include "migration/misc.h"
#include "migration/migration.h"
#include "qemu/cutils.h"
#include "hw/qdev-properties.h"
#include "hw/clock.h"
/*