2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
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.
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.
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/>.
24 /* print a talloc tree report for a talloc python object */
25 static PyObject
*py_talloc_report_full(PyObject
*self
, PyObject
*args
)
27 PyObject
*py_obj
= Py_None
;
30 if (!PyArg_ParseTuple(args
, "|O", &py_obj
))
33 if (py_obj
== Py_None
) {
34 talloc_report_full(NULL
, stdout
);
36 type
= (PyTypeObject
*)PyObject_Type(py_obj
);
37 talloc_report_full(py_talloc_get_mem_ctx(py_obj
), stdout
);
42 /* enable null tracking */
43 static PyObject
*py_talloc_enable_null_tracking(PyObject
*self
)
45 talloc_enable_null_tracking();
49 /* return the number of talloc blocks */
50 static PyObject
*py_talloc_total_blocks(PyObject
*self
, PyObject
*args
)
52 PyObject
*py_obj
= Py_None
;
55 if (!PyArg_ParseTuple(args
, "|O", &py_obj
))
58 if (py_obj
== Py_None
) {
59 return PyLong_FromLong(talloc_total_blocks(NULL
));
62 type
= (PyTypeObject
*)PyObject_Type(py_obj
);
64 return PyLong_FromLong(talloc_total_blocks(py_talloc_get_mem_ctx(py_obj
)));
67 static PyMethodDef talloc_methods
[] = {
68 { "report_full", (PyCFunction
)py_talloc_report_full
, METH_VARARGS
,
69 "show a talloc tree for an object"},
70 { "enable_null_tracking", (PyCFunction
)py_talloc_enable_null_tracking
, METH_NOARGS
,
71 "enable tracking of the NULL object"},
72 { "total_blocks", (PyCFunction
)py_talloc_total_blocks
, METH_VARARGS
,
73 "return talloc block count"},
78 * Default (but only slightly more useful than the default) implementation of Repr().
80 static PyObject
*py_talloc_default_repr(PyObject
*obj
)
82 py_talloc_Object
*talloc_obj
= (py_talloc_Object
*)obj
;
83 PyTypeObject
*type
= (PyTypeObject
*)PyObject_Type(obj
);
85 return PyString_FromFormat("<%s talloc object at 0x%p>",
86 type
->tp_name
, talloc_obj
->ptr
);
90 * Simple dealloc for talloc-wrapping PyObjects
92 static void py_talloc_dealloc(PyObject
* self
)
94 py_talloc_Object
*obj
= (py_talloc_Object
*)self
;
95 assert(talloc_unlink(NULL
, obj
->talloc_ctx
) != -1);
96 obj
->talloc_ctx
= NULL
;
97 self
->ob_type
->tp_free(self
);
101 * Default (but only slightly more useful than the default) implementation of cmp.
103 static int py_talloc_default_cmp(PyObject
*_obj1
, PyObject
*_obj2
)
105 py_talloc_Object
*obj1
= (py_talloc_Object
*)_obj1
,
106 *obj2
= (py_talloc_Object
*)_obj2
;
107 if (obj1
->ob_type
!= obj2
->ob_type
)
108 return (obj1
->ob_type
- obj2
->ob_type
);
110 return ((char *)py_talloc_get_ptr(obj1
) - (char *)py_talloc_get_ptr(obj2
));
115 static PyTypeObject TallocObject_Type
= {
116 .tp_name
= "talloc.Object",
117 .tp_basicsize
= sizeof(py_talloc_Object
),
118 .tp_dealloc
= (destructor
)py_talloc_dealloc
,
119 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
120 .tp_repr
= py_talloc_default_repr
,
121 .tp_compare
= py_talloc_default_cmp
,
124 void inittalloc(void)
128 if (PyType_Ready(&TallocObject_Type
) < 0)
131 m
= Py_InitModule3("talloc", talloc_methods
, "Debug utilities for talloc-wrapped objects.");
135 Py_INCREF(&TallocObject_Type
);
136 PyModule_AddObject(m
, "Object", (PyObject
*)&TallocObject_Type
);