util.c: Rewrite `addmethods' to remove dependency on <mLib/darray.h>.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 20 Oct 2019 17:03:35 +0000 (18:03 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 10 Apr 2020 21:42:39 +0000 (22:42 +0100)
catacomb-python.h
util.c

index fe64aeb..85adf7d 100644 (file)
@@ -39,7 +39,6 @@
 #include <longintrepr.h>
 #include <structmember.h>
 
 #include <longintrepr.h>
 #include <structmember.h>
 
-#include <mLib/darray.h>
 #include <mLib/dstr.h>
 #include <mLib/macros.h>
 #include <mLib/quis.h>
 #include <mLib/dstr.h>
 #include <mLib/macros.h>
 #include <mLib/quis.h>
diff --git a/util.c b/util.c
index 55de789..b39b7d5 100644 (file)
--- a/util.c
+++ b/util.c
@@ -293,25 +293,32 @@ void setconstants(PyObject *mod, const struct nameval *c)
 
 /*----- Building method tables --------------------------------------------*/
 
 
 /*----- Building method tables --------------------------------------------*/
 
-DA_DECL(method_v, PyMethodDef);
-static method_v global_pymethods = DA_INIT;
+static PyMethodDef *global_methods;
+static size_t nmethods, methodsz;
+
 void addmethods(const PyMethodDef *m)
 {
 void addmethods(const PyMethodDef *m)
 {
-  size_t n;
+  size_t n, want, newsz;
 
   for (n = 0; m[n].ml_name; n++);
 
   for (n = 0; m[n].ml_name; n++);
-  DA_ENSURE(&global_pymethods, n);
-  memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods),
-        m, n * sizeof(*m));
-  DA_EXTEND(&global_pymethods, n);
+  want = nmethods + n + 1;
+  if (want > methodsz) {
+    newsz = methodsz ? 2*methodsz : 16;
+    while (want > newsz) newsz *= 2;
+    if (!global_methods)
+      global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef));
+    else
+      global_methods = PyObject_Realloc(global_methods,
+                                       newsz*sizeof(PyMethodDef));
+    assert(global_methods);
+    methodsz = newsz;
+  }
+  memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef));
+  nmethods += n;
+  global_methods[nmethods].ml_name = 0;
 }
 
 }
 
-PyMethodDef *donemethods(void)
-{
-  static const PyMethodDef mzero = { 0 };
-  DA_PUSH(&global_pymethods, mzero);
-  return (DA(&global_pymethods));
-}
+PyMethodDef *donemethods(void) { return (global_methods); }
 
 /*----- Exceptions --------------------------------------------------------*/
 
 
 /*----- Exceptions --------------------------------------------------------*/