pygi-marshal-cleanup.c 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
 
 #include "pygi-marshal-cleanup.h"
 #include <glib.h>
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
static inline void
_cleanup_caller_allocates (PyGIInvokeState    *state,
                           PyGIInterfaceCache *cache,
                           gpointer            data)
{
    if (cache->g_type == G_TYPE_BOXED) {
        gsize size;
        size = g_struct_info_get_size (cache->interface_info);
        g_slice_free1 (size, data);
    } else if (cache->is_foreign) {
        pygi_struct_foreign_release ((GIBaseInfo *)cache->interface_info,
                                     data);
    } else {
        g_free (data);
    }
}
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
70
71
72
73
74
75
76
77
78
79
/**
 * Cleanup during invoke can happen in multiple
 * stages, each of which can be the result of a
 * successful compleation of that stage or an error
 * occured which requires partial cleanup.
 *
 * For the most part, either the C interface being
 * invoked or the python object which wraps the
 * parameters, handle their lifecycles but in some
 * cases, where we have intermediate objects,
 * or when we fail processing a parameter, we need
 * to handle the clean up manually.
 *
 * There are two argument processing stages.
 * They are the in stage, where we process python
 * parameters into their C counterparts, and the out
 * stage, where we process out C parameters back
 * into python objects. The in stage also sets up
 * temporary out structures for caller allocated
 * parameters which need to be cleaned up either on
 * in stage failure or at the completion of the out
 * stage (either success or failure)
 *
 * The in stage must call one of these cleanup functions:
 *    - pygi_marshal_cleanup_args_in_marshal_success
 *       (continue to out stage)
 *    - pygi_marshal_cleanup_args_in_parameter_fail
 *       (final, exit from invoke)
 *
 * The out stage must call one of these cleanup functions which are all final:
 *    - pygi_marshal_cleanup_args_out_marshal_success
 *    - pygi_marshal_cleanup_args_return_fail
 *    - pygi_marshal_cleanup_args_out_parameter_fail
 *
 **/
void
pygi_marshal_cleanup_args_in_marshal_success (PyGIInvokeState   *state,
                                              PyGICallableCache *cache)
{
80
    gsize i;
81

82
83
84
85
86
87
88
89
    /* For in success, call cleanup for all GI_DIRECTION_IN values only. */
    for (i = 0; i < cache->n_args; i++) {
        PyGIArgCache *arg_cache = cache->args_cache[i];
        PyGIMarshalCleanupFunc cleanup_func = arg_cache->in_cleanup;

        if (cleanup_func && arg_cache->direction == GI_DIRECTION_IN)
            cleanup_func (state, arg_cache, state->args[i]->v_pointer, TRUE);
    }
90
91
92
}

void
93
94
pygi_marshal_cleanup_args_out_marshal_success (PyGIInvokeState   *state,
                                               PyGICallableCache *cache)
95
{
96
97
98
99
100
101
102
103
104
    /* clean up the return if available */
    if (cache->return_cache != NULL) {
        PyGIMarshalCleanupFunc cleanup_func = cache->return_cache->out_cleanup;
        if (cleanup_func)
            cleanup_func (state,
                          cache->return_cache,
                          state->return_arg.v_pointer,
                          TRUE);
    }
105

106
107
108
109
110
111
112
113
114
115
116
117
118
    /* Now clean up args */
    GSList *cache_item = cache->out_args;
    while (cache_item) {
        PyGIArgCache *arg_cache = (PyGIArgCache *) cache_item->data;
        PyGIMarshalCleanupFunc cleanup_func = arg_cache->out_cleanup;
        if (cleanup_func != NULL)
            cleanup_func (state,
                          arg_cache,
                          state->args[arg_cache->c_arg_index]->v_pointer,
                          TRUE);

        cache_item = cache_item->next;
    }
119
120
121
122
123
124
125
}

void
pygi_marshal_cleanup_args_in_parameter_fail (PyGIInvokeState   *state,
                                             PyGICallableCache *cache,
                                             gssize failed_arg_index)
{
126
    state->failed = TRUE;
127
128
129
130
131
132
}

void
pygi_marshal_cleanup_args_return_fail (PyGIInvokeState   *state,
                                       PyGICallableCache *cache)
{
133
    state->failed = TRUE;
134
135
136
137
138
139
140
}

void
pygi_marshal_cleanup_args_out_parameter_fail (PyGIInvokeState   *state,
                                              PyGICallableCache *cache,
                                              gssize failed_out_arg_index)
{
141
142
    state->failed = TRUE;
}
143

144
145
146
147
148
149
150
void 
_pygi_marshal_cleanup_closure_unref (PyGIInvokeState *state,
                                     PyGIArgCache    *arg_cache,
                                     gpointer         data,
                                     gboolean         was_processed)
{
    g_closure_unref ( (GClosure *)data);
151
152
}

153
void
154
155
156
157
_pygi_marshal_cleanup_in_utf8 (PyGIInvokeState *state,
                               PyGIArgCache    *arg_cache,
                               gpointer         data,
                               gboolean         was_processed)
