I'm not yet sure what you mean by "scope"—if you mean dynamic extent, then it looks like CFFI can do stack allocation via the WITH-FOREIGN-OBJECT
macro. If you mean finalization—that is, garbage-collecting data when it's not reachable from the Lisp—then that's apparently outside the domain of CFFI, and into implementation-specific territory, or some other kind of library. Clozure CL, for example, has a facility called "termination" which can be used to free a foreign vector when the garbage collector goes into effect. For this to work, it's necessary to wrap the foreign object within a Lisp object, so that it can participate in the termination protocol. The foreign allocation system
in Allegro CL has a :FOREIGN-STATIC-GC option that appears to do something similar with a foreign-array thing that tracks the actual memory. Then there's this package called Trivial Garbage
, which I haven't used, but it's supposed to provide a "portable API to finalizers" for the various implementations.