When a foreign function declared as
int open_db(char *path, Db **db)
creates internally an instance of Db and assigns the pointer to *db
, what is the most efficient way to handle this from Chez Scheme?
The only thing I could come up with was allocating memory for a C-pointer using foreign-alloc, passing the address to it, copying the address and then immediately freeing this memory:
(define open_db (foreign-procedure "open_db" (string void*) int))
(define-record-type db (fields (mutable ptr)))
(define (open-db path)
(let ((pptr (foreign-alloc (foreign-sizeof 'void*))))
(open_db path pptr)
(let ((ptr (foreign-ref 'void* pptr 0)))
(foreign-free pptr)
(make-db ptr))))
Is there a way to avoid this temporary allocation of memory for the pointer?