r2280: *** empty log message ***
[uffi.git] / uffi / mcl / objects.cl
diff --git a/uffi/mcl/objects.cl b/uffi/mcl/objects.cl
new file mode 100644 (file)
index 0000000..f959dc1
--- /dev/null
@@ -0,0 +1,113 @@
+;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
+;;;; *************************************************************************
+;;;; FILE IDENTIFICATION
+;;;;
+;;;; Name:          objects.cl
+;;;; Purpose:       UFFI source to handle objects and pointers
+;;;; Programmers:   Kevin M. Rosenberg and John DeSoi
+;;;; Date Started:  Feb 2002
+;;;;
+;;;; $Id: objects.cl,v 1.1 2002/08/02 14:39:12 kevin Exp $
+;;;;
+;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
+;;;; and John DeSoi
+;;;;
+;;;; UFFI users are granted the rights to distribute and use this software
+;;;; as governed by the terms of the Lisp Lesser GNU Public License
+;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
+;;;; *************************************************************************
+
+(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
+(in-package :uffi)
+
+
+;;;
+;;; Some MCL specific utilities
+;;;
+(defun foreign-object-size (type)
+  "Returns the size for the specified mcl type or record type"
+  (let ((mcl-type (ccl:find-mactype type nil t)))
+    (if mcl-type 
+      (ccl::mactype-record-size mcl-type)
+      (ccl::record-descriptor-length (ccl:find-record-descriptor type t t)) ) ) ) ;error if not a record
+
+
+; trap macros don't work right directly in the macros  
+(eval-when (:compile-toplevel :load-toplevel :execute)
+
+(defun new-ptr (size)
+  (#_NewPtr size))
+
+(defun dispose-ptr (ptr)
+  (#_DisposePtr ptr))
+
+)
+
+;;;
+;;; Start of standard UFFI
+;;;
+(defmacro allocate-foreign-object (type &optional (size :unspecified))
+  "Allocates an instance of TYPE. If size is specified, then allocate
+an array of TYPE with size SIZE."
+  (if (eq size :unspecified)
+    `(new-ptr ,(foreign-object-size (convert-from-uffi-type type :allocation)))
+    `(new-ptr ,(* size (foreign-object-size (convert-from-uffi-type type :allocation))))))
+
+
+
+(defmacro free-foreign-object (obj)
+  `(dispose-ptr ,obj))
+
+(defmacro null-pointer-p (obj)
+ `(ccl:%null-ptr-p ,obj))
+
+
+(defmacro make-null-pointer (type)
+  (declare (ignore type))
+  `(ccl:%null-ptr))
+
+
+;! need to check uffi update and see if :routine is the right context
+
+(defun accessor-symbol (type get-or-set)
+  "Returns the symbol used to access the foreign type."
+  (let* ((mcl-type (convert-from-uffi-type (eval type) :routine))
+         (mac-type (ccl:find-mactype mcl-type))
+         name)
+    (ecase get-or-set
+      (:get (setf name (ccl::mactype-get-function mac-type)))
+      (:set (setf name (ccl::mactype-set-function mac-type))))
+    (find-symbol (symbol-name name) :ccl)))
+
+(defmacro deref-pointer (ptr type)
+  `(,(accessor-symbol type :get) ,ptr))
+
+
+(defmacro deref-pointer-set (ptr type value)
+  `(,(accessor-symbol type :set) ,ptr ,value))
+
+
+(defsetf deref-pointer deref-pointer-set)
+
+
+(defmacro pointer-address (obj)
+  `(ccl:%ptr-to-int ,obj))
+
+
+(defmacro with-foreign-objects (bindings &rest body)
+  (let ((simple nil) (recs nil) type)
+    (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
+      (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
+      (if (ccl:mactype-p type)
+        (push (list (first spec) (foreign-object-size type)) simple)
+        (push spec recs)))
+    (cond ((and simple recs)
+           `(ccl:%stack-block ,simple
+              (ccl:rlet ,recs
+                ,@body)))
+          (simple `(ccl:%stack-block ,simple ,@body))
+          (recs `(ccl:rlet ,recs ,@body)))))
+
+
+(defmacro with-foreign-object ((var type) &rest body)
+  `(with-foreign-objects ((,var ,type)) ,@body))