1 ;;;; -*- Mode: 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.17 2002/04/06 19:53:08 kevin 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)))
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)
28 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation))
30 `(fli:allocate-foreign-object :type ,(convert-from-uffi-type type :allocate))
32 `(ff:allocate-fobject ,(convert-from-uffi-type type :allocate) :c))
35 `(alien:make-alien ,(convert-from-uffi-type (eval type) :allocation) ,size)
37 `(fli:allocate-foreign-object :type ,(convert-from-uffi-type type :allocate) :nelems ,size)
39 `(ff:allocate-fobject '(:array ,(convert-from-uffi-type (eval type) :allocate) ,(eval size)) :c)
43 (defmacro free-foreign-object (obj)
45 `(alien:free-alien ,obj)
47 `(fli:free-foreign-object ,obj)
49 `(ff:free-fobject ,obj)
52 (defmacro null-pointer-p (obj)
53 #+lispworks `(fli:null-pointer-p ,obj)
54 #+allegro `(zerop ,obj)
55 #+cmu `(alien:null-alien ,obj)
59 (defmacro make-null-pointer (type)
60 #+(or allegro cmu) (declare (ignore type))
62 #+cmu `(system:int-sap 0)
64 #+lispworks `(fli:make-pointer :address 0 :type ,type)
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))
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 ,(convert-from-uffi-type type :deref) :c ,ptr)
82 #+lispworks ;; with LW, deref is a character
83 (defmacro ensure-char-character (obj)
88 (defmacro ensure-char-character (obj)
93 (defmacro ensure-char-integer (obj)
97 (defmacro ensure-char-integer (obj)
99 ) ;; (* :char) dereference is already an integer
101 (defmacro pointer-address (obj)
103 `(system:sap-int (alien:alien-sap ,obj))
105 `(fli:pointer-address ,obj)
110 ;; TYPE is evaluated.
111 (defmacro with-foreign-object ((var type) &rest body)
112 #-(or cmu lispworks) ; default version
113 `(let ((,var (allocate-foreign-object ,type)))
116 (free-foreign-object ,var)))
118 (let ((obj (gensym)))
119 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
120 (let ((,var (alien:addr ,obj)))
123 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
124 (eval type) :allocate)))
129 (defmacro with-foreign-objects (bindings &rest body)
131 `(with-foreign-object ,(car bindings)
132 (with-foreign-objects ,(cdr bindings)