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:
parent
c3e01fdd43
commit
943e2035fa
10
client.c
10
client.c
|
@ -771,19 +771,19 @@ client_markup_parse(client_t *c, const char *str, ssize_t len)
|
||||||
const char *elements[] = { "title", NULL };
|
const char *elements[] = { "title", NULL };
|
||||||
char *title_esc = g_markup_escape_text(c->name, -1);
|
char *title_esc = g_markup_escape_text(c->name, -1);
|
||||||
const char *elements_sub[] = { title_esc , NULL };
|
const char *elements_sub[] = { title_esc , NULL };
|
||||||
markup_parser_data_t *p;
|
markup_parser_data_t p;
|
||||||
char *ret;
|
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
|
else
|
||||||
ret = a_strdup(str);
|
ret = a_strdup(str);
|
||||||
|
|
||||||
markup_parser_data_delete(&p);
|
markup_parser_data_wipe(&p);
|
||||||
p_delete(&title_esc);
|
p_delete(&title_esc);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -221,49 +221,49 @@ draw_text_markup_expand(draw_parser_data_t *data,
|
||||||
const char *str, ssize_t slen)
|
const char *str, ssize_t slen)
|
||||||
{
|
{
|
||||||
const char *elements[] = { "bg", "text", "margin", NULL };
|
const char *elements[] = { "bg", "text", "margin", NULL };
|
||||||
markup_parser_data_t *p;
|
markup_parser_data_t p;
|
||||||
int i;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bg */
|
/* bg */
|
||||||
if(p->attribute_names[0])
|
if(p.attribute_names[0])
|
||||||
for(i = 0; p->attribute_names[0][i]; i++)
|
for(i = 0; p.attribute_names[0][i]; i++)
|
||||||
if(!a_strcmp(p->attribute_names[0][i], "color"))
|
if(!a_strcmp(p.attribute_names[0][i], "color"))
|
||||||
data->has_bg_color = xcolor_new(data->connection, data->phys_screen,
|
data->has_bg_color = xcolor_new(data->connection, data->phys_screen,
|
||||||
p->attribute_values[0][i], &data->bg_color);
|
p.attribute_values[0][i], &data->bg_color);
|
||||||
else if(!a_strcmp(p->attribute_names[0][i], "image"))
|
else if(!a_strcmp(p.attribute_names[0][i], "image"))
|
||||||
data->bg_image = draw_image_new(p->attribute_values[0][i]);
|
data->bg_image = draw_image_new(p.attribute_values[0][i]);
|
||||||
|
|
||||||
/* text */
|
/* text */
|
||||||
if(p->attribute_names[1])
|
if(p.attribute_names[1])
|
||||||
for(i = 0; p->attribute_names[1][i]; i++)
|
for(i = 0; p.attribute_names[1][i]; i++)
|
||||||
if(!a_strcmp(p->attribute_names[1][i], "align"))
|
if(!a_strcmp(p.attribute_names[1][i], "align"))
|
||||||
data->align = draw_align_get_from_str(p->attribute_values[1][i]);
|
data->align = draw_align_get_from_str(p.attribute_values[1][i]);
|
||||||
else if(!a_strcmp(p->attribute_names[1][i], "shadow"))
|
else if(!a_strcmp(p.attribute_names[1][i], "shadow"))
|
||||||
xcolor_new(data->connection, data->phys_screen,
|
xcolor_new(data->connection, data->phys_screen,
|
||||||
p->attribute_values[1][i], &data->shadow.color);
|
p.attribute_values[1][i], &data->shadow.color);
|
||||||
else if(!a_strcmp(p->attribute_names[1][i], "shadow_offset"))
|
else if(!a_strcmp(p.attribute_names[1][i], "shadow_offset"))
|
||||||
data->shadow.offset = atoi(p->attribute_values[1][i]);
|
data->shadow.offset = atoi(p.attribute_values[1][i]);
|
||||||
|
|
||||||
/* margin */
|
/* margin */
|
||||||
if(p->attribute_names[2])
|
if(p.attribute_names[2])
|
||||||
for(i = 0; p->attribute_names[2][i]; i++)
|
for(i = 0; p.attribute_names[2][i]; i++)
|
||||||
if(!a_strcmp(p->attribute_names[2][i], "left"))
|
if(!a_strcmp(p.attribute_names[2][i], "left"))
|
||||||
data->margin.left = atoi(p->attribute_values[2][i]);
|
data->margin.left = atoi(p.attribute_values[2][i]);
|
||||||
else if(!a_strcmp(p->attribute_names[2][i], "right"))
|
else if(!a_strcmp(p.attribute_names[2][i], "right"))
|
||||||
data->margin.right = atoi(p->attribute_values[2][i]);
|
data->margin.right = atoi(p.attribute_values[2][i]);
|
||||||
|
|
||||||
/* stole text */
|
/* stole text */
|
||||||
data->text = p->text;
|
data->text = p.text;
|
||||||
buffer_init(&p->text);
|
buffer_init(&p.text);
|
||||||
markup_parser_data_delete(&p);
|
markup_parser_data_wipe(&p);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -132,54 +132,48 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a markup_parser_data_t structure with elements list.
|
/** 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 an array of elements to look for, NULL terminated
|
||||||
* \param elements_sub an optional array of values to substitude to elements, NULL
|
* \param elements_sub an optional array of values to substitude to elements, NULL
|
||||||
* terminated, or NULL if not needed
|
* terminated, or NULL if not needed
|
||||||
* \param nelem number of elements in the array (without NULL)
|
* \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 *
|
void
|
||||||
markup_parser_data_new(const char **elements, const char **elements_sub, ssize_t nelem)
|
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);
|
buffer_init(&p->text);
|
||||||
p->elements = elements;
|
p->elements = elements;
|
||||||
p->elements_sub = elements_sub;
|
p->elements_sub = elements_sub;
|
||||||
p->attribute_names = p_new(char **, nelem);
|
p->attribute_names = p_new(char **, nelem);
|
||||||
p->attribute_values = p_new(char **, nelem);
|
p->attribute_values = p_new(char **, nelem);
|
||||||
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Delete a markup_parser_data_t allocated.
|
/** Wipe a markup_parser_data_t initialized with markup_parser_data_init.
|
||||||
* \param p markup_parser_data_t pointer address
|
* \param p markup_parser_data_t address
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
markup_parser_data_delete(markup_parser_data_t **p)
|
markup_parser_data_wipe(markup_parser_data_t *p)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
for(i = 0; (*p)->elements[i]; i++)
|
for(i = 0; p->elements[i]; i++)
|
||||||
if((*p)->attribute_names[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_names[i][j]);
|
||||||
p_delete(&((*p)->attribute_values[i][j]));
|
p_delete(&p->attribute_values[i][j]);
|
||||||
}
|
}
|
||||||
p_delete(&((*p)->attribute_names[i]));
|
p_delete(&p->attribute_names[i]);
|
||||||
p_delete(&((*p)->attribute_values[i]));
|
p_delete(&p->attribute_values[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
p_delete(&(*p)->attribute_names);
|
p_delete(&p->attribute_names);
|
||||||
p_delete(&(*p)->attribute_values);
|
p_delete(&p->attribute_values);
|
||||||
|
|
||||||
buffer_wipe(&(*p)->text);
|
buffer_wipe(&p->text);
|
||||||
p_delete(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Parse markup defined in data on the string str.
|
/** Parse markup defined in data on the string str.
|
||||||
|
|
|
@ -33,8 +33,8 @@ typedef struct
|
||||||
char ***attribute_values;
|
char ***attribute_values;
|
||||||
} markup_parser_data_t;
|
} markup_parser_data_t;
|
||||||
|
|
||||||
markup_parser_data_t * markup_parser_data_new(const char **, const char **, ssize_t);
|
void markup_parser_data_init(markup_parser_data_t *, const char **, const char **, ssize_t);
|
||||||
void markup_parser_data_delete(markup_parser_data_t **);
|
void markup_parser_data_wipe(markup_parser_data_t *);
|
||||||
bool markup_parse(markup_parser_data_t *data, const char *, ssize_t);
|
bool markup_parse(markup_parser_data_t *data, const char *, ssize_t);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -60,19 +60,19 @@ tag_markup_parse(tag_t *t, const char *str, ssize_t len)
|
||||||
const char *elements[] = { "title", NULL };
|
const char *elements[] = { "title", NULL };
|
||||||
char *title_esc = g_markup_escape_text(t->name, -1);
|
char *title_esc = g_markup_escape_text(t->name, -1);
|
||||||
const char *elements_sub[] = { title_esc , NULL };
|
const char *elements_sub[] = { title_esc , NULL };
|
||||||
markup_parser_data_t *p;
|
markup_parser_data_t p;
|
||||||
char *ret;
|
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
|
else
|
||||||
ret = a_strdup(str);
|
ret = a_strdup(str);
|
||||||
|
|
||||||
markup_parser_data_delete(&p);
|
markup_parser_data_wipe(&p);
|
||||||
p_delete(&title_esc);
|
p_delete(&title_esc);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -74,7 +74,7 @@ tasklist_draw(draw_context_t *ctx, int screen,
|
||||||
char *text;
|
char *text;
|
||||||
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0, j = 0;
|
int n = 0, i = 0, box_width = 0, icon_width = 0, box_width_rest = 0, j = 0;
|
||||||
netwm_icon_t *icon;
|
netwm_icon_t *icon;
|
||||||
markup_parser_data_t *p;
|
markup_parser_data_t p;
|
||||||
const char *elements[] = { "bg", NULL };
|
const char *elements[] = { "bg", NULL };
|
||||||
xcolor_t bg_color;
|
xcolor_t bg_color;
|
||||||
draw_image_t *image;
|
draw_image_t *image;
|
||||||
|
@ -122,16 +122,16 @@ tasklist_draw(draw_context_t *ctx, int screen,
|
||||||
|
|
||||||
/* Actually look for the proper background color, since
|
/* Actually look for the proper background color, since
|
||||||
* otherwise the background statusbar color is used instead */
|
* otherwise the background statusbar color is used instead */
|
||||||
p = markup_parser_data_new(elements, NULL, countof(elements));
|
markup_parser_data_init(&p, elements, NULL, countof(elements));
|
||||||
if(markup_parse(p, text, a_strlen(text)) && p->attribute_names[0])
|
if(markup_parse(&p, text, a_strlen(text)) && p.attribute_names[0])
|
||||||
for(j = 0; p->attribute_names[0][j]; j++)
|
for(j = 0; p.attribute_names[0][j]; j++)
|
||||||
if(!a_strcmp(p->attribute_names[0][j], "color"))
|
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);
|
draw_rectangle(ctx, area, 1.0, true, bg_color);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
markup_parser_data_delete(&p);
|
markup_parser_data_wipe(&p);
|
||||||
|
|
||||||
if((image = draw_image_new(c->icon_path)))
|
if((image = draw_image_new(c->icon_path)))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue