RE: Using shared objects
Ron Weidner wrote:
> }}}
<eucode>
> atom dogLib -- used like a pointer
> -- You would need to use "./dog.so" here if dog.so
> -- is actually in the current directory or you could
> -- use ./somedir/dog.so if the .so is nested deeper
> -- relative to the current directory
>
> -- load the dll and return a pointer to its mem location
> dogLib = open_dll("./dog.so")
>
> -- This is like porting the C header files to Euphoria
> constant
> mush_ = define_c_proc(dogLib, "mush", {C_POINTER, C_INT}),
> bark_ = define_c_func(dogLib, "bark", {}, C_INT)
>
> global procedure mush(sequence p)
> atom lp -- used as a pointer
> sequence lps -- used as an array of pointers
> integer len -- len of p where p is expected to be a sequence of
> sequences
> len = length(p)
>
> -- create a pointer to a block of mem 4 bytes long * the len of p
> -- the reason for 4 bytes is because that's the size of a pointer
> lp = allocate(len * 4)
>
> -- delete any garbage in the memory locations
> lps = repeat(0, len)
</eucode>
{{{
lps = repeat(0,len) doesn't delete any garbage in the memory location, it
creates
a euphoria sequence of len, filled with 0's. In other words, if len is 4, you
get lps = {0,0,0,0}
> -- start making pointers to each sequence in p
> for loop = 1 to len do
>
> -- allocate enough mem for p[loop] + 1, strcpy the val
> -- in p[loop] & "\0" to that location and return a
> -- pointer to it.
> lps[loop] = allocate_string(p[loop])
>
> end for
>
> -- poke4(atom where_p, object what)
> -- put the 4 pointers just created into the lp mem location
> poke4(lp, lps)
>
> -- call the c function (procedure since the C return val is void)
> c_proc(mush_, {lp, len})
>
> -- look ma... mem leaks in euphoria are posible. :)
> for loop = 1 to len do
> free(lps[loop]) -- free the strings
> end for
Memory Leaks are possible in Euphoria, yes. But Memory leaks are only
possible during the execution of Euphoria, at the end of the execution of
your Euphoria program, any memory allocated BY the Interpreter, will get
freed automatically. Any memory allocated by External C libraries, must
maintain their own memory cleanup, as Euphoria has no clue about the
memory allocated by them.
> free(lp) -- free the mem storing pointers to the strings
> end procedure -- how'd I do??
>
> global function bark()
> return c_func(bark_, {})
> end function
>
> <font color="#330033">
</font>
Other then the two points I made, you are exactly right on the money for the
rest of the stuff.
Mario Steele
http://enchantedblade.trilake.net
Attaining World Dominiation, one byte at a time...
|
Not Categorized, Please Help
|
|