Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 142 lines (121 sloc) 3.448 kb
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

#ifndef SDL_PERL_HELPER_H
#define SDL_PERL_HELPER_H

#include <SDL.h>
#include "SDL_thread.h"
#ifndef Uint32
#define Uint32 uint32_t
#endif

PerlInterpreter * perl = NULL;

void *bag2obj( SV *bag )
{
    void *obj = NULL;

    if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
    {
        void **pointers = (void **)INT2PTR(void *, SvIV((SV *)SvRV( bag )));
        obj = (void *)(pointers[0]);
    }
    
    return obj;
}

SV *obj2bag( int size_ptr, void *obj, char *CLASS )
{
    SV * objref;
    void** pointers;
    Uint32 *threadid;

    objref = newSV( size_ptr );
    pointers = safemalloc(3 * sizeof(void*));
    pointers[0] = (void*)obj;
    pointers[1] = (void*)PERL_GET_CONTEXT;
    threadid = (Uint32 *)safemalloc(sizeof(Uint32));
    *threadid = SDL_ThreadID();
    pointers[2] = (void*)threadid;
    sv_setref_pv( objref, CLASS, (void *)pointers);
    return objref;
}

SV *cpy2bag( void *object, int p_size, int s_size, char *package )
{
    void** pointers;
    Uint32 *threadid;
    SV* ref;
    SV* a;
    void *copy;
    
    ref = newSV( p_size );
    copy = safemalloc( s_size );
    memcpy( copy, object, s_size );

    pointers = safemalloc(3 * sizeof(void*));
    pointers[0] = (void*)copy;
    pointers[1] = (void*)PERL_GET_CONTEXT;
    threadid = (Uint32 *)safemalloc(sizeof(Uint32));
    *threadid = SDL_ThreadID();
    pointers[2] = (void*)threadid;
    a = sv_setref_pv(ref, package, (void *)pointers);
    return a;
}

void objDESTROY(SV *bag, void (* callback)(void *object))
{
    if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
    {
        void** pointers = (void**)INT2PTR(void *, SvIV((SV *)SvRV( bag )));
        void* object = pointers[0];
        Uint32 *threadid = (Uint32*)(pointers[2]);
        
        if(PERL_GET_CONTEXT == pointers[1]
        && *threadid == SDL_ThreadID())
        {
            pointers[0] = NULL;
            if(object)
                callback(object);
            safefree(threadid);
            safefree(pointers);
        }
    }
}

SV *_sv_ref( void *object, int p_size, int s_size, char *package )
{
    SV *ref;
    void *copy;
    void** pointers;
    Uint32 *threadid;

    ref = newSV( p_size );
    copy = safemalloc( s_size );
    memcpy( copy, object, s_size );
    pointers = safemalloc(3 * sizeof(void*));
    pointers[0] = (void*)copy;
    pointers[1] = (void*)perl;
    threadid = (Uint32 *)safemalloc(sizeof(Uint32));
    *threadid = SDL_ThreadID();
    pointers[2] = (void*)threadid;

    return sv_setref_pv(ref, package, (void *)pointers);
}

void _svinta_free(Sint16* av, int len_from_av_len)
{
    if( av == NULL )
        return;
    safefree( av ); /* we only need to free the malloc'd array. It is one block. */
    av = NULL;
}

Sint16* av_to_sint16 (AV* av)
{
    int len = av_len(av);
    if( len != -1)
    {
        int i;
        Sint16* table = (Sint16 *)safemalloc(sizeof(Sint16)*(len+1));
        for ( i = 0; i < len+1 ; i++ )
        {
            SV ** temp = av_fetch(av,i,0);
            if( temp != NULL )
                table[i] = (Sint16) SvIV ( *temp );
            else
                table[i] = 0;
        }
        return table;

    }
    return NULL;
}

void _int_range( int *val, int min, int max )
{
    if( *val < min )
       *val = min;
    else if ( *val > max )
       *val = max;
}

#endif
Something went wrong with that request. Please try again.