File: _superlu_utils.c

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,500 kB
  • ctags: 36,081
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,576; ansic: 62,118; makefile: 243; sh: 17
file content (69 lines) | stat: -rw-r--r-- 1,875 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include "Python.h"
#include <setjmp.h>

jmp_buf _superlu_py_jmpbuf;
PyObject *_superlumodule_memory_dict=NULL;

/* Abort to be used inside the superlu module so that memory allocation 
   errors don't exit Python and memory allocated internal to SuperLU is freed.
   Calling program should deallocate (using SUPERLU_FREE) all memory that could have 
   been allocated.  (It's ok to FREE unallocated memory)---will be ignored.
*/

void superlu_python_module_abort(char *msg)
{
  PyErr_SetString(PyExc_RuntimeError, msg);
  longjmp(_superlu_py_jmpbuf, -1);
}

void *superlu_python_module_malloc(size_t size)
{
  PyObject *key=NULL;
  long keyval;
  void *mem_ptr; 

  if (_superlumodule_memory_dict == NULL) {
    _superlumodule_memory_dict = PyDict_New();
  }
  mem_ptr = malloc(size);
  if (mem_ptr == NULL) return NULL;
  keyval = (long) mem_ptr;
  key = PyInt_FromLong(keyval);
  if (key == NULL) goto fail;
  if (PyDict_SetItem(_superlumodule_memory_dict, key, Py_None)) goto fail;
  Py_DECREF(key);
  return mem_ptr;

 fail:
  Py_XDECREF(key);
  free(mem_ptr);
  superlu_python_module_abort("superlu_malloc: Cannot set dictionary key value in malloc.");
  return NULL;
 
}

void superlu_python_module_free(void *ptr)
{
  PyObject *key;
  long keyval;
  PyObject *ptype, *pvalue, *ptraceback;

  if (ptr == NULL) return;
  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
  keyval = (long )ptr;
  key = PyInt_FromLong(keyval);
  /* This will only free the pointer if it could find it in the dictionary
     of already allocated pointers --- thus after abort, the module can free all
     the memory that "might" have been allocated to avoid memory leaks on abort 
     calls.
   */ 
  if (_superlumodule_memory_dict && \
      !(PyDict_DelItem(_superlumodule_memory_dict, key))) {
    free(ptr);
  }
  Py_DECREF(key);
  PyErr_Restore(ptype, pvalue, ptraceback);
  return; 
}