summaryrefslogtreecommitdiff
path: root/deallocator.c
blob: 56917771fda79d207891bb486fd3f9a643ac4e13 (plain)
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
/** deallocator.c
*
* Pattern described in http://blog.enthought.com/?p=62
*
*/

#include "deallocator.h"

void attach_deallocator(PyObject *array, void * mem)
{
    PyObject *newobj ;

    newobj =  _PyObject_New(&_MyDeallocType) ;
    ((struct _MyDeallocStruct *)newobj)->memory = mem ;
    PyArray_BASE(array) = newobj ;
    if (DEBUG_MEM_ALLOC == 1)
    {
        printf("ALLOCATED %p\n", mem) ;
    }
} ;

static void _mydealloc_dealloc(PyObject *self)
{
    if (DEBUG_MEM_ALLOC == 1)
    {
        printf("FREEING %p\n", ((struct _MyDeallocStruct*) self)->memory) ;
    }
    free(((struct _MyDeallocStruct*) self)->memory);
    self->ob_type->tp_free((PyObject *) self);
} ;

PyTypeObject _MyDeallocType =
{
    PyObject_HEAD_INIT(NULL)
    0,                              /*ob_size*/
    "mydeallocator",                /*tp_name*/
    sizeof(_MyDeallocObject),   	/*tp_basicsize*/
    0,                              /*tp_itemsize*/
    _mydealloc_dealloc,             /*tp_dealloc*/
    0,                         	/*tp_print*/
    0,                         	/*tp_getattr*/
    0,                         	/*tp_setattr*/
    0,                         	/*tp_compare*/
    0,                         	/*tp_repr*/
    0,                         	/*tp_as_number*/
    0,                         	/*tp_as_sequence*/
    0,                         	/*tp_as_mapping*/
    0,                         	/*tp_hash */
    0,                         	/*tp_call*/
    0,                         	/*tp_str*/
    0,                         	/*tp_getattro*/
    0,                         	/*tp_setattro*/
    0,                         	/*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        	/*tp_flags*/
    "Internal deallocator object",  /* tp_doc */
} ;