pygi-cache.c 44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
 *
 * 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
 */
21 22

#include "pygi-info.h"
23 24
#include "pygi-cache.h"
#include "pygi-argument.h"
25
#include "pygi-type.h"
26
#include <girepository.h>
27

28 29 30 31 32 33 34 35 36 37
PyGIArgCache * _arg_cache_new_from_type_info (GITypeInfo *type_info,
                                              PyGIFunctionCache *function_cache,
                                              GIArgInfo *arg_info,
                                              GITypeTag type_tag,
                                              GITransfer transfer,
                                              GIDirection direction,
                                              gboolean is_caller_allocates,
                                              gint c_arg_index,
                                              gint py_arg_index);

38
/* cleanup */
John (J5) Palmieri's avatar
John (J5) Palmieri committed
39 40 41 42 43 44
void
_pygi_arg_cache_free(PyGIArgCache *cache)
{
    if (cache == NULL)
        return;

45 46
    if (cache->type_info != NULL)
        g_base_info_unref( (GIBaseInfo *)cache->type_info);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
47 48 49 50 51 52 53
    if (cache->destroy_notify)
        cache->destroy_notify(cache);
    else
        g_slice_free(PyGIArgCache, cache);
}

static void
54
_interface_cache_free_func (PyGIInterfaceCache *cache)
55 56 57
{
    if (cache != NULL) {
        Py_XDECREF(cache->py_type);
58 59
        if (cache->type_name != NULL)
            g_free(cache->type_name);
60 61 62
        if (cache->interface_info != NULL)
            g_base_info_unref( (GIBaseInfo *)cache->interface_info);
        g_slice_free(PyGIInterfaceCache, cache);
63 64 65
    }
}

John (J5) Palmieri's avatar
John (J5) Palmieri committed
66 67
static void
_hash_cache_free_func(PyGIHashCache *cache)
68
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
69 70 71
    if (cache != NULL) {
        _pygi_arg_cache_free(cache->key_cache);
        _pygi_arg_cache_free(cache->value_cache);
72
        g_slice_free(PyGIHashCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
73
    }
74 75
}

76
static void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
77
_sequence_cache_free_func(PyGISequenceCache *cache)
78
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
79 80
    if (cache != NULL) {
        _pygi_arg_cache_free(cache->item_cache);
81
        g_slice_free(PyGISequenceCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
82
    }
83 84
}

85
static void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
86
_callback_cache_free_func(PyGICallbackCache *cache)
87
{
88 89 90 91
    if (cache != NULL) {
        if (cache->interface_info != NULL)
            g_base_info_unref( (GIBaseInfo *)cache->interface_info);

92
        g_slice_free(PyGICallbackCache, cache);
93
    }
94 95 96
}

void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
97
_pygi_function_cache_free(PyGIFunctionCache *cache)
98 99 100
{
    int i;

101 102 103
    if (cache == NULL)
        return;

104 105 106
    g_slist_free(cache->out_args);
    for (i = 0; i < cache->n_args; i++) {
        PyGIArgCache *tmp = cache->args_cache[i];
107
        _pygi_arg_cache_free(tmp);
108
    }
109 110 111
    if (cache->return_cache != NULL)
        _pygi_arg_cache_free(cache->return_cache);

112
    g_slice_free1(cache->n_args * sizeof(PyGIArgCache *), cache->args_cache);
113 114 115 116 117
    g_slice_free(PyGIFunctionCache, cache);
}

/* cache generation */
static inline PyGIFunctionCache *
118
_function_cache_new_from_function_info(GIFunctionInfo *function_info)
119 120 121 122 123
{
    PyGIFunctionCache *fc;
    GIFunctionInfoFlags flags;

    fc = g_slice_new0(PyGIFunctionCache);
124 125

    fc->name = g_base_info_get_name((GIBaseInfo *)function_info);
126 127 128
    flags = g_function_info_get_flags(function_info);
    fc->is_method = flags & GI_FUNCTION_IS_METHOD;
    fc->is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
129

130
    fc->n_args = g_callable_info_get_n_args ( (GICallableInfo *) function_info) + (fc->is_method ? 1: 0);
131 132
    if (fc->n_args > 0)
        fc->args_cache = g_slice_alloc0(fc->n_args * sizeof(PyGIArgCache *));
133 134 135 136

    return fc;
}

