Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2014 11:43
Show Gist options
  • Save anonymous/8897943 to your computer and use it in GitHub Desktop.
Save anonymous/8897943 to your computer and use it in GitHub Desktop.
/** To be compiled with gcc -shared -o libTestJulia.so libTest.c -fPIC -L/usr/lib/julia/ -ljulia -std=gnu99 **/
#include <julia.h>
#include <string.h>
#include <stdio.h>
// returns a jl_array_t pointer
void * testJulia( void *thisModule )
{
const char *str = "Hey it is me";
jl_gc_disable(); // disable GC so we don't have to deal with POP/PUSH GC
jl_value_t *msg = jl_pchar_to_string((char*)str, strlen(str));
// get type 'MyType' from module
jl_datatype_t *dtype = jl_get_global(thisModule, jl_symbol("MyType"));
// allocate N-element double array
const unsigned N = 131072000;
jl_value_t* array_type = jl_apply_array_type(jl_float64_type, 1);
jl_array_t* x = jl_alloc_array_1d(array_type, N);
// create 'uninitialized' type
jl_value_t *ret = jl_new_struct_uninit( dtype );
// fill in structure
jl_set_nth_field( ret, 0, msg );
jl_set_nth_field( ret, 1, jl_box_int64(231) );
jl_set_nth_field( ret, 2, x );
jl_gc_enable(); // very important before returning
return ret;
}
module MyModule
# sample structure to be filled by ccall
type MyType
msg::String
num::Int64
arr::Array
MyType() = new()
end
# the ccall will feed MyType
function test()
ret = ccall( (:testJulia, "./libTestJulia.so"), Any, (Ptr{Void},), (pointer_from_objref(MyModule)) )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment