Rework the markup_parser_* allocation API.

We have a stack, don't be ashamed to use it.

Instead of:

    foo_t *foo;

    foo = foo_new();
    /* work with foo */
    foo_delete(&foo);

It's way better to:

    foo_t foo;

    foo_init(&foo);
    /* work with &foo */
    foo_wipe(&foo);

Remember: more mallocs == more fragmentation.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
This commit is contained in:
Pierre Habouzit 2008-06-22 16:17:12 +02:00
parent c3e01fdd43
commit 943e2035fa
6 changed files with 64 additions and 70 deletions

View File

@ -771,19 +771,19 @@ client_markup_parse(client_t *c, const char *str, ssize_t len)
const char *elements[] = { "title", NULL };
char *title_esc = g_markup_escape_text(c->name, -1);
const char *elements_sub[] = { title_esc , NULL };
markup_parser_data_t *p;
markup_parser_data_t p;
char *ret;
p = markup_parser_data_new(elements, elements_sub, countof(elements));
markup_parser_data_init(&p, elements, elements_sub, countof(elements));
if(markup_parse(p, str, len))
if(markup_parse(&p, str, len))
{
ret = buffer_detach(&p->text);
ret = buffer_detach(&p.text);
}
else
ret = a_strdup(str);
markup_parser_data_delete(&p);
markup_parser_data_wipe(&p);
p_delete(&title_esc);
return ret;

View File

@ -221,49 +221,49 @@ draw_text_markup_expand(draw_parser_data_t *data,
const char *str, ssize_t slen)
{
const char *elements[] = { "bg", "text", "margin", NULL };
markup_parser_data_t *p;
markup_parser_data_t p;
int i;
p = markup_parser_data_new(elements, NULL, countof(elements));
markup_parser_data_init(&p, elements, NULL, countof(elements));
if(!markup_parse(p, str, slen))
if(!markup_parse(&p, str, slen))
{
markup_parser_data_delete(&p);
markup_parser_data_wipe(&p);
return false;
}
/* bg */
if(p->attribute_names[0])
for(i = 0; p->attribute_names[0][i]; i++)
if(!a_strcmp(p->attribute_names[0][i], "color"))
if(p.attribute_names[0])
for(i = 0; p.attribute_names[0][i]; i++)
if(!a_strcmp(p.attribute_names[0][i], "color"))
data->has_bg_color = xcolor_new(data->connection, data->phys_screen,
p->attribute_values[0][i], &data->bg_color);
else if(!a_strcmp(p->attribute_names[0][i], "image"))
data->bg_image = draw_image_new(p->attribute_values[0][i]);
p.attribute_values[0][i], &data->bg_color);
else if(!a_strcmp(p.attribute_names[0][i], "image"))
data->bg_image = draw_image_new(p.attribute_values[0][i]);
/* text */
if(p->attribute_names[1])
for(i = 0; p->attribute_names[1][i]; i++)
if(!a_strcmp(p->attribute_names[1][i], "align"))
data->align = draw_align_get_from_str(p->attribute_values[1][i]);
else if(!a_strcmp(p->attribute_names[1][i], "shadow"))
if(p.attribute_names[1])
for(i = 0; p.attribute_names[1][i]; i++)
if(!a_strcmp(p.attribute_names[1][i], "align"))
data->align = draw_align_get_from_str(p.attribute_values[1][i]);
else if(!a_strcmp(p.attribute_names[1][i], "shadow"))
xcolor_new(data->connection, data->phys_screen,
p->attribute_values[1][i], &data->shadow.color);
else if(!a_strcmp(p->attribute_names[1][i], "shadow_offset"))
data->shadow.offset = atoi(p->attribute_values[1][i]);
p.attribute_values[1][i], &data->shadow.color);
else if(!a_strcmp(p.attribute_names[1][i], "shadow_offset"))
data->shadow.offset = atoi(p.attribute_values[1][i]);
/* margin */
if(p->attribute_names[2])
for(i = 0; p->attribute_names[2][i]; i++)
if(!a_strcmp(p->attribute_names[2][i], "left"))
data->margin.left = atoi(p->attribute_values[2][i]);
else if(!a_strcmp(p->attribute_names[2][i], "right"))
data->margin.right = atoi(p->attribute_values[2][i]);
if(p.attribute_names[2])
for(i = 0; p.attribute_names[2][i]; i++)
if(!a_strcmp(p.attribute_names[2][i], "left"))
data->margin.left = atoi(p.attribute_values[2][i]);
else if(!a_strcmp(p.attribute_names[2][i], "right"))
data->margin.right = atoi(p.attribute_values[2][i]);
/* stole text */
data->text = p->text;
buffer_init(&p->text);
markup_parser_data_delete(&p);
data->text = p.text;
buffer_init(&p.text);
markup_parser_data_wipe(&p);
return true;
}

View File