137 138 139 140
static inline PyGIInterfaceCache *
_interface_cache_new_from_interface_info(GIInterfaceInfo *iface_info)
{
    PyGIInterfaceCache *ic;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
141

142
    ic = g_slice_new0(PyGIInterfaceCache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
143
    ((PyGIArgCache *)ic)->destroy_notify = (GDestroyNotify)_interface_cache_free_func;
144 145 146 147 148 149 150 151 152 153 154
    ic->g_type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *)iface_info);
    if (ic->g_type != G_TYPE_NONE) {
        ic->py_type = _pygi_type_get_from_g_type (ic->g_type);
    } else {
        /* FIXME: avoid passing GI infos to noncache API */
        ic->py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) iface_info);
    }

    if (ic->py_type == NULL)
        return NULL;

155
    ic->type_name = _pygi_g_base_info_get_fullname(iface_info);
156 157 158
    return ic;
}

159
static inline PyGISequenceCache *
160
_sequence_cache_new_from_type_info(GITypeInfo *type_info,
161 162
                                   GIDirection direction,
                                   GITransfer transfer,
163
                                   gint aux_offset)
164 165 166
{
    PyGISequenceCache *sc;
    GITypeInfo *item_type_info;
167
    GITypeTag item_type_tag;
168
    GITransfer item_transfer;
169 170

    sc = g_slice_new0(PyGISequenceCache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
171
    ((PyGIArgCache *)sc)->destroy_notify = (GDestroyNotify)_sequence_cache_free_func;
172 173 174 175

    sc->fixed_size = -1;
    sc->len_arg_index = -1;
    sc->is_zero_terminated = g_type_info_is_zero_terminated(type_info);
176
    if (!sc->is_zero_terminated)
177 178
        sc->fixed_size = g_type_info_get_array_fixed_size(type_info);
    if (sc->fixed_size < 0)
179
        sc->len_arg_index = g_type_info_get_array_length (type_info) + aux_offset;
180

181
    item_type_info = g_type_info_get_param_type (type_info, 0);
182
    item_type_tag = g_type_info_get_tag (item_type_info);
183

184 185 186 187 188
    item_transfer = GI_TRANSFER_NOTHING;
    if (transfer == GI_TRANSFER_EVERYTHING ||
        transfer == GI_TRANSFER_CONTAINER)
        item_transfer = GI_TRANSFER_EVERYTHING;

189 190 191 192 193 194 195 196
    sc->item_cache = _arg_cache_new_from_type_info(item_type_info,
                                                   NULL,
                                                   NULL,
                                                   item_type_tag,
                                                   item_transfer,
                                                   direction,
                                                   FALSE,
                                                   0, 0);
197

198 199 200 201
    if (sc->item_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)sc);
        return NULL;
    }
John (J5) Palmieri's avatar
John (J5) Palmieri committed
202

203
    sc->item_cache->type_tag = item_type_tag;
204
    sc->item_size = _pygi_g_type_info_size(item_type_info);
205
    g_base_info_unref( (GIBaseInfo *) item_type_info);
206

207
    return sc;
208
}
John (J5) Palmieri's avatar
John (J5) Palmieri committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static inline PyGIHashCache *
_hash_cache_new_from_type_info(GITypeInfo *type_info)
{
    PyGIHashCache *hc;
    GITypeInfo *key_type_info;
    GITypeTag key_type_tag;
    GITypeInfo *value_type_info;
    GITypeTag value_type_tag;

    hc = g_slice_new0(PyGIHashCache);
    ((PyGIArgCache *)hc)->destroy_notify = (GDestroyNotify)_hash_cache_free_func;
    key_type_info = g_type_info_get_param_type (type_info, 0);
    key_type_tag = g_type_info_get_tag (key_type_info);
    value_type_info = g_type_info_get_param_type (type_info, 1);
    value_type_tag = g_type_info_get_tag (value_type_info);

225 226 227 228 229 230 231 232
    hc->key_cache = _arg_cache_new_from_type_info(key_type_info,
                                                  NULL,
                                                  NULL,
                                                  key_type_tag,
                                                  GI_TRANSFER_EVERYTHING,
                                                  GI_DIRECTION_IN,
                                                  FALSE,
                                                  0, 0);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
233 234 235 236 237 238

    if (hc->key_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)hc);
        return NULL;
    }

