[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] "closing" pointers returned by C when they are not neede

From: Alejandro Forero Cuervo
Subject: [Chicken-users] "closing" pointers returned by C when they are not needed
Date: Mon, 10 May 2004 14:51:11 -0500
User-agent: Mutt/


I'm using foreign-lambda*  to use the functionality  of the iconv
library.   My  program needs  to  read  files in  many  different
charsets and convert them all to a single one (usually UTF-8).

iconv_open(3) returns a C-pointer (iconv_t is void *) that I need
to pass  back to the  Scheme programmer  so he can  call iconv(3)
with it.  When the C-pointer  is no longer needed, iconv_close(3)
should be called on it deallocate it.

Here is what I'm doing:

> (declare (unit iconv))
> (declare (foreign-declare "#include <iconv.h>\n"))
> (define iconv-open
>   (foreign-lambda* c-pointer ((c-string from) (c-string to))
>     "iconv_t result;"
>     "result = iconv_open(from, to);"
>     "if (result == (iconv_t) -1)"
>     "  return(NULL);"
>     "return(result);"))
> (define iconv
>   (foreign-lambda* c-string* ((c-pointer cd) (c-string src))
>     "size_t srclen = strlen(src), dstlen = 2 * srclen;"
>     "char *dst = malloc(dstlen), *dstcpy = dst;"
>     "iconv(cd, &src, &srclen, &dstcpy, &dstlen);"
>     "if (dstlen > 0)"
>     "  *dstcpy = 0;"
>     "else"
>     "  return(NULL);"
>     "return(dst);"))

(Yes, iconv should  really be smarter: the "dstlen =  2 * srclen"
is ugly,  there should be  some proper  way to report  the error,

How can I let the GC code know that it should call iconv_close(3)
when the C-pointer (returned by foreign-lambda* in iconv-open) is
no longer required?


---=(  Comunidad de Usuarios de Software Libre en Colombia  )=---               
---=( )=--=( address@hidden  )=---                   

Attachment: signature.asc
Description: Digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]