5
jmp_buf _superlu_py_jmpbuf;
6
PyObject *_superlumodule_memory_dict=NULL;
8
/* Abort to be used inside the superlu module so that memory allocation
9
errors don't exit Python and memory allocated internal to SuperLU is freed.
10
Calling program should deallocate (using SUPERLU_FREE) all memory that could have
11
been allocated. (It's ok to FREE unallocated memory)---will be ignored.
14
void superlu_python_module_abort(char *msg)
16
PyErr_SetString(PyExc_RuntimeError, msg);
17
longjmp(_superlu_py_jmpbuf, -1);
20
void *superlu_python_module_malloc(size_t size)
26
if (_superlumodule_memory_dict == NULL) {
27
_superlumodule_memory_dict = PyDict_New();
29
mem_ptr = malloc(size);
30
if (mem_ptr == NULL) return NULL;
31
keyval = (long) mem_ptr;
32
key = PyInt_FromLong(keyval);
33
if (key == NULL) goto fail;
34
if (PyDict_SetItem(_superlumodule_memory_dict, key, Py_None)) goto fail;
41
superlu_python_module_abort("superlu_malloc: Cannot set dictionary key value in malloc.");
46
void superlu_python_module_free(void *ptr)
50
PyObject *ptype, *pvalue, *ptraceback;
52
if (ptr == NULL) return;
53
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
55
key = PyInt_FromLong(keyval);
56
/* This will only free the pointer if it could find it in the dictionary
57
of already allocated pointers --- thus after abort, the module can free all
58
the memory that "might" have been allocated to avoid memory leaks on abort
61
if (!(PyDict_DelItem(_superlumodule_memory_dict, key))) {
65
PyErr_Restore(ptype, pvalue, ptraceback);