anchor.diff:     #:%mem-ref
      ;#:make-shareable-byte-vector
      ;#:with-pointer-to-vector-data
  -   #:foreign-var-ptr))
  +   #:foreign-var-ptr
  +   #:defcfun-helper))
   
   (in-package #:cffi-sys)
   
  +;; Helper package where some foreign functions are kept.
  +(defpackage #:cffi-sys-ff)
  +
   ;;;# Basic Pointer Operations
  -
  -;; TODO: figure out if there might be any issue with using
  -;; integers as pointers directly..
   
   (defun pointerp (ptr)
     "Return true if PTR is a foreign pointer."
     (integerp ptr))
  -  ;(ff:foreign-pointer-p ptr))
   
   (defun null-ptr ()
     "Return a null pointer."
  -  ;(ff:make-foreign-pointer :foreign-address 0))
     0)
   
   (defun null-ptr-p (ptr)
     "Return true if PTR is a null pointer."
  -  ;(zerop (ff:foreign-pointer-address ptr)))
     (zerop ptr))
   
   (defun inc-ptr (ptr offset)
     "Return a pointer pointing OFFSET bytes past PTR."
  -  ;(ff:make-foreign-pointer
  -  ; :foreign-address (+ (ff:foreign-pointer-address ptr) offset)))
     (+ ptr offset))
   
   ;;;# Allocation
  
positive.code:   #+macosx (concatenate 'string "_" name)
    #-macosx name)
  
  ;; FIXME: ff:get-entry-point will also grab functions
  (defun foreign-var-ptr (name)
    "Return a pointer pointing to the foreign variable NAME."
    (nth-value 0 (ff:get-entry-point (convert-external-name name))))
negative.code: 
  (defun %foreign-type-size (type-keyword)
    "Return the size in bytes of a foreign type."
    (ff:sizeof-fobject (convert-foreign-type type-keyword)))
  
  (defun %foreign-type-alignment (type-keyword)
    (ff::sized-ftype-prim-align
     (ff::iforeign-type-sftype
      (ff:get-foreign-type
       (convert-foreign-type type-keyword)))))
  
  (defun foreign-funcall-type-and-args (args)
    "Returns a list of types, list of args and return type."
    (let ((return-type :void))
      (loop for (type arg) on args by #'cddr
         if arg collect (convert-foreign-type type :funcall) into types
            and collect arg into fargs
         else do (setf return-type (convert-foreign-type type :funcall))
         finally (return (values types fargs return-type)))))
  
  (defun convert-to-lisp-type (type)
    (if (equal '(* :void) type)
        '(* :void)
        (ecase type
          ((:char           
            :unsigned-char) 'unsigned-byte)
          ((:short
            :unsigned-short
            :int
            :unsigned-int
            :long
            :unsigned-long) 'integer)
          (:float 'single-float)
          (:double 'double-float)
          (:foreign-address :foreign-address)
          (:void 'null))))
  
  (defun foreign-allegro-type (type)
    (if (eq type :foreign-address)
        nil
        type))
  
  (defun allegro-type-pair (type)
    (list (foreign-allegro-type type)
          (convert-to-lisp-type type)))
  
  #+ignore
  (defun note-named-foreign-function (symbol name types rettype)
    "Give Allegro's compiler a hint to perform a direct call."
    `(eval-when (:compile-toplevel :load-toplevel :execute)
       (setf (get ',symbol 'system::direct-ff-call)
             (list '(,name :language :c)
                   t  ; callback
                   :c ; convention
                   ;; return type '(:c-type lisp-type)
                   ',(allegro-type-pair (convert-foreign-type rettype :funcall))
                   ;; arg types '({(:c-type lisp-type)}*)
                   '(,@(loop for type in types
                             collect (allegro-type-pair
                                      (convert-foreign-type type :funcall))))
                   nil ; arg-checking
                   ff::ep-flag-never-release))))
  
  (defmacro %foreign-funcall (name &rest args)
    (multiple-value-bind (types fargs rettype)
        (foreign-funcall-type-and-args args)
      `(system::ff-funcall
        (load-time-value (excl::determine-foreign-address
                          '(,name :language :c)
                          ff::ep-flag-never-release
                          nil ; method-index
                          ))
        ;; arg types {'(:c-type lisp-type) argN}*
        ,@(mapcan (lambda (type arg)
                    `(',(allegro-type-pair type) ,arg))
                  types fargs)
        ;; return type '(:c-type lisp-type)
        ',(allegro-type-pair rettype))))
  
  (defun defcfun-helper (name rettype args types)
    "Return two 2 values for DEFCFUN. A prelude form and a caller form."
    (let ((ff-name (intern (format nil "~A%%~A" (length args) name)
                           '#:cffi-sys-ff)))
      (values
       `(ff:def-foreign-call (,ff-name ,name)
            ,(mapcar (lambda (ty) 
                       (let ((allegro-type (convert-foreign-type ty)))
                         (list (gensym) allegro-type
                               (convert-to-lisp-type allegro-type))))
                     types)
          :returning ,(allegro-type-pair
                       (convert-foreign-type rettype :funcall))
          :call-direct t
          :arg-checking nil
          :strings-convert nil)
       `(,ff-name ,@args))))
  
  ;;;# Loading Foreign Libraries
  
  (defun %load-foreign-library (name)
    "Load the foreign library NAME."
    #+macosx ; At least ACL 6.2 sets this to '("dylib") only.. not good.
    (let ((excl::*load-foreign-types* '("dylib" "so" "bundle")))
      (load name))
    #-macosx
  