guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/8] Create src/pyutil.c for utility functions


From: Rob Browning
Subject: [PATCH 2/8] Create src/pyutil.c for utility functions
Date: Tue, 30 May 2023 19:49:38 -0500

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
---
 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 <Python.h>
+
+#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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]