1 | Using talloc in Samba4
|
---|
2 | ======================
|
---|
3 |
|
---|
4 | .. contents::
|
---|
5 |
|
---|
6 | Jelmer Vernooij
|
---|
7 | August 2013
|
---|
8 |
|
---|
9 | The most current version of this document is available at
|
---|
10 | http://samba.org/ftp/unpacked/talloc/pytalloc_guide.txt
|
---|
11 |
|
---|
12 | pytalloc is a small library that provides glue for wrapping
|
---|
13 | talloc-allocated objects from C in Python objects.
|
---|
14 |
|
---|
15 | What is pytalloc, and what is it not?
|
---|
16 | -------------------------------------
|
---|
17 |
|
---|
18 | pytalloc is merely a helper library - it provides a convenient base type object
|
---|
19 | for objects that wrap talloc-maintained memory in C. It won't write your
|
---|
20 | bindings for you but it will make it easier to write C bindings that involve
|
---|
21 | talloc, and take away some of the boiler plate.
|
---|
22 |
|
---|
23 | Python 3
|
---|
24 | --------
|
---|
25 |
|
---|
26 | pytalloc can be used with Python 3. Usage from Python extension remains
|
---|
27 | the same, but for the C utilities, the library to link to is tagged with
|
---|
28 | Python's PEP3149 ABI tag, for example "pytalloc.cpython34m".
|
---|
29 | To make a build for Python 3, configure with PYTHON=/usr/bin/python3.
|
---|
30 | .
|
---|
31 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
32 | pytalloc_Object / pytalloc_BaseObject
|
---|
33 |
|
---|
34 | This is the new base class that all Python objects that wrap talloc pointers
|
---|
35 | derive from. It is itself a subclass of the "Object" type that all objects
|
---|
36 | in Python derive from.
|
---|
37 |
|
---|
38 | Note that you will almost never create objects of the pytalloc_Object type
|
---|
39 | itself, as they are just opaque pointers that can not be accessed from
|
---|
40 | Python. A common pattern is other objects that subclass pytalloc_Object and
|
---|
41 | rely on it for their memory management.
|
---|
42 |
|
---|
43 | Each `pytalloc_Object` wraps two core of information - a talloc context
|
---|
44 | and a pointer. The pointer is the actual data that is wrapped. The talloc
|
---|
45 | context is used for memory management purposes only; when the wrapping Python object
|
---|
46 | goes away, it unlinks the talloc context. The talloc context pointer and the ptr
|
---|
47 | can (and often do) have the same value.
|
---|
48 |
|
---|
49 | Each pytalloc_Object has a custom __repr__ implementation that
|
---|
50 | describes that it is a talloc object and the location of the
|
---|
51 | pointer it is wrapping. it also has a custom __cmp__/__eq__/__neq__ method that
|
---|
52 | compares the pointers the object is wrapping rather than the objects
|
---|
53 | themselves (since there can be multiple objects that wrap the same talloc
|
---|
54 | pointer).
|
---|
55 |
|
---|
56 | It is preferred to use pytalloc_BaseObject as this implementation
|
---|
57 | exposes less in the C ABI and correctly supports pointers in C arrays
|
---|
58 | in the way needed by PIDL.
|
---|
59 |
|
---|
60 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
61 | PyTypeObject *pytalloc_GetObjectType(void)
|
---|
62 |
|
---|
63 | Obtain a pointer to the PyTypeObject for `pytalloc_Object`. The
|
---|
64 | reference counter for the object will be NOT incremented, so the
|
---|
65 | caller MUST NOT decrement it when it no longer needs it (eg by using
|
---|
66 | `Py_DECREF`).
|
---|
67 |
|
---|
68 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
69 | PyTypeObject *pytalloc_GetBaseObjectType(void)
|
---|
70 |
|
---|
71 | Obtain a pointer to the PyTypeObject for `pytalloc_BaseObject`. The
|
---|
72 | reference counter for the object will be NOT incremented, so the
|
---|
73 | caller MUST NOT decrement it when it no longer needs it (eg by using
|
---|
74 | `Py_DECREF`).
|
---|
75 |
|
---|
76 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
77 | int pytalloc_BaseObject_PyType_Ready(PyTypeObject *type);
|
---|
78 |
|
---|
79 | Wrapper for PyType_Ready() that will set the correct values into
|
---|
80 | the PyTypeObject to create a BaseObject
|
---|
81 |
|
---|
82 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
|
---|
83 | int pytalloc_Check(PyObject *)
|
---|
84 |
|
---|
85 | Check whether a specific object is a talloc Object. Returns non-zero if it is
|
---|
86 | a pytalloc_Object and zero otherwise.
|
---|
87 |
|
---|
88 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-
|
---|
89 | int pytalloc_BaseObject_Check(PyObject *)
|
---|
90 |
|
---|
91 | Check whether a specific object is a talloc BaseObject. Returns non-zero if it is
|
---|
92 | a pytalloc_BaseObject and zero otherwise.
|
---|
93 |
|
---|
94 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
95 | type *pytalloc_get_type(PyObject *py_obj, type)
|
---|
96 |
|
---|
97 | Retrieve the pointer from a `pytalloc_Object` py_obj. type should be a
|
---|
98 | C type, similar to a type passed to `talloc_get_type`.
|
---|
99 |
|
---|
100 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
101 | pytalloc_get_ptr(PyObject *py_obj)
|
---|
102 |
|
---|
103 | Retrieve the pointer from a `pytalloc_Object` or `pytalloc_BaseObject`
|
---|
104 | py_obj. There is no type checking - use `pytalloc_get_type` if
|
---|
105 | possible.
|
---|
106 |
|
---|
107 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
108 | TALLOC_CTX *pytalloc_get_mem_ctx(PyObject *py_obj)
|
---|
109 |
|
---|
110 | Retrieve the talloc context associated with a pytalloc_Object or pytalloc_BaseObject.
|
---|
111 |
|
---|
112 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
113 | PyObject *pytalloc_steal_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
|
---|
114 |
|
---|
115 | Create a new Python wrapping object for a talloc pointer and context, with
|
---|
116 | py_type as associated Python sub type object.
|
---|
117 |
|
---|
118 | This will *not* increment the reference counter for the talloc context,
|
---|
119 | so the caller should make sure such an increment has happened. When the Python
|
---|
120 | object goes away, it will unreference the talloc context.
|
---|
121 |
|
---|
122 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
123 | PyObject *pytalloc_steal(PyTypeObject *py_type, void *ptr)
|
---|
124 |
|
---|
125 | Create a new Python wrapping object for a talloc pointer and context, with
|
---|
126 | py_type as associated Python sub type object.
|
---|
127 |
|
---|
128 | This will *not* increment the reference counter for the talloc context,
|
---|
129 | so the caller should make sure such an increment has happened. When the Python
|
---|
130 | object goes away, it will unreference the talloc context.
|
---|
131 |
|
---|
132 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
133 | PyObject *pytalloc_reference_ex(PyTypeObject *py_type, TALLOC_CTX *mem_ctx, void *ptr)
|
---|
134 |
|
---|
135 | Create a new Python wrapping object for a talloc pointer and context, with
|
---|
136 | py_type as associated Python sub type object.
|
---|
137 |
|
---|
138 | This will increment the reference counter for the talloc context.
|
---|
139 |
|
---|
140 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
141 | PyObject *pytalloc_reference(PyTypeObject *py_type, void *talloc_ptr)
|
---|
142 |
|
---|
143 | Create a new Python wrapping object for a talloc pointer, with
|
---|
144 | py_type as associated Python sub type object. The pointer will also be used
|
---|
145 | as the talloc context.
|
---|
146 |
|
---|
147 | This will increment the reference counter for the talloc context.
|
---|
148 |
|
---|
149 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
150 | PyObject *pytalloc_new(type, PyTypeObject *typeobj)
|
---|
151 |
|
---|
152 | Create a new, empty pytalloc_Object with the specified Python type object. type
|
---|
153 | should be a C type, similar to talloc_new().
|
---|
154 |
|
---|
155 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
156 | PyObject *pytalloc_CObject_FromTallocPtr(void *);
|
---|
157 |
|
---|
158 | Create a new pytalloc_Object for an abitrary talloc-maintained C pointer. This will
|
---|
159 | use a generic VoidPtr Python type, which just provides an opaque object in
|
---|
160 | Python. The caller is responsible for incrementing the talloc reference count before calling
|
---|
161 | this function - it will dereference the talloc pointer when it is garbage collected.
|
---|
162 |
|
---|
163 | This function is only available on Python 2.
|
---|
164 |
|
---|
165 | Debug function for talloc in Python
|
---|
166 | -----------------------------------
|
---|
167 |
|
---|
168 | The "talloc" module in Python provides a couple of functions that can be used
|
---|
169 | to debug issues with objects wrapped by pytalloc.
|
---|
170 |
|
---|
171 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
172 | report_full(obj?)
|
---|
173 |
|
---|
174 | Print a full report on a specific object or on all allocated objects by Python.
|
---|
175 | Same behaviour as the `talloc_report_full()` function that is provided by
|
---|
176 | C talloc.
|
---|
177 |
|
---|
178 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
179 | enable_null_tracking()
|
---|
180 |
|
---|
181 | This enables tracking of the NULL memory context without enabling leak
|
---|
182 | reporting on exit. Useful for when you want to do your own leak
|
---|
183 | reporting call via talloc_report_null_full().
|
---|
184 |
|
---|
185 | =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
---|
186 | pytalloc_total_blocks(obj?)
|
---|
187 |
|
---|
188 | Return the talloc block count for all allocated objects or a specific object if
|
---|
189 | specified.
|
---|