From: Hoehle, Joerg-C. <Joe...@t-...> - 2005-03-29 09:53:52
|
Hi, generally, please post a URL to the API documentation when asking advice for how to interface to a particular foreign function. The reference material is needed to answer. I searched and found http://www.sqlite.org/capi3ref.html#sqlite3_open Yaroslav Kavenchuk wrote: > (db (ffi:c-ptr ffi:c-pointer) :out :alloca)) >It's work. It's simple. Nice! Excellent!!! :) Great. It's a typical example of :out parameter >The second variant (only sqlite3-open): > (db ffi:c-pointer)) >(setq db (ffi:foreign-allocate ffi:c-pointer)) ^'/quote missing here >(sqlite3-open *db-name* (ffi:foreign-address db)) >(sqlite3-close (ffi:foreign-value db)) >How to make that sqlite3-close it was called as well as in the first >variant? Sorry, I don't understand the question. >I should free memory (db) in the first or second variant? Yes. More precisely, you should extract the value right after the call to open, then free the container. There's no need to repeat (ffi:foreign-value db) every time you need to access a DB function. You need to realize that it's only for OPEN that you need to provide a location where to put the second return value from sqlite_open(). After that, you should free this temporary storage. (let ((db foreign-allocate)) (cons (open3 filename db) ; benefit from left-to-right evaluation order (prog1 (foreign-value db) (foreign-free db))) The SQLite documentation states that the pointer is always filled, even in case of an error. As you have seen, :out parameters cover exactly this scenario. CLISP will stack-allocate a pointer for you. CLISP's FFI is quite expressive. The second variant looks more like what UFFI would look like. But even UFFI has a with-foreign-object macro that may do stack allocations on some implementations: (defun my-open (filename) (ffi:with-foreign-object (db 'c-pointer) ; no need for malloc (cons (sqlite-open filename db) (foreign-value db)))) But why use these cumbersome forms when :out does it for you? Regards, Jorg Hohle. |
From: Yaroslav K. <kav...@je...> - 2005-03-29 10:24:51
|
Hoehle, Joerg-Cyril: > generally, please post a URL to the API documentation when asking advice > for how to interface to a particular foreign function. The reference > material is needed to answer. Sorry, I shall be corrected. Thanks. >>How to make that sqlite3-close it was called as well as in the first >>variant? > > Sorry, I don't understand the question. Excuse my bad English. But you have fine answered it. Many thanks. >>I should free memory (db) in the first or second variant? > > Yes. More precisely, you should extract the value right after the call > to open, then free the container. There's no need to repeat > (ffi:foreign-value db) every time you need to access a DB function. Thanks. I did not know. > As you have seen, :out parameters cover exactly this scenario. CLISP > will stack-allocate a pointer for you. CLISP's FFI is quite expressive. <skip> > But why use these cumbersome forms when :out does it for you? You are right. Many thanks once again. Where still it is possible to learn about ffi except for impnotes? -- WBR, Yaroslav Kavenchuk. |