239 240 241 242 243 244 245 246
    hc->value_cache = _arg_cache_new_from_type_info(value_type_info,
                                                    NULL,
                                                    NULL,
                                                    value_type_tag,
                                                    GI_TRANSFER_EVERYTHING,
                                                    GI_DIRECTION_IN,
                                                    FALSE,
                                                    0, 0);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260

    if (hc->value_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)hc);
        return NULL;
    }

    hc->key_cache->type_tag = key_type_tag;
    hc->value_cache->type_tag = value_type_tag;

    g_base_info_unref( (GIBaseInfo *)key_type_info);
    g_base_info_unref( (GIBaseInfo *)value_type_info);

    return hc;
}
261

John (J5) Palmieri's avatar
John (J5) Palmieri committed
262
static inline PyGICallbackCache *
263
_callback_cache_new_from_arg_info(GIArgInfo *arg_info,
264
                                  GIInterfaceInfo *iface_info,
265
                                  gint aux_offset)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
266 267 268 269
{
   PyGICallbackCache *cc;

   cc = g_slice_new0(PyGICallbackCache);
270 271
   cc->user_data_index = g_arg_info_get_closure(arg_info) + aux_offset;
   cc->destroy_notify_index = g_arg_info_get_destroy(arg_info) + aux_offset;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
272
   cc->scope = g_arg_info_get_scope(arg_info);
273 274
   g_base_info_ref( (GIBaseInfo *)iface_info);
   cc->interface_info = iface_info;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
275 276 277
   return cc;
}

278 279 280 281 282
static inline PyGIArgCache *
_arg_cache_new(void)
{
    return g_slice_new0(PyGIArgCache);
}
283

284 285
static inline void 
_arg_cache_in_void_setup(PyGIArgCache *arg_cache)
286
{
287 288
    arg_cache->in_marshaller = _pygi_marshal_in_void;
}
289

290 291 292 293
static inline void 
_arg_cache_out_void_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_void;
294 295
}

296 297
static inline void
_arg_cache_in_boolean_setup(PyGIArgCache *arg_cache)
298
{
299
    arg_cache->in_marshaller = _pygi_marshal_in_boolean;
300 301
}

302 303 304 305 306 307 308 309
static inline void 
_arg_cache_out_boolean_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_boolean;
}

static inline void
_arg_cache_in_int8_setup(PyGIArgCache *arg_cache)
310
{
311
    arg_cache->in_marshaller = _pygi_marshal_in_int8;
312 313
}

314 315 316 317 318 319 320 321
static inline void
_arg_cache_out_int8_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_int8;
}

static inline void
_arg_cache_in_uint8_setup(PyGIArgCache *arg_cache)
322
{
323
    arg_cache->in_marshaller = _pygi_marshal_in_uint8;
324 325
}

326 327 328 329 330 331 332 333
static inline void
_arg_cache_out_uint8_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_uint8;
}

static inline void
_arg_cache_in_int16_setup(PyGIArgCache *arg_cache)
334
{
335
    arg_cache->in_marshaller = _pygi_marshal_in_int16;
336 337
}

338 339 340 341 342 343 344 345
static inline void
_arg_cache_out_int16_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_int16;
}

static inline void
_arg_cache_in_uint16_setup(PyGIArgCache *arg_cache)
346
{
347
    arg_cache->in_marshaller = _pygi_marshal_in_uint16;
348 349
}

350 351 352 353 354 355 356 357
static inline void
_arg_cache_out_uint16_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_uint16;
}

