From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Rob Browning Newsgroups: gmane.lisp.guile.devel Subject: [PATCH 2/8] Create src/pyutil.c for utility functions Date: Tue, 30 May 2023 19:49:38 -0500 Message-ID: <20230531004944.1657633-3-rlb@defaultvalue.org> References: <20230531004944.1657633-1-rlb@defaultvalue.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19459"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Wed May 31 02:51:09 2023 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q4A3R-0004kk-F3 for guile-devel@m.gmane-mx.org; Wed, 31 May 2023 02:51:09 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A2D-0006Lj-Gz; Tue, 30 May 2023 20:49:53 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q4A2B-0006Kc-Sw for guile-devel@gnu.org; Tue, 30 May 2023 20:49:51 -0400 Original-Received: from defaultvalue.org ([45.33.119.55]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4A27-0008E6-Lh for guile-devel@gnu.org; Tue, 30 May 2023 20:49:50 -0400 Original-Received: from trouble.defaultvalue.org (localhost [127.0.0.1]) (Authenticated sender: rlb@defaultvalue.org) by defaultvalue.org (Postfix) with ESMTPSA id CB39220331 for ; Tue, 30 May 2023 19:49:45 -0500 (CDT) Original-Received: by trouble.defaultvalue.org (Postfix, from userid 1000) id 0ACD914E08C; Tue, 30 May 2023 19:49:45 -0500 (CDT) X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531004944.1657633-1-rlb@defaultvalue.org> Received-SPF: pass client-ip=45.33.119.55; envelope-from=rlb@defaultvalue.org; helo=defaultvalue.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:21848 Archived-At: Signed-off-by: Rob Browning --- GNUmakefile | 2 +- lib/bup/_helpers.c | 29 ++--------------------------- src/bup/pyutil.c | 37 +++++++++++++++++++++++++++++++++++++ src/bup/pyutil.h | 4 ++++ 4 files changed, 44 insertions(+), 28 deletions(-) create mode 100644 src/bup/pyutil.c create mode 100644 src/bup/pyutil.h diff --git a/GNUmakefile b/GNUmakefile index d762f32b7..d218698df 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -197,7 +197,7 @@ lib/cmd/bup: lib/cmd/bup.c src/bup/compat.c src/bup/io.c clean_paths += lib/bup/_helpers$(soext) generated_dependencies += lib/bup/_helpers.d -lib/bup/_helpers$(soext): lib/bup/_helpers.c lib/bup/bupsplit.c lib/bup/_hashsplit.c +lib/bup/_helpers$(soext): lib/bup/_helpers.c src/bup/pyutil.c lib/bup/bupsplit.c lib/bup/_hashsplit.c $(CC) $(helpers_cflags) $(CPPFLAGS) $(CFLAGS) $^ \ $(helpers_ldflags) $(LDFLAGS) -o $@ diff --git a/lib/bup/_helpers.c b/lib/bup/_helpers.c index 507b5842e..6874a2bc6 100644 --- a/lib/bup/_helpers.c +++ b/lib/bup/_helpers.c @@ -64,8 +64,9 @@ # pragma GCC diagnostic pop #endif -#include "bupsplit.h" #include "bup/intprops.h" +#include "bup/pyutil.h" +#include "bupsplit.h" #include "_hashsplit.h" #if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) @@ -108,32 +109,6 @@ typedef struct { #define rbuf_argf "y#" #define wbuf_argf "y*" - -static void *checked_calloc(size_t n, size_t size) -{ - void *result = calloc(n, size); - if (!result) - PyErr_NoMemory(); - return result; -} - -static void *checked_malloc(size_t n, size_t size) -{ - size_t total; - if (!INT_MULTIPLY_OK(n, size, &total)) - { - PyErr_Format(PyExc_OverflowError, - "request to allocate %zu items of size %zu is too large", - n, size); - return NULL; - } - void *result = malloc(total); - if (!result) - return PyErr_NoMemory(); - return result; -} - - #ifndef htonll // This function should technically be macro'd out if it's going to be used // more than ocasionally. As of this writing, it'll actually never be called diff --git a/src/bup/pyutil.c b/src/bup/pyutil.c new file mode 100644 index 000000000..53ca39c1d --- /dev/null +++ b/src/bup/pyutil.c @@ -0,0 +1,37 @@ +#define _LARGEFILE64_SOURCE 1 +#define PY_SSIZE_T_CLEAN 1 +#undef NDEBUG +#include "../../config/config.h" + +// According to Python, its header has to go first: +// http://docs.python.org/3/c-api/intro.html#include-files +#include + +#include "bup/pyutil.h" + +#include "bup/intprops.h" + + +void *checked_calloc(size_t n, size_t size) +{ + void *result = calloc(n, size); + if (!result) + PyErr_NoMemory(); + return result; +} + +void *checked_malloc(size_t n, size_t size) +{ + size_t total; + if (!INT_MULTIPLY_OK(n, size, &total)) + { + PyErr_Format(PyExc_OverflowError, + "request to allocate %zu items of size %zu is too large", + n, size); + return NULL; + } + void *result = malloc(total); + if (!result) + return PyErr_NoMemory(); + return result; +} diff --git a/src/bup/pyutil.h b/src/bup/pyutil.h new file mode 100644 index 000000000..680ca5460 --- /dev/null +++ b/src/bup/pyutil.h @@ -0,0 +1,4 @@ +#pragma once + +void *checked_calloc(size_t n, size_t size); +void *checked_malloc(size_t n, size_t size); -- 2.39.2