b0b34f07ca98fb8191238cdf7f9fd8e5285c24eb
[uffi.git] / src / objects.cl
1 ;;;; -*- Mode: ANSI-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.11 2002/03/21 14:49:14 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 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)
73 )
74
75 #+lispworks ;; with LW, deref is a character
76 (defmacro ensure-char-character (obj)
77   obj
78   )
79
80 #+(or allegro cmu)
81 (defmacro ensure-char-character (obj)
82   `(code-char ,obj)
83   )
84   
85 #+lispworks
86 (defmacro ensure-char-integer (obj)
87  `(char-code ,obj))
88
89 #+(or allegro cmu)
90 (defmacro ensure-char-integer (obj)
91   obj
92   ) ;; (* :char) dereference is already an integer
93
94 (defmacro pointer-address (obj)
95   #+cmu
96   `(system:sap-int (alien:alien-sap ,obj))
97   #+lispworks
98   `(fli:pointer-address ,obj)
99   #+allegro
100   obj
101   )
102
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)
110       ,result)))
111
112 (defmacro with-foreign-objects (bindings &rest body)
113   (if bindings
114       `(with-foreign-object ,(car bindings)
115         (with-foreign-objects ,(cdr bindings)
116           ,@body))
117       `(progn ,@body)))
118
119             
120