qapi: Change data type of the FOO_lookup generated for enum FOO

Currently, a FOO_lookup is an array of strings terminated by a NULL
sentinel.

A future patch will generate enums with "holes".  NULL-termination
will cease to work then.

To prepare for that, store the length in the FOO_lookup by wrapping it
in a struct and adding a member for the length.

The sentinel will be dropped next.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20170822132255.23945-13-marcandre.lureau@redhat.com>
[Basically redone]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <1503564371-26090-16-git-send-email-armbru@redhat.com>
[Rebased]
This commit is contained in:
Marc-André Lureau 2017-08-24 10:46:10 +02:00 committed by Markus Armbruster
parent 788b305c91
commit f7abe0ecd4
33 changed files with 106 additions and 89 deletions

View File

@ -395,7 +395,7 @@ host_memory_backend_class_init(ObjectClass *oc, void *data)
host_memory_backend_set_host_nodes, host_memory_backend_set_host_nodes,
NULL, NULL, &error_abort); NULL, NULL, &error_abort);
object_class_property_add_enum(oc, "policy", "HostMemPolicy", object_class_property_add_enum(oc, "policy", "HostMemPolicy",
HostMemPolicy_lookup, &HostMemPolicy_lookup,
host_memory_backend_get_policy, host_memory_backend_get_policy,
host_memory_backend_set_policy, &error_abort); host_memory_backend_set_policy, &error_abort);
object_class_property_add_str(oc, "id", get_id, set_id, &error_abort); object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);

View File

@ -1332,7 +1332,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
detect_zeroes = qemu_opt_get(opts, "detect-zeroes"); detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
if (detect_zeroes) { if (detect_zeroes) {
BlockdevDetectZeroesOptions value = BlockdevDetectZeroesOptions value =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
detect_zeroes, detect_zeroes,
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_err); &local_err);

View File

@ -169,7 +169,7 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
error_setg(errp, "Missing event name for rule"); error_setg(errp, "Missing event name for rule");
return -1; return -1;
} }
event = qapi_enum_parse(BlkdebugEvent_lookup, event_name, -1, errp); event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
if (event < 0) { if (event < 0) {
return -1; return -1;
} }
@ -732,7 +732,7 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
struct BlkdebugRule *rule; struct BlkdebugRule *rule;
int blkdebug_event; int blkdebug_event;
blkdebug_event = qapi_enum_parse(BlkdebugEvent_lookup, event, -1, NULL); blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
if (blkdebug_event < 0) { if (blkdebug_event < 0) {
return -ENOENT; return -ENOENT;
} }

View File

