[gambit-list] Getting char*, uchar*, short*, unsigned short*, ...
James Long
longster at gmail.com
Fri Apr 17 13:33:05 EDT 2009
I believe a better way to solve these issues is to create a generic
library for allocating C memory.
------------
(define alloc-u8
(c-lambda (int) u8*
"___result_voidstar = malloc(___arg1*sizeof(unsigned char));"))
(define u8*-ref
(c-lambda (u8* int) int
"___result = ___arg1[___arg2];"))
(define u8*-set!
(c-lambda (u8* int int) void
"___arg1[___arg2] = ___arg3;"))
(define (u8vector->u8* v)
(let* ((len (u8vector-length v))
(c-buffer (alloc-u8 len)))
(let loop (i 0)
(if (< i len)
(begin
(u8*-set! c-buffer i (u8vector-ref v i))
(loop (+ i 1)))
c-buffer))))
(define free
(c-lambda ((pointer void #f)) void "free((void*)___arg1);"))
-----------
void do_something_with_u8(unsigned char *data) {
...
}
(define %%do-something-with-u8
(c-lambda ((pointer unsigned-char)) void "do_something_with_u8"))
(define (do-something-with-u8 v)
(let ((buf (u8vector->u8* v)))
(%%do-something-with-u8 buf)
(free buf)))
----------------
I'm working on automatically generating this interface for all the
possible types. This lets you access all FFI's without the need to
change C code. Unfortunately, it requires allocation for every call,
but there are ways to optimize this. That code is completely untested
and just wanted to give you the idea.
- James
More information about the Gambit-list
mailing list