From 0f31ef53420b755c3978818bcd2e5a86e141413d Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Sat, 11 Jan 2025 23:39:06 -0700 Subject: [PATCH] buffer: clear out one line functions Buffer is fully exposed to the program, no need to rely on the linker to optimize useless code. --- buffer.c | 18 +----------------- buffer.h | 13 ------------- sam.c | 26 ++++++++++---------------- test/core/buffer-test.c | 30 +++++++++++++++--------------- ui-terminal-vt100.c | 4 ++-- vis-core.h | 1 - vis-operators.c | 2 +- vis-registers.c | 6 +++--- vis.c | 4 ++-- 9 files changed, 34 insertions(+), 70 deletions(-) diff --git a/buffer.c b/buffer.c index 67da2e051..eefeaa994 100644 --- a/buffer.c +++ b/buffer.c @@ -44,10 +44,6 @@ void buffer_release(Buffer *buf) { *buf = (Buffer){0}; } -void buffer_clear(Buffer *buf) { - buf->len = 0; -} - bool buffer_put(Buffer *buf, const void *data, size_t len) { if (!buffer_reserve(buf, len)) return false; @@ -140,7 +136,7 @@ bool buffer_appendf(Buffer *buf, const char *fmt, ...) { } bool buffer_printf(Buffer *buf, const char *fmt, ...) { - buffer_clear(buf); + buf->len = 0; va_list ap; va_start(ap, fmt); bool ret = buffer_vappendf(buf, fmt, ap); @@ -155,18 +151,6 @@ size_t buffer_length0(Buffer *buf) { return len; } -size_t buffer_length(Buffer *buf) { - return buf->len; -} - -size_t buffer_capacity(Buffer *buf) { - return buf->size; -} - -const char *buffer_content(Buffer *buf) { - return buf->data; -} - const char *buffer_content0(Buffer *buf) { if (buf->len == 0 || !buffer_terminate(buf)) return ""; diff --git a/buffer.h b/buffer.h index a155163ea..f099b3290 100644 --- a/buffer.h +++ b/buffer.h @@ -22,8 +22,6 @@ typedef struct { /** Release all resources, reinitialize buffer. */ void buffer_release(Buffer*); -/** Set buffer length to zero, keep allocated memory. */ -void buffer_clear(Buffer*); /** Reserve space to store at least ``size`` bytes.*/ bool buffer_reserve(Buffer*, size_t size); /** Reserve space for at least ``len`` *more* bytes. */ @@ -54,22 +52,11 @@ bool buffer_printf(Buffer*, const char *fmt, ...) __attribute__((format(printf, bool buffer_appendf(Buffer*, const char *fmt, ...) __attribute__((format(printf, 2, 3))); /** Return length of a buffer without trailing NUL byte. */ size_t buffer_length0(Buffer*); -/** Return length of a buffer including possible NUL byte. */ -size_t buffer_length(Buffer*); -/** Return current maximal capacity in bytes of this buffer. */ -size_t buffer_capacity(Buffer*); /** * Get pointer to buffer data. * Guaranteed to return a NUL terminated string even if buffer is empty. */ const char *buffer_content0(Buffer*); -/** - * Get pointer to buffer data. - * @rst - * .. warning:: Might be NULL, if empty. Might not be NUL terminated. - * @endrst - */ -const char *buffer_content(Buffer*); /** * Borrow underlying buffer data. * @rst diff --git a/sam.c b/sam.c index 1380971f5..de547d1a5 100644 --- a/sam.c +++ b/sam.c @@ -1349,11 +1349,9 @@ static bool cmd_insert(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (!win) return false; Buffer buf = text(vis, argv[1]); - size_t len = buffer_length(&buf); - char *data = buffer_move(&buf); - bool ret = sam_insert(win, sel, range->start, data, len, cmd->count.start); + bool ret = sam_insert(win, sel, range->start, buf.data, buf.len, cmd->count.start); if (!ret) - free(data); + free(buf.data); return ret; } @@ -1361,11 +1359,9 @@ static bool cmd_append(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (!win) return false; Buffer buf = text(vis, argv[1]); - size_t len = buffer_length(&buf); - char *data = buffer_move(&buf); - bool ret = sam_insert(win, sel, range->end, data, len, cmd->count.start); + bool ret = sam_insert(win, sel, range->end, buf.data, buf.len, cmd->count.start); if (!ret) - free(data); + free(buf.data); return ret; } @@ -1373,11 +1369,9 @@ static bool cmd_change(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (!win) return false; Buffer buf = text(vis, argv[1]); - size_t len = buffer_length(&buf); - char *data = buffer_move(&buf); - bool ret = sam_change(win, sel, range, data, len, cmd->count.start); + bool ret = sam_change(win, sel, range, buf.data, buf.len, cmd->count.start); if (!ret) - free(data); + free(buf.data); return ret; } @@ -1751,10 +1745,10 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (vis->interrupted) { vis_info_show(vis, "Command cancelled"); } else if (status == 0) { - size_t len = buffer_length(&bufout); - char *data = buffer_move(&bufout); - if (!sam_change(win, sel, range, data, len, 1)) - free(data); + if (!sam_change(win, sel, range, bufout.data, bufout.len, 1)) { + free(bufout.data); + bufout.data = 0; + } } else { vis_info_show(vis, "Command failed %s", buffer_content0(&buferr)); } diff --git a/test/core/buffer-test.c b/test/core/buffer-test.c index a102b0188..72aff9721 100644 --- a/test/core/buffer-test.c +++ b/test/core/buffer-test.c @@ -19,9 +19,9 @@ int main(int argc, char *argv[]) { plan_no_plan(); - ok(buffer_content(&buf) == NULL && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Initialization"); - ok(buffer_insert(&buf, 0, "foo", 0) && buffer_content(&buf) == NULL && - buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Insert zero length data"); + ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Initialization"); + ok(buffer_insert(&buf, 0, "foo", 0) && buf.data == NULL && + buf.len == 0 && buf.size == 0, "Insert zero length data"); ok(!buffer_insert0(&buf, 1, "foo"), "Insert string at invalid position"); ok(buffer_insert0(&buf, 0, "") && compare0(&buf, ""), "Insert empty string"); @@ -36,16 +36,16 @@ int main(int argc, char *argv[]) { ok(buffer_append0(&buf, "baz") && compare0(&buf, "foobarbaz"), "Append string"); buffer_release(&buf); - ok(buf.data == NULL && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0, "Release"); + ok(buf.data == NULL && buf.len == 0 && buf.size == 0, "Release"); ok(buffer_insert(&buf, 0, "foo", 0) && compare(&buf, "", 0), "Insert zero length data"); ok(buffer_insert(&buf, 0, "foo", 3) && compare(&buf, "foo", 3), "Insert data at start"); ok(buffer_insert(&buf, 1, "l", 1) && compare(&buf, "floo", 4), "Insert data in middle"); ok(buffer_insert(&buf, 4, "r", 1) && compare(&buf, "floor", 5), "Insert data at end"); - size_t cap = buffer_capacity(&buf); - buffer_clear(&buf); - ok(buf.data && buffer_length(&buf) == 0 && buffer_capacity(&buf) == cap, "Clear"); + size_t cap = buf.size; + buf.len = 0; + ok(buf.data && buf.len == 0 && buf.size == cap, "Clear"); ok(buffer_put(&buf, "foo", 0) && compare(&buf, "", 0), "Put zero length data"); ok(buffer_put(&buf, "bar", 3) && compare(&buf, "bar", 3), "Put data"); @@ -53,34 +53,34 @@ int main(int argc, char *argv[]) { ok(buffer_prepend(&buf, "foo\0", 4) && compare(&buf, "foo\0bar", 7), "Prepend data"); ok(buffer_append(&buf, "\0baz", 4) && compare(&buf, "foo\0bar\0baz", 11), "Append data"); - ok(buffer_grow(&buf, cap+1) && compare(&buf, "foo\0bar\0baz", 11) && buffer_capacity(&buf) >= cap+1, "Grow"); + ok(buffer_grow(&buf, cap+1) && compare(&buf, "foo\0bar\0baz", 11) && buf.size >= cap+1, "Grow"); - const char *content = buffer_content(&buf); + const char *content = buf.data; char *data = buffer_move(&buf); - ok(data == content && buffer_length(&buf) == 0 && buffer_capacity(&buf) == 0 && buffer_content(&buf) == NULL, "Move"); - ok(buffer_append0(&buf, "foo") && buffer_content(&buf) != data, "Modify after move"); + ok(data == content && buf.len == 0 && buf.size == 0 && buf.data == NULL, "Move"); + ok(buffer_append0(&buf, "foo") && buf.data != data, "Modify after move"); free(data); skip_if(TIS_INTERPRETER, 1, "vsnprintf not supported") { ok(buffer_printf(&buf, "Test: %d\n", 42) && compare0(&buf, "Test: 42\n"), "Set formatted"); ok(buffer_printf(&buf, "%d\n", 42) && compare0(&buf, "42\n"), "Set formatted overwrite"); - buffer_clear(&buf); + buf.len = 0; ok(buffer_printf(&buf, "%s", "") && compare0(&buf, ""), "Set formatted empty string"); - buffer_clear(&buf); + buf.len = 0; bool append = true; for (int i = 1; i <= 10; i++) append &= buffer_appendf(&buf, "%d", i); ok(append && compare0(&buf, "12345678910"), "Append formatted"); - buffer_clear(&buf); + buf.len = 0; append = true; for (int i = 1; i <= 10; i++) append &= buffer_appendf(&buf, "%s", ""); ok(append && compare0(&buf, ""), "Append formatted empty string"); - buffer_clear(&buf); + buf.len = 0; } buffer_release(&buf); diff --git a/ui-terminal-vt100.c b/ui-terminal-vt100.c index 3c06bff65..0362c4bdd 100644 --- a/ui-terminal-vt100.c +++ b/ui-terminal-vt100.c @@ -91,7 +91,7 @@ static void cursor_visible(bool visible) { static void ui_term_backend_blit(Ui *tui) { Buffer *buf = tui->ctx; - buffer_clear(buf); + buf->len = 0; CellAttr attr = CELL_ATTR_NORMAL; CellColor fg = CELL_COLOR_DEFAULT, bg = CELL_COLOR_DEFAULT; int w = tui->width, h = tui->height; @@ -152,7 +152,7 @@ static void ui_term_backend_blit(Ui *tui) { cell++; } } - output(buffer_content(buf), buffer_length0(buf)); + output(buf->data, buffer_length0(buf)); } static void ui_term_backend_clear(Ui *tui) { } diff --git a/vis-core.h b/vis-core.h index cb24624fc..92770eed5 100644 --- a/vis-core.h +++ b/vis-core.h @@ -109,7 +109,6 @@ typedef struct { /* a macro is just a sequence of symbolic keys as received from ui->getkey */ typedef Buffer Macro; #define macro_release buffer_release -#define macro_reset buffer_clear #define macro_append buffer_append0 typedef struct { /** collects all information until an operator is executed */ diff --git a/vis-operators.c b/vis-operators.c index 59abcf6bb..6b7f873ed 100644 --- a/vis-operators.c +++ b/vis-operators.c @@ -264,7 +264,7 @@ bool vis_operator(Vis *vis, enum VisOperator id, ...) { case VIS_OP_REPLACE: { Macro *macro = macro_get(vis, VIS_REG_DOT); - macro_reset(macro); + macro->len = 0; macro_append(macro, va_arg(ap, char*)); vis->action.arg.s = macro->data; break; diff --git a/vis-registers.c b/vis-registers.c index 8b12119b8..0ac2d7d65 100644 --- a/vis-registers.c +++ b/vis-registers.c @@ -67,7 +67,7 @@ const char *register_slot_get(Vis *vis, Register *reg, size_t slot, size_t *len) Buffer *buf = array_get(®->values, slot); if (!buf) return NULL; - buffer_clear(buf); + buf->len = 0; if (id == VIS_REG_PRIMARY) cmd[3] = "primary"; @@ -270,8 +270,8 @@ Array vis_register_get(Vis *vis, enum VisRegister id) { for (size_t i = 0; i < len; i++) { Buffer *buf = array_get(®->values, i); TextString string = { - .data = buffer_content(buf), - .len = buffer_length(buf), + .data = buf->data, + .len = buf->len, }; array_add(&data, &string); } diff --git a/vis.c b/vis.c index 0f2e32564..c561a3b05 100644 --- a/vis.c +++ b/vis.c @@ -1353,7 +1353,7 @@ void macro_operator_record(Vis *vis) { if (vis->macro_operator) return; vis->macro_operator = macro_get(vis, VIS_MACRO_OPERATOR); - macro_reset(vis->macro_operator); + vis->macro_operator->len = 0; } void macro_operator_stop(Vis *vis) { @@ -1370,7 +1370,7 @@ bool vis_macro_record(Vis *vis, enum VisRegister id) { if (vis->recording || !macro) return false; if (!(VIS_REG_A <= id && id <= VIS_REG_Z)) - macro_reset(macro); + macro->len = 0; vis->recording = macro; vis_event_emit(vis, VIS_EVENT_WIN_STATUS, vis->win); return true;