9e025b454effd8d90a0621fe2cbbf3658381c73a
[uffi.git] / src / mcl / objects.cl
1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          objects.cl
6 ;;;; Purpose:       UFFI source to handle objects and pointers
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: objects.cl,v 1.1 2002/04/04 05:02:27 desoi Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :uffi)
21
22
23 ;;;
24 ;;; Some MCL specific utilities
25 ;;;
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)))
29     (if mcl-type 
30       (ccl::mactype-record-size mcl-type)
31       (ccl:record-length (ccl:find-record-descriptor type t t)) ) ) ) ;error if not a record
32
33
34 ; trap macros don't work right directly in the macros  
35 (eval-when (:compile-toplevel :load-toplevel :execute)
36
37 (defun new-ptr (size)
38   (#_NewPtr size))
39
40 (defun dispose-ptr (ptr)
41   (#_DisposePtr ptr))
42
43 )
44
45 ;;;
46 ;;; Start of standard UFFI
47 ;;;
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))))))
54
55
56
57 (defmacro free-foreign-object (obj)
58   `(dispose-ptr ,obj))
59
60 (defmacro null-pointer-p (obj)
61  `(ccl:%null-ptr-p ,obj))
62
63
64 (defmacro make-null-pointer (type)
65   (declare (ignore type))
66   `(ccl:%null-ptr))
67
68
69 ;! need to check uffi update and see if :routine is the right context
70
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))
75          name)
76     (ecase get-or-set
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)))
80
81 (defmacro deref-pointer (ptr type)
82   `(,(accessor-symbol type :get) ,ptr))
83
84
85 (defmacro deref-pointer-set (ptr type value)
86   `(,(accessor-symbol type :set) ,ptr ,value))
87
88
89 (defsetf deref-pointer deref-pointer-set)
90
91
92 (defmacro pointer-address (obj)
93   `(ccl:%ptr-to-int ,obj))
94
95
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)
102         (push spec recs)))
103     (cond ((and simple recs)
104            `(ccl:%stack-block ,simple
105               (ccl:rlet ,recs
106                 ,@body)))
107           (simple `(ccl:%stack-block ,simple ,@body))
108           (recs `(ccl:rlet ,recs ,@body)))))
109
110
111 (defmacro with-foreign-object ((var type) &rest body)
112   `(with-foreign-objects ((,var ,type)) ,@body))