Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp101001iof; Sun, 5 Jun 2022 22:23:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWlrGaJYIh9hhWg3ypqZSMdUQ129gj+cKVUIvZgavNIGwS4MLwmOPcPHDLGxVB/ZEjJDuA X-Received: by 2002:a17:903:1c3:b0:166:303e:124a with SMTP id e3-20020a17090301c300b00166303e124amr22083257plh.7.1654493030167; Sun, 05 Jun 2022 22:23:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654493030; cv=none; d=google.com; s=arc-20160816; b=p47hVc4CX32cl+HCNgu5RYxeCvLql77q/cy+UWyzYurEoy7UgBPqZYHxAgjFHzs/Jg cUbbO+QvMg01NKoCbq7xJzKk8S5yS3xtvJpugVS5zFQrHuUGO8FOWrYwiq5qkJ3k9PgO p5rYB5LgPCi1myN2TLkVMCBf93HwRkq1qTwLaYddWMup1APPMRDwdI3rslLoC4hj5fF7 JqFMktxo8auEY3Or/ilT4Bv5GEn04Eo1W/Sa7U1bSpIgJ5/5IuHrr8pig6l53gadFU0N 8ieEDDKLZFwUmy1MAYBBoG6L+Ta3K2oltffTAz1liWy54Y4NnFbv4mquMow1vjLd6STz Dt4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Pibz/KENrk8dWYPVgji9GTuoqUjjn4PZ4o1W4g097Gg=; b=F7rscH1MQvs6baW2J0OJucure7N15ph5bxQe/GVa1StlmE+yV9W4Tqw9ak50iA2ZXo cmSInd41BKYG/wvuzCKi/oynJZ5+0YAxL3/UU7HKhJpe3IBoHDSz1isAV0P//01TosK/ kdYuDtDJRDUhWmzzsW2QRyGkV//3tY2dJI/MYW/GRZduv0L8Q+VhII9xWF2dt7Oz9iLN vOcTknsAwKz4Mdx1qwfk1PFATP3lUSAfO1I90ZkYpH4edFYzxHt4hIyrJPeW73cR97p+ BseB0RwfQk8+XUrAJhTc33pT7Txt+pTG87tT6J4x1fUYgmUW/LJf/akViJwHIl105ugc /mlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=e4nBobTg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id v19-20020a170902d09300b00163e051ed3bsi16196434plv.338.2022.06.05.22.23.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:23:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=e4nBobTg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 34C1B1A7D16; Sun, 5 Jun 2022 21:26:15 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239967AbiFDTa4 (ORCPT + 99 others); Sat, 4 Jun 2022 15:30:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239929AbiFDTax (ORCPT ); Sat, 4 Jun 2022 15:30:53 -0400 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAB8922519 for ; Sat, 4 Jun 2022 12:30:52 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id k4so1354273qth.8 for ; Sat, 04 Jun 2022 12:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Pibz/KENrk8dWYPVgji9GTuoqUjjn4PZ4o1W4g097Gg=; b=e4nBobTgPn+CP4VPeQfBie8q+Tc0uce9HxMY3ySOdDDEaejD6kTUJJbMPVfsf9NJhH qP/dYAIIPCQ64UBzy3+TglHoVkl2B6YChj6UmnkRtkWVjtD+x2jVVJmHVYbKxxfokRze cCC/so1OBZfyBE3h/WXnS0wNzbXsPpg5rQ6k/sxpvtAHbYHIdVsTp5VvQwqXLHR7yD3M CqsYYrKr41Szj80vPehyPaVxnV0SQTgLeFUYp1bLzZigOatpDhu+Juk17/MoYDqe0WM3 Ef0Psb3C7zZkE6TLavDET2mYK+oVKx7VidVZYHc3nTuFzZjiy+WYnDCkAON44q6KNdW0 8MRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Pibz/KENrk8dWYPVgji9GTuoqUjjn4PZ4o1W4g097Gg=; b=DEdQvRwsvrASdQ/zsyS8ALrkLNRFzPJtfjpuBhECQ+5Sgmift/RcETcAaeSa3AIfzv YLDacfrKlRl5V3MGpT7SSKYL4PnecQZtXAmhf+oNKZhpEgNUID9yKLQXHwc1ihq66Ujq Od2F5utZOFEeirzbbQLGpUxoy7nvuHVgTCv7QbVCh3qFF36SD90Q3Uks1I5b9l8lwsMz tDRxXJcxqPq+nK+/5+EvMiOcUxBHuSdOD4AQmtMNfG0cOvwXMCbiAX9i/YUVPKWCewXk BoyBXz9rjzZ1xlQSN2q+FrauRtJfCZmNTcPCcVPOZMQuegibh+diSQA2dyeIzskbvaYj lgLA== X-Gm-Message-State: AOAM532bnq25SLCC/q+HN4L7gcLbuaEmYkfmyo3SAahzjR0D7ENcCKxX m3KBPi0hIqb4EyuSOOn9gcEUPdpgCd70 X-Received: by 2002:a05:622a:1101:b0:304:e4ad:f492 with SMTP id e1-20020a05622a110100b00304e4adf492mr4123050qty.452.1654371051359; Sat, 04 Jun 2022 12:30:51 -0700 (PDT) Received: from moria.home.lan (c-73-219-103-14.hsd1.vt.comcast.net. [73.219.103.14]) by smtp.gmail.com with ESMTPSA id o17-20020ac84291000000b00304defdb1b3sm3537426qtl.85.2022.06.04.12.30.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jun 2022 12:30:50 -0700 (PDT) From: Kent Overstreet To: linux-kernel@vger.kernel.org Cc: Kent Overstreet , pmladek@suse.com, rostedt@goodmis.org, Matthew Wilcox Subject: [PATCH v3 01/33] lib/printbuf: New data structure for printing strings Date: Sat, 4 Jun 2022 15:30:10 -0400 Message-Id: <20220604193042.1674951-2-kent.overstreet@gmail.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220604193042.1674951-1-kent.overstreet@gmail.com> References: <20220604193042.1674951-1-kent.overstreet@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds printbufs: a printbuf points to a char * buffer and knows the size of the output buffer as well as the current output position. Future patches will be adding more features to printbuf, but initially printbufs are targeted at refactoring and improving our existing code in lib/vsprintf.c - so this initial printbuf patch has the features required for that. Signed-off-by: Kent Overstreet Reviewed-by: Matthew Wilcox (Oracle) --- include/linux/printbuf.h | 118 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 include/linux/printbuf.h diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h new file mode 100644 index 0000000000..8b3797dc4b --- /dev/null +++ b/include/linux/printbuf.h @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* Copyright (C) 2022 Kent Overstreet */ + +#ifndef _LINUX_PRINTBUF_H +#define _LINUX_PRINTBUF_H + +#include + +/* + * Printbufs: String buffer for outputting (printing) to, for vsnprintf + */ + +struct printbuf { + char *buf; + unsigned size; + unsigned pos; +}; + +/* + * Returns size remaining of output buffer: + */ +static inline unsigned printbuf_remaining_size(struct printbuf *out) +{ + return out->pos < out->size ? out->size - out->pos : 0; +} + +/* + * Returns number of characters we can print to the output buffer - i.e. + * excluding the terminating nul: + */ +static inline unsigned printbuf_remaining(struct printbuf *out) +{ + return out->pos < out->size ? out->size - out->pos - 1 : 0; +} + +static inline unsigned printbuf_written(struct printbuf *out) +{ + return min(out->pos, out->size); +} + +/* + * Returns true if output was truncated: + */ +static inline bool printbuf_overflowed(struct printbuf *out) +{ + return out->pos >= out->size; +} + +static inline void printbuf_nul_terminate(struct printbuf *out) +{ + if (out->pos < out->size) + out->buf[out->pos] = 0; + else if (out->size) + out->buf[out->size - 1] = 0; +} + +static inline void __prt_chars(struct printbuf *out, char c, unsigned n) +{ + memset(out->buf + out->pos, + c, + min(n, printbuf_remaining(out))); + out->pos += n; +} + +static inline void prt_chars(struct printbuf *out, char c, unsigned n) +{ + __prt_chars(out, c, n); + printbuf_nul_terminate(out); +} + +static inline void __prt_char(struct printbuf *out, char c) +{ + if (printbuf_remaining(out)) + out->buf[out->pos] = c; + out->pos++; +} + +static inline void prt_char(struct printbuf *out, char c) +{ + __prt_char(out, c); + printbuf_nul_terminate(out); +} + +static inline void prt_bytes(struct printbuf *out, const void *b, unsigned n) +{ + memcpy(out->buf + out->pos, + b, + min(n, printbuf_remaining(out))); + out->pos += n; + printbuf_nul_terminate(out); +} + +static inline void prt_str(struct printbuf *out, const char *str) +{ + prt_bytes(out, str, strlen(str)); +} + +static inline void prt_hex_byte(struct printbuf *out, u8 byte) +{ + __prt_char(out, hex_asc_hi(byte)); + __prt_char(out, hex_asc_lo(byte)); + printbuf_nul_terminate(out); +} + +static inline void prt_hex_byte_upper(struct printbuf *out, u8 byte) +{ + __prt_char(out, hex_asc_upper_hi(byte)); + __prt_char(out, hex_asc_upper_lo(byte)); + printbuf_nul_terminate(out); +} + +#define PRINTBUF_EXTERN(_buf, _size) \ +((struct printbuf) { \ + .buf = _buf, \ + .size = _size, \ +}) + +#endif /* _LINUX_PRINTBUF_H */ -- 2.36.0