Loading src/shared_array_leon.c→src/map_owner.c +10 −14 Original line number Diff line number Diff line Loading @@ -16,33 +16,29 @@ * along with SharedArray. If not, see <http://www.gnu.org/licenses/>. */ #define NPY_NO_DEPRECATED_API NPY_1_8_API_VERSION #define PY_ARRAY_UNIQUE_SYMBOL SHARED_ARRAY_ARRAY_API #define NO_IMPORT_ARRAY #include <Python.h> #include <sys/mman.h> #include "shared_array.h" #include <Python.h> #include "map_owner.h" /* * Deallocation function * Destructor */ static void leon_dealloc(PyLeonObject *op) static void do_dealloc(PyMapOwnerObject *self) { /* Unmap the data */ if (munmap(op->data, op->size) < 0) if (munmap(self->map_addr, self->map_size) < 0) PyErr_SetFromErrno(PyExc_RuntimeError); } /* * SharedArrayObject type definition * MapOwner type definition */ PyTypeObject PyLeonObject_Type = { PyTypeObject PyMapOwner_Type = { PyVarObject_HEAD_INIT(NULL, 0) "shared_array.leon", /* tp_name */ sizeof (PyLeonObject), /* tp_basicsize */ "shared_array.map_owner", /* tp_name */ sizeof (PyMapOwnerObject), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor) leon_dealloc, /* tp_dealloc */ (destructor) do_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ Loading src/map_owner.h 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * This file is part of SharedArray. * Copyright (C) 2014-2016 Mathieu Mirmont <mat@parad0x.org> * * SharedArray is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * SharedArray is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SharedArray. If not, see <http://www.gnu.org/licenses/>. */ #ifndef __MAP_OWNER_H__ #define __MAP_OWNER_H__ #include <Python.h> /* MapOwner object */ typedef struct { PyObject_HEAD void *map_addr; size_t map_size; } PyMapOwnerObject; /* Class type definition */ extern PyTypeObject PyMapOwner_Type; /* C API */ #define PyMapOwner_Check(op) PyObject_TypeCheck(op, &PyMapOwner_Type) #endif /* !__MAP_OWNER_H__ */ src/shared_array.c +6 −5 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <structseq.h> #include <numpy/arrayobject.h> #include "shared_array.h" #include "map_owner.h" /* Module name */ static const char module_name[] = "SharedArray"; Loading Loading @@ -95,12 +96,12 @@ static PyObject *module_init(void) if (!(m = CREATE_MODULE(module_name, module_functions, module_docstring))) return NULL; /* Register the Leon type */ PyType_Ready(&PyLeonObject_Type); Py_INCREF(&PyLeonObject_Type); PyModule_AddObject(m, module_name, (PyObject *) &PyLeonObject_Type); /* Register the MapOwner type */ PyType_Ready(&PyMapOwner_Type); Py_INCREF(&PyMapOwner_Type); PyModule_AddObject(m, module_name, (PyObject *) &PyMapOwner_Type); /* Register the Descr type */ /* Register the ArrayDescr type */ PyStructSequence_InitType(&PyArrayDescObject_Type, &PyArrayDescObject_Desc); PyType_Ready(&PyArrayDescObject_Type); Py_INCREF(&PyArrayDescObject_Type); Loading src/shared_array.h +1 −10 Original line number Diff line number Diff line Loading @@ -43,17 +43,8 @@ struct array_meta { extern PyStructSequence_Desc PyArrayDescObject_Desc; extern PyTypeObject PyArrayDescObject_Type; /* Leon object */ typedef struct { PyObject_HEAD void *data; size_t size; } PyLeonObject; extern PyTypeObject PyLeonObject_Type; /* Module functions */ extern PyObject *shared_array_create(PyObject *self, PyObject *args, PyObject *kw); extern PyObject *shared_array_create(PyObject *self, PyObject *args, PyObject *kwds); extern PyObject *shared_array_attach(PyObject *self, PyObject *args); extern PyObject *shared_array_delete(PyObject *self, PyObject *args); extern PyObject *shared_array_list(PyObject *self, PyObject *args); Loading src/shared_array_attach.c +12 −11 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ #include <fcntl.h> #include <unistd.h> #include "shared_array.h" #include "map_owner.h" /* * Attach a numpy array from shared memory Loading @@ -37,8 +38,8 @@ static PyObject *do_attach(const char *name) int fd; size_t map_size; void *map_addr; PyObject *ret; PyLeonObject *leon; PyObject *array; PyMapOwnerObject *map_owner; /* Open the file */ if ((fd = open_file(name, O_RDWR, 0)) < 0) Loading Loading @@ -81,18 +82,18 @@ static PyObject *do_attach(const char *name) if (map_addr == MAP_FAILED) return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); /* Summon Leon */ leon = PyObject_MALLOC(sizeof (*leon)); PyObject_INIT((PyObject *) leon, &PyLeonObject_Type); leon->data = map_addr; leon->size = map_size; /* Hand over the memory map to a MapOwner instance */ map_owner = PyObject_MALLOC(sizeof (*map_owner)); PyObject_INIT((PyObject *) map_owner, &PyMapOwner_Type); map_owner->map_addr = map_addr; map_owner->map_size = map_size; /* Create the array object */ ret = PyArray_SimpleNewFromData(meta.ndims, meta.dims, meta.typenum, map_addr); array = PyArray_SimpleNewFromData(meta.ndims, meta.dims, meta.typenum, map_addr); /* Attach Leon to the array */ PyArray_SetBaseObject((PyArrayObject *) ret, (PyObject *) leon); return ret; /* Attach MapOwner to the array */ PyArray_SetBaseObject((PyArrayObject *) array, (PyObject *) map_owner); return array; } /* Loading Loading
src/shared_array_leon.c→src/map_owner.c +10 −14 Original line number Diff line number Diff line Loading @@ -16,33 +16,29 @@ * along with SharedArray. If not, see <http://www.gnu.org/licenses/>. */ #define NPY_NO_DEPRECATED_API NPY_1_8_API_VERSION #define PY_ARRAY_UNIQUE_SYMBOL SHARED_ARRAY_ARRAY_API #define NO_IMPORT_ARRAY #include <Python.h> #include <sys/mman.h> #include "shared_array.h" #include <Python.h> #include "map_owner.h" /* * Deallocation function * Destructor */ static void leon_dealloc(PyLeonObject *op) static void do_dealloc(PyMapOwnerObject *self) { /* Unmap the data */ if (munmap(op->data, op->size) < 0) if (munmap(self->map_addr, self->map_size) < 0) PyErr_SetFromErrno(PyExc_RuntimeError); } /* * SharedArrayObject type definition * MapOwner type definition */ PyTypeObject PyLeonObject_Type = { PyTypeObject PyMapOwner_Type = { PyVarObject_HEAD_INIT(NULL, 0) "shared_array.leon", /* tp_name */ sizeof (PyLeonObject), /* tp_basicsize */ "shared_array.map_owner", /* tp_name */ sizeof (PyMapOwnerObject), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor) leon_dealloc, /* tp_dealloc */ (destructor) do_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ Loading
src/map_owner.h 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * This file is part of SharedArray. * Copyright (C) 2014-2016 Mathieu Mirmont <mat@parad0x.org> * * SharedArray is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * SharedArray is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SharedArray. If not, see <http://www.gnu.org/licenses/>. */ #ifndef __MAP_OWNER_H__ #define __MAP_OWNER_H__ #include <Python.h> /* MapOwner object */ typedef struct { PyObject_HEAD void *map_addr; size_t map_size; } PyMapOwnerObject; /* Class type definition */ extern PyTypeObject PyMapOwner_Type; /* C API */ #define PyMapOwner_Check(op) PyObject_TypeCheck(op, &PyMapOwner_Type) #endif /* !__MAP_OWNER_H__ */
src/shared_array.c +6 −5 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <structseq.h> #include <numpy/arrayobject.h> #include "shared_array.h" #include "map_owner.h" /* Module name */ static const char module_name[] = "SharedArray"; Loading Loading @@ -95,12 +96,12 @@ static PyObject *module_init(void) if (!(m = CREATE_MODULE(module_name, module_functions, module_docstring))) return NULL; /* Register the Leon type */ PyType_Ready(&PyLeonObject_Type); Py_INCREF(&PyLeonObject_Type); PyModule_AddObject(m, module_name, (PyObject *) &PyLeonObject_Type); /* Register the MapOwner type */ PyType_Ready(&PyMapOwner_Type); Py_INCREF(&PyMapOwner_Type); PyModule_AddObject(m, module_name, (PyObject *) &PyMapOwner_Type); /* Register the Descr type */ /* Register the ArrayDescr type */ PyStructSequence_InitType(&PyArrayDescObject_Type, &PyArrayDescObject_Desc); PyType_Ready(&PyArrayDescObject_Type); Py_INCREF(&PyArrayDescObject_Type); Loading
src/shared_array.h +1 −10 Original line number Diff line number Diff line Loading @@ -43,17 +43,8 @@ struct array_meta { extern PyStructSequence_Desc PyArrayDescObject_Desc; extern PyTypeObject PyArrayDescObject_Type; /* Leon object */ typedef struct { PyObject_HEAD void *data; size_t size; } PyLeonObject; extern PyTypeObject PyLeonObject_Type; /* Module functions */ extern PyObject *shared_array_create(PyObject *self, PyObject *args, PyObject *kw); extern PyObject *shared_array_create(PyObject *self, PyObject *args, PyObject *kwds); extern PyObject *shared_array_attach(PyObject *self, PyObject *args); extern PyObject *shared_array_delete(PyObject *self, PyObject *args); extern PyObject *shared_array_list(PyObject *self, PyObject *args); Loading
src/shared_array_attach.c +12 −11 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ #include <fcntl.h> #include <unistd.h> #include "shared_array.h" #include "map_owner.h" /* * Attach a numpy array from shared memory Loading @@ -37,8 +38,8 @@ static PyObject *do_attach(const char *name) int fd; size_t map_size; void *map_addr; PyObject *ret; PyLeonObject *leon; PyObject *array; PyMapOwnerObject *map_owner; /* Open the file */ if ((fd = open_file(name, O_RDWR, 0)) < 0) Loading Loading @@ -81,18 +82,18 @@ static PyObject *do_attach(const char *name) if (map_addr == MAP_FAILED) return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name); /* Summon Leon */ leon = PyObject_MALLOC(sizeof (*leon)); PyObject_INIT((PyObject *) leon, &PyLeonObject_Type); leon->data = map_addr; leon->size = map_size; /* Hand over the memory map to a MapOwner instance */ map_owner = PyObject_MALLOC(sizeof (*map_owner)); PyObject_INIT((PyObject *) map_owner, &PyMapOwner_Type); map_owner->map_addr = map_addr; map_owner->map_size = map_size; /* Create the array object */ ret = PyArray_SimpleNewFromData(meta.ndims, meta.dims, meta.typenum, map_addr); array = PyArray_SimpleNewFromData(meta.ndims, meta.dims, meta.typenum, map_addr); /* Attach Leon to the array */ PyArray_SetBaseObject((PyArrayObject *) ret, (PyObject *) leon); return ret; /* Attach MapOwner to the array */ PyArray_SetBaseObject((PyArrayObject *) array, (PyObject *) map_owner); return array; } /* Loading