@ -132,54 +132,48 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
}
/** Create a markup_parser_data_t structure with elements list.
* \param a pointer to an allocated markup_parser_data_t which must be wiped
* with markup_parser_data_wipe()
* \param elements an array of elements to look for, NULL terminated
* \param elements_sub an optional array of values to substitude to elements, NULL
* terminated, or NULL if not needed
* \param nelem number of elements in the array (without NULL)
* \return a pointer to an allocated markup_parser_data_t which must be freed
* with markup_parser_data_delete()
*/
markup_parser_data_t *
markup_parser_data_new(const char **elements, const char **elements_sub, ssize_t nelem)
void
markup_parser_data_init(markup_parser_data_t *p, const char **elements,
const char **elements_sub, ssize_t nelem)
{
markup_parser_data_t *p;
p = p_new(markup_parser_data_t, 1);
buffer_init(&p->text);
p->elements = elements;
p->elements_sub = elements_sub;
p->attribute_names = p_new(char **, nelem);
p->attribute_values = p_new(char **, nelem);
return p;
}
/** Delete a markup_parser_data_t allocated.
* \param p markup_parser_data_t pointer address
/** Wipe a markup_parser_data_t initialized with markup_parser_data_init.
* \param p markup_parser_data_t address
*/
void
markup_parser_data_delete(markup_parser_data_t **p)
markup_parser_data_wipe(markup_parser_data_t *p)
{
int i, j;
for(i = 0; (*p)->elements[i]; i++)
if((*p)->attribute_names[i])
for(i = 0; p->elements[i]; i++)
if(p->attribute_names[i])
{
for(j = 0; (*p)->attribute_names[i][j]; j++)
for(j = 0; p->attribute_names[i][j]; j++)
{
p_delete(&((*p)->attribute_names[i][j]));
p_delete(&((*p)->attribute_values[i][j]));
p_delete(&p->attribute_names[i][j]);
p_delete(&p->attribute_values[i][j]);
}
p_delete(&((*p)->attribute_names[i]));
p_delete(&((*p)->attribute_values[i]));
p_delete(&p->attribute_names[i]);
p_delete(&p->attribute_values[i]);
}
p_delete(&(*p)->attribute_names);
p_delete(&(*p)->attribute_values);
p_delete(&p->attribute_names);
p_delete(&p->attribute_values);
buffer_wipe(&(*p)->text);
p_delete(p);
buffer_wipe(&p->text);
}
/** Parse markup defined in data on the string str.

View File

@ -33,8 +33,8 @@ typedef struct
char ***attribute_values;
} markup_parser_data_t;
markup_parser_data_t * markup_parser_data_new(const char **, const char **, ssize_t);
void markup_parser_data_delete(markup_parser_data_t **);
void markup_parser_data_init(markup_parser_data_t *, const char **, const char **, ssize_t);
void markup_parser_data_wipe(markup_parser_data_t *);
bool markup_parse(markup_parser_data_t *data, const char *, ssize_t);
#endif

View File

@ -60,19 +60,19 @@ tag_markup_parse(tag_t *t, const char *str, ssize_t len)
const char *elements[] = { "title", NULL };
char *title_esc = g_markup_escape_text(t->name, -1);
const char *elements_sub[] = { title_esc , NULL };
markup_parser_data_t *p;
markup_parser_data_t p;
char *ret;
p = markup_parser_data_new(elements, elements_sub, countof(elements));
markup_parser_data_init(&p, elements, elements_sub, countof(elements));
if(markup_parse(p, str, len))
if(markup_parse(&p, str, len))
{
ret = buffer_detach(&p->text);
ret = buffer_detach(&p.text);
}
else
ret = a_strdup(str);
markup_parser_data_delete(&p);
markup_parser_data_wipe(&p);
p_delete(&title_esc);
return ret;

View File

@ -74,7 +74,7 @@ tasklist_draw(draw_context_t *ctx, int screen,
char *text;
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0, j = 0;
netwm_icon_t *icon;
markup_parser_data_t *p;
markup_parser_data_t p;
const char *elements[] = { "bg", NULL };
xcolor_t bg_color;
draw_image_t *image;
@ -122,16 +122,16 @@ tasklist_draw(draw_context_t *ctx, int screen,
/* Actually look for the proper background color, since
* otherwise the background statusbar color is used instead */
p = markup_parser_data_new(elements, NULL, countof(elements));
if(markup_parse(p, text, a_strlen(text)) && p->attribute_names[0])
for(j = 0; p->attribute_names[0][j]; j++)
if(!a_strcmp(p->attribute_names[0][j], "color"))
markup_parser_data_init(&p, elements, NULL, countof(elements));
if(markup_parse(&p, text, a_strlen(text)) && p.attribute_names[0])
for(j = 0; p.attribute_names[0][j]; j++)
if(!a_strcmp(p.attribute_names[0][j], "color"))
{
xcolor_new(ctx->connection, ctx->phys_screen, p->attribute_values[0][j], &bg_color);
xcolor_new(ctx->connection, ctx->phys_screen, p.attribute_values[0][j], &bg_color);
draw_rectangle(ctx, area, 1.0, true, bg_color);
break;
}
markup_parser_data_delete(&p);
markup_parser_data_wipe(&p);
if((image = draw_image_new(c->icon_path)))
{