Commit 2a0b0fdf authored by Mat's avatar Mat
Browse files

Added the delete() method

parent a8cb9e6b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ static PyMethodDef module_functions[] = {
	  METH_VARARGS | METH_KEYWORDS, "Create a numpy array in shared memory" },
	{ "attach", (PyCFunction) shared_array_attach,
	  METH_VARARGS, "Attach a numpy array from shared memory" },
	{ "delete", (PyCFunction) shared_array_delete,
	  METH_VARARGS, "Delete a numpy array from shared memory" },
	{ NULL, NULL, 0, NULL }
};

+1 −0
Original line number Diff line number Diff line
@@ -35,5 +35,6 @@ struct array_meta {
/* Module functions */
extern PyObject *shared_array_create(PyObject *self, PyObject *args, PyObject *kw);
extern PyObject *shared_array_attach(PyObject *self, PyObject *args);
extern PyObject *shared_array_delete(PyObject *self, PyObject *args);

#endif /* !__SHARED_ARRAY_H__ */
+72 −0
Original line number Diff line number Diff line
/*
 *      Project: SharedArray
 * 
 *         File: shared_array_delete.c
 * 
 *  Description: Delete a numpy array from shared memory
 * 
 *     Author/s: Mathieu Mirmont <mat@parad0x.org>
 * 
 *   Created on: 10/12/2014
 * 
 */

#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 <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "shared_array.h"

/*
 * Delete a numpy array from shared memory
 */
static PyObject *do_delete(const char *name)
{
	struct array_meta meta;
	int fd;

	/* Open the shm block */
	if ((fd = shm_open(name, O_RDWR, 0)) < 0)
		return PyErr_SetFromErrno(PyExc_RuntimeError);

	/* Read the meta data structure */
	if (read(fd, &meta, sizeof (meta)) != sizeof (meta)) {
		close(fd);
		return PyErr_SetFromErrno(PyExc_RuntimeError);
	}

	/* Close the shm block */
	close(fd);

	/* Check the meta data */
	if (strncmp(meta.magic, SHARED_ARRAY_MAGIC, sizeof (meta.magic))) {
		PyErr_SetString(PyExc_RuntimeError, "No SharedArray at this address");
		return NULL;
	}

	/* Unlink the shm block */
	if (shm_unlink(name) < 0)
		return PyErr_SetFromErrno(PyExc_RuntimeError);

	Py_RETURN_NONE;
}

/*
 * Method: SharedArray.delete()
 */
PyObject *shared_array_delete(PyObject *self, PyObject *args)
{
	const char *name;

	/* Parse the arguments */
	if (!PyArg_ParseTuple(args, "s", &name))
		return NULL;

	/* Now do the real thing */
	return do_delete(name);
}