chiark / gitweb /
Bug fix in SCALE-TO-DEVICE
[clg] / gffi / memory.lisp
1 ;; Common Lisp bindings for GTK+ v2.x
2 ;; Copyright 1999-2006 Espen S. Johnsen <espen@users.sf.net>
3 ;;
4 ;; Permission is hereby granted, free of charge, to any person obtaining
5 ;; a copy of this software and associated documentation files (the
6 ;; "Software"), to deal in the Software without restriction, including
7 ;; without limitation the rights to use, copy, modify, merge, publish,
8 ;; distribute, sublicense, and/or sell copies of the Software, and to
9 ;; permit persons to whom the Software is furnished to do so, subject to
10 ;; the following conditions:
11 ;;
12 ;; The above copyright notice and this permission notice shall be
13 ;; included in all copies or substantial portions of the Software.
14 ;;
15 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 ;; $Id: memory.lisp,v 1.4 2007-06-02 07:26:23 espen Exp $
24
25
26 (in-package "GFFI")
27
28
29 (defun make-pointer (address)
30   #+(or cmu sbcl)(int-sap address)
31   #+clisp(ffi:unsigned-foreign-address address)
32   #-(or cmu sbcl clisp)address)
33
34 (defun pointer-address (pointer)
35   #+(or cmu sbcl)(sap-int pointer)
36   #+clisp(ffi:foreign-address-unsigned pointer)
37   #-(or cmu sbcl clisp)pointer)
38
39 (defun null-pointer-p (pointer)
40   #+(or cmu sbcl)(zerop (sap-int pointer))
41   #+clisp(or (not pointer) (zerop (pointer-address pointer)))
42   #-(or cmu sbcl clisp)(zerop pointer))
43
44 (defun pointer= (pointer1 pointer2)
45   #+(or cmu sbcl)(sap= pointer1 pointer2)
46   #+clisp(= (pointer-address pointer1) (pointer-address pointer2))
47   #-(or cmu sbcl clisp)(= pointer1 pointer2))
48
49 (defun pointer+ (pointer offset)
50   #+(or cmu sbcl)(sap+ pointer offset)
51   #+clisp(make-pointer (+ (pointer-address pointer) offset))
52   #-(or cmu sbcl clisp)(+ pointer offset))
53
54 (defun ref-pointer (location &optional (offset 0))
55   #+(or cmu sbcl)(sap-ref-sap location offset)
56   #+clisp(ffi:memory-as location 'ffi:c-pointer offset))
57
58 (defun (setf ref-pointer) (pointer location &optional (offset 0))
59   (setf 
60    #+(or cmu sbcl)(sap-ref-sap location offset)
61    #+clisp(ffi:memory-as location 'ffi:c-pointer offset)
62    pointer))
63
64 (defun ref-byte (location &optional (offset 0))
65   #+(or cmu sbcl)(sap-ref-8 location offset)
66   #+clisp(ffi:memory-as location 'ffi:uchar offset))
67
68 (defun (setf ref-byte) (byte location &optional (offset 0))
69   (setf 
70    #+(or cmu sbcl)(sap-ref-8 location offset)
71    #+clisp(ffi:memory-as location 'ffi:uchar offset)
72    byte))
73
74 (defun ref-int-32 (location &optional (offset 0))
75   #+(or cmu sbcl)(signed-sap-ref-32 location offset)
76   #+clisp(ffi:memory-as location 'ffi:sint32 offset))
77
78 (defun (setf ref-int-32) (value location &optional (offset 0))
79   (setf 
80    #+(or cmu sbcl)(signed-sap-ref-32 location offset)
81    #+clisp(ffi:memory-as location 'ffi:sint32 offset)
82    value))
83
84 (defun ref-uint-32 (location &optional (offset 0))
85   #+(or cmu sbcl)(sap-ref-32 location offset)
86   #+clisp(ffi:memory-as location 'ffi:uint32 offset))
87
88 (defun (setf ref-uint-32) (value location &optional (offset 0))
89   (setf 
90    #+(or cmu sbcl)(sap-ref-32 location offset)
91    #+clisp(ffi:memory-as location 'ffi:uint32 offset)
92    value))
93
94 (defun ref-single-float (location &optional (offset 0))
95   #+(or cmu sbcl)(sap-ref-single location offset)
96   #+clisp(ffi:memory-as location 'single-float offset))
97
98 (defun (setf ref-single-float) (value location &optional (offset 0))
99   (setf 
100    #+(or cmu sbcl)(sap-ref-single location offset)
101    #+clisp(ffi:memory-as location 'single-float offset)
102    value))
103
104 (defun ref-double-float (location &optional (offset 0))
105   #+(or cmu sbcl)(sap-ref-double location offset)
106   #+clisp(ffi:memory-as location 'double-float offset))
107
108 (defun (setf ref-double-float) (value location &optional (offset 0))
109   (setf 
110    #+(or cmu sbcl)(sap-ref-double location offset)
111    #+clisp(ffi:memory-as location 'double-float offset)
112    value))
113
114
115
116 (defparameter *memory-allocator* nil)
117 (defparameter *memory-deallocator* nil)
118
119 (defun allocate-memory (size)
120   (if *memory-allocator*
121       (funcall *memory-allocator* size)
122     (error "Memory allocator not set")))
123 (declaim (ftype (function (integer) system-area-pointer) allocate-memory))
124
125 (defun deallocate-memory (location)
126   (if *memory-deallocator*
127       (funcall *memory-deallocator* location)
128     (warn "Memory deallocator not set")))
129
130 (defun copy-memory (from length &optional (to (allocate-memory length)))
131   #+cmu(system-area-copy from 0 to 0 (* 8 length))
132   #+sbcl(system-area-ub8-copy from 0 to 0 length)
133   #-(or cmu sbcl)
134   (loop
135    for offset below length
136    do (setf (ref-byte to offset) (ref-byte from offset)))
137   to)
138
139 (defun clear-memory (from length &optional (offset 0))
140   #+sbcl(system-area-ub8-fill 0 from offset length)
141   #-sbcl
142   (loop
143    repeat length
144    for byte-offset from offset
145    do (setf (ref-byte from byte-offset) 0)))
146
147 (defun memory-clear-p (from length &optional (offset 0))
148   (loop
149    repeat length
150    for byte-offset from offset
151    unless (zerop (ref-byte from byte-offset))
152    do (return-from memory-clear-p nil))
153   t)
154
155 (defmacro with-memory ((var size) &body body)
156   #-clisp
157   (if (and #+(or cmu sbcl)t (constantp size))
158       (let ((memory (make-symbol "MEMORY"))
159             (size (eval size)))
160         `(with-alien ((,memory (array #+sbcl(sb-alien:unsigned 8) #+cmu(alien:unsigned 8) ,size)))
161            (let ((,var (alien-sap ,memory)))
162              (clear-memory ,var ,size)
163              ,@body)))
164     `(let ((,var (allocate-memory ,size)))
165        (unwind-protect
166            (progn ,@body)
167          (deallocate-memory ,var))))
168   #+clisp
169   (let ((memory (make-symbol "MEMORY")))           
170     `(ffi:with-foreign-object (,memory `(ffi:c-array ffi:uint8 ,,size))
171        (let ((,var (ffi:foreign-address ,memory)))
172          ,@body))))
173
174 (defmacro with-pointer ((var &optional (pointer '(make-pointer 0))) &body body)
175   "Binds POINTER to VAR in a way which makes it possible to pass the location of VAR to in foreign function call."
176   #+(or cmu sbcl)
177   `(with-alien ((,var system-area-pointer ,pointer))
178      ,@body)
179   #+clisp
180   `(ffi:with-c-var (,var `ffi:c-pointer ,pointer)
181      ,@body))
182
183
184 #+sbcl
185 (progn
186   (defun sb-sizeof-bits (type)
187     (sb-alien-internals:alien-type-bits
188      (sb-alien-internals:parse-alien-type type nil)))
189
190   (defun sb-sizeof (type)
191     (/ (sb-sizeof-bits type) 8))
192
193   (defun sb-alignment (type)
194     (/ (sb-alien-internals:alien-type-alignment
195         (sb-alien-internals:parse-alien-type type nil))
196        8)))