r2386: *** empty log message ***
[uffi.git] / uffi / objects.cl
1 ;;;; -*- Mode: 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.3 2002/08/23 19:21:54 kevin 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 (defmacro allocate-foreign-object (type &optional (size :unspecified))
23   "Allocates an instance of TYPE. If size is specified, then allocate
24 an array of TYPE with size SIZE. The TYPE parameter is evaluated."
25   (if (eq size :unspecified)
26       (progn
27         #+cmu
28         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
29         #+lispworks
30         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
31         #+allegro
32         `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c))
33       (progn
34         #+cmu
35         `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
36         #+lispworks
37         `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate) :nelems ,size)
38         #+allegro
39         `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
40       )
41   ))
42
43 (defmacro free-foreign-object (obj)
44   #+cmu
45   `(alien:free-alien ,obj)
46   #+lispworks
47   `(fli:free-foreign-object ,obj)
48   #+allegro
49   `(ff:free-fobject ,obj)
50   )
51
52 (defmacro null-pointer-p (obj)
53   #+lispworks `(fli:null-pointer-p ,obj)
54   #+allegro `(zerop ,obj)
55   #+cmu   `(alien:null-alien ,obj)
56   )
57
58 (defmacro size-of-foreign-type (type)
59   #+lispworks `(fli:size-of ,type)
60   #+allegro `(ff:sizeof-fobject ,type)
61   #+cmu   `(alien:alien-size ,type)
62   #+clisp   `(values (ffi:size-of ,type))
63   )
64
65
66 (defmacro make-null-pointer (type)
67   #+(or allegro cmu) (declare (ignore type))
68   
69   #+cmu `(system:int-sap 0)
70   #+allegro 0
71   #+lispworks `(fli:make-pointer :address 0 :type ,type)
72   )
73
74 (defmacro char-array-to-pointer (obj)
75   #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
76   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
77                                 :address (fli:pointer-address ,obj))
78   #+allegro obj
79   )
80
81 (defmacro deref-pointer (ptr type)
82   "Returns a object pointed"
83   #+(or cmu lispworks) (declare (ignore type))
84   #+cmu  `(alien:deref ,ptr)
85   #+lispworks `(fli:dereference ,ptr)
86   #+allegro `(ff:fslot-value-typed ,(convert-from-uffi-type type :deref) :c ,ptr)
87 )
88
89 #+lispworks ;; with LW, deref is a character
90 (defmacro ensure-char-character (obj)
91   obj
92   )
93
94 #+(or allegro cmu)
95 (defmacro ensure-char-character (obj)
96   `(code-char ,obj)
97   )
98   
99 #+lispworks
100 (defmacro ensure-char-integer (obj)
101  `(char-code ,obj))
102
103 #+(or allegro cmu)
104 (defmacro ensure-char-integer (obj)
105   obj
106   ) ;; (* :char) dereference is already an integer
107
108 (defmacro pointer-address (obj)
109   #+cmu
110   `(system:sap-int (alien:alien-sap ,obj))
111   #+lispworks
112   `(fli:pointer-address ,obj)
113   #+allegro
114   obj
115   )
116
117 ;; TYPE is evaluated.
118 (defmacro with-foreign-object ((var type) &rest body)
119   #-(or cmu lispworks) ; default version
120   `(let ((,var (allocate-foreign-object ,type)))
121     (unwind-protect
122          (progn ,@body)
123       (free-foreign-object ,var)))
124   #+cmu
125   (let ((obj (gensym)))
126     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
127        (let ((,var (alien:addr ,obj)))
128          ,@body)))
129   #+lispworks
130   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
131                                               (eval type) :allocate)))
132     ,@body)
133   )
134
135
136 (defmacro with-foreign-objects (bindings &rest body)
137   (if bindings
138       `(with-foreign-object ,(car bindings)
139         (with-foreign-objects ,(cdr bindings)
140           ,@body))
141       `(progn ,@body)))
142
143             
144