Lisp HUG Maillist Archive

Special variables in FLI

I have a DLL that I'm interfacing with (obviously), during the
lifetime of which, I must initialize a global variables, use it and
free it. Simple enough. All C functions take this global pointer as
the first parameter. So, I quickly whipped this up:

(defvar *gp*) ; global pointer

(define-foreign-function (init-gp "init_gp")
    ((gp-pointer (:pointer (:pointer gp-type))))
  :result-type :boolean)

(define-foreign-function (free-gp "free_gp")
    ((:constant *gp* (:pointer gp-type)))
  :result-type :boolean)

(defmacro with-gp (&body body)
  `(let ((*gp* (allocate-foreign-object :type '(:pointer gp-type))))
     (when (init-gp (make-pointer :address (pointer-address *gp*)
:type 'gp-type))
       ,@body
       (free-gp))))

It should be noted that init-gp *does* take a ** pointer (it allocates
the structure internally and returns it through the pointer). All
other functions just take a * pointer - the one returned by the init
function.

This compiles just fine, but doesn't quite work. The body executes
just fine, which leads me to believe that *gp* was allocated
successfully, a pointer to it created and the init-gp function did its
magic well. However, when free-gp is called, I get an #<unbound> error
(can't coerce #<unbound> to foreign type (:pointer gp-type)).

Are special variables able to be used like this with the FLI? Perhaps
there is something else I am doing here that is causing me grief. Any
advice is appreciated. Thanks!

Jeff M.

-- 
http://www.retrobyte.org
mailto:massung@gmail.com


Re: Special variables in FLI

Just to followup with a solution for anyone else that runs into this... Doing a
macroexpand reveals that :CONSTANT forces a LOAD-TIME-VALUE form, which of
course results in *gp* being unbound. However, all is good by using the
:LAMBDA-LIST argument to the foreign function definition and passing the
argument last (&optional-ly).

Jeff M.


Updated at: 2020-12-10 08:54 UTC