Add memory limiting capability to talloc
[sfrench/samba-autobuild/.git] / lib / talloc / pytalloc.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Python Talloc Module
4    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010-2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <Python.h>
21 #include <talloc.h>
22 #include <pytalloc.h>
23
24 void inittalloc(void);
25
26 /* print a talloc tree report for a talloc python object */
27 static PyObject *pytalloc_report_full(PyObject *self, PyObject *args)
28 {
29         PyObject *py_obj = Py_None;
30
31         if (!PyArg_ParseTuple(args, "|O", &py_obj))
32                 return NULL;
33
34         if (py_obj == Py_None) {
35                 talloc_report_full(NULL, stdout);
36         } else {
37                 talloc_report_full(pytalloc_get_mem_ctx(py_obj), stdout);
38         }
39         return Py_None;
40 }
41
42 /* enable null tracking */
43 static PyObject *pytalloc_enable_null_tracking(PyObject *self)
44 {
45         talloc_enable_null_tracking();
46         return Py_None;
47 }
48
49 /* return the number of talloc blocks */
50 static PyObject *pytalloc_total_blocks(PyObject *self, PyObject *args)
51 {
52         PyObject *py_obj = Py_None;
53
54         if (!PyArg_ParseTuple(args, "|O", &py_obj))
55                 return NULL;
56
57         if (py_obj == Py_None) {
58                 return PyLong_FromLong(talloc_total_blocks(NULL));
59         }
60
61         return PyLong_FromLong(talloc_total_blocks(pytalloc_get_mem_ctx(py_obj)));
62 }
63
64 static PyMethodDef talloc_methods[] = {
65         { "report_full", (PyCFunction)pytalloc_report_full, METH_VARARGS,
66                 "show a talloc tree for an object"},
67         { "enable_null_tracking", (PyCFunction)pytalloc_enable_null_tracking, METH_NOARGS,
68                 "enable tracking of the NULL object"},
69         { "total_blocks", (PyCFunction)pytalloc_total_blocks, METH_VARARGS,
70                 "return talloc block count"},
71         { NULL }
72 };
73
74 /**
75  * Default (but only slightly more useful than the default) implementation of Repr().
76  */
77 static PyObject *pytalloc_default_repr(PyObject *obj)
78 {
79         pytalloc_Object *talloc_obj = (pytalloc_Object *)obj;
80         PyTypeObject *type = (PyTypeObject*)PyObject_Type(obj);
81
82         return PyString_FromFormat("<%s talloc object at 0x%p>", 
83                                    type->tp_name, talloc_obj->ptr);
84 }
85
86 /**
87  * Simple dealloc for talloc-wrapping PyObjects
88  */
89 static void pytalloc_dealloc(PyObject* self)
90 {
91         pytalloc_Object *obj = (pytalloc_Object *)self;
92         assert(talloc_unlink(NULL, obj->talloc_ctx) != -1);
93         obj->talloc_ctx = NULL;
94         self->ob_type->tp_free(self);
95 }
96
97 /**
98  * Default (but only slightly more useful than the default) implementation of cmp.
99  */
100 static int pytalloc_default_cmp(PyObject *_obj1, PyObject *_obj2)
101 {
102         pytalloc_Object *obj1 = (pytalloc_Object *)_obj1,
103                                          *obj2 = (pytalloc_Object *)_obj2;
104         if (obj1->ob_type != obj2->ob_type)
105                 return (obj1->ob_type - obj2->ob_type);
106
107         return ((char *)pytalloc_get_ptr(obj1) - (char *)pytalloc_get_ptr(obj2));
108 }
109
110 static PyTypeObject TallocObject_Type = {
111         .tp_name = "talloc.Object",
112         .tp_doc = "Python wrapper for a talloc-maintained object.",
113         .tp_basicsize = sizeof(pytalloc_Object),
114         .tp_dealloc = (destructor)pytalloc_dealloc,
115         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
116         .tp_repr = pytalloc_default_repr,
117         .tp_compare = pytalloc_default_cmp,
118 };
119
120 void inittalloc(void)
121 {
122         PyObject *m;
123
124         if (PyType_Ready(&TallocObject_Type) < 0)
125                 return;
126
127         m = Py_InitModule3("talloc", talloc_methods,
128                                            "Python wrapping of talloc-maintained objects.");
129         if (m == NULL)
130                 return;
131
132         Py_INCREF(&TallocObject_Type);
133         PyModule_AddObject(m, "Object", (PyObject *)&TallocObject_Type);
134 }