Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/indent indent: clean up memory and buffer management
details: https://anonhg.NetBSD.org/src/rev/fc625e067213
branches: trunk
changeset: 375326:fc625e067213
user: rillig <rillig%NetBSD.org@localhost>
date: Mon May 15 07:28:45 2023 +0000
description:
indent: clean up memory and buffer management
Remove the need to explicitly initialize the buffers. To avoid
subtracting null pointers or comparing them using '<', migrate the
buffers from the (start, end) form to the (start, len) form. This form
also avoids inconsistencies in whether 'buf.e == buf.s' or 'buf.s ==
buf.e' is used.
Make buffer.st const, to avoid accidental modification of the buffer's
content.
Replace '*buf.e++ = ch' with buf_add_char, to avoid having to keep track
how much unwritten space is left in the buffer. Remove all safety
margins, that is, no more unchecked access to buf.st[-1] or appending
using '*buf.e++'.
Fix line number counting in lex_word for words that contain line breaks.
No functional change.
diffstat:
tests/usr.bin/indent/lsym_comment.c | 14 +--
usr.bin/indent/debug.c | 22 +-
usr.bin/indent/indent.c | 211 +++++++++++++----------------------
usr.bin/indent/indent.h | 20 +--
usr.bin/indent/io.c | 103 ++++++-----------
usr.bin/indent/lexi.c | 68 ++++------
usr.bin/indent/parse.c | 6 +-
usr.bin/indent/pr_comment.c | 71 ++++++-----
8 files changed, 207 insertions(+), 308 deletions(-)
diffs (truncated from 1450 to 300 lines):
diff -r 2473e032118a -r fc625e067213 tests/usr.bin/indent/lsym_comment.c
--- a/tests/usr.bin/indent/lsym_comment.c Mon May 15 01:42:42 2023 +0000
+++ b/tests/usr.bin/indent/lsym_comment.c Mon May 15 07:28:45 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.9 2023/05/14 22:26:37 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.10 2023/05/15 07:28:45 rillig Exp $ */
/*
* Tests for the token lsym_comment, which starts a comment.
@@ -1031,18 +1031,6 @@ line 4
/*
- * Cover the code for expanding the comment buffer. As of 2021-11-07, the
- * default buffer size is 200. To actually fill the comment buffer, there must
- * be a single line of a comment that is longer than 200 bytes.
- */
-//indent input
-/*-_____10________20________30________40________50________60________70________80________90_______100_______110_______120_______130_______140_______150_______160_______170_______180_______190_______200
*/
-//indent end
-
-//indent run-equals-input
-
-
-/*
* Since 2019-04-04 and before pr_comment.c 1.123 from 2021-11-25, the
* function analyze_comment wrongly joined the two comments.
*/
diff -r 2473e032118a -r fc625e067213 usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c Mon May 15 01:42:42 2023 +0000
+++ b/usr.bin/indent/debug.c Mon May 15 07:28:45 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.5 2023/05/15 07:28:45 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.4 2023/05/13 15:34:22 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.5 2023/05/15 07:28:45 rillig Exp $");
#include "indent.h"
@@ -108,26 +108,26 @@ static bool debug_full_parser_state = tr
static void
debug_print_buf(const char *name, const struct buffer *buf)
{
- if (buf->s < buf->e) {
+ if (buf->len > 0) {
debug_printf("%s ", name);
- debug_vis_range("\"", buf->s, buf->e, "\"\n");
+ debug_vis_range("\"", buf->st, buf->len, "\"\n");
}
}
void
debug_buffers(void)
{
- if (lab.e != lab.s) {
+ if (lab.len > 0) {
debug_printf(" label ");
- debug_vis_range("\"", lab.s, lab.e, "\"");
+ debug_vis_range("\"", lab.st, lab.len, "\"");
}
- if (code.e != code.s) {
+ if (code.len > 0) {
debug_printf(" code ");
- debug_vis_range("\"", code.s, code.e, "\"");
+ debug_vis_range("\"", code.st, code.len, "\"");
}
- if (com.e < com.s) {
+ if (com.len > 0) {
debug_printf(" comment ");
- debug_vis_range("\"", com.s, com.e, "\"");
+ debug_vis_range("\"", com.st, com.len, "\"");
}
}
@@ -192,7 +192,7 @@ debug_parser_state(lexer_symbol lsym)
debug_println("");
debug_printf("line %d: %s", line_no, lsym_name[lsym]);
- debug_vis_range(" \"", token.s, token.e, "\"\n");
+ debug_vis_range(" \"", token.st, token.len, "\"\n");
debug_print_buf("label", &lab);
debug_print_buf("code", &code);
diff -r 2473e032118a -r fc625e067213 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c Mon May 15 01:42:42 2023 +0000
+++ b/usr.bin/indent/indent.c Mon May 15 07:28:45 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.267 2023/05/14 22:26:37 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.268 2023/05/15 07:28:45 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.267 2023/05/14 22:26:37 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.268 2023/05/15 07:28:45 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -101,66 +101,34 @@ static char bakfile[MAXPATHLEN] = "";
static void
-buf_init(struct buffer *buf)
-{
- size_t size = 200;
- buf->mem = xmalloc(size);
- buf->limit = buf->mem + size - 5 /* safety margin */;
- buf->s = buf->mem + 1; /* allow accessing buf->e[-1] */
- buf->e = buf->s;
- buf->mem[0] = ' ';
-}
-
-static size_t
-buf_len(const struct buffer *buf)
-{
- return (size_t)(buf->e - buf->s);
-}
-
-void
buf_expand(struct buffer *buf, size_t add_size)
{
- size_t new_size = (size_t)(buf->limit - buf->s) + 400 + add_size;
- size_t len = buf_len(buf);
- buf->mem = xrealloc(buf->mem, new_size);
- buf->limit = buf->mem + new_size - 5;
- buf->s = buf->mem + 1;
- buf->e = buf->s + len;
-}
-
-static void
-buf_reserve(struct buffer *buf, size_t n)
-{
- if (n >= (size_t)(buf->limit - buf->e))
- buf_expand(buf, n);
+ buf->cap = buf->cap + add_size + 400;
+ buf->mem = xrealloc(buf->mem, buf->cap);
+ buf->st = buf->mem;
}
void
buf_add_char(struct buffer *buf, char ch)
{
- buf_reserve(buf, 1);
- *buf->e++ = ch;
+ if (buf->len == buf->cap)
+ buf_expand(buf, 1);
+ buf->mem[buf->len++] = ch;
}
void
-buf_add_range(struct buffer *buf, const char *s, const char *e)
+buf_add_chars(struct buffer *buf, const char *s, size_t len)
{
- size_t len = (size_t)(e - s);
- buf_reserve(buf, len);
- memcpy(buf->e, s, len);
- buf->e += len;
+ if (len > buf->cap - buf->len)
+ buf_expand(buf, len);
+ memcpy(buf->mem + buf->len, s, len);
+ buf->len += len;
}
static void
buf_add_buf(struct buffer *buf, const struct buffer *add)
{
- buf_add_range(buf, add->s, add->e);
-}
-
-static void
-buf_reset(struct buffer *buf)
-{
- buf->e = buf->s;
+ buf_add_chars(buf, add->st, add->len);
}
void
@@ -180,13 +148,13 @@ diag(int level, const char *msg, ...)
}
/*
- * Compute the indentation from starting at 'ind' and adding the text from
- * 'start' to 'end'.
+ * Compute the indentation from starting at 'ind' and adding the text starting
+ * at 's'.
*/
int
-ind_add(int ind, const char *start, const char *end)
+ind_add(int ind, const char *s, size_t len)
{
- for (const char *p = start; p != end; ++p) {
+ for (const char *p = s; len > 0; p++, len--) {
if (*p == '\n' || *p == '\f')
ind = 0;
else if (*p == '\t')
@@ -202,14 +170,6 @@ ind_add(int ind, const char *start, cons
static void
main_init_globals(void)
{
- inp_init();
-
- buf_init(&token);
-
- buf_init(&lab);
- buf_init(&code);
- buf_init(&com);
-
ps.s_sym[0] = psym_stmt_list;
ps.prev_token = lsym_semicolon;
ps.next_col_1 = true;
@@ -345,9 +305,9 @@ static void
code_add_decl_indent(int decl_ind, bool tabs_to_var)
{
int base_ind = ps.ind_level * opt.indent_size;
- int ind = base_ind + (int)buf_len(&code);
+ int ind = base_ind + (int)code.len;
int target_ind = base_ind + decl_ind;
- const char *orig_code_e = code.e;
+ size_t orig_code_len = code.len;
if (tabs_to_var)
for (int next; (next = next_tab(ind)) <= target_ind; ind = next)
@@ -356,7 +316,7 @@ code_add_decl_indent(int decl_ind, bool
for (; ind < target_ind; ind++)
buf_add_char(&code, ' ');
- if (code.e == orig_code_e && ps.want_blank) {
+ if (code.len == orig_code_len && ps.want_blank) {
buf_add_char(&code, ' ');
ps.want_blank = false;
}
@@ -365,7 +325,7 @@ code_add_decl_indent(int decl_ind, bool
static int
process_eof(void)
{
- if (lab.s != lab.e || code.s != code.e || com.s != com.e)
+ if (lab.len > 0 || code.len > 0 || com.len > 0)
output_line();
if (ps.tos > 1) /* check for balanced braces */
@@ -394,11 +354,11 @@ maybe_break_line(lexer_symbol lsym)
static void
move_com_to_code(void)
{
- if (lab.e != lab.s || code.e != code.s)
+ if (lab.len > 0 || code.len > 0)
buf_add_char(&code, ' ');
buf_add_buf(&code, &com);
buf_add_char(&code, ' ');
- buf_reset(&com);
+ com.len = 0;
ps.want_blank = false;
}
@@ -414,7 +374,7 @@ process_newline(void)
{
if (ps.prev_token == lsym_comma && ps.nparen == 0 && !ps.block_init &&
!opt.break_after_comma && break_comma &&
- com.s == com.e)
+ com.len == 0)
goto stay_in_line;
output_line();
@@ -426,7 +386,7 @@ stay_in_line:
static bool
is_function_pointer_declaration(void)
{
- return token.s[0] == '('
+ return token.st[0] == '('
&& ps.in_decl
&& !ps.block_init
&& !ps.decl_indent_done
@@ -465,11 +425,11 @@ process_lparen_or_lbracket(void)
code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
ps.decl_indent_done = true;
} else if (want_blank_before_lparen())
- *code.e++ = ' ';
+ buf_add_char(&code, ' ');
ps.want_blank = false;
- *code.e++ = token.s[0];
+ buf_add_char(&code, token.st[0]);
- ps.paren[ps.nparen - 1].indent = (short)ind_add(0, code.s, code.e);
+ ps.paren[ps.nparen - 1].indent = (short)ind_add(0, code.st, code.len);
debug_println("paren_indents[%d] is now %d",
ps.nparen - 1, ps.paren[ps.nparen - 1].indent);
@@ -479,7 +439,7 @@ process_lparen_or_lbracket(void)
debug_println("paren_indents[0] is now %d", ps.paren[0].indent);
}
Home |
Main Index |
Thread Index |
Old Index