Skip to content

Commit

Permalink
TEST try without OPAL_OBJECT
Browse files Browse the repository at this point in the history
Signed-off-by: Wenduo Wang <[email protected]>
  • Loading branch information
wenduwan committed May 17, 2024
1 parent eafc65b commit 61cc14b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
36 changes: 16 additions & 20 deletions opal/util/json/opal_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@ struct opal_json_internal_t {
};
typedef struct opal_json_internal_t opal_json_internal_t;

static void opal_json_internal_reset(opal_json_internal_t *json)
static void opal_json_internal_free(opal_json_internal_t *json)
{
json->parent.type = OPAL_JSON_TYPE_COUNT;
json->value = NULL;
}
if (NULL == json) {
return;
}

static void opal_json_internal_destruct(opal_json_internal_t *json)
{
/* The root JSON object will release the memory of its children */
if (json->value && NULL == json->value->parent) {
json_value_free(json->value);
}
opal_json_internal_reset(json);
}

OBJ_CLASS_INSTANCE(opal_json_internal_t, opal_json_t, opal_json_internal_reset,
opal_json_internal_destruct);
free(json);
}

static inline int opal_json_internal_translate_type(json_type type, opal_json_type *out)
{
Expand Down Expand Up @@ -84,11 +80,13 @@ static inline int opal_json_internal_translate_type(json_type type, opal_json_ty

static int opal_json_internal_new(const json_value *in, opal_json_internal_t **out)
{
int ret = OPAL_SUCCESS;
*out = OBJ_NEW(opal_json_internal_t);
*out = malloc(sizeof(opal_json_internal_t));
if (NULL == *out) {
return OPAL_ERROR;
}

(*out)->value = (json_value *) in;
ret = opal_json_internal_translate_type(in->type, &(*out)->parent.type);
return ret;
return opal_json_internal_translate_type(in->type, &(*out)->parent.type);
}

int opal_json_load(const char *str, const size_t len, opal_json_t **json)
Expand All @@ -109,7 +107,7 @@ int opal_json_load(const char *str, const size_t len, opal_json_t **json)
if (OPAL_SUCCESS == ret) {
*json = (opal_json_t *) out;
} else if (out) {
OBJ_RELEASE(out);
opal_json_internal_free(out);
}

return ret;
Expand Down Expand Up @@ -181,17 +179,15 @@ int opal_json_get_key(const opal_json_t *json, const char *key, opal_json_t **ou
if (OPAL_SUCCESS == ret) {
*out = (opal_json_t *) result;
} else if (result) {
OBJ_RELEASE(result);
opal_json_internal_free(result);
}

return ret;
}

void opal_json_free(opal_json_t *json)
{
if (json) {
OBJ_RELEASE(json);
}
opal_json_internal_free((struct opal_json_internal_t *) json);
}

int opal_json_get_index(const opal_json_t *json, const size_t index, opal_json_t **out)
Expand All @@ -213,7 +209,7 @@ int opal_json_get_index(const opal_json_t *json, const size_t index, opal_json_t
if (OPAL_SUCCESS == ret) {
*out = (opal_json_t *) result;
} else if (result) {
OBJ_RELEASE(result);
opal_json_internal_free(result);
}

return ret;
Expand Down
3 changes: 0 additions & 3 deletions opal/util/json/opal_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ typedef enum {
} opal_json_type;

struct opal_json_t {
opal_object_t super;
opal_json_type type;
};
typedef struct opal_json_t opal_json_t;

OBJ_CLASS_INSTANCE(opal_json_t, opal_object_t, NULL, NULL);

/**
* Load JSON from a string.
*
Expand Down

0 comments on commit 61cc14b

Please sign in to comment.