static inline void
_arg_cache_in_int32_setup(PyGIArgCache *arg_cache)
358
{
359
    arg_cache->in_marshaller = _pygi_marshal_in_int32;
360 361
}

362 363 364 365 366 367 368 369
static inline void
_arg_cache_out_int32_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_int32;
}

static inline void
_arg_cache_in_uint32_setup(PyGIArgCache *arg_cache)
370
{
371
    arg_cache->in_marshaller = _pygi_marshal_in_uint32;
372 373
}

374 375 376 377 378 379 380 381
static inline void
_arg_cache_out_uint32_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_uint32;
}

static inline void
_arg_cache_in_int64_setup(PyGIArgCache *arg_cache)
382
{
383
    arg_cache->in_marshaller = _pygi_marshal_in_int64;
384 385
}

386 387 388 389 390 391 392 393
static inline void
_arg_cache_out_int64_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_int64;
}

static inline void
_arg_cache_in_uint64_setup(PyGIArgCache *arg_cache)
394
{
395
    arg_cache->in_marshaller = _pygi_marshal_in_uint64;
396 397
}

398 399 400 401 402 403 404 405
static inline void
_arg_cache_out_uint64_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_uint64;
}

static inline void
_arg_cache_in_float_setup(PyGIArgCache *arg_cache)
406
{
407
    arg_cache->in_marshaller = _pygi_marshal_in_float;
408 409
}

410 411 412 413 414 415 416 417
static inline void
_arg_cache_out_float_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_float;
}

static inline void
_arg_cache_in_double_setup(PyGIArgCache *arg_cache)
418
{
419
    arg_cache->in_marshaller = _pygi_marshal_in_double;
420 421
}

422 423 424 425 426 427 428 429
static inline void
_arg_cache_out_double_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_double;
}

static inline void
_arg_cache_in_unichar_setup(PyGIArgCache *arg_cache)
430
{
431
    arg_cache->in_marshaller = _pygi_marshal_in_unichar;
432 433
}

434 435 436 437 438 439 440 441
static inline void
_arg_cache_out_unichar_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_unichar;
}

static inline void
_arg_cache_in_gtype_setup(PyGIArgCache *arg_cache)
442
{
443
    arg_cache->in_marshaller = _pygi_marshal_in_gtype;
444 445
}

446 447 448 449 450 451 452 453 454
static inline void
_arg_cache_out_gtype_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_gtype;
}

static inline void
_arg_cache_in_utf8_setup(PyGIArgCache *arg_cache,
                         GITransfer transfer)
455
{
456
    arg_cache->in_marshaller = _pygi_marshal_in_utf8;
457
}
458

459 460 461 462 463
static inline void
_arg_cache_out_utf8_setup(PyGIArgCache *arg_cache,
                          GITransfer transfer)
{
    arg_cache->out_marshaller = _pygi_marshal_out_utf8;
464 465
}

466 467 468
static inline void
_arg_cache_in_filename_setup(PyGIArgCache *arg_cache,
                             GITransfer transfer)
469
{
470
    arg_cache->in_marshaller = _pygi_marshal_in_filename;
471 472
}

473 474 475
static inline void
_arg_cache_out_filename_setup(PyGIArgCache *arg_cache,
                              GITransfer transfer)
476
{
477 478
    arg_cache->out_marshaller = _pygi_marshal_out_filename;
}
479

