dfilter: Cache stnode_tostr()
This avoids having to save/free the pointer for each tostr() invocation (or leak memory).
This commit is contained in:
parent
c2b6a27e97
commit
5dd90e3b30
|
@ -611,7 +611,6 @@ check_drange_sanity(dfwork_t *dfw, stnode_t *st)
|
|||
header_field_info *hfinfo1;
|
||||
ftenum_t ftype1;
|
||||
struct check_drange_sanity_args args;
|
||||
char *s;
|
||||
|
||||
entity1 = sttype_range_entity(st);
|
||||
if (entity1 && stnode_type_id(entity1) == STTYPE_FIELD) {
|
||||
|
@ -638,10 +637,8 @@ check_drange_sanity(dfwork_t *dfw, stnode_t *st)
|
|||
/* Should this be rejected instead? */
|
||||
check_drange_sanity(dfw, entity1);
|
||||
} else if (entity1) {
|
||||
s = stnode_tostr(entity1);
|
||||
dfilter_fail(dfw, "Range is not supported for entity %s of type %s",
|
||||
s, stnode_type_name(entity1));
|
||||
g_free(s);
|
||||
stnode_tostr(entity1), stnode_type_name(entity1));
|
||||
THROW(TypeError);
|
||||
} else {
|
||||
dfilter_fail(dfw, "Range is not supported, details: " G_STRLOC " entity: NULL");
|
||||
|
|
|
@ -57,16 +57,13 @@ function_tostr(const void *data)
|
|||
const df_func_def_t *def = stfuncrec->funcdef;
|
||||
GSList *params = stfuncrec->params;
|
||||
GString *repr = g_string_new("");
|
||||
char *s;
|
||||
|
||||
ws_assert(def);
|
||||
|
||||
g_string_printf(repr, "%s(", def->name);
|
||||
while (params != NULL) {
|
||||
ws_assert(params->data);
|
||||
s = stnode_tostr(params->data);
|
||||
g_string_append(repr, s);
|
||||
g_free(s);
|
||||
g_string_append(repr, stnode_tostr(params->data));
|
||||
params = params->next;
|
||||
if (params != NULL) {
|
||||
g_string_append(repr, ", ");
|
||||
|
|
|
@ -77,13 +77,12 @@ range_tostr(const void *data)
|
|||
{
|
||||
range_t *range = (range_t*)data;
|
||||
ws_assert_magic(range, RANGE_MAGIC);
|
||||
char *repr, *entity_str, *drange_str;
|
||||
char *repr, *drange_str;
|
||||
|
||||
entity_str = stnode_tostr(range->entity);
|
||||
drange_str = drange_tostr(range->drange);
|
||||
|
||||
repr = g_strdup_printf("%s[%s]", entity_str, drange_str);
|
||||
g_free(entity_str);
|
||||
repr = g_strdup_printf("%s[%s]",
|
||||
stnode_tostr(range->entity),
|
||||
drange_str);
|
||||
g_free(drange_str);
|
||||
|
||||
return repr;
|
||||
|
|
|
@ -47,13 +47,10 @@ sttype_set_tostr(const void *data)
|
|||
GSList* nodelist = (GSList *)data;
|
||||
stnode_t *lower, *upper;
|
||||
GString *repr = g_string_new("");
|
||||
char *str;
|
||||
|
||||
while (nodelist) {
|
||||
lower = nodelist->data;
|
||||
str = stnode_tostr(lower);
|
||||
g_string_append(repr, str);
|
||||
g_free(str);
|
||||
g_string_append(repr, stnode_tostr(lower));
|
||||
|
||||
/* Set elements are always in pairs; upper may be null. */
|
||||
nodelist = g_slist_next(nodelist);
|
||||
|
@ -61,9 +58,7 @@ sttype_set_tostr(const void *data)
|
|||
upper = nodelist->data;
|
||||
if (upper != NULL) {
|
||||
g_string_append(repr, "..");
|
||||
str = stnode_tostr(upper);
|
||||
g_string_append(repr, str);
|
||||
g_free(str);
|
||||
g_string_append(repr, stnode_tostr(upper));
|
||||
}
|
||||
|
||||
nodelist = g_slist_next(nodelist);
|
||||
|
|
|
@ -88,6 +88,8 @@ _node_clear(stnode_t *node)
|
|||
node->type = NULL;
|
||||
node->flags = 0;
|
||||
node->data = NULL;
|
||||
g_free(node->repr);
|
||||
node->repr = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -107,6 +109,7 @@ _node_init(stnode_t *node, sttype_id_t type_id, gpointer data)
|
|||
ws_assert(!node->type);
|
||||
ws_assert(!node->data);
|
||||
node->flags = 0;
|
||||
node->repr = NULL;
|
||||
|
||||
if (type_id == STTYPE_UNINITIALIZED) {
|
||||
node->type = NULL;
|
||||
|
@ -253,20 +256,24 @@ stnode_set_inside_parens(stnode_t *node, gboolean inside)
|
|||
}
|
||||
}
|
||||
|
||||
char *
|
||||
const char *
|
||||
stnode_tostr(stnode_t *node)
|
||||
{
|
||||
if (node->type->func_tostr == NULL)
|
||||
return g_strdup("<FIXME>");
|
||||
if (node->repr != NULL)
|
||||
return node->repr;
|
||||
|
||||
return node->type->func_tostr(node->data);
|
||||
if (node->type->func_tostr == NULL)
|
||||
node->repr = g_strdup("<FIXME>");
|
||||
else
|
||||
node->repr = node->type->func_tostr(node->data);
|
||||
|
||||
return node->repr;
|
||||
}
|
||||
|
||||
static char *
|
||||
sprint_node(stnode_t *node)
|
||||
{
|
||||
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
|
||||
char *s;
|
||||
|
||||
wmem_strbuf_append_printf(buf, "stnode <%p> = {\n", (void *)node);
|
||||
wmem_strbuf_append_printf(buf, "\tmagic = %"PRIx32"\n", node->magic);
|
||||
|
@ -274,9 +281,7 @@ sprint_node(stnode_t *node)
|
|||
wmem_strbuf_append_printf(buf,
|
||||
"\tflags = %"PRIx16" (inside_parens = %s)\n",
|
||||
node->flags, true_or_false(stnode_inside_parens(node)));
|
||||
s = stnode_tostr(node);
|
||||
wmem_strbuf_append_printf(buf, "\tdata = %s<%s>\n", stnode_type_name(node), s);
|
||||
g_free(s);
|
||||
wmem_strbuf_append_printf(buf, "\tdata = %s<%s>\n", stnode_type_name(node), stnode_tostr(node));
|
||||
wmem_strbuf_append_printf(buf, "}\n");
|
||||
return wmem_strbuf_finalize(buf);
|
||||
}
|
||||
|
@ -307,12 +312,9 @@ static void
|
|||
visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
|
||||
{
|
||||
stnode_t *left, *right;
|
||||
char *str;
|
||||
|
||||
if (stnode_type_id(node) == STTYPE_TEST) {
|
||||
str = stnode_tostr(node);
|
||||
wmem_strbuf_append_printf(buf, "%s(", str);
|
||||
g_free(str);
|
||||
wmem_strbuf_append_printf(buf, "%s(", stnode_tostr(node));
|
||||
sttype_test_get(node, NULL, &left, &right);
|
||||
if (left && right) {
|
||||
wmem_strbuf_append_c(buf, '\n');
|
||||
|
@ -335,9 +337,7 @@ visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
|
|||
wmem_strbuf_append(buf, ")");
|
||||
}
|
||||
else {
|
||||
str = stnode_tostr(node);
|
||||
wmem_strbuf_append_printf(buf, "%s<%s>", stnode_type_name(node), str);
|
||||
g_free(str);
|
||||
wmem_strbuf_append_printf(buf, "%s<%s>", stnode_type_name(node), stnode_tostr(node));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,6 +59,7 @@ typedef struct {
|
|||
uint16_t flags;
|
||||
gpointer data;
|
||||
char *token_value;
|
||||
char *repr;
|
||||
} stnode_t;
|
||||
|
||||
/* These are the sttype_t registration function prototypes. */
|
||||
|
@ -112,7 +113,7 @@ stnode_steal_data(stnode_t *node);
|
|||
const char *
|
||||
stnode_token_value(stnode_t *node);
|
||||
|
||||
char *
|
||||
const char *
|
||||
stnode_tostr(stnode_t *node);
|
||||
|
||||
gboolean
|
||||
|
|
Loading…
Reference in New Issue