1 ;;;; -*- Mode: ANSI-Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
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.11 2002/03/21 14:49:14 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."
25 (if (eq size :unspecified)
28 `(alien:make-alien ,(convert-from-uffi-type 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 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 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 deref-pointer (ptr type)
68 "Returns a object pointed"
69 #+(or cmu lispworks) (declare (ignore type))
70 #+cmu `(alien:deref ,ptr)
71 #+lispworks `(fli:dereference ,ptr)
72 #+allegro `(ff:fslot-value-typed ,type :c ,ptr)
75 #+lispworks ;; with LW, deref is a character
76 (defmacro ensure-char-character (obj)
81 (defmacro ensure-char-character (obj)
86 (defmacro ensure-char-integer (obj)
90 (defmacro ensure-char-integer (obj)
92 ) ;; (* :char) dereference is already an integer
94 (defmacro pointer-address (obj)
96 `(system:sap-int (alien:alien-sap ,obj))
98 `(fli:pointer-address ,obj)
103 ;; Simple first pass. Will later create optimized routines for
104 ;; various platforms.
105 (defmacro with-foreign-object ((var type &rest etc) &rest body)
106 (let ((result (gensym)))
107 `(let* ((,var (allocate-foreign-object ,type ,@etc))
108 (,result (progn ,@body)))
109 (free-foreign-object ,var)
112 (defmacro with-foreign-objects (bindings &rest body)
114 `(with-foreign-object ,(car bindings)
115 (with-foreign-objects ,(cdr bindings)