480 481 482 483 484 485 486
static inline gboolean
_arg_cache_in_array_setup(PyGIArgCache *arg_cache,
                          PyGIFunctionCache *function_cache,
                          GITypeInfo *type_info,
                          GITransfer transfer)
{
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache; 
487 488
    seq_cache->array_type = g_type_info_get_array_type(type_info);

489 490
    if (seq_cache->len_arg_index >= 0) {
        PyGIArgCache *aux_cache = _arg_cache_new();
John (J5) Palmieri's avatar
John (J5) Palmieri committed
491
        aux_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
492 493 494 495 496 497 498 499 500
        if (function_cache->args_cache[seq_cache->len_arg_index] != NULL) {
            PyGIArgCache *invalid_cache = function_cache->args_cache[seq_cache->len_arg_index];
            arg_cache->c_arg_index = invalid_cache->c_arg_index;
            _pygi_arg_cache_free(invalid_cache);
        }

        function_cache->args_cache[seq_cache->len_arg_index] = aux_cache;
    }

501
    arg_cache->in_marshaller = _pygi_marshal_in_array;
502 503

    /* arg_cache->cleanup = _pygi_cleanup_array; */
504 505
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
static inline gboolean
_arg_cache_out_array_setup(PyGIArgCache *arg_cache,
                           PyGIFunctionCache *function_cache,
                           GITypeInfo *type_info,
                           GITransfer transfer)
{
    PyGISequenceCache *seq_cache = (PyGISequenceCache *)arg_cache; 
    arg_cache->out_marshaller = _pygi_marshal_out_array;

    seq_cache->array_type = g_type_info_get_array_type(type_info);

    if (seq_cache->len_arg_index >= 0) {
        PyGIArgCache *aux_cache = function_cache->args_cache[seq_cache->len_arg_index];
        if (aux_cache == NULL)
            aux_cache = _arg_cache_new();
        else if (aux_cache->aux_type == PYGI_AUX_TYPE_IGNORE)
            return TRUE;

        aux_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
        if (function_cache->args_cache[seq_cache->len_arg_index] != NULL) {
            PyGIArgCache *invalid_cache = function_cache->args_cache[seq_cache->len_arg_index];
            arg_cache->c_arg_index = invalid_cache->c_arg_index;
            _pygi_arg_cache_free(invalid_cache);
        }

        function_cache->args_cache[seq_cache->len_arg_index] = aux_cache;
    }

    return TRUE;
    /* arg_cache->cleanup = _pygi_cleanup_array; */
}

static inline void
_arg_cache_in_glist_setup(PyGIArgCache *arg_cache,
                          GITransfer transfer)
541
{
542
    arg_cache->in_marshaller = _pygi_marshal_in_glist;
543
    /* arg_cache->cleanup = */
544
}
545

546 547 548 549 550 551
static inline void
_arg_cache_out_glist_setup(PyGIArgCache *arg_cache,
                           GITransfer transfer)
{
    arg_cache->out_marshaller = _pygi_marshal_out_glist;
    /* arg_cache->cleanup = */
552 553
}

554 555 556
static inline void
_arg_cache_in_gslist_setup(PyGIArgCache *arg_cache,
                           GITransfer transfer)
557
{
558
    arg_cache->in_marshaller = _pygi_marshal_in_gslist;
559
    /* arg_cache->cleanup = */
560
}
561

562 563 564 565 566 567
static inline void
_arg_cache_out_gslist_setup(PyGIArgCache *arg_cache,
                            GITransfer transfer)
{
    arg_cache->out_marshaller = _pygi_marshal_out_gslist;
    /* arg_cache->cleanup = */
568 569
}

570 571
static inline void
_arg_cache_in_ghash_setup(PyGIArgCache *arg_cache)
572
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
573
    arg_cache->in_marshaller = _pygi_marshal_in_ghash;
574 575
}

576 577 578 579 580 581 582 583 584
static inline void 
_arg_cache_out_ghash_setup(PyGIArgCache *arg_cache)
{
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for Out GHash is not fully implemented yet");
}

static inline void
_arg_cache_in_gerror_setup(PyGIArgCache *arg_cache)
585
{
586
    arg_cache->in_marshaller = _pygi_marshal_in_gerror;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
587
    arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
588 589
}

590 591 592 593 594 595 596 597 598 599
static inline void
_arg_cache_out_gerror_setup(PyGIArgCache *arg_cache)
{
    arg_cache->out_marshaller = _pygi_marshal_out_gerror;
    arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
}

static inline void
_arg_cache_in_interface_union_setup(PyGIArgCache *arg_cache,
                                    GITransfer transfer)