158
{
159
160
161
162
    /* We strdup strings so always free if we have processed this
       parameter for input */
    if (was_processed)
        g_free (data);
163
164
}

165
166
167
168
169
void
_pygi_marshal_cleanup_out_utf8 (PyGIInvokeState *state,
                                PyGIArgCache    *arg_cache,
                                gpointer         data,
                                gboolean         was_processed)
170
{
171
172
173
174
175
    /* Python copies the string so we need to free it
       if the interface is transfering ownership, 
       whether or not it has been processed yet */
    if (arg_cache->transfer == GI_TRANSFER_EVERYTHING)
        g_free (data);
176
177
}

178
void
179
180
181
182
_pygi_marshal_cleanup_in_interface_object (PyGIInvokeState *state,
                                           PyGIArgCache    *arg_cache,
                                           gpointer         data,
                                           gboolean         was_processed)
183
{
184
185
186
187
188
    /* If we processed the parameter but fail before invoking the method,
       we need to remove the ref we added */
    if (was_processed && state->failed && data != NULL &&
            arg_cache->transfer == GI_TRANSFER_EVERYTHING)
        g_object_unref (G_OBJECT(data));
189
}
190
191

void
192
193
194
195
_pygi_marshal_cleanup_out_interface_object (PyGIInvokeState *state,
                                            PyGIArgCache    *arg_cache,
                                            gpointer         data,
                                            gboolean         was_processed)
196
{
197
198
199
200
    /* If we error out and the object is not marshalled into a PyGObject
       we must take care of removing the ref */
    if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
        g_object_unref (G_OBJECT(data));
201
202
203
}

void 
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
_pygi_marshal_cleanup_in_interface_struct_gvalue (PyGIInvokeState *state,
                                                  PyGIArgCache    *arg_cache,
                                                  gpointer         data,
                                                  gboolean         was_processed)
{
    if (was_processed) {
        PyObject *py_arg = PyTuple_GET_ITEM (state->py_in_args,
                                             arg_cache->py_arg_index);
        GType py_object_type =
            pyg_type_from_object_strict ( (PyObject *) py_arg->ob_type, FALSE);

        if (py_object_type != G_TYPE_VALUE) {
            g_value_unset ((GValue *) data);
            g_slice_free (GValue, data);
        }
    }
}

void
_pygi_marshal_cleanup_in_interface_struct_foreign (PyGIInvokeState *state,
                                                   PyGIArgCache    *arg_cache,
                                                   gpointer         data,
                                                   gboolean         was_processed)
{
    if (state->failed && was_processed)
        pygi_struct_foreign_release (
            ( (PyGIInterfaceCache *)arg_cache)->interface_info,
            data);
}

void
_pygi_marshal_cleanup_out_interface_struct_foreign (PyGIInvokeState *state,
                                                    PyGIArgCache    *arg_cache,
                                                    gpointer         data,
                                                    gboolean         was_processed)
239
{
240
241
242
243
    if (!was_processed && arg_cache->transfer == GI_TRANSFER_EVERYTHING)
        pygi_struct_foreign_release ( 
            ( (PyGIInterfaceCache *)arg_cache)->interface_info,
            data);
244
}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

void
_pygi_marshal_cleanup_in_array (PyGIInvokeState *state,
                                PyGIArgCache    *arg_cache,
                                gpointer         data,
                                gboolean         was_processed)
{
    if (was_processed) {
        GArray *array_;
        PyGISequenceCache *sequence_cache = (PyGISequenceCache *)arg_cache;

        /* If this isn't a garray create one to help process variable sized
           array elements */
        if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
            gsize len;
            if (sequence_cache->fixed_size >= 0) {
                len = sequence_cache->fixed_size;
            } else if (sequence_cache->is_zero_terminated) {
                len = g_strv_length ((gchar **)data);
            } else {
                GIArgument *len_arg = state->args[sequence_cache->len_arg_index];
                len = len_arg->v_long;
            }

            array_ = g_array_new (FALSE,
                                  FALSE,
                                  sequence_cache->item_size);

            if (array_ == NULL)
                return;

            array_->data = data;
            array_->len = len;

        } else {
            array_ = (GArray *) data;
        }

        /* clean up items first */
        if (sequence_cache->item_cache->in_cleanup != NULL) {
            gsize i;
            PyGIMarshalCleanupFunc cleanup_func =
                sequence_cache->item_cache->in_cleanup;

            for(i = 0; i < array_->len; i++) {
                cleanup_func (state,
                              sequence_cache->item_cache,
                              g_array_index (array_, gpointer, i),
                              TRUE);
            }
        }

        if (state->failed ||
            arg_cache->transfer == GI_TRANSFER_NOTHING ||
            arg_cache->transfer == GI_TRANSFER_CONTAINER) {
            g_array_free (array_, TRUE);
        } else if (sequence_cache->array_type == GI_ARRAY_TYPE_C) {
            g_array_free (array_, FALSE);
        }
    }
}

void
_pygi_marshal_cleanup_out_array (PyGIInvokeState *state,
                                 PyGIArgCache    *arg_cache,
                                 gpointer         data,
                                 gboolean         was_processed)
{

}