[Ecls-list] :cstring in ffi

Daniel Crettol crad at sof.ch
Fri Jun 10 13:57:50 UTC 2005


Hi there.

I got a problem when trying ffi interface with SDL.

The following function caused me a lot of trouble.

(defun render-text-solid (font text fg-red fg-green fg-blue)
  (declare (fixnum fg-red fg-green fg-blue))
  (ffi:c-inline (font text fg-red fg-green fg-blue)
	    (:pointer-void :cstring :fixnum :fixnum :fixnum) :object "
SDL_Color c = { #2, #3, #4 };
SDL_Surface *s = TTF_RenderText_Solid(#0, #1, c);
cl_object rtype = @'#.(ffi::%convert-to-ffi-type 'sdl:surface);
cl_object rsize = MAKE_FIXNUM(sizeof(SDL_Surface));
@(return) = ecl_make_foreign_data(rtype, rsize, s);
" :side-effects t :one-liner nil))

The compiler generated the following code:

....	{
	cl_object V6;
	
SDL_Color c = { fix(V3), fix(V4), fix(V5) };
SDL_Surface *s = TTF_RenderText_Solid(ecl_foreign_data_pointer_safe(V1),
ecl_string_pointer_safe(V2), c);
cl_object rtype = VV[29];
cl_object rsize = MAKE_FIXNUM(sizeof(SDL_Surface));
V6= ecl_make_foreign_data(rtype, rsize, s);

	value0=V6; NVALUES=1;
	return value0;}
	}

After investigating a while, I found that the cstring was not always 
null terminated. I don't know exaclty why, but I applied the following 
patch and now it works.

--- ffi.d       23 May 2005 12:39:21 -0000      1.7
+++ ffi.d       10 Jun 2005 20:39:15 -0000
@@ -47,6 +47,7 @@
 {
        if (type_of(f) != t_string)
                FEwrong_type_argument(@'string', f);
+       f->string.self[f->string.fillp] = 0;    /* force 0 terminator!
*/
        return f->string.self;
 }
 
BTW, is there a reason why the compiler uses the si_foreign_data_recast 
function instead of compiling directly the type with the 
ecl_make_foreign_data function call ?

Thanks a lot and have fun...






More information about the ecl-devel mailing list