600
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
601
    arg_cache->in_marshaller = _pygi_marshal_in_interface_struct;
602 603 604 605 606 607 608
}

static inline void
_arg_cache_out_interface_union_setup(PyGIArgCache *arg_cache,
                                     GITransfer transfer)
{
    arg_cache->out_marshaller = _pygi_marshal_out_interface_struct;
609 610
}

John (J5) Palmieri's avatar
John (J5) Palmieri committed
611
static void
612 613 614 615
_g_slice_free_gvalue_func(GValue *value) {
    g_slice_free(GValue, value);
}

616 617 618 619
static inline void
_arg_cache_in_interface_struct_setup(PyGIArgCache *arg_cache,
                                     GIInterfaceInfo *iface_info,
                                     GITransfer transfer)
620
{
621
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
622 623 624 625 626 627
    iface_cache->is_foreign = g_struct_info_is_foreign( (GIStructInfo*)iface_info);
    arg_cache->in_marshaller = _pygi_marshal_in_interface_struct;
    if (iface_cache->g_type == G_TYPE_VALUE)
        arg_cache->cleanup = _g_slice_free_gvalue_func;
    if (iface_cache->g_type == G_TYPE_CLOSURE)
        arg_cache->cleanup = g_closure_unref;
628
}
629

630 631 632 633 634 635 636 637
static inline void
_arg_cache_out_interface_struct_setup(PyGIArgCache *arg_cache,
                                      GIInterfaceInfo *iface_info,
                                      GITransfer transfer)
{
    PyGIInterfaceCache *iface_cache = (PyGIInterfaceCache *)arg_cache;
    iface_cache->is_foreign = g_struct_info_is_foreign( (GIStructInfo*)iface_info);
    arg_cache->out_marshaller = _pygi_marshal_out_interface_struct;
638 639
}

640 641 642
static inline void
_arg_cache_in_interface_object_setup(PyGIArgCache *arg_cache,
                                     GITransfer transfer)
643 644 645 646 647 648
{
    arg_cache->in_marshaller = _pygi_marshal_in_interface_object;
    if (transfer == GI_TRANSFER_EVERYTHING)
        arg_cache->cleanup = (GDestroyNotify)g_object_unref;
}

649 650
static inline void 
_arg_cache_out_interface_object_setup(PyGIArgCache *arg_cache,
651
                                      GITransfer transfer)
652
{
653 654 655 656 657 658 659
    arg_cache->out_marshaller = _pygi_marshal_out_interface_object;
}

static inline void
_arg_cache_in_interface_boxed_setup(PyGIArgCache *arg_cache,
                                    GITransfer transfer)
{
660
    arg_cache->in_marshaller = _pygi_marshal_in_interface_boxed;
661 662
}

663 664 665 666 667 668 669 670 671 672
static inline void
_arg_cache_out_interface_boxed_setup(PyGIArgCache *arg_cache,
                                     GITransfer transfer)
{
    arg_cache->out_marshaller = _pygi_marshal_out_interface_struct;
}

static inline void
_arg_cache_in_interface_callback_setup(PyGIArgCache *arg_cache,
                                       PyGIFunctionCache *function_cache)
John (J5) Palmieri's avatar
John (J5) Palmieri committed
673
{
674
    PyGICallbackCache *callback_cache = (PyGICallbackCache *)arg_cache;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
675 676 677 678 679
    if (callback_cache->user_data_index >= 0) {
        PyGIArgCache *user_data_arg_cache = _arg_cache_new();
        user_data_arg_cache->aux_type = PYGI_AUX_TYPE_HAS_PYARG;
        function_cache->args_cache[callback_cache->user_data_index] = user_data_arg_cache;
    }
680 681

    if (callback_cache->destroy_notify_index >= 0) {
John (J5) Palmieri's avatar
John (J5) Palmieri committed
682 683 684 685 686
        PyGIArgCache *destroy_arg_cache = _arg_cache_new();
        destroy_arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
        function_cache->args_cache[callback_cache->destroy_notify_index] = destroy_arg_cache;
    }
    arg_cache->in_marshaller = _pygi_marshal_in_interface_callback;
687 688
}

