[clean-list] Sub-arrays How do I manipulate an array pointer?

John van Groningen johnvg at cs.ru.nl
Wed Jun 7 17:45:15 MEST 2006


Matthew Bromberg wrote:
>Interesting, thanks I'll take a look at this.  I didn't see too much documentation on the garbage collector, or how one might control it.

The marking garbage collector can be enabled using a check box
in the Project Options dialogue. This garbage collector is usually
faster than the copying/compacting collector for programs with a lot
of unboxed data.

>The idea I had for passing function pointers would be to enable certain kinds of initialization or manipulation of data in an array from
>some kind of configuration held in clean. An example from wireless comm. would be say a function that computes a channel matrix, based
>on a whole bunch of configuration parameters, such as node positions velocities, terrain type, weather conditions etc.  call it hfunc(f, P) Once all the configuration information is processed however, the output can be thought of as a simple function of frequency or time or array index, etc.  So if the 'data structure' P has been computed  hfunc(f,P) is now just a function of f.

It is possible to export a Clean function to C using 'foreign export'. But
only integers can be passed in this way (I can add doubles if you like).
But by implementing a global variable using a CAF and some abc instructions,
function pointers can be passed as well. This trick is used in the C
interface for Object IO, see the module clCrossCall_12 (in Object IO/OS Windows).

>Thus the configuration information is essentially curried into the definition of the function.  Now I just pass that to the low level computation unit, who need not know anything about all that P stuff.  All of a sudden, the supposed stateless functional language  is carrying a lot of state information.  Probably the only way this is going to happen is to export it via some dll as you suggest, which, if general enough, may be the best way to actually interface software in Clean to the outside world.

It is not required to use a dll. Functions exported using 'foreign export'
can also be called from C functions called with ccall. But a G character
has to be added at the beginning of the string describing the parameters
of the ccall. For those ccalls the heap and stack pointer are saved in
global variables before the call to c, so that the heap and stack pointer
can be used if a function exported by 'foreign export' is called from C.

>Marco Kesseler wrote:
>>
>>On Jun 2, 2006, at 10:54 PM, Matthew Bromberg wrote:
>>
>>>
>>>It would be nice though to use Clean's array comprehensions to initialize or alter values held in a matrix.
>>>Are there any nice hacks for processing array data held in C but not managed by Clean? Can the
>>>garbage collector be told to leave an array alone?  I think it's possible already to return data from a
>>>C array, but it's copied as soon as it gets into Clean I believe. (At least it works that way for strings I'm told.)
>>
>>I have attached a small zip file with some experimental code for my "funlib" project. In "Files.icl" there is a "newStringFile" function that creates a memory mapped writable file for the characters in a string. The header is left out of the file, using a virtual memory trick. It avoids copying the string by passing the pointer to it as an Int to Clean. Then, some abc instructions are used to push this integer as a pointer on the a-stack and "cast" it to a String.
>>
>>Provided that you keep the string unique, you can write characters into this string in Clean itself, and these will "magically" end up in the file. As long as the string is located outside the Clean heap, the garbage collector will not touch it. For large strings this improves garbage collection times considerably, because Clean does not need to copy it around. Keeping the string unique is essential though, as otherwise you will end up with a copy of the string inside the heap...
>>
>>I believe that similar things can be done for unboxed arrays, but only if you use the marking garbage collector.

That is correct.

>>>
>>>Equivalently if I could have a C enabled function pointer from Clean that could be used to process raw data,
>>>that would be pretty useful.  In fact even if I could get very simple functions  like
>>>
>>>Int Int -> Int or
>>>Double -> Double
>>>
>>>from Clean into C it could be quite useful.  These functions would probably be curried with lots of configuration data.
>>
>>I am not 100% certain that I follow you here. It is possible to call clean functions from C, if you let the clean compiler generate a DLL, For a bit of info, see: http://www.xs4all.nl/~keslr/functional/clean/howto/cleandll.html

It is not required to use a DLL (see above) to call clean functions from c.

>>..

Kind regards,

John van Groningen


More information about the clean-list mailing list