r1581: *** empty log message ***
[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.6 2002/03/17 17:33:30 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)
23   #+cmu
24   `(alien:make-alien ,(convert-from-uffi-type type :allocation))
25   #+lispworks
26   `(fli:allocate-foreign-object :type ',(convert-from-uffi-type type :allocate))
27   #+allegro
28   `(ff:allocate-fobject ',(convert-from-uffi-type type :allocate) :c)
29   )
30
31 (defmacro free-foreign-object (obj)
32   #+cmu
33   `(alien:free-alien ,obj)
34   #+lispworks
35   `(fli:free-foreign-object ,obj)
36   #+allegro
37   `(ff:free-fobject ,obj)
38   )
39
40 (defmacro null-pointer-p (obj)
41   #+lispworks `(fli:null-pointer-p ,obj)
42   #+allegro `(zerop ,obj)
43   #+cmu   `(alien:null-alien ,obj)
44   )
45
46
47 (defmacro make-null-pointer (type)
48   #+(or allegro cmu) (declare (ignore type))
49   
50   #+cmu `(system:int-sap 0)
51   #+allegro 0
52   #+lispworks `(fli:make-pointer :address 0 :type ,type)
53   )
54
55 (defmacro deref-pointer (ptr type)
56   "Returns a object pointed"
57   (let ((result (gensym)))
58     `(let ((,result
59             #+cmu  (alien:deref ,ptr)
60             #+lispworks (fli:dereference ,ptr)
61             #+allegro (ff:fslot-value-typed ,type :c ,ptr)
62             ))
63       (if (and
64            (or (eq ,type :char)
65                (eq ,type :unsigned-char))
66            (numberp ,result))
67           (code-char ,result)
68           ,result))))
69
70 (defmacro pointer-address (obj)
71   #+cmu
72   `(system:sap-int (alien:alien-sap ,obj))
73   #+lispworks
74   `(fli:pointer-address ,obj)
75   #+allegro
76   obj
77   )