Skip to content

Commit

Permalink
qapi: Change data type of the FOO_lookup generated for enum FOO
Browse files Browse the repository at this point in the history
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 <[email protected]>
Message-Id: <[email protected]>
[Basically redone]
Signed-off-by: Markus Armbruster <[email protected]>
Message-Id: <[email protected]>
[Rebased]
  • Loading branch information
elmarco authored and Markus Armbruster committed Sep 4, 2017
1 parent 788b305 commit f7abe0e
Show file tree
Hide file tree
Showing 33 changed files with 106 additions and 89 deletions.
2 changes: 1 addition & 1 deletion backends/hostmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ host_memory_backend_class_init(ObjectClass *oc, void *data)
host_memory_backend_set_host_nodes,
NULL, NULL, &error_abort);
object_class_property_add_enum(oc, "policy", "HostMemPolicy",
HostMemPolicy_lookup,
&HostMemPolicy_lookup,
host_memory_backend_get_policy,
host_memory_backend_set_policy, &error_abort);
object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
Expand Down
2 changes: 1 addition & 1 deletion block.c
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
detect_zeroes = qemu_opt_get(opts, "detect-zeroes");
if (detect_zeroes) {
BlockdevDetectZeroesOptions value =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
detect_zeroes,
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_err);
Expand Down
4 changes: 2 additions & 2 deletions block/blkdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
error_setg(errp, "Missing event name for rule");
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) {
return -1;
}
Expand Down Expand Up @@ -732,7 +732,7 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
struct BlkdebugRule *rule;
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) {
return -ENOENT;
}
Expand Down
8 changes: 5 additions & 3 deletions block/file-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
aio_default = (bdrv_flags & BDRV_O_NATIVE_AIO)
? BLOCKDEV_AIO_OPTIONS_NATIVE
: 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);
if (local_err) {
error_propagate(errp, local_err);
Expand All @@ -446,7 +447,8 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
}
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);
if (local_err) {
error_propagate(errp, local_err);
Expand Down Expand Up @@ -1973,7 +1975,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
BDRV_SECTOR_SIZE);
nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
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);
g_free(buf);
if (local_err) {
Expand Down
2 changes: 1 addition & 1 deletion block/file-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
: 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);

