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.3 2002/08/23 19:21:54 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)
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))
66 (defmacro make-null-pointer (type)
67 #+(or allegro cmu) (declare (ignore type))
69 #+cmu `(system:int-sap 0)
71 #+lispworks `(fli:make-pointer :address 0 :type ,type)
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))
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)
89 #+lispworks ;; with LW, deref is a character
90 (defmacro ensure-char-character (obj)
95 (defmacro ensure-char-character (obj)
100 (defmacro ensure-char-integer (obj)
104 (defmacro ensure-char-integer (obj)
106 ) ;; (* :char) dereference is already an integer
108 (defmacro pointer-address (obj)
110 `(system:sap-int (alien:alien-sap ,obj))
112 `(fli:pointer-address ,obj)
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)))
123 (free-foreign-object ,var)))
125 (let ((obj (gensym)))
126 `(alien:with-alien ((,obj ,(convert-from-uffi-type (eval type) :allocate)))
127 (let ((,var (alien:addr ,obj)))
130 `(fli:with-dynamic-foreign-objects ((,var ,(convert-from-uffi-type
131 (eval type) :allocate)))
136 (defmacro with-foreign-objects (bindings &rest body)
138 `(with-foreign-object ,(car bindings)
139 (with-foreign-objects ,(cdr bindings)