@ -437,7 +437,8 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO) aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO)
? BLOCKDEV_AIO_OPTIONS_NATIVE ? BLOCKDEV_AIO_OPTIONS_NATIVE
: BLOCKDEV_AIO_OPTIONS_THREADS; : BLOCKDEV_AIO_OPTIONS_THREADS;
aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), aio = qapi_enum_parse(&BlockdevAioOptions_lookup,
qemu_opt_get(opts, "aio"),
aio_default, &local_err); aio_default, &local_err);
if (local_err) { if (local_err) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
@ -446,7 +447,8 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
} }
s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE); s->use_linux_aio = (aio == BLOCKDEV_AIO_OPTIONS_NATIVE);
locking = qapi_enum_parse(OnOffAuto_lookup, qemu_opt_get(opts, "locking"), locking = qapi_enum_parse(&OnOffAuto_lookup,
qemu_opt_get(opts, "locking"),
ON_OFF_AUTO_AUTO, &local_err); ON_OFF_AUTO_AUTO, &local_err);
if (local_err) { if (local_err) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
@ -1973,7 +1975,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
BDRV_SECTOR_SIZE); BDRV_SECTOR_SIZE);
nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false); nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
prealloc = qapi_enum_parse(PreallocMode_lookup, buf, prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
PREALLOC_MODE_OFF, &local_err); PREALLOC_MODE_OFF, &local_err);
g_free(buf); g_free(buf);
if (local_err) { if (local_err) {

View File

@ -302,7 +302,7 @@ static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
: BLOCKDEV_AIO_OPTIONS_THREADS; : BLOCKDEV_AIO_OPTIONS_THREADS;
aio = qapi_enum_parse(BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"), aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
aio_default, errp); aio_default, errp);
switch (aio) { switch (aio) {

View File

@ -543,7 +543,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
if (!strcmp(ptr, "tcp")) { if (!strcmp(ptr, "tcp")) {
ptr = "inet"; /* accept legacy "tcp" */ ptr = "inet"; /* accept legacy "tcp" */
} }
type = qapi_enum_parse(SocketAddressType_lookup, ptr, -1, NULL); type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
if (type != SOCKET_ADDRESS_TYPE_INET if (type != SOCKET_ADDRESS_TYPE_INET
&& type != SOCKET_ADDRESS_TYPE_UNIX) { && type != SOCKET_ADDRESS_TYPE_UNIX) {
error_setg(&local_err, error_setg(&local_err,
@ -1000,7 +1000,7 @@ static int qemu_gluster_create(const char *filename,
BDRV_SECTOR_SIZE); BDRV_SECTOR_SIZE);
tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
prealloc = qapi_enum_parse(PreallocMode_lookup, tmp, PREALLOC_MODE_OFF, prealloc = qapi_enum_parse(&PreallocMode_lookup, tmp, PREALLOC_MODE_OFF,
&local_err); &local_err);
g_free(tmp); g_free(tmp);
if (local_err) { if (local_err) {

View File

@ -68,13 +68,15 @@ typedef enum ParallelsPreallocMode {
PRL_PREALLOC_MODE__MAX = 2, PRL_PREALLOC_MODE__MAX = 2,
} ParallelsPreallocMode; } ParallelsPreallocMode;
static const char *prealloc_mode_lookup[] = { static QEnumLookup prealloc_mode_lookup = {
"falloc", .array = (const char *const[]) {
"truncate", "falloc",
NULL, "truncate",
NULL,
},
.size = PRL_PREALLOC_MODE__MAX
}; };
typedef struct BDRVParallelsState { typedef struct BDRVParallelsState {
/** Locking is conservative, the lock protects /** Locking is conservative, the lock protects
* - image file extending (truncate, fallocate) * - image file extending (truncate, fallocate)
@ -695,7 +697,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0); qemu_opt_get_size_del(opts, PARALLELS_OPT_PREALLOC_SIZE, 0);
s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS); s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE); buf = qemu_opt_get_del(opts, PARALLELS_OPT_PREALLOC_MODE);
s->prealloc_mode = qapi_enum_parse(prealloc_mode_lookup, buf, s->prealloc_mode = qapi_enum_parse(&prealloc_mode_lookup, buf,
PRL_PREALLOC_MODE_FALLOCATE, PRL_PREALLOC_MODE_FALLOCATE,
&local_err); &local_err);
g_free(buf); g_free(buf);

View File

@ -2915,7 +2915,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
goto finish; goto finish;
} }
buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
prealloc = qapi_enum_parse(PreallocMode_lookup, buf, prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
PREALLOC_MODE_OFF, &local_err); PREALLOC_MODE_OFF, &local_err);
if (local_err) { if (local_err) {
error_propagate(errp, local_err); error_propagate(errp, local_err);
@ -3605,7 +3605,7 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
} }
optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
prealloc = qapi_enum_parse(PreallocMode_lookup, optstr, prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
PREALLOC_MODE_OFF, &local_err); PREALLOC_MODE_OFF, &local_err);
g_free(optstr); g_free(optstr);
if (local_err) { if (local_err) {

View File

@ -912,7 +912,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
if (!pattern_str) { if (!pattern_str) {
ret = QUORUM_READ_PATTERN_QUORUM; ret = QUORUM_READ_PATTERN_QUORUM;
} else { } else {
ret = qapi_enum_parse(QuorumReadPattern_lookup, pattern_str, ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str,
-EINVAL, NULL); -EINVAL, NULL);
} }
if (ret < 0) { if (ret < 0) {

View File

@ -437,7 +437,7 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,
if (detect_zeroes) { if (detect_zeroes) {
*detect_zeroes = *detect_zeroes =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
qemu_opt_get(opts, "detect-zeroes"), qemu_opt_get(opts, "detect-zeroes"),
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_error); &local_error);

View File

@ -264,7 +264,7 @@ qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg,
/* XXX replace with qapi_enum_parse() in future, when we can /* XXX replace with qapi_enum_parse() in future, when we can
* make that function emit a more friendly error message */ * make that function emit a more friendly error message */
static int qcrypto_block_luks_name_lookup(const char *name, static int qcrypto_block_luks_name_lookup(const char *name,
const char *const *map, const QEnumLookup *map,
const char *type, const char *type,
Error **errp) Error **errp)
{ {
@ -279,19 +279,19 @@ static int qcrypto_block_luks_name_lookup(const char *name,
#define qcrypto_block_luks_cipher_mode_lookup(name, errp) \ #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \ qcrypto_block_luks_name_lookup(name, \
QCryptoCipherMode_lookup, \ &QCryptoCipherMode_lookup, \
"Cipher mode", \ "Cipher mode", \
errp) errp)
#define qcrypto_block_luks_hash_name_lookup(name, errp) \ #define qcrypto_block_luks_hash_name_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \ qcrypto_block_luks_name_lookup(name, \
QCryptoHashAlgorithm_lookup, \ &QCryptoHashAlgorithm_lookup, \
"Hash algorithm", \ "Hash algorithm", \
errp) errp)
#define qcrypto_block_luks_ivgen_name_lookup(name, errp) \ #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \ qcrypto_block_luks_name_lookup(name, \
QCryptoIVGenAlgorithm_lookup, \ &QCryptoIVGenAlgorithm_lookup, \
"IV generator", \ "IV generator", \
errp) errp)

View File

@ -378,7 +378,7 @@ qcrypto_secret_class_init(ObjectClass *oc, void *data)
NULL); NULL);
object_class_property_add_enum(oc, "format", object_class_property_add_enum(oc, "format",
"QCryptoSecretFormat", "QCryptoSecretFormat",
QCryptoSecretFormat_lookup, &QCryptoSecretFormat_lookup,
qcrypto_secret_prop_get_format, qcrypto_secret_prop_get_format,
qcrypto_secret_prop_set_format, qcrypto_secret_prop_set_format,
NULL); NULL);

View File

@ -233,7 +233,7 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
NULL); NULL);
object_class_property_add_enum(oc, "endpoint", object_class_property_add_enum(oc, "endpoint",
"QCryptoTLSCredsEndpoint", "QCryptoTLSCredsEndpoint",
QCryptoTLSCredsEndpoint_lookup, &QCryptoTLSCredsEndpoint_lookup,
qcrypto_tls_creds_prop_get_endpoint, qcrypto_tls_creds_prop_get_endpoint,
qcrypto_tls_creds_prop_set_endpoint, qcrypto_tls_creds_prop_set_endpoint,
NULL); NULL);

6
hmp.c
View File

@ -1528,7 +1528,7 @@ void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps)); MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
int val; int val;
val = qapi_enum_parse(MigrationCapability_lookup, cap, -1, &err); val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
if (val < 0) { if (val < 0) {
goto end; goto end;
} }
@ -1557,7 +1557,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
Error *err = NULL; Error *err = NULL;
int val, ret; int val, ret;
val = qapi_enum_parse(MigrationParameter_lookup, param, -1, &err); val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
if (val < 0) { if (val < 0) {
goto cleanup; goto cleanup;
} }
@ -1735,7 +1735,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
} else { } else {
if (read_only) { if (read_only) {
read_only_mode = read_only_mode =
qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup, qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
read_only, read_only,
BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err); BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
if (err) { if (err) {

View File

@ -587,7 +587,7 @@ const PropertyInfo qdev_prop_macaddr = {
const PropertyInfo qdev_prop_on_off_auto = { const PropertyInfo qdev_prop_on_off_auto = {
.name = "OnOffAuto", .name = "OnOffAuto",
.description = "on/off/auto", .description = "on/off/auto",
.enum_table = OnOffAuto_lookup, .enum_table = &OnOffAuto_lookup,
.get = get_enum, .get = get_enum,
.set = set_enum, .set = set_enum,
.set_default_value = set_default_value_enum, .set_default_value = set_default_value_enum,
@ -599,7 +599,7 @@ QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
const PropertyInfo qdev_prop_losttickpolicy = { const PropertyInfo qdev_prop_losttickpolicy = {
.name = "LostTickPolicy", .name = "LostTickPolicy",
.enum_table = LostTickPolicy_lookup, .enum_table = &LostTickPolicy_lookup,
.get = get_enum, .get = get_enum,
.set = set_enum, .set = set_enum,
.set_default_value = set_default_value_enum, .set_default_value = set_default_value_enum,
@ -613,7 +613,7 @@ const PropertyInfo qdev_prop_blockdev_on_error = {
.name = "BlockdevOnError", .name = "BlockdevOnError",
.description = "Error handling policy, " .description = "Error handling policy, "
"report/ignore/enospc/stop/auto", "report/ignore/enospc/stop/auto",
.enum_table = BlockdevOnError_lookup, .enum_table = &BlockdevOnError_lookup,
.get = get_enum, .get = get_enum,
.set = set_enum, .set = set_enum,
.set_default_value = set_default_value_enum, .set_default_value = set_default_value_enum,
@ -627,7 +627,7 @@ const PropertyInfo qdev_prop_bios_chs_trans = {
.name = "BiosAtaTranslation", .name = "BiosAtaTranslation",
.description = "Logical CHS translation algorithm, " .description = "Logical CHS translation algorithm, "
"auto/none/lba/large/rechs", "auto/none/lba/large/rechs",
.enum_table = BiosAtaTranslation_lookup, .enum_table = &BiosAtaTranslation_lookup,
.get = get_enum, .get = get_enum,
.set = set_enum, .set = set_enum,
.set_default_value = set_default_value_enum, .set_default_value = set_default_value_enum,
@ -639,7 +639,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
.name = "FdcDriveType", .name = "FdcDriveType",
.description = "FDC drive type, " .description = "FDC drive type, "
"144/288/120/none/auto", "144/288/120/none/auto",
.enum_table = FloppyDriveType_lookup, .enum_table = &FloppyDriveType_lookup,
.get = get_enum, .get = get_enum,
.set = set_enum, .set = set_enum,
.set_default_value = set_default_value_enum, .set_default_value = set_default_value_enum,

View File

@ -249,7 +249,7 @@ struct Property {
struct PropertyInfo { struct PropertyInfo {
const char *name; const char *name;
const char *description; const char *description;
const char * const *enum_table; const QEnumLookup *enum_table;
int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
void (*set_default_value)(Object *obj, const Property *prop); void (*set_default_value)(Object *obj, const Property *prop);
void (*create)(Object *obj, Property *prop, Error **errp); void (*create)(Object *obj, Property *prop, Error **errp);

View File

@ -11,8 +11,13 @@
#ifndef QAPI_UTIL_H #ifndef QAPI_UTIL_H
#define QAPI_UTIL_H #define QAPI_UTIL_H
const char *qapi_enum_lookup(const char *const lookup[], int val); typedef struct QEnumLookup {
int qapi_enum_parse(const char * const lookup[], const char *buf, const char *const *array;
int size;
} QEnumLookup;
const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
int def, Error **errp); int def, Error **errp);
int parse_qapi_name(const char *name, bool complete); int parse_qapi_name(const char *name, bool complete);

View File

@ -469,7 +469,7 @@ bool visit_optional(Visitor *v, const char *name, bool *present);
* that visit_type_str() must have no unwelcome side effects. * that visit_type_str() must have no unwelcome side effects.
*/ */
void visit_type_enum(Visitor *v, const char *name, int *obj, void visit_type_enum(Visitor *v, const char *name, int *obj,
const char *const strings[], Error **errp); const QEnumLookup *lookup, Error **errp);
/* /*
* Check if visitor is an input visitor. * Check if visitor is an input visitor.

View File

@ -1415,14 +1415,14 @@ void object_class_property_add_bool(ObjectClass *klass, const char *name,
*/ */
void object_property_add_enum(Object *obj, const char *name, void object_property_add_enum(Object *obj, const char *name,
const char *typename, const char *typename,
const char * const *strings, const QEnumLookup *lookup,
int (*get)(Object *, Error **), int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **), void (*set)(Object *, int, Error **),
Error **errp); Error **errp);
void object_class_property_add_enum(ObjectClass *klass, const char *name, void object_class_property_add_enum(ObjectClass *klass, const char *name,
const char *typename, const char *typename,
const char * const *strings, const QEnumLookup *lookup,
int (*get)(Object *, Error **), int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **), void (*set)(Object *, int, Error **),
Error **errp); Error **errp);

View File

@ -88,7 +88,7 @@ static int global_state_post_load(void *opaque, int version_id)
s->received = true; s->received = true;
trace_migrate_global_state_post_load(runstate); trace_migrate_global_state_post_load(runstate);
r = qapi_enum_parse(RunState_lookup, runstate, -1, &local_err); r = qapi_enum_parse(&RunState_lookup, runstate, -1, &local_err);
if (r == -1) { if (r == -1) {
if (local_err) { if (local_err) {

View File

@ -179,7 +179,7 @@ static void netfilter_init(Object *obj)
netfilter_get_netdev_id, netfilter_set_netdev_id, netfilter_get_netdev_id, netfilter_set_netdev_id,
NULL); NULL);
object_property_add_enum(obj, "queue", "NetFilterDirection", object_property_add_enum(obj, "queue", "NetFilterDirection",
NetFilterDirection_lookup, &NetFilterDirection_lookup,
netfilter_get_direction, netfilter_set_direction, netfilter_get_direction, netfilter_set_direction,
NULL); NULL);
object_property_add_str(obj, "status", object_property_add_str(obj, "status",

View File

@ -14,14 +14,14 @@
#include "qapi/error.h" #include "qapi/error.h"
#include "qemu-common.h" #include "qemu-common.h"
const char *qapi_enum_lookup(const char *const lookup[], int val) const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
{ {
assert(val >= 0); assert(val >= 0 && val < lookup->size);
return lookup[val]; return lookup->array[val];
} }
int qapi_enum_parse(const char * const lookup[], const char *buf, int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
int def, Error **errp) int def, Error **errp)
{ {
int i; int i;
@ -30,8 +30,8 @@ int qapi_enum_parse(const char * const lookup[], const char *buf,
return def; return def;
} }
for (i = 0; lookup[i]; i++) { for (i = 0; i < lookup->size; i++) {
if (!strcmp(buf, lookup[i])) { if (!strcmp(buf, lookup->array[i])) {
return i; return i;
} }
} }

View File

@ -333,24 +333,26 @@ void visit_type_null(Visitor *v, const char *name, QNull **obj,
} }
static void output_type_enum(Visitor *v, const char *name, int *obj, static void output_type_enum(Visitor *v, const char *name, int *obj,
const char *const strings[], Error **errp) const QEnumLookup *lookup, Error **errp)
{ {
int i = 0;
int value = *obj; int value = *obj;
char *enum_str; char *enum_str;
while (strings[i++] != NULL); /*
if (value < 0 || value >= i - 1) { * TODO why is this an error, not an assertion? If assertion:
* delete, and rely on qapi_enum_lookup()
*/
if (value < 0 || value >= lookup->size) {
error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null"); error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
return; return;
} }
enum_str = (char *)qapi_enum_lookup(strings, value); enum_str = (char *)qapi_enum_lookup(lookup, value);
visit_type_str(v, name, &enum_str, errp); visit_type_str(v, name, &enum_str, errp);
} }
static void input_type_enum(Visitor *v, const char *name, int *obj, static void input_type_enum(Visitor *v, const char *name, int *obj,
const char *const strings[], Error **errp) const QEnumLookup *lookup, Error **errp)
{ {
Error *local_err = NULL; Error *local_err = NULL;
int64_t value; int64_t value;
@ -362,7 +364,7 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
return; return;
} }
value = qapi_enum_parse(strings, enum_str, -1, NULL); value = qapi_enum_parse(lookup, enum_str, -1, NULL);
if (value < 0) { if (value < 0) {
error_setg(errp, QERR_INVALID_PARAMETER, enum_str); error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
g_free(enum_str); g_free(enum_str);
@ -374,16 +376,16 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
} }
void visit_type_enum(Visitor *v, const char *name, int *obj, void visit_type_enum(Visitor *v, const char *name, int *obj,
const char *const strings[], Error **errp) const QEnumLookup *lookup, Error **errp)
{ {
assert(obj && strings); assert(obj && lookup);
trace_visit_type_enum(v, name, obj); trace_visit_type_enum(v, name, obj);
switch (v->type) { switch (v->type) {
case VISITOR_INPUT: case VISITOR_INPUT:
input_type_enum(v, name, obj, strings, errp); input_type_enum(v, name, obj, lookup, errp);
break; break;
case VISITOR_OUTPUT: case VISITOR_OUTPUT:
output_type_enum(v, name, obj, strings, errp); output_type_enum(v, name, obj, lookup, errp);
break; break;
case VISITOR_CLONE: case VISITOR_CLONE:
/* nothing further to do, scalar value was already copied by /* nothing further to do, scalar value was already copied by

View File

@ -3489,7 +3489,7 @@ static int img_resize(int argc, char **argv)
image_opts = true; image_opts = true;
break; break;
case OPTION_PREALLOCATION: case OPTION_PREALLOCATION:
prealloc = qapi_enum_parse(PreallocMode_lookup, optarg, prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
PREALLOC_MODE__MAX, NULL); PREALLOC_MODE__MAX, NULL);
if (prealloc == PREALLOC_MODE__MAX) { if (prealloc == PREALLOC_MODE__MAX) {
error_report("Invalid preallocation mode '%s'", optarg); error_report("Invalid preallocation mode '%s'", optarg);

View File

@ -638,7 +638,7 @@ int main(int argc, char **argv)
break; break;
case QEMU_NBD_OPT_DETECT_ZEROES: case QEMU_NBD_OPT_DETECT_ZEROES:
detect_zeroes = detect_zeroes =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
optarg, optarg,
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_err); &local_err);

View File

@ -1246,7 +1246,7 @@ uint64_t object_property_get_uint(Object *obj, const char *name,
} }
typedef struct EnumProperty { typedef struct EnumProperty {
const char * const *strings; const QEnumLookup *lookup;
int (*get)(Object *, Error **); int (*get)(Object *, Error **);
void (*set)(Object *, int, Error **); void (*set)(Object *, int, Error **);
} EnumProperty; } EnumProperty;
@ -1284,7 +1284,7 @@ int object_property_get_enum(Object *obj, const char *name,
visit_complete(v, &str); visit_complete(v, &str);
visit_free(v); visit_free(v);
v = string_input_visitor_new(str); v = string_input_visitor_new(str);
visit_type_enum(v, name, &ret, enumprop->strings, errp); visit_type_enum(v, name, &ret, enumprop->lookup, errp);
g_free(str); g_free(str);
visit_free(v); visit_free(v);
@ -1950,7 +1950,7 @@ static void property_get_enum(Object *obj, Visitor *v, const char *name,
return; return;
} }
visit_type_enum(v, name, &value, prop->strings, errp); visit_type_enum(v, name, &value, prop->lookup, errp);
} }
static void property_set_enum(Object *obj, Visitor *v, const char *name, static void property_set_enum(Object *obj, Visitor *v, const char *name,
@ -1960,7 +1960,7 @@ static void property_set_enum(Object *obj, Visitor *v, const char *name,
int value; int value;
Error *err = NULL; Error *err = NULL;
visit_type_enum(v, name, &value, prop->strings, &err); visit_type_enum(v, name, &value, prop->lookup, &err);
if (err) { if (err) {
error_propagate(errp, err); error_propagate(errp, err);
return; return;
@ -1977,7 +1977,7 @@ static void property_release_enum(Object *obj, const char *name,
void object_property_add_enum(Object *obj, const char *name, void object_property_add_enum(Object *obj, const char *name,
const char *typename, const char *typename,
const char * const *strings, const QEnumLookup *lookup,
int (*get)(Object *, Error **), int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **), void (*set)(Object *, int, Error **),
Error **errp) Error **errp)
@ -1985,7 +1985,7 @@ void object_property_add_enum(Object *obj, const char *name,
Error *local_err = NULL; Error *local_err = NULL;
EnumProperty *prop = g_malloc(sizeof(*prop)); EnumProperty *prop = g_malloc(sizeof(*prop));
prop->strings = strings; prop->lookup = lookup;
prop->get = get; prop->get = get;
prop->set = set; prop->set = set;
@ -2002,7 +2002,7 @@ void object_property_add_enum(Object *obj, const char *name,
void object_class_property_add_enum(ObjectClass *klass, const char *name, void object_class_property_add_enum(ObjectClass *klass, const char *name,
const char *typename, const char *typename,
const char * const *strings, const QEnumLookup *lookup,
int (*get)(Object *, Error **), int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **), void (*set)(Object *, int, Error **),
Error **errp) Error **errp)
@ -2010,7 +2010,7 @@ void object_class_property_add_enum(ObjectClass *klass, const char *name,
Error *local_err = NULL; Error *local_err = NULL;
EnumProperty *prop = g_malloc(sizeof(*prop)); EnumProperty *prop = g_malloc(sizeof(*prop));
prop->strings = strings; prop->lookup = lookup;
prop->get = get; prop->get = get;
prop->set = set; prop->set = set;

View File

@ -153,7 +153,7 @@ def gen_visit_enum(name):
void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp) void visit_type_%(c_name)s(Visitor *v, const char *name, %(c_name)s *obj, Error **errp)
{ {
int value = *obj; int value = *obj;
visit_type_enum(v, name, &value, %(c_name)s_lookup, errp); visit_type_enum(v, name, &value, &%(c_name)s_lookup, errp);
*obj = value; *obj = value;
} }
''', ''',

View File

@ -1849,19 +1849,22 @@ def guardend(name):
def gen_enum_lookup(name, values, prefix=None): def gen_enum_lookup(name, values, prefix=None):
ret = mcgen(''' ret = mcgen('''
const char *const %(c_name)s_lookup[] = { const QEnumLookup %(c_name)s_lookup = {
.array = (const char *const[]) {
''', ''',
c_name=c_name(name)) c_name=c_name(name))
for value in values: for value in values:
index = c_enum_const(name, value, prefix) index = c_enum_const(name, value, prefix)
ret += mcgen(''' ret += mcgen('''
[%(index)s] = "%(value)s", [%(index)s] = "%(value)s",
''', ''',
index=index, value=value) index=index, value=value)
max_index = c_enum_const(name, '_MAX', prefix) max_index = c_enum_const(name, '_MAX', prefix)
ret += mcgen(''' ret += mcgen('''
[%(max_index)s] = NULL, [%(max_index)s] = NULL,
},
.size = %(max_index)s
}; };
''', ''',
max_index=max_index) max_index=max_index)
@ -1895,9 +1898,9 @@ typedef enum %(c_name)s {
ret += mcgen(''' ret += mcgen('''
#define %(c_name)s_str(val) \\ #define %(c_name)s_str(val) \\
qapi_enum_lookup(%(c_name)s_lookup, (val)) qapi_enum_lookup(&%(c_name)s_lookup, (val))
extern const char *const %(c_name)s_lookup[]; extern const QEnumLookup %(c_name)s_lookup;
''', ''',
c_name=c_name(name)) c_name=c_name(name))
return ret return ret

View File

@ -46,11 +46,14 @@ enum DummyAnimal {
DUMMY_LAST, DUMMY_LAST,
}; };
static const char *const dummy_animal_map[DUMMY_LAST + 1] = { const QEnumLookup dummy_animal_map = {
[DUMMY_FROG] = "frog", .array = (const char *const[]) {
[DUMMY_ALLIGATOR] = "alligator", [DUMMY_FROG] = "frog",
[DUMMY_PLATYPUS] = "platypus", [DUMMY_ALLIGATOR] = "alligator",
[DUMMY_LAST] = NULL, [DUMMY_PLATYPUS] = "platypus",
[DUMMY_LAST] = NULL,
},
.size = DUMMY_LAST
}; };
struct DummyObject { struct DummyObject {
@ -142,7 +145,7 @@ static void dummy_class_init(ObjectClass *cls, void *data)
NULL); NULL);
object_class_property_add_enum(cls, "av", object_class_property_add_enum(cls, "av",
"DummyAnimal", "DummyAnimal",
dummy_animal_map, &dummy_animal_map,
dummy_get_av, dummy_get_av,
dummy_set_av, dummy_set_av,
NULL); NULL);

View File

@ -181,7 +181,7 @@ static void test_query(const void *data)
g_assert(qdict_haskey(resp, "return")); g_assert(qdict_haskey(resp, "return"));
} else { } else {
g_assert(error); g_assert(error);
g_assert_cmpint(qapi_enum_parse(QapiErrorClass_lookup, error_class, g_assert_cmpint(qapi_enum_parse(&QapiErrorClass_lookup, error_class,
-1, &error_abort), -1, &error_abort),
==, expected_error_class); ==, expected_error_class);
} }

View File

@ -19,19 +19,19 @@ static void test_qapi_enum_parse(void)
Error *err = NULL; Error *err = NULL;
int ret; int ret;
ret = qapi_enum_parse(QType_lookup, NULL, QTYPE_NONE, &error_abort); ret = qapi_enum_parse(&QType_lookup, NULL, QTYPE_NONE, &error_abort);
g_assert_cmpint(ret, ==, QTYPE_NONE); g_assert_cmpint(ret, ==, QTYPE_NONE);
ret = qapi_enum_parse(QType_lookup, "junk", -1, NULL); ret = qapi_enum_parse(&QType_lookup, "junk", -1, NULL);
g_assert_cmpint(ret, ==, -1); g_assert_cmpint(ret, ==, -1);
ret = qapi_enum_parse(QType_lookup, "junk", -1, &err); ret = qapi_enum_parse(&QType_lookup, "junk", -1, &err);
error_free_or_abort(&err); error_free_or_abort(&err);
ret = qapi_enum_parse(QType_lookup, "none", -1, &error_abort); ret = qapi_enum_parse(&QType_lookup, "none", -1, &error_abort);
g_assert_cmpint(ret, ==, QTYPE_NONE); g_assert_cmpint(ret, ==, QTYPE_NONE);
ret = qapi_enum_parse(QType_lookup, QType_str(QTYPE__MAX - 1), ret = qapi_enum_parse(&QType_lookup, QType_str(QTYPE__MAX - 1),
QTYPE__MAX - 1, &error_abort); QTYPE__MAX - 1, &error_abort);
g_assert_cmpint(ret, ==, QTYPE__MAX - 1); g_assert_cmpint(ret, ==, QTYPE__MAX - 1);
} }

View File

@ -1110,7 +1110,7 @@ static void test_visitor_in_fail_struct_missing(TestInputVisitorData *data,
error_free_or_abort(&err); error_free_or_abort(&err);
visit_optional(v, "optional", &present); visit_optional(v, "optional", &present);
g_assert(!present); g_assert(!present);
visit_type_enum(v, "enum", &en, EnumOne_lookup, &err); visit_type_enum(v, "enum", &en, &EnumOne_lookup, &err);
error_free_or_abort(&err); error_free_or_abort(&err);
visit_type_int(v, "i64", &i64, &err); visit_type_int(v, "i64", &i64, &err);
error_free_or_abort(&err); error_free_or_abort(&err);

2
tpm.c
View File

@ -33,7 +33,7 @@ void tpm_register_model(enum TpmModel model)
const TPMDriverOps *tpm_get_backend_driver(const char *type) const TPMDriverOps *tpm_get_backend_driver(const char *type)
{ {
int i = qapi_enum_parse(TpmType_lookup, type, -1, NULL); int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL);
return i >= 0 ? be_drivers[i] : NULL; return i >= 0 ? be_drivers[i] : NULL;
} }