From 24d8212c8b5071086d30bb230183dfc9ab3e44d5 Mon Sep 17 00:00:00 2001 From: maedoc Date: Fri, 29 Jan 2016 18:23:38 +0100 Subject: [PATCH] 'organization; --- makefile | 5 --- matlab/README.md | 27 +++++++++++++ python/capi/sddekit.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ python/capi/setup.py | 19 +++++++++ 4 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 matlab/README.md create mode 100644 python/capi/sddekit.c create mode 100644 python/capi/setup.py diff --git a/makefile b/makefile index 345798d..46a6618 100644 --- a/makefile +++ b/makefile @@ -52,11 +52,6 @@ libSDDEKit.$(DLLEXT): $(o_lib) clean: $(RM) $(o_lib) $(o_test) tests* *.dat *.exe *.$(DLLEXT) -pyext: - python setup.py build_ext --inplace - del sddekit.pyd - ren sddekit*.pyd sddekit.pyd - # }}} # generic rules {{{ diff --git a/matlab/README.md b/matlab/README.md new file mode 100644 index 0000000..59700e0 --- /dev/null +++ b/matlab/README.md @@ -0,0 +1,27 @@ +# MATLAB interface to SDDEKit + +This is a MATLAB toolbox wrapping SDDEKit, a C library. + +## Examples + +_TODO_ + +## Development + +While MATLAB has a nice libffi-like set of functions with working with +shared library via ABI but it does not support calling function pointers +in structs, which is used by SDDEKit extensively. + +A MEX function gateway is therefore used in combination with a set of new-style +MATLAB handle classes, to automatically & safely handle memory alloc/free. +The C code implementing these is in the the private/ folder and most lines of +code are concerned with marshalling data between MATLAB and C. + +A major goal for this wrapper was that a user could 1) use each of the components +of SDDEKit written in C individually and 2) define a component in a MATLAB +function / struct, and then use it in conjuction with other components of SDDEKit. + +Because of the semantics of functions calls in MATLAB (specifically that modifications +to input arguments are not visible after function exits), some data copying is inevitable. +However, we hope (to be verified) that the MATLAB memory manager recognizes and +optimizes certain cases (identical pointer, etc.). diff --git a/python/capi/sddekit.c b/python/capi/sddekit.c new file mode 100644 index 0000000..639b8c8 --- /dev/null +++ b/python/capi/sddekit.c @@ -0,0 +1,106 @@ +/* copyright 2016 Apache 2 sddekit authors */ + +#include "Python.h" +#include "sddekit.h" + +struct module_state { + PyObject *error; +}; + +/* GETSTATE {{{ */ +#if PY_MAJOR_VERSION >= 3 +#define GETSTATE(m) ((struct module_state*) PyModule_GetState(m)) +#else +#define GETSTATE(m) (&_state) +static struct module_state _state; +#endif +/* }}} */ + +static PyObject * +error_out(PyObject *m) +{ + struct module_state *st = GETSTATE(m); + PyErr_SetString(st->error, "oops"); + return NULL; +} + +static PyMethodDef methods[] = { + {"error_out", (PyCFunction) error_out, METH_NOARGS, NULL}, + {NULL, NULL} +}; + +/* module init {{{ */ +#if PY_MAJOR_VERSION >= 3 + +static int traverse(PyObject *m, visitproc visit, void *arg) +{ + Py_VISIT(GETSTATE(m)->error); + return 0; +} + +static int clear(PyObject *m) +{ + Py_CLEAR(GETSTATE(m)->error); + return 0; +} + +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "sddekit", + NULL, + sizeof(struct module_state), + methods, + NULL, + traverse, + clear, + NULL +}; + +#define INITERROR return NULL + +PyObject * PyInit_sddekit(void) + +#else + +#define INITERROR return + +void initsddekit(void) + +#endif + +{ +#if PY_MAJOR_VERSION >= 3 + PyObject *module = PyModule_Create(&moduledef); +#else + PyObject *module = Py_InitModule("sddekit", methods); +#endif + + if (module == NULL) + INITERROR; + + struct module_state *st = GETSTATE(module); + + st->error = PyErr_NewException("sddekit.Error", NULL, NULL); + + if (st->error == NULL) + { + Py_DECREF(module); + INITERROR; + } + + PyModule_AddObject(module, "__version__", +#if PY_MAJOR_VERSION >= 3 + PyBytes_FromFormat +#else + PyString_FromFormat +#endif + ("%d.%d", sd_ver_major(), sd_ver_minor())); + +#if PY_MAJOR_VERSION >= 3 + return module; +#endif +} +/* }}} */ + +/* vim: foldmethod=marker + */ diff --git a/python/capi/setup.py b/python/capi/setup.py new file mode 100644 index 0000000..50b3077 --- /dev/null +++ b/python/capi/setup.py @@ -0,0 +1,19 @@ +# copyright 2016 Apache 2 sddekit authors + +import os +import glob +from distutils.core import setup, Extension +import numpy as np + +here = os.path.dirname(os.path.abspath(__file__)) +sources = glob.glob('src/*.c') + +mod = Extension( + 'sddekit', + sources=['sddekit.c'] + sources, + include_dirs=['src', np.get_include()]) + +setup(name='SDDEKit', + version=0.0, + description='n/a', + ext_modules=[mod]) -- 2.11.4.GIT