1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: UFFI source to handle objects and pointers
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: objects.cl,v 1.1 2002/04/04 05:02:27 desoi Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 ;;; Some MCL specific utilities
26 (defun foreign-object-size (type)
27 "Returns the size for the specified mcl type or record type"
28 (let ((mcl-type (ccl:find-mactype type nil t)))
30 (ccl::mactype-record-size mcl-type)
31 (ccl:record-length (ccl:find-record-descriptor type t t)) ) ) ) ;error if not a record
34 ; trap macros don't work right directly in the macros
35 (eval-when (:compile-toplevel :load-toplevel :execute)
40 (defun dispose-ptr (ptr)
46 ;;; Start of standard UFFI
48 (defmacro allocate-foreign-object (type &optional (size :unspecified))
49 "Allocates an instance of TYPE. If size is specified, then allocate
50 an array of TYPE with size SIZE."
51 (if (eq size :unspecified)
52 `(new-ptr ,(foreign-object-size (convert-from-uffi-type type :allocation)))
53 `(new-ptr ,(* size (foreign-object-size (convert-from-uffi-type type :allocation))))))
57 (defmacro free-foreign-object (obj)
60 (defmacro null-pointer-p (obj)
61 `(ccl:%null-ptr-p ,obj))
64 (defmacro make-null-pointer (type)
65 (declare (ignore type))
69 ;! need to check uffi update and see if :routine is the right context
71 (defun accessor-symbol (type get-or-set)
72 "Returns the symbol used to access the foreign type."
73 (let* ((mcl-type (convert-from-uffi-type (eval type) :routine))
74 (mac-type (ccl:find-mactype mcl-type))
77 (:get (setf name (ccl::mactype-get-function mac-type)))
78 (:set (setf name (ccl::mactype-set-function mac-type))))
79 (find-symbol (symbol-name name) :ccl)))
81 (defmacro deref-pointer (ptr type)
82 `(,(accessor-symbol type :get) ,ptr))
85 (defmacro deref-pointer-set (ptr type value)
86 `(,(accessor-symbol type :set) ,ptr ,value))
89 (defsetf deref-pointer deref-pointer-set)
92 (defmacro pointer-address (obj)
93 `(ccl:%ptr-to-int ,obj))
96 (defmacro with-foreign-objects (bindings &rest body)
97 (let ((simple nil) (recs nil) type)
98 (dolist (spec (reverse bindings)) ;keep order - macroexpands to let*
99 (setf type (convert-from-uffi-type (eval (nth 1 spec)) :allocate))
100 (if (ccl:mactype-p type)
101 (push (list (first spec) (foreign-object-size type)) simple)
103 (cond ((and simple recs)
104 `(ccl:%stack-block ,simple
107 (simple `(ccl:%stack-block ,simple ,@body))
108 (recs `(ccl:rlet ,recs ,@body)))))
111 (defmacro with-foreign-object ((var type) &rest body)
112 `(with-foreign-objects ((,var ,type)) ,@body))