689 690
static inline void 
_arg_cache_out_interface_callback_setup(void)
691
{
692 693 694
    PyErr_Format(PyExc_NotImplementedError,
                 "Callback returns are not supported");
}
695

696 697 698 699
static inline void 
_arg_cache_in_interface_enum_setup(PyGIArgCache *arg_cache,
                                   GITransfer transfer)
{
700
    arg_cache->in_marshaller = _pygi_marshal_in_interface_enum;
701 702
}

703 704 705
static inline void
_arg_cache_out_interface_enum_setup(PyGIArgCache *arg_cache,
                                    GITransfer transfer)
706
{
707 708
    arg_cache->out_marshaller = _pygi_marshal_out_interface_enum;
}
709

710 711 712 713
static inline void 
_arg_cache_in_interface_flags_setup(PyGIArgCache *arg_cache,
                                    GITransfer transfer)
{
714
    arg_cache->in_marshaller = _pygi_marshal_in_interface_flags;
715 716
}

717 718 719
static inline void
_arg_cache_out_interface_flags_setup(PyGIArgCache *arg_cache,
                                     GITransfer transfer)
720
{
721 722 723 724 725 726 727 728 729 730 731 732 733 734
    arg_cache->out_marshaller = _pygi_marshal_out_interface_flags;
}

