pygi-cache.c 18.4 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* -*- 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
 */


/* cleanup */
static inline void
_pygi_interface_cache_free (PyGIInterfaceCache *cache)
{
    if (cache != NULL) {
        Py_XDECREF(cache->py_type);
        g_slice_free(PyGIInterfaceCache, cache);
    }
}

static inline void
_pygi_hash_cache_free (PyGIHashCache *cache)
{
    if (cache != NULL)
        g_slice_free(PyGIHashCache, cache);
}

static inline void
_pygi_sequence_cache_free (PyGISequenceCache *cache)
{
    if (cache != NULL)
        g_slice_free(PyGISequenceCache, cache);
}

static inline void
_pygi_callback_cache_free (PyGICallbackCache *cache)
{
    if (cache != NULL)
        g_slice_free(PyGICallbackCache, cache);
}

void
_pygi_arg_cache_clear (PyGIArgCache *cache);
{
    cache->is_aux = FALSE;
    cache->is_pointer = FALSE;
    cache->direction = 0;
    g_base_info_unref(cache->arg_info);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
61 62 63 64 65

    cache->in_validator = NULL;
    cache->in_marshaler = NULL;
    cache->out_marshaler = NULL;
    cache->cleanup = NULL;
66 67 68 69 70 71

    _pygi_sequence_cache_free(cache->sequence_cache);
    cache->sequence_cache = NULL;
    _pygi_interface_cache_free(cache->interface_cache);
    cache->interface_cache = NULL;
    _pygi_hash_cache_free(cache->hash_cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
72
    cache->hash_cache = NULL;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    _pygi_callback_cache_free(cache->callback_cache);
    cache->callback_cache = NULL;

    gint c_arg_index = -1;
    gint py_arg_index = -1;
}

void
_pygi_function_cache_free (PyGIFunctionCache *cache)
{
    int i;

    g_slist_free(cache->in_args);
    g_slist_free(cache->out_args);
    for (i = 0; i < cache->n_args; i++) {
        PyGIArgCache *tmp = cache->args_cache[i];
        _pygi_arg_cache_clear(tmp);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
90
        g_slice_free(PyGIArgCache, tmp);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }

    g_slice_free(PyGIFunctionCache, cache);
}

/* cache generation */
static inline PyGIFunctionCache *
_function_cache_init(GIFunctionInfo *function_info)
{
    PyGIFunctionCache *fc;
    GIFunctionInfoFlags flags;

    fc = g_slice_new0(PyGIFunctionCache);
    flags = g_function_info_get_flags(function_info);
    fc->is_method = flags & GI_FUNCTION_IS_METHOD;
    fc->is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
    fc->n_args = g_callable_info_get_n_args ( (GICallableInfo *) function_info);
    fc->args_cache = g_slice_alloc0(fc->n_args * sizeof(PyGIArgInfo *));

    return fc;
}

/* process in args */

static inline boolean
_arg_cache_generate_metadata_in_void(PyGIArgCache *arg_cache,
                                     PyGIFunctionCache *function_cache,
                                     GIArgInfo *arg_info)
{
120
     arg_cache->in_marshaler = _pygi_marshal_in_void;
121 122 123 124 125 126 127 128 129

     return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_boolean(PyGIArgCache *arg_cache,
                                        PyGIFunctionCache *function_cache,
                                        GIArgInfo *arg_info)
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
130
    arg_cache->in_marshaler = _pygi_marshal_in_boolean;
131 132 133 134 135 136 137 138
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_int8(PyGIArgCache *arg_cache,
                                     PyGIFunctionCache *function_cache,
                                     GIArgInfo *arg_info)
{
139
    arg_cache->in_marshaler = _pygi_marshal_in_int8;
140 141 142 143 144 145 146 147
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_uint8(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
148
    arg_cache->in_marshaler = _pygi_marshal_in_uint8;
149 150 151 152 153 154 155 156
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_int16(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
157
    arg_cache->in_marshaler = _pygi_marshal_in_int16;
158 159 160 161 162 163 164 165
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_uint16(PyGIArgCache *arg_cache,
                                       PyGIFunctionCache *function_cache,
                                       GIArgInfo *arg_info)
{
166
    arg_cache->in_marshaler = _pygi_marshal_in_uint16;
167 168 169 170 171 172 173 174
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_int32(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
175
    arg_cache->in_marshaler = _pygi_marshal_in_int32;
176 177 178 179 180 181 182 183
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_uint32(PyGIArgCache *arg_cache,
                                       PyGIFunctionCache *function_cache,
                                       GIArgInfo *arg_info)
{
184
    arg_cache->in_marshaler = _pygi_marshal_in_uint32;
185 186 187 188 189 190 191 192
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_int64(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
193
    arg_cache->in_marshaler = _pygi_marshal_in_int64;
194 195 196 197 198 199 200 201
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_uint64(PyGIArgCache *arg_cache,
                                       PyGIFunctionCache *function_cache,
                                       GIArgInfo *arg_info)
{
202
    arg_cache->in_marshaler = _pygi_marshal_in_uint64;
203 204 205 206 207 208 209 210
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_float(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
211
    arg_cache->in_marshaler = _pygi_marshal_in_float;
212 213 214 215 216 217 218 219
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_double(PyGIArgCache *arg_cache,
                                       PyGIFunctionCache *function_cache,
                                       GIArgInfo *arg_info)
{
220
    arg_cache->in_marshaler = _pygi_marshal_in_double;
221 222 223 224 225 226 227 228
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_unichar(PyGIArgCache *arg_cache,
                                        PyGIFunctionCache *function_cache,
                                        GIArgInfo *arg_info)
{
229 230
    arg_cache->in_marshaler = _pygi_marshal_in_unichar;

231 232 233 234 235 236 237 238
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_gtype(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
239
    arg_cache->in_marshaler = _pygi_marshal_in_gtype;
240 241 242 243 244 245 246 247
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_utf8(PyGIArgCache *arg_cache,
                                     PyGIFunctionCache *function_cache,
                                     GIArgInfo *arg_info)
{
248 249 250 251
    arg_cache->in_marshaler = _pygi_marshal_in_utf8;
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

252 253 254 255 256 257 258 259
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_filename(PyGIArgCache *arg_cache,
                                         PyGIFunctionCache *function_cache,
                                         GIArgInfo *arg_info)
{
260 261 262 263
    arg_cache->in_marshaler = _pygi_marshal_in_filename;
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

264 265 266 267 268 269 270 271
    return TRUE;
}

static inline boolean
_arg_cache_generate_metadata_in_array(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
272 273 274 275
    arg_cache->in_marshaler = _pygi_marshal_in_array;
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
276 277 278 279 280 281 282
}

static inline boolean
_arg_cache_generate_metadata_in_interface(PyGIArgCache *arg_cache,
                                          PyGIFunctionCache *function_cache,
                                          GIArgInfo *arg_info)
{
283 284 285 286
    /* TODO: Switch on GI_INFO_TYPE_ to determine caching */
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
287 288 289 290 291 292 293
}

static inline boolean
_arg_cache_generate_metadata_in_glist(PyGIArgCache *arg_cache,
                                     PyGIFunctionCache *function_cache,
                                     GIArgInfo *arg_info)
{
294 295 296 297
    arg_cache->in_marshaler = _pygi_marshal_in_glist;
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
298 299 300 301 302 303 304
}

static inline boolean
_arg_cache_generate_metadata_in_gslist(PyGIArgCache *arg_cache,
                                     PyGIFunctionCache *function_cache,
                                     GIArgInfo *arg_info)
{
305 306 307 308
    arg_cache->in_marshaler = _pygi_marshal_in_gslist;
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
309 310 311 312 313 314 315
}

static inline boolean
_arg_cache_generate_metadata_in_ghash(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
316 317 318 319
    arg_cache->in_marshaler = _pygi_marshal_in_ghash;
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
320 321 322 323 324 325 326
}

static inline boolean
_arg_cache_generate_metadata_in_error(PyGIArgCache *arg_cache,
                                      PyGIFunctionCache *function_cache,
                                      GIArgInfo *arg_info)
{
327 328 329 330
    arg_cache->in_marshaler = _pygi_marshal_in_error;
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return FALSE;
331 332 333 334 335 336
}

static inline boolean
_arg_cache_generate_metadata_in(PyGIArgCache *arg_cache,
                                PyGIFunctionCache *function_cache,
                                GIArgInfo *arg_info,
337
                                GITypeTag type_tag)
338 339 340
{
    gboolean success = True;

341 342 343 344 345
    arg_cache->c_arg_index = i + function_cache->is_method;
    arg_cache->py_arg_index =
        function_info->n_in_args + function_cache->is_method;

    function_info->n_in_args++;
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
           success = _arg_cache_generate_metadata_in_void(arg_cache,
                                                          function_cache,
                                                          arg_info);
           break;
       case GI_TYPE_TAG_BOOLEAN:
           success = _arg_cache_generate_metadata_in_boolean(arg_cache,
                                                             function_cache,
                                                             arg_info);
           break;
       case GI_TYPE_TAG_INT8:
           success = _arg_cache_generate_metadata_in_int8(arg_cache,
                                                          function_cache,
                                                          arg_info);
           break;
       case GI_TYPE_TAG_UINT8:
           success = _arg_cache_generate_metadata_in_uint8(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_INT16:
           success = _arg_cache_generate_metadata_in_uint16(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_UINT16:
           success = _arg_cache_generate_metadata_in_uint16(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_INT32:
           success = _arg_cache_generate_metadata_in_int32(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_UINT32:
           success = _arg_cache_generate_metadata_in_uint32(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_INT64:
           success = _arg_cache_generate_metadata_in_int64(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_UINT64:
           success = _arg_cache_generate_metadata_in_uint64(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_FLOAT:
           success = _arg_cache_generate_metadata_in_float(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_DOUBLE:
           success = _arg_cache_generate_metadata_in_double(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_UNICHAR:
           success = _arg_cache_generate_metadata_in_unichar(arg_cache,
                                                             function_cache,
                                                             arg_info);
           break;
       case GI_TYPE_TAG_GTYPE:
           success = _arg_cache_generate_metadata_in_gtype(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_UTF8:
           success = _arg_cache_generate_metadata_in_utf8(arg_cache,
                                                          function_cache,
                                                          arg_info);
           break;
       case GI_TYPE_TAG_FILENAME:
           success = _arg_cache_generate_metadata_in_filename(arg_cache,
                                                              function_cache,
                                                              arg_info);
           break;
       case GI_TYPE_TAG_ARRAY:
           success = _arg_cache_generate_metadata_in_array(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_INTERFACE:
           success = _arg_cache_generate_metadata_in_interface(arg_cache,
                                                               function_cache,
                                                               arg_info);
           break;
       case GI_TYPE_TAG_GLIST:
           success = _arg_cache_generate_metadata_in_glist(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_GSLIST:
           success = _arg_cache_generate_metadata_in_gslist(arg_cache,
                                                            function_cache,
                                                            arg_info);
           break;
       case GI_TYPE_TAG_GHASH:
           success = _arg_cache_generate_metadata_in_ghash(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
       case GI_TYPE_TAG_ERROR:
           success = _arg_cache_generate_metadata_in_error(arg_cache,
                                                           function_cache,
                                                           arg_info);
           break;
    }

John (J5) Palmieri's avatar
John (J5) Palmieri committed
459
    function_cache->in_args =
460 461 462 463 464 465
        g_slist_append(function_cache->in_args, arg_cache);

    return success;
}

static inline boolean
John (J5) Palmieri's avatar
John (J5) Palmieri committed
466
_args_cache_generate(GIFunctionInfo *function_info,
467 468 469 470 471 472 473 474 475 476
                     PyGIFunctionCache *function_cache)
{
    for (i = 0; i < function_cache->n_args; i++) {
        PyGIArgCache *arg_cache;
        GIArgInfo *arg_info;

        /* must be an aux arg filled in by its owner so skip */
        if (function_cache->args_cache[i] != NULL)
            continue;

John (J5) Palmieri's avatar
John (J5) Palmieri committed
477
        arg_info =
478 479 480 481 482 483 484 485 486
            g_callable_info_get_arg ( (GICallableInfo *) function_info, i);

        arg_cache = function_cache->args_cache[i] = g_slice_new0(PyGIArgCache);
        arg_cache->direction = g_arg_info_get_direction (arg_info);
        type_info = g_base_info_get_type ( (GIBaseInfo *) arg_info);
        type_tag = g_type_info_get_tag (type_info);

        switch(direction) {
            case GI_DIRECTION_IN:
487 488 489 490 491 492
                _arg_cache_generate_metadata_in(arg_cache,
                                                function_cache,
                                                arg_info,
                                                type_tag);

                break;
493 494 495 496 497

            case GI_DIRECTION_OUT:
                function_info->n_out_args++;
 		    switch (type_tag) {
                    case GI_TYPE_TAG_...:
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
                        ac->out_marshaler = <type marshaling function pointer>
                        ac->cleanup = <type cleanup function pointer>
                        fc->out_args = g_slist_append(fc->out_args, ac);
                        break;
           }
        }
    }
}

PyGIFunctionCache *
_pygi_function_cache_new (GIFunctionInfo *function_info)
{
    PyGIFunction *fc = _init_function_cache(function_info);
    if (!_args_cache_generate(function_info, fc))
        goto err;

err:
    _pygi_function_cache_free(fc);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
517
    return NULL;
518
}