From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E43F0C433EF for ; Tue, 21 Jun 2022 08:14:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F2E66B0072; Tue, 21 Jun 2022 04:14:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A22C6B0073; Tue, 21 Jun 2022 04:14:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 369928E0001; Tue, 21 Jun 2022 04:14:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 233FF6B0072 for ; Tue, 21 Jun 2022 04:14:14 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DE9F51079 for ; Tue, 21 Jun 2022 08:14:13 +0000 (UTC) X-FDA: 79601530386.15.9199C1F Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) by imf27.hostedemail.com (Postfix) with ESMTP id 5FDDC4008C for ; Tue, 21 Jun 2022 08:14:13 +0000 (UTC) Received: by mail-lf1-f54.google.com with SMTP id y32so21057134lfa.6 for ; Tue, 21 Jun 2022 01:14:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=0qbX+HZcIdI4yeZAAxEQa15Bz902lFD5WKfEE9fTfjU=; b=VHglxW5+rgr1w513Qfypb0rHsKD+qePqFA2997oV3AXqcYfySQLbZBrNYgW5VUJ5IV ePLX3lhtM44wnAItTgSDtAxzPC3KO4M+88PIJA9hm4JWaljuM5GTAqCglwh/42XIfr6m yU2pV6kk8yX/wJiPOydMjXhOK0/XtVwl7CTFY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=0qbX+HZcIdI4yeZAAxEQa15Bz902lFD5WKfEE9fTfjU=; b=G4ancZOaMDzfumHZfSXZxKRK8aYPrH41aAQTtxRs+jhpVnQ/tir4Z474Q8FUmGGCkb 5RdIOTvtK4ajX6CvcT3vIf/FDV+JfBlwPiVhtWnjXLKgkFRG68+/T39ulEGitIl0Kw0o rh2jR2OMHKUL7WlEPfjH0/KkKez64SL9SRYhj5o36FRvI+dg+sM16h9zQatYmjBuhV6X ZuGczwvYK4MSsVT1vBTvUUzLauacmPKs1sVZTzku7hjkyUuKOXip34TT0ZQJnH8QbrBS chlWnOmBgitVBKE+A+CJR61i0hamp+DVA3gLXef85ezfY/93DpfIlechbTpF5EEigwVh u7ig== X-Gm-Message-State: AJIora/Q+tF7gDQOpIxnqW6iz1RerG2RmKky/ntopX6GYKkWho5Mcb31 gszuw2bPxnBqMGx4a26PBVbnow== X-Google-Smtp-Source: AGRyM1u+zUhfCCGCZCym7AJJEi4Df5fDdiy1vGPqBJfNs6HvSYHsUiVAKy7oYac1oliTxywrw+k6kg== X-Received: by 2002:a05:6512:11d2:b0:47f:7ca3:c533 with SMTP id h18-20020a05651211d200b0047f7ca3c533mr1432629lfr.388.1655799251768; Tue, 21 Jun 2022 01:14:11 -0700 (PDT) Received: from [172.16.11.74] ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id p5-20020a2eb7c5000000b0025a7b36d222sm74100ljo.135.2022.06.21.01.14.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Jun 2022 01:14:11 -0700 (PDT) Message-ID: Date: Tue, 21 Jun 2022 10:14:09 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH v4 08/34] lib/printbuf: Tabstops, indenting Content-Language: en-US To: Kent Overstreet , linux-kernel@vger.kernel.org, linux-mm@kvack.org, pmladek@suse.com Cc: rostedt@goodmis.org, enozhatsky@chromium.org, willy@infradead.org References: <20220620004233.3805-1-kent.overstreet@gmail.com> <20220620004233.3805-9-kent.overstreet@gmail.com> From: Rasmus Villemoes In-Reply-To: <20220620004233.3805-9-kent.overstreet@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655799253; a=rsa-sha256; cv=none; b=FWA/uUEMjwMTRfppjCCsrBCfdE08tl+bQHHSXLIH2F2RVNVWBw6sMGRXmgbsQH07gqRCHF +IhXSGFOsEbFviR4W/qqeW9yIbYfHBubMNeq1kB06u/e02eMdBsPqyNILbKlzbszSZiTEq zQs8FB80xMHrItDg13cXFqddtueETGs= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=rasmusvillemoes.dk header.s=google header.b=VHglxW5+; dmarc=none; spf=pass (imf27.hostedemail.com: domain of linux@rasmusvillemoes.dk designates 209.85.167.54 as permitted sender) smtp.mailfrom=linux@rasmusvillemoes.dk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655799253; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0qbX+HZcIdI4yeZAAxEQa15Bz902lFD5WKfEE9fTfjU=; b=AlwZKawdzOi2S2e3rRVH0JV7pPI69nPJlgGJF9eY/MXs7k5Yns4isLwr1fY6GAT0ydHFxF D5G4qX21K/AhozwYNL6rPLQgbSAph0M+ZwJpLV7NaaugnTHyhlZb0Ce6hdfaRButUYfFcR 7eEJxpiLzNySWUVYwJufonNBhEi46Po= Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=rasmusvillemoes.dk header.s=google header.b=VHglxW5+; dmarc=none; spf=pass (imf27.hostedemail.com: domain of linux@rasmusvillemoes.dk designates 209.85.167.54 as permitted sender) smtp.mailfrom=linux@rasmusvillemoes.dk X-Rspam-User: X-Stat-Signature: ti5ctraxmyjuy7qmiy99trrjfe196q9i X-Rspamd-Queue-Id: 5FDDC4008C X-Rspamd-Server: rspam08 X-HE-Tag: 1655799253-816146 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 20/06/2022 02.42, Kent Overstreet wrote: > This patch adds two new features to printbuf for structured formatting: > > - Indent level: the indent level, as a number of spaces, may be > increased with pr_indent_add() and decreased with pr_indent_sub(). > > Subsequent lines, when started with pr_newline() (not "\n", although > that may change) will then be intended according to the current > indent level. This helps with pretty-printers that structure a large > amonut of data across multiple lines and multiple functions. > > - Tabstops: Tabstops may be set by assigning to the printbuf->tabstops > array. > > Then, pr_tab() may be used to advance to the next tabstop, printing > as many spaces as required - leaving previous output left justified > to the previous tabstop. pr_tab_rjust() advances to the next tabstop > but inserts the spaces just after the previous tabstop - right > justifying the previously-outputted text to the next tabstop. I am really, really, really not convinced that we want or need this. But as long as this doesn't add overhead to those not using it (in particular, as long as it doesn't grow a "scan whatever contents was just added for maybe a \n so ->last_newline can be updated"), meh. > + * > + * Make sure you use prt_newline() instead of \n in the format string for indent > + * level and tabstops to work corretly. > */ > > #include > @@ -45,18 +62,29 @@ struct printbuf { > char *buf; > unsigned size; > unsigned pos; > + unsigned last_newline; > + unsigned last_field; > + unsigned indent; > /* > * If nonzero, allocations will be done with GFP_ATOMIC: > */ > u8 atomic; > bool allocation_failure:1; > bool heap_allocated:1; > + u8 tabstop; > + u8 tabstops[4]; > }; > > int printbuf_make_room(struct printbuf *, unsigned); > const char *printbuf_str(const struct printbuf *); > void printbuf_exit(struct printbuf *); > > +void prt_newline(struct printbuf *); > +void printbuf_indent_add(struct printbuf *, unsigned); > +void printbuf_indent_sub(struct printbuf *, unsigned); > +void prt_tab(struct printbuf *); > +void prt_tab_rjust(struct printbuf *); > + > /* Initializer for a heap allocated printbuf: */ > #define PRINTBUF ((struct printbuf) { .heap_allocated = true }) > > @@ -187,6 +215,8 @@ static inline void printbuf_reset(struct printbuf *buf) > { > buf->pos = 0; > buf->allocation_failure = 0; > + buf->indent = 0; > + buf->tabstop = 0; > } > > /** > diff --git a/lib/printbuf.c b/lib/printbuf.c > index 8c70128e31..a7f80f63ca 100644 > --- a/lib/printbuf.c > +++ b/lib/printbuf.c > @@ -12,6 +12,11 @@ > #include > #include > > +static inline size_t printbuf_linelen(struct printbuf *buf) > +{ > + return buf->pos - buf->last_newline; > +} > + > int printbuf_make_room(struct printbuf *out, unsigned extra) > { > unsigned new_size; > @@ -69,3 +74,123 @@ void printbuf_exit(struct printbuf *buf) > } > } > EXPORT_SYMBOL(printbuf_exit); > + > +void prt_newline(struct printbuf *buf) > +{ > + unsigned i; > + > + printbuf_make_room(buf, 1 + buf->indent); > + > + __prt_char(buf, '\n'); > + > + buf->last_newline = buf->pos; > + > + for (i = 0; i < buf->indent; i++) > + __prt_char(buf, ' '); Why the loop? Don't you have a _chars variant? > +void printbuf_indent_add(struct printbuf *buf, unsigned spaces) > +{ > + if (WARN_ON_ONCE(buf->indent + spaces < buf->indent)) > + spaces = 0; > + > + buf->indent += spaces; > + while (spaces--) > + prt_char(buf, ' '); > +} > +EXPORT_SYMBOL(printbuf_indent_add); > + > +/** > + * printbuf_indent_sub - subtract from the current indent level > + * > + * @buf: printbuf to control > + * @spaces: number of spaces to subtract from the current indent level > + * > + * Subsequent lines, and the current line if the output position is at the start > + * of the current line, will be indented by @spaces less spaces. > + */ > +void printbuf_indent_sub(struct printbuf *buf, unsigned spaces) > +{ > + if (WARN_ON_ONCE(spaces > buf->indent)) > + spaces = buf->indent; > + > + if (buf->last_newline + buf->indent == buf->pos) { > + buf->pos -= spaces; > + printbuf_nul_terminate(buf); > + } > + buf->indent -= spaces; > +} > +EXPORT_SYMBOL(printbuf_indent_sub); > + > +/** > + * prt_tab - Advance printbuf to the next tabstop > + * > + * @buf: printbuf to control > + * > + * Advance output to the next tabstop by printing spaces. > + */ > +void prt_tab(struct printbuf *out) > +{ > + int spaces = max_t(int, 0, out->tabstops[out->tabstop] - printbuf_linelen(out)); > + > + BUG_ON(out->tabstop > ARRAY_SIZE(out->tabstops)); So this accesses out->tabstops first, then does a (buggy) bounds check. And kills the machine if somebody managed to corrupt ->tabstop. Instead of limping along and living with less-pretty-printed output. I don't think you'll get this code accepted by the Great Penguin. Rasmus