switch (aio) {
Expand Down
4 changes: 2 additions & 2 deletions block/gluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
if (!strcmp(ptr, "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
&& type != SOCKET_ADDRESS_TYPE_UNIX) {
error_setg(&local_err,
Expand Down Expand Up @@ -1000,7 +1000,7 @@ static int qemu_gluster_create(const char *filename,
BDRV_SECTOR_SIZE);

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);
g_free(tmp);
if (local_err) {
Expand Down
14 changes: 8 additions & 6 deletions block/parallels.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ typedef enum ParallelsPreallocMode {
PRL_PREALLOC_MODE__MAX = 2,
} ParallelsPreallocMode;

static const char *prealloc_mode_lookup[] = {
"falloc",
"truncate",
NULL,
static QEnumLookup prealloc_mode_lookup = {
.array = (const char *const[]) {
"falloc",
"truncate",
NULL,
},
.size = PRL_PREALLOC_MODE__MAX
};


typedef struct BDRVParallelsState {
/** Locking is conservative, the lock protects
* - image file extending (truncate, fallocate)
Expand Down Expand Up @@ -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);
s->prealloc_size = MAX(s->tracks, s->prealloc_size >> BDRV_SECTOR_BITS);
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,
&local_err);
g_free(buf);
Expand Down
4 changes: 2 additions & 2 deletions block/qcow2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
goto finish;
}
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);
if (local_err) {
error_propagate(errp, local_err);
Expand Down Expand Up @@ -3605,7 +3605,7 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
}

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);
g_free(optstr);
if (local_err) {
Expand Down
2 changes: 1 addition & 1 deletion block/quorum.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
if (!pattern_str) {
ret = QUORUM_READ_PATTERN_QUORUM;
} else {
ret = qapi_enum_parse(QuorumReadPattern_lookup, pattern_str,
ret = qapi_enum_parse(&QuorumReadPattern_lookup, pattern_str,
-EINVAL, NULL);
}
if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags,

if (detect_zeroes) {
*detect_zeroes =
qapi_enum_parse(BlockdevDetectZeroesOptions_lookup,
qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
qemu_opt_get(opts, "detect-zeroes"),
BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
&local_error);
Expand Down
8 changes: 4 additions & 4 deletions crypto/block-luks.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg,
/* XXX replace with qapi_enum_parse() in future, when we can
* make that function emit a more friendly error message */
static int qcrypto_block_luks_name_lookup(const char *name,
const char *const *map,
const QEnumLookup *map,
const char *type,
Error **errp)
{
Expand All @@ -279,19 +279,19 @@ static int qcrypto_block_luks_name_lookup(const char *name,

#define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \
QCryptoCipherMode_lookup, \
&QCryptoCipherMode_lookup, \
"Cipher mode", \
errp)

#define qcrypto_block_luks_hash_name_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \
QCryptoHashAlgorithm_lookup, \
&QCryptoHashAlgorithm_lookup, \
"Hash algorithm", \
errp)

#define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
qcrypto_block_luks_name_lookup(name, \
QCryptoIVGenAlgorithm_lookup, \
&QCryptoIVGenAlgorithm_lookup, \
"IV generator", \
errp)

Expand Down
2 changes: 1 addition & 1 deletion crypto/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ qcrypto_secret_class_init(ObjectClass *oc, void *data)
NULL);
object_class_property_add_enum(oc, "format",
"QCryptoSecretFormat",
QCryptoSecretFormat_lookup,
&QCryptoSecretFormat_lookup,
qcrypto_secret_prop_get_format,
qcrypto_secret_prop_set_format,
NULL);
Expand Down
2 changes: 1 addition & 1 deletion crypto/tlscreds.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
NULL);
object_class_property_add_enum(oc, "endpoint",
"QCryptoTLSCredsEndpoint",
QCryptoTLSCredsEndpoint_lookup,
&QCryptoTLSCredsEndpoint_lookup,
qcrypto_tls_creds_prop_get_endpoint,
qcrypto_tls_creds_prop_set_endpoint,
NULL);
Expand Down
6 changes: 3 additions & 3 deletions hmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
int val;

val = qapi_enum_parse(MigrationCapability_lookup, cap, -1, &err);
val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
if (val < 0) {
goto end;
}
Expand Down Expand Up @@ -1557,7 +1557,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
Error *err = NULL;
int val, ret;

val = qapi_enum_parse(MigrationParameter_lookup, param, -1, &err);
val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
if (val < 0) {
goto cleanup;
}
Expand Down Expand Up @@ -1735,7 +1735,7 @@ void hmp_change(Monitor *mon, const QDict *qdict)
} else {
if (read_only) {
read_only_mode =
qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
read_only,
BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
if (err) {
Expand Down
10 changes: 5 additions & 5 deletions hw/core/qdev-properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ const PropertyInfo qdev_prop_macaddr = {
const PropertyInfo qdev_prop_on_off_auto = {
.name = "OnOffAuto",
.description = "on/off/auto",
.enum_table = OnOffAuto_lookup,
.enum_table = &OnOffAuto_lookup,
.get = get_enum,
.set = set_enum,
.set_default_value = set_default_value_enum,
Expand All @@ -599,7 +599,7 @@ QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));

const PropertyInfo qdev_prop_losttickpolicy = {
.name = "LostTickPolicy",
.enum_table = LostTickPolicy_lookup,
.enum_table = &LostTickPolicy_lookup,
.get = get_enum,
.set = set_enum,
.set_default_value = set_default_value_enum,
Expand All @@ -613,7 +613,7 @@ const PropertyInfo qdev_prop_blockdev_on_error = {
.name = "BlockdevOnError",
.description = "Error handling policy, "
"report/ignore/enospc/stop/auto",
.enum_table = BlockdevOnError_lookup,
.enum_table = &BlockdevOnError_lookup,
.get = get_enum,
.set = set_enum,
.set_default_value = set_default_value_enum,
Expand All @@ -627,7 +627,7 @@ const PropertyInfo qdev_prop_bios_chs_trans = {
.name = "BiosAtaTranslation",
.description = "Logical CHS translation algorithm, "
"auto/none/lba/large/rechs",
.enum_table = BiosAtaTranslation_lookup,
.enum_table = &BiosAtaTranslation_lookup,
.get = get_enum,
.set = set_enum,
.set_default_value = set_default_value_enum,
Expand All @@ -639,7 +639,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
.name = "FdcDriveType",
.description = "FDC drive type, "
"144/288/120/none/auto",
.enum_table = FloppyDriveType_lookup,
.enum_table = &FloppyDriveType_lookup,
.get = get_enum,
.set = set_enum,
.set_default_value = set_default_value_enum,
Expand Down
2 changes: 1 addition & 1 deletion include/hw/qdev-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ struct Property {
struct PropertyInfo {
const char *name;
const char *description;
const char * const *enum_table;
const QEnumLookup *enum_table;
int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len);
void (*set_default_value)(Object *obj, const Property *prop);
void (*create)(Object *obj, Property *prop, Error **errp);
Expand Down
9 changes: 7 additions & 2 deletions include/qapi/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
#ifndef QAPI_UTIL_H
#define QAPI_UTIL_H

const char *qapi_enum_lookup(const char *const lookup[], int val);
int qapi_enum_parse(const char * const lookup[], const char *buf,
typedef struct QEnumLookup {
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 parse_qapi_name(const char *name, bool complete);
Expand Down
2 changes: 1 addition & 1 deletion include/qapi/visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ bool visit_optional(Visitor *v, const char *name, bool *present);
* that visit_type_str() must have no unwelcome side effects.
*/
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.
Expand Down
4 changes: 2 additions & 2 deletions include/qom/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
const char *typename,
const char * const *strings,
const QEnumLookup *lookup,
int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **),
Error **errp);

void object_class_property_add_enum(ObjectClass *klass, const char *name,
const char *typename,
const char * const *strings,
const QEnumLookup *lookup,
int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **),
Error **errp);
Expand Down
2 changes: 1 addition & 1 deletion migration/global_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static int global_state_post_load(void *opaque, int version_id)
s->received = true;
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 (local_err) {
Expand Down
2 changes: 1 addition & 1 deletion net/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static void netfilter_init(Object *obj)
netfilter_get_netdev_id, netfilter_set_netdev_id,
NULL);
object_property_add_enum(obj, "queue", "NetFilterDirection",
NetFilterDirection_lookup,
&NetFilterDirection_lookup,
netfilter_get_direction, netfilter_set_direction,
NULL);
object_property_add_str(obj, "status",
Expand Down
12 changes: 6 additions & 6 deletions qapi/qapi-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#include "qapi/error.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 i;
Expand All @@ -30,8 +30,8 @@ int qapi_enum_parse(const char * const lookup[], const char *buf,
return def;
}

for (i = 0; lookup[i]; i++) {
if (!strcmp(buf, lookup[i])) {
for (i = 0; i < lookup->size; i++) {
if (!strcmp(buf, lookup->array[i])) {
return i;
}
}
Expand Down
Loading

0 comments on commit f7abe0e

Please sign in to comment.