6a33f9b1cc04a9b145e6c27924ad13ddbef7d4e0
[uffi.git] / src / objects.cl
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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.13 2002/03/21 16:47:46 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."
25   (if (eq size :unspecified)
26       (progn
27         #+cmu
28         `(alien:make-alien ,(convert-from-uffi-type 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 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 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
59 (defmacro make-null-pointer (type)
60   #+(or allegro cmu) (declare (ignore type))
61   
62   #+cmu `(system:int-sap 0)
63   #+allegro 0
64   #+lispworks `(fli:make-pointer :address 0 :type ,type)
65   )
66
67 (defmacro char-array-to-pointer (obj)
68   #+cmu `(alien:cast ,obj (* (alien:unsigned 8)))
69   #+lispworks `(fli:make-pointer :type '(:unsigned :char)
70                                 :address (fli:pointer-address ,obj))
71   #+allegro obj
72   )
73
74 (defmacro deref-pointer (ptr type)
75   "Returns a object pointed"
76   #+(or cmu lispworks) (declare (ignore type))
77   #+cmu  `(alien:deref ,ptr)
78   #+lispworks `(fli:dereference ,ptr)
79   #+allegro `(ff:fslot-value-typed ,type :c ,ptr)
80 )
81
82 #+lispworks ;; with LW, deref is a character
83 (defmacro ensure-char-character (obj)
84   obj
85   )
86
87 #+(or allegro cmu)
88 (defmacro ensure-char-character (obj)
89   `(code-char ,obj)
90   )
91   
92 #+lispworks
93 (defmacro ensure-char-integer (obj)
94  `(char-code ,obj))
95
96 #+(or allegro cmu)
97 (defmacro ensure-char-integer (obj)
98   obj
99   ) ;; (* :char) dereference is already an integer
100
101 (defmacro pointer-address (obj)
102   #+cmu
103   `(system:sap-int (alien:alien-sap ,obj))
104   #+lispworks
105   `(fli:pointer-address ,obj)
106   #+allegro
107   obj
108   )
109
110 ;; Simple first pass. Will later create optimized routines for
111 ;; various platforms.
112 (defmacro with-foreign-object ((var type) &rest body)
113   #+allegro
114   `(let ((,var (allocate-foreign-object ,type)))
115     (unwind-protect
116          (progn ,@body)
117       (free-foreign-object ,var)))
118   #+cmu
119   `(alien:with-alien ((,var ,(convert-from-uffi-type type :allocate)))
120     (setq ,var (alien:addr ,var))
121     ,@body)
122   #+lispworks
123   `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type type :allocate)))
124     ,@body)
125   )
126
127
128 (defmacro with-foreign-objects (bindings &rest body)
129   (if bindings
130       `(with-foreign-object ,(car bindings)
131         (with-foreign-objects ,(cdr bindings)
132           ,@body))
133       `(progn ,@body)))
134
135             
136