aboutsummaryrefslogtreecommitdiff
path: root/qapi
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2017-08-24 10:46:10 +0200
committerMarkus Armbruster <armbru@redhat.com>2017-09-04 13:09:13 +0200
commitf7abe0ecd4973dfe36944b916c5b9cf8ec199b8a (patch)
tree99ca676afbfcf2da0182b3bf99811d000cb69c58 /qapi
parent788b305c91398f18e5952667b929d7f45e2c211c (diff)
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]
Diffstat (limited to 'qapi')
-rw-r--r--qapi/qapi-util.c12
-rw-r--r--qapi/qapi-visit-core.c24
2 files changed, 19 insertions, 17 deletions
diff --git a/qapi/qapi-util.c b/qapi/qapi-util.c
index 7af2f04d36..e9b266bb70 100644
--- a/qapi/qapi-util.c
+++ b/qapi/qapi-util.c
@@ -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;
@@ -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;
}
}
diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
index 30dc85b6f3..3dcb968867 100644
--- a/qapi/qapi-visit-core.c
+++ b/qapi/qapi-visit-core.c
@@ -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,
- const char *const strings[], Error **errp)
+ const QEnumLookup *lookup, Error **errp)
{
- int i = 0;
int value = *obj;
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");
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);
}
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;
int64_t value;
@@ -362,7 +364,7 @@ static void input_type_enum(Visitor *v, const char *name, int *obj,
return;
}
- value = qapi_enum_parse(strings, enum_str, -1, NULL);
+ value = qapi_enum_parse(lookup, enum_str, -1, NULL);
if (value < 0) {
error_setg(errp, QERR_INVALID_PARAMETER, 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,
- const char *const strings[], Error **errp)
+ const QEnumLookup *lookup, Error **errp)
{
- assert(obj && strings);
+ assert(obj && lookup);
trace_visit_type_enum(v, name, obj);
switch (v->type) {
case VISITOR_INPUT:
- input_type_enum(v, name, obj, strings, errp);
+ input_type_enum(v, name, obj, lookup, errp);
break;
case VISITOR_OUTPUT:
- output_type_enum(v, name, obj, strings, errp);
+ output_type_enum(v, name, obj, lookup, errp);
break;
case VISITOR_CLONE:
/* nothing further to do, scalar value was already copied by