mirror of
https://github.com/labwc/labwc.git
synced 2026-04-12 08:21:13 -04:00
treewide: add buf_finish() and replace manual free(buf.buf) calls
This commit is contained in:
parent
6da5efb815
commit
a5a1757479
8 changed files with 57 additions and 11 deletions
|
|
@ -34,7 +34,7 @@ void buf_expand_shell_variables(struct buf *s);
|
||||||
/**
|
/**
|
||||||
* buf_init - allocate NULL-terminated C string buffer
|
* buf_init - allocate NULL-terminated C string buffer
|
||||||
* @s: buffer
|
* @s: buffer
|
||||||
* Note: use free(s->buf) to free it
|
* Note: use buf_finish(buf) to free it
|
||||||
*/
|
*/
|
||||||
void buf_init(struct buf *s);
|
void buf_init(struct buf *s);
|
||||||
|
|
||||||
|
|
@ -55,13 +55,41 @@ void buf_add_char(struct buf *s, char data);
|
||||||
/**
|
/**
|
||||||
* buf_clear - clear the buffer, internal allocations are preserved
|
* buf_clear - clear the buffer, internal allocations are preserved
|
||||||
* @s: buffer
|
* @s: buffer
|
||||||
|
*
|
||||||
|
* This is the appropriate function to call to re-use the buffer
|
||||||
|
* in a loop or similar situations as it simply reuses the already
|
||||||
|
* existing heap allocations.
|
||||||
*/
|
*/
|
||||||
void buf_clear(struct buf *s);
|
void buf_clear(struct buf *s);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* buf_reset - reset the buffer, internal allocations are free'd
|
* buf_reset - reset the buffer, internal allocations are free'd
|
||||||
* @s: buffer
|
* @s: buffer
|
||||||
|
*
|
||||||
|
* The buffer will automatically be re-initialized so can be used again.
|
||||||
|
*
|
||||||
|
* This function should not be used in loops as it will free and then
|
||||||
|
* re-allocate heap memory. However, if the buffer becomes too big
|
||||||
|
* and the internal allocation should be free'd this is the correct
|
||||||
|
* function to use.
|
||||||
*/
|
*/
|
||||||
void buf_reset(struct buf *s);
|
void buf_reset(struct buf *s);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* buf_finish - free all internal allocations
|
||||||
|
* @s: buffer
|
||||||
|
*
|
||||||
|
* The state of the buffer after this call is undefined.
|
||||||
|
* It can be re-initalized via buf_init() however.
|
||||||
|
*
|
||||||
|
* Use this for the usual case of a stack allocated buf instance:
|
||||||
|
* struct buf buf;
|
||||||
|
* buf_init(&buf);
|
||||||
|
* buf_add(&buf, "some");
|
||||||
|
* buf_add(&buf, "thing");
|
||||||
|
* do_something_with(buf.buf);
|
||||||
|
* buf_finish(&buf);
|
||||||
|
*/
|
||||||
|
void buf_finish(struct buf *s);
|
||||||
|
|
||||||
#endif /* LABWC_BUF_H */
|
#endif /* LABWC_BUF_H */
|
||||||
|
|
|
||||||
|
|
@ -709,7 +709,7 @@ actions_run(struct view *activator, struct server *server,
|
||||||
buf_add(&cmd, action_get_str(action, "command", NULL));
|
buf_add(&cmd, action_get_str(action, "command", NULL));
|
||||||
buf_expand_tilde(&cmd);
|
buf_expand_tilde(&cmd);
|
||||||
spawn_async_no_shell(cmd.buf);
|
spawn_async_no_shell(cmd.buf);
|
||||||
free(cmd.buf);
|
buf_finish(&cmd);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ACTION_TYPE_EXIT:
|
case ACTION_TYPE_EXIT:
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ buf_expand_shell_variables(struct buf *s)
|
||||||
buf_add_char(&new, s->buf[i]);
|
buf_add_char(&new, s->buf[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(environment_variable.buf);
|
buf_finish(&environment_variable);
|
||||||
free(s->buf);
|
free(s->buf);
|
||||||
s->buf = new.buf;
|
s->buf = new.buf;
|
||||||
s->len = new.len;
|
s->len = new.len;
|
||||||
|
|
@ -79,7 +79,10 @@ buf_expand_shell_variables(struct buf *s)
|
||||||
void
|
void
|
||||||
buf_init(struct buf *s)
|
buf_init(struct buf *s)
|
||||||
{
|
{
|
||||||
/* we can't assert(!s->buf) here because struct may be uninitialized */
|
/*
|
||||||
|
* We can't assert(!s->buf) here because
|
||||||
|
* the supplied struct may be uninitialized.
|
||||||
|
*/
|
||||||
|
|
||||||
s->alloc = 256;
|
s->alloc = 256;
|
||||||
s->buf = xmalloc(s->alloc);
|
s->buf = xmalloc(s->alloc);
|
||||||
|
|
@ -130,6 +133,21 @@ buf_clear(struct buf *s)
|
||||||
void
|
void
|
||||||
buf_reset(struct buf *s)
|
buf_reset(struct buf *s)
|
||||||
{
|
{
|
||||||
zfree(s->buf);
|
assert(s->buf);
|
||||||
|
|
||||||
|
buf_finish(s);
|
||||||
buf_init(s);
|
buf_init(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
buf_finish(struct buf *s)
|
||||||
|
{
|
||||||
|
assert(s->buf);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Using zfree rather than free ensures that the
|
||||||
|
* asserts will be triggered whenever something
|
||||||
|
* other than buf_init() is called on the buffer.
|
||||||
|
*/
|
||||||
|
zfree(s->buf);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ find_dir(struct ctx *ctx)
|
||||||
char *pfxenv = getenv(d.prefix);
|
char *pfxenv = getenv(d.prefix);
|
||||||
buf_add(&prefix, pfxenv ? pfxenv : d.default_prefix);
|
buf_add(&prefix, pfxenv ? pfxenv : d.default_prefix);
|
||||||
if (!prefix.len) {
|
if (!prefix.len) {
|
||||||
free(prefix.buf);
|
buf_finish(&prefix);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,7 +130,7 @@ find_dir(struct ctx *ctx)
|
||||||
wl_list_append(ctx->list, &path->link);
|
wl_list_append(ctx->list, &path->link);
|
||||||
}
|
}
|
||||||
g_strfreev(prefixes);
|
g_strfreev(prefixes);
|
||||||
free(prefix.buf);
|
buf_finish(&prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1539,7 +1539,7 @@ rcxml_read(const char *filename)
|
||||||
zfree(line);
|
zfree(line);
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
rcxml_parse_xml(&b);
|
rcxml_parse_xml(&b);
|
||||||
zfree(b.buf);
|
buf_finish(&b);
|
||||||
if (!should_merge_config) {
|
if (!should_merge_config) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ process_line(char *line)
|
||||||
buf_expand_shell_variables(&value);
|
buf_expand_shell_variables(&value);
|
||||||
buf_expand_tilde(&value);
|
buf_expand_tilde(&value);
|
||||||
setenv(key, value.buf, 1);
|
setenv(key, value.buf, 1);
|
||||||
free(value.buf);
|
buf_finish(&value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return true on successful read */
|
/* return true on successful read */
|
||||||
|
|
|
||||||
|
|
@ -543,7 +543,7 @@ parse(struct server *server, FILE *stream)
|
||||||
xmlFreeDoc(d);
|
xmlFreeDoc(d);
|
||||||
xmlCleanupParser();
|
xmlCleanupParser();
|
||||||
err:
|
err:
|
||||||
free(b.buf);
|
buf_finish(&b);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
||||||
|
|
@ -442,7 +442,7 @@ render_osd(struct server *server, cairo_t *cairo, int w, int h,
|
||||||
|
|
||||||
y += theme->osd_window_switcher_item_height;
|
y += theme->osd_window_switcher_item_height;
|
||||||
}
|
}
|
||||||
free(buf.buf);
|
buf_finish(&buf);
|
||||||
g_object_unref(layout);
|
g_object_unref(layout);
|
||||||
|
|
||||||
cairo_surface_flush(surf);
|
cairo_surface_flush(surf);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue