From cb64d899bea8d29fdc2c21022f7e071820e39387 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 20 Oct 2019 18:03:35 +0100 Subject: [PATCH] util.c: Rewrite `addmethods' to remove dependency on . --- catacomb-python.h | 1 - util.c | 33 ++++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/catacomb-python.h b/catacomb-python.h index fe64aeb..85adf7d 100644 --- a/catacomb-python.h +++ b/catacomb-python.h @@ -39,7 +39,6 @@ #include #include -#include #include #include #include diff --git a/util.c b/util.c index 55de789..b39b7d5 100644 --- a/util.c +++ b/util.c @@ -293,25 +293,32 @@ void setconstants(PyObject *mod, const struct nameval *c) /*----- 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) { - size_t n; + size_t n, want, newsz; 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 --------------------------------------------------------*/ -- 2.11.0