Subject: [clisp-list] FFI:foreign-address function

From: "Hoehle, Joerg-Cyril" <[email-masked]>
Date: Tue Mar 11 00:22:02 2003

Sam Steingold suggested:
>(defun foreign-address (o)
>  (ffi::unsigned-foreign-address (ffi::foreign-address-unsigned o)))

Don't do this. It does not preserve EQ-ness of foreign-address objects.

EQ-ness is important because of
1. VALIDP
2. MARK-INVALID-FOREIGN
3. sharing
4. FINALIZE

In fact, I have a whole concept sitting in my brain about sharing FOREIGN-POINTER and FOREIGN-ADDRESS objects, with the ability to mark all related objects as invalid with a single MARK-INVALID-FOREIGN, and another concept not yet well thought out of bringing some of them back to life again, e.g. when restarting an image containing functions from a foreign library.

The Amiga-CLISP FFI has shared foreign library bases for 7 years now, resurrecting foreign variables and functions based on this sharing of address objects. In fact, the Amiga is why CLISP got two separate FOREIGN-POINTER and FOREIGN-ADDRESS objects, where the latter encapsulates the former with the addition of a constant offset.

My WITH-FOREIGN-STRING and -OBJECT macros (or rather, their functional equivalents) already mark the base pointers as stale when scope exits, preventing use of stale memory addresses.

I recommend MARK-INVALID-FOREIGN to people using the new dynamic memory allocation functions FFI:DEEP-MALLOC and FFI:SIMPLE-CALLOC. This will safe you from C diseases of using deallocated memory via dangling pointers (see my malloc+free patch in sourceforge). If somebody asks, I'll explain why FOREIGN-FREE doesn't do this automatically.

Regards,
	Jorg Hohle.