From: Anthony PERARD <anthony.perard@citrix.com>
To: Xen Devel <xen-devel@lists.xen.org>
Cc: Anthony PERARD <anthony.perard@citrix.com>,
Ian Jackson <Ian.Jackson@eu.citrix.com>,
Ian Campbell <ian.campbell@citrix.com>
Subject: [PATCH 1/2] libxl_json: Use libxl alloc function
Date: Wed, 26 Sep 2012 14:31:33 +0100 [thread overview]
Message-ID: <1348666294-18182-2-git-send-email-anthony.perard@citrix.com> (raw)
In-Reply-To: <1348666294-18182-1-git-send-email-anthony.perard@citrix.com>
This patch makes use of the libxl allocation API and removes the check for
allocation failure.
This patch also assume that flexarray does not need to be freed as it will be
gc'd in the next patch.
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---
tools/libxl/libxl_internal.h | 1 -
tools/libxl/libxl_json.c | 135 ++++++-------------------------------------
tools/libxl/libxl_qmp.c | 1 -
3 files changed, 18 insertions(+), 119 deletions(-)
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index b6f54ba..55fa771 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1519,7 +1519,6 @@ libxl__json_map_node *libxl__json_map_node_get(const libxl__json_object *o,
_hidden const libxl__json_object *libxl__json_map_get(const char *key,
const libxl__json_object *o,
libxl__json_node_type expected_type);
-_hidden void libxl__json_object_free(libxl__gc *gc, libxl__json_object *obj);
_hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s);
diff --git a/tools/libxl/libxl_json.c b/tools/libxl/libxl_json.c
index 8e17842..25df4a9 100644
--- a/tools/libxl/libxl_json.c
+++ b/tools/libxl/libxl_json.c
@@ -210,23 +210,12 @@ static libxl__json_object *json_object_alloc(libxl__gc *gc,
{
libxl__json_object *obj;
- obj = calloc(1, sizeof (libxl__json_object));
- if (obj == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(gc), LIBXL__LOG_ERROR,
- "Failed to allocate a libxl__json_object");
- return NULL;
- }
+ obj = libxl__zalloc(gc, sizeof(*obj));
obj->type = type;
if (type == JSON_MAP || type == JSON_ARRAY) {
flexarray_t *array = flexarray_make(1, 1);
- if (array == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(gc), LIBXL__LOG_ERROR,
- "Failed to allocate a flexarray");
- free(obj);
- return NULL;
- }
if (type == JSON_MAP)
obj->u.map = array;
else
@@ -256,11 +245,7 @@ static int json_object_append_to(libxl__gc *gc,
break;
}
case JSON_ARRAY:
- if (flexarray_append(dst->u.array, obj) == 2) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(gc), LIBXL__LOG_ERROR,
- "Failed to grow a flexarray");
- return -1;
- }
+ flexarray_append(dst->u.array, obj);
break;
default:
LIBXL__LOG(libxl__gc_owner(gc), LIBXL__LOG_ERROR,
@@ -273,50 +258,6 @@ static int json_object_append_to(libxl__gc *gc,
return 0;
}
-void libxl__json_object_free(libxl__gc *gc, libxl__json_object *obj)
-{
- int idx = 0;
-
- if (obj == NULL)
- return;
- switch (obj->type) {
- case JSON_STRING:
- case JSON_NUMBER:
- free(obj->u.string);
- break;
- case JSON_MAP: {
- libxl__json_map_node *node = NULL;
-
- for (idx = 0; idx < obj->u.map->count; idx++) {
- if (flexarray_get(obj->u.map, idx, (void**)&node) != 0)
- break;
- libxl__json_object_free(gc, node->obj);
- free(node->map_key);
- free(node);
- node = NULL;
- }
- flexarray_free(obj->u.map);
- break;
- }
- case JSON_ARRAY: {
- libxl__json_object *node = NULL;
- break;
-
- for (idx = 0; idx < obj->u.array->count; idx++) {
- if (flexarray_get(obj->u.array, idx, (void**)&node) != 0)
- break;
- libxl__json_object_free(gc, node);
- node = NULL;
- }
- flexarray_free(obj->u.array);
- break;
- }
- default:
- break;
- }
- free(obj);
-}
-
libxl__json_object *libxl__json_array_get(const libxl__json_object *o, int i)
{
flexarray_t *array = NULL;
@@ -393,11 +334,9 @@ static int json_callback_null(void *opaque)
DEBUG_GEN(ctx, null);
- if ((obj = json_object_alloc(ctx->gc, JSON_NULL)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_NULL);
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
@@ -411,12 +350,10 @@ static int json_callback_boolean(void *opaque, int boolean)
DEBUG_GEN_VALUE(ctx, bool, boolean);
- if ((obj = json_object_alloc(ctx->gc,
- boolean ? JSON_TRUE : JSON_FALSE)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc,
+ boolean ? JSON_TRUE : JSON_FALSE);
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
@@ -448,8 +385,7 @@ static int json_callback_number(void *opaque, const char *s, libxl_yajl_length l
goto error;
}
- if ((obj = json_object_alloc(ctx->gc, JSON_DOUBLE)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_DOUBLE);
obj->u.d = d;
} else {
long long i = strtoll(s, NULL, 10);
@@ -458,23 +394,16 @@ static int json_callback_number(void *opaque, const char *s, libxl_yajl_length l
goto error;
}
- if ((obj = json_object_alloc(ctx->gc, JSON_INTEGER)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_INTEGER);
obj->u.i = i;
}
goto out;
error:
/* If the conversion fail, we just store the original string. */
- if ((obj = json_object_alloc(ctx->gc, JSON_NUMBER)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_NUMBER);
- t = malloc(len + 1);
- if (t == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
- "Failed to allocate");
- return 0;
- }
+ t = libxl__zalloc(ctx->gc, len + 1);
strncpy(t, s, len);
t[len] = 0;
@@ -482,7 +411,6 @@ error:
out:
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
@@ -496,26 +424,17 @@ static int json_callback_string(void *opaque, const unsigned char *str,
char *t = NULL;
libxl__json_object *obj = NULL;
- t = malloc(len + 1);
- if (t == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
- "Failed to allocate");
- return 0;
- }
+ t = libxl__zalloc(ctx->gc, len + 1);
DEBUG_GEN_STRING(ctx, str, len);
strncpy(t, (const char *) str, len);
t[len] = 0;
- if ((obj = json_object_alloc(ctx->gc, JSON_STRING)) == NULL) {
- free(t);
- return 0;
- }
+ obj = json_object_alloc(ctx->gc, JSON_STRING);
obj->u.string = t;
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
@@ -528,13 +447,9 @@ static int json_callback_map_key(void *opaque, const unsigned char *str,
libxl__yajl_ctx *ctx = opaque;
char *t = NULL;
libxl__json_object *obj = ctx->current;
+ libxl__gc *gc = ctx->gc;
- t = malloc(len + 1);
- if (t == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
- "Failed to allocate");
- return 0;
- }
+ t = libxl__zalloc(gc, len + 1);
DEBUG_GEN_STRING(ctx, str, len);
@@ -542,21 +457,13 @@ static int json_callback_map_key(void *opaque, const unsigned char *str,
t[len] = 0;
if (libxl__json_object_is_map(obj)) {
- libxl__json_map_node *node = malloc(sizeof (libxl__json_map_node));
- if (node == NULL) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
- "Failed to allocate");
- return 0;
- }
+ libxl__json_map_node *node;
+ GCNEW(node);
node->map_key = t;
node->obj = NULL;
- if (flexarray_append(obj->u.map, node) == 2) {
- LIBXL__LOG_ERRNO(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
- "Failed to grow a flexarray");
- return 0;
- }
+ flexarray_append(obj->u.map, node);
} else {
LIBXL__LOG(libxl__gc_owner(ctx->gc), LIBXL__LOG_ERROR,
"Current json object is not a map");
@@ -573,12 +480,10 @@ static int json_callback_start_map(void *opaque)
DEBUG_GEN(ctx, map_open);
- if ((obj = json_object_alloc(ctx->gc, JSON_MAP)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_MAP);
if (ctx->current) {
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
}
@@ -615,12 +520,10 @@ static int json_callback_start_array(void *opaque)
DEBUG_GEN(ctx, array_open);
- if ((obj = json_object_alloc(ctx->gc, JSON_ARRAY)) == NULL)
- return 0;
+ obj = json_object_alloc(ctx->gc, JSON_ARRAY);
if (ctx->current) {
if (json_object_append_to(ctx->gc, obj, ctx->current) == -1) {
- libxl__json_object_free(ctx->gc, obj);
return 0;
}
}
@@ -710,8 +613,6 @@ out:
LIBXL__LOG(libxl__gc_owner(gc), LIBXL__LOG_ERROR, "yajl error: %s", str);
yajl_free_error(yajl_ctx.hand, str);
-
- libxl__json_object_free(gc, yajl_ctx.head);
yajl_ctx_free(&yajl_ctx);
return NULL;
}
diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index 2c4d269..0757fc2 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -484,7 +484,6 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
if (o) {
rc = qmp_handle_response(qmp, o);
- libxl__json_object_free(gc, o);
} else {
LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
"Parse error of : %s\n", s);
--
Anthony PERARD
next prev parent reply other threads:[~2012-09-26 13:31 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-26 13:31 [PATCH 0/2] Cleanup: flexarray taking gc Anthony PERARD
2012-09-26 13:31 ` Anthony PERARD [this message]
2012-09-28 17:22 ` [PATCH 1/2] libxl_json: Use libxl alloc function Ian Jackson
2012-10-01 14:04 ` Anthony PERARD
2012-09-26 13:31 ` [PATCH 2/2] libxl: Have flexarray using the GC Anthony PERARD
2012-09-28 17:27 ` Ian Jackson
2012-10-01 14:15 ` Anthony PERARD
2012-10-01 14:17 ` Ian Jackson
2012-10-01 14:23 ` Anthony PERARD
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1348666294-18182-2-git-send-email-anthony.perard@citrix.com \
--to=anthony.perard@citrix.com \
--cc=Ian.Jackson@eu.citrix.com \
--cc=ian.campbell@citrix.com \
--cc=xen-devel@lists.xen.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.