static inline PyGIArgCache *
_arg_cache_new_from_interface_info (GIInterfaceInfo *iface_info,
                                    PyGIFunctionCache *function_cache,
                                    GIArgInfo *arg_info,
                                    GIInfoType info_type,
                                    GITransfer transfer,
                                    GIDirection direction,
                                    gint c_arg_index,
                                    gint py_arg_index)
{
    PyGIInterfaceCache *iface_cache = NULL;
735 736
    PyGIArgCache *arg_cache = NULL;

737 738 739 740 741 742 743 744 745
    /* Callbacks are special cased */
    if (info_type != GI_INFO_TYPE_CALLBACK) {
        iface_cache = _interface_cache_new_from_interface_info(iface_info);

        arg_cache = (PyGIArgCache *)iface_cache;
        if (arg_cache == NULL)
            return NULL;
    }

746 747
    switch (info_type) {
        case GI_INFO_TYPE_UNION:
748 749 750 751 752 753
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_union_setup(arg_cache, transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_union_setup(arg_cache, transfer);

754 755
            break;
        case GI_INFO_TYPE_STRUCT:
756 757 758 759 760 761 762 763 764 765
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_struct_setup(arg_cache,
                                                    iface_info, 
                                                    transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_struct_setup(arg_cache,
                                                     iface_info,
                                                     transfer);

766 767 768
            break;
        case GI_INFO_TYPE_OBJECT:
        case GI_INFO_TYPE_INTERFACE:
769 770 771 772 773 774
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_object_setup(arg_cache, transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_object_setup(arg_cache, transfer);

775 776
            break;
        case GI_INFO_TYPE_BOXED:
777 778 779 780 781 782
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_boxed_setup(arg_cache, transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_boxed_setup(arg_cache, transfer);

783 784
            break;
        case GI_INFO_TYPE_CALLBACK:
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
            {
                PyGICallbackCache *callback_cache;

                if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT) {
                    _arg_cache_out_interface_callback_setup();
                    return NULL;
                }

                callback_cache = 
                    _callback_cache_new_from_arg_info(arg_info,
                                                      iface_info,
                                                      function_cache->is_method ? 1: 0);

                arg_cache = (PyGIArgCache *)callback_cache;
                if (arg_cache == NULL)
                    return NULL;

                if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
                    _arg_cache_in_interface_callback_setup(arg_cache, function_cache);

                break;
            }
807
        case GI_INFO_TYPE_ENUM:
808 809 810 811 812 813
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_enum_setup(arg_cache, transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_enum_setup(arg_cache, transfer);

814 815
            break;
        case GI_INFO_TYPE_FLAGS:
816 817 818 819 820 821
            if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_interface_flags_setup(arg_cache, transfer);

            if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_interface_flags_setup(arg_cache, transfer);

822 823 824 825 826 827 828 829 830 831 832
            break;
        default:
            g_assert_not_reached();
    }

    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = GI_TYPE_TAG_INTERFACE;
        arg_cache->py_arg_index = py_arg_index;
        arg_cache->c_arg_index = c_arg_index;
833

834 835 836 837
        if (iface_cache != NULL) {
            g_base_info_ref( (GIBaseInfo *)iface_info);
            iface_cache->interface_info = iface_info;
        }
838
    }
839 840

    return arg_cache;
841 842
}

843 844 845 846 847 848 849 850 851 852
PyGIArgCache *
_arg_cache_new_from_type_info (GITypeInfo *type_info,
                               PyGIFunctionCache *function_cache,
                               GIArgInfo *arg_info,
                               GITypeTag type_tag,
                               GITransfer transfer,
                               GIDirection direction,
                               gboolean is_caller_allocates,
                               gint c_arg_index,
                               gint py_arg_index)
853
{
854
    PyGIArgCache *arg_cache = NULL;
855

856 857 858 859 860
    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
861

862 863
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_void_setup(arg_cache);
864

865 866 867 868 869 870 871
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_void_setup(arg_cache);
           break;
       case GI_TYPE_TAG_BOOLEAN:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
872

873 874
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_boolean_setup(arg_cache);
875

876 877
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_boolean_setup(arg_cache);
878

879 880 881 882 883
           break;
       case GI_TYPE_TAG_INT8:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
884

885 886
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_int8_setup(arg_cache);
887

888 889
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_int8_setup(arg_cache);
890

891 892 893 894 895
           break;
       case GI_TYPE_TAG_UINT8:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
896

897 898
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_uint8_setup(arg_cache);
899

900 901 902 903 904 905 906 907 908 909 910
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_uint8_setup(arg_cache);
 
           break;
       case GI_TYPE_TAG_INT16:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
           
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_int16_setup(arg_cache);
911

912 913
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_int16_setup(arg_cache);
914

915 916 917 918 919
           break;
       case GI_TYPE_TAG_UINT16:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
920

921 922
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_uint16_setup(arg_cache);
923

924 925
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_uint16_setup(arg_cache);
926

927 928 929 930 931
           break;
       case GI_TYPE_TAG_INT32:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
932

933 934
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_int32_setup(arg_cache);
935

936 937
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_int32_setup(arg_cache);
938

939 940 941 942 943
           break;
       case GI_TYPE_TAG_UINT32:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
944

945 946
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_uint32_setup(arg_cache);
947

948 949
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_uint32_setup(arg_cache);
950

951 952 953 954 955
           break;
       case GI_TYPE_TAG_INT64:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
956

957 958
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_int64_setup(arg_cache);
959

960 961
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_int64_setup(arg_cache);
962

963 964 965 966 967 968 969 970
           break;
       case GI_TYPE_TAG_UINT64:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
           
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_uint64_setup(arg_cache);
971

972 973
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_uint64_setup(arg_cache);
974

975 976 977 978 979
           break;
       case GI_TYPE_TAG_FLOAT:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
980

981 982
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_float_setup(arg_cache);
983

984 985
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_float_setup(arg_cache);
986

987 988 989 990 991
           break;
       case GI_TYPE_TAG_DOUBLE:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
992

993 994
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_double_setup(arg_cache);
995

996 997 998 999 1000 1001 1002 1003
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_double_setup(arg_cache);
           
           break;
       case GI_TYPE_TAG_UNICHAR:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;
1004

1005 1006
           if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT)
               _arg_cache_in_unichar_setup(arg_cache);
1007

1008 1009
           if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT)
               _arg_cache_out_unichar_setup(arg_cache);
1010

1011 1012 1013 1014 1015
           break;
       case GI_TYPE_TAG_GTYPE:
           arg_cache = _arg_cache_new();
           if (arg_cache == NULL)
               break;