r2185: *** empty log message ***
[uffi.git] / src / 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.19 2002/06/28 21:08:00 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   )
63
64
65 (defmacro make-null-pointer (type)
66   #+(or allegro cmu) (declare (ignore type))
67   
68   #+cmu `(system:int-sap 0)
69   #+allegro 0
70   #+lispworks `(fli:make-pointer :address 0 :type ,type)
71   )
72
73 (defmacro char-array-to-pointer (obj)
74   #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
75   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
76                                 :address (fli:pointer-address ,obj))
77   #+allegro obj
78   )
79
80 (defmacro deref-pointer (ptr type)
81   "Returns a object pointed"
82   #+(or cmu lispworks) (declare (ignore type))
83   #+cmu  `(alien:deref ,ptr)
84   #+lispworks `(fli:dereference ,ptr)
85   #+allegro `(ff:fslot-value-typed ,(convert-from-uffi-type type :deref) :c ,ptr)
86 )
87
88 #+lispworks ;; with LW, deref is a character
89 (defmacro ensure-char-character (obj)
90   obj
91   )
92
93 #+(or allegro cmu)
94 (defmacro ensure-char-character (obj)
95   `(code-char ,obj)
96   )
97   
98 #+lispworks
99 (defmacro ensure-char-integer (obj)
100  `(char-code ,obj))
101
102 #+(or allegro cmu)
103 (defmacro ensure-char-integer (obj)
104   obj
105   ) ;; (* :char) dereference is already an integer
106
107 (defmacro pointer-address (obj)
108   #+cmu
109   `(system:sap-int (alien:alien-sap ,obj))
110   #+lispworks
111   `(fli:pointer-address ,obj)
112   #+allegro
113   obj
114   )
115
116 ;; TYPE is evaluated.
117 (defmacro with-foreign-object ((var type) &rest body)
118   #-(or cmu lispworks) ; default version
119   `(let ((,var (allocate-foreign-object ,type)))
120     (unwind-protect
121          (progn ,@body)
122       (free-foreign-object ,var)))
123   #+cmu
124   (let ((obj (gensym)))
125     `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
126        (let ((,var (alien:addr ,obj)))
127          ,@body)))
128   #+lispworks
129   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
130                                               (eval type) :allocate)))
131     ,@body)
132   )
133
134
135 (defmacro with-foreign-objects (bindings &rest body)
136   (if bindings
137       `(with-foreign-object ,(car bindings)
138         (with-foreign-objects ,(cdr bindings)
139           ,@body))
140       `(progn ,@body)))
141
142             
143