[clean-list] htoclean question

John van Groningen johnvg at cs.ru.nl
Wed Mar 30 17:57:05 MEST 2005


John Small wrote:
>..
>My question is can htoclean and the Clean runtime be modified
>to receive a returned C string copying it only once on
>the fly directly into the final Clean string within the garbaging
>collecting perimeter?

Yes, this is possible. If the result of the c function
is of type char* and the type of this function in Clean
is String, htoclean could be modified to generate code
that calls a function that converts the string. This would
require some extra code in htoclean, in the codegenerator
(file cginstructions.c) and the runtime system (istartup.a).

>I want to write a FFI to the MySQL database dynamic link library
>and I'm forced to wrap another C layer around it and handle the
>malloc/frees simply for this third buffer and performance suffers.

Instead of calling malloc/free each time, you could
preallocate a small buffer that is usually large enough,
and only use malloc when the string is larger than this
buffer. For small strings it probably takes more time
to allocate memory for it with malloc, than to copy the string.

Or you could do the copying in c, by first returning
the string pointer to Clean as an Int (and maybe also the length
of the string), then allocate a *{#Char} with createArray,
and pass these to a c function that overwrites the characters
of this string. You can force evaluation of this c call, by
yielding a 0, and then use a guard to test if the result is 0.

createArray will initialize the array. You can prevent this
by using:

create_uninitialized_string :: !Int -> .{#Char};
create_uninitialized_string n = code inline {
        create_array_  CHAR 0 1
    };

Regards,

John van Groningen


More information about the clean-list mailing list