chiark / gitweb /
New function PIXBUF-GET-FROM-DRAWABLE
[clg] / gffi / memory.lisp
CommitLineData
b286c6ed 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.1 2006-04-25 20:31:35 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 allocate-memory (size)
75 (declare (ignore size))
76 (error "Memory allocator not set"))
77(declaim (ftype (function (integer) system-area-pointer) allocate-memory))
78
79(defun deallocate-memory (location)
80 (declare (ignore location))
81 (warn "Memory deallocator not set"))
82
83(defun copy-memory (from length &optional (to (allocate-memory length)))
84 #+cmu(system-area-copy from 0 to 0 (* 8 length))
85 #+sbcl(system-area-ub8-copy from 0 to 0 length)
86 #-(or cmu sbcl)
87 (loop
88 for offset below length
89 do (setf (ref-byte to offset) (ref-byte from offset)))
90 to)
91
92(defun clear-memory (from length &optional (offset 0))
93 #+sbcl(system-area-ub8-fill 0 from offset length)
94 #-sbcl
95 (loop
96 repeat length
97 for byte-offset from offset
98 do (setf (ref-byte from byte-offset) 0)))
99
100(defun memory-clear-p (from length &optional (offset 0))
101 (loop
102 repeat length
103 for byte-offset from offset
104 unless (zerop (ref-byte from byte-offset))
105 do (return-from memory-clear-p nil))
106 t)
107
108(defmacro with-memory ((var size) &body body)
109 #-clisp
110 (if (and #+(or cmu sbcl)t (constantp size))
111 (let ((memory (make-symbol "MEMORY"))
112 (size (eval size)))
113 `(with-alien ((,memory (array #+sbcl(sb-alien:unsigned 8) #+cmu(alien:unsigned 8) ,size)))
114 (let ((,var (alien-sap ,memory)))
115 (clear-memory ,var ,size)
116 ,@body)))
117 `(let ((,var (allocate-memory ,size)))
118 (unwind-protect
119 (progn ,@body)
120 (deallocate-memory ,var))))
121 #+clisp
122 (let ((memory (make-symbol "MEMORY")))
123 `(ffi:with-foreign-object (,memory `(ffi:c-array ffi:uint8 ,,size))
124 (let ((,var (ffi:foreign-address ,memory)))
125 ,@body))))
126
127(defmacro with-pointer ((var &optional (pointer '(make-pointer 0))) &body body)
128 "Binds POINTER to VAR in a way which makes it possible to pass the location of VAR to in foreign function call."
129 #+(or cmu sbcl)
130 `(with-alien ((,var system-area-pointer ,pointer))
131 ,@body)
132 #+clisp
133 `(ffi:with-c-var (,var `ffi:c-pointer ,pointer)
134 ,@body))
135
136
137#+sbcl
138(progn
139 (defun sb-sizeof-bits (type)
140 (sb-alien-internals:alien-type-bits
141 (sb-alien-internals:parse-alien-type type nil)))
142
143 (defun sb-sizeof (type)
144 (/ (sb-sizeof-bits type) 8)))