common: remove buf_init(), add BUF_INIT and buf_move()

Add a BUF_INIT macro, which makes it easier to initialize a struct buf
to an empty string (without a heap allocation).

Add buf_move() to move the contents of one struct buf to another (the
source is reset to BUF_INIT, analogous to C++ move-assignment).

Use buf_reset() instead of directly calling `free(s->buf)` since the
internal buf may not always be allocated by malloc() now.
This commit is contained in:
John Lindgren 2024-04-14 14:20:57 -04:00 committed by Johan Malm
parent 343918dee0
commit 0573f16693
12 changed files with 122 additions and 90 deletions

View file

@ -15,7 +15,6 @@
#include "common/dir.h"
#include "common/font.h"
#include "common/list.h"
#include "common/match.h"
#include "common/mem.h"
#include "common/nodename.h"
#include "common/scaled_font_buffer.h"
@ -639,9 +638,8 @@ parse_stream(struct server *server, FILE *stream)
{
char *line = NULL;
size_t len = 0;
struct buf b;
struct buf b = BUF_INIT;
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
@ -651,7 +649,7 @@ parse_stream(struct server *server, FILE *stream)
}
free(line);
parse_buf(server, &b);
free(b.buf);
buf_reset(&b);
}
static void
@ -1140,7 +1138,7 @@ pipemenu_ctx_destroy(struct pipe_context *ctx)
wl_event_source_remove(ctx->event_read);
wl_event_source_remove(ctx->event_timeout);
spawn_piped_close(ctx->pid, ctx->pipe_fd);
free(ctx->buf.buf);
buf_reset(&ctx->buf);
free(ctx);
waiting_for_pipe_menu = false;
}
@ -1230,7 +1228,7 @@ parse_pipemenu(struct menuitem *item)
ctx->item = item;
ctx->pid = pid;
ctx->pipe_fd = pipe_fd;
buf_init(&ctx->buf);
ctx->buf = BUF_INIT;
ctx->event_read = wl_event_loop_add_fd(ctx->server->wl_event_loop,
pipe_fd, WL_EVENT_READABLE, handle_pipemenu_readable, ctx);