This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.lisp
53 lines (40 loc) · 1.44 KB
/
memory.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(in-package :net.mwatters.libc-misc)
(defmacro with-foreign-object ((var type &optional (count 1) (zero-memory-p 't)) &body forms
&environment env)
"allocate memory for COUNT elements of type TYPE, pointed to by the
pointer bound to VAR, and initially zeroed if ZERO-MEMORY-P evaluates
to a non-nil value."
(let* ((c (gensym "C"))
(count-var (if (constantp count env) count c)))
`(let (,@(if (constantp count env) nil `((,c ,count))))
(cffi:with-foreign-object (,var ,type ,count-var)
#+with-assertions (assert (not (cffi:null-pointer-p ,var)))
(when ,zero-memory-p
(os-bzero ,var (* ,count-var
,(if (constantp type env)
(cffi:foreign-type-size (eval type))
`(cffi:foreign-type-size ,type)))))
,@forms))))
(defmacro with-foreign-objects (bindings &body forms)
(do ((body `(progn ,@forms))
(bindings (reverse bindings) (cdr bindings)))
((not bindings) body)
(setq body `(with-foreign-object ,(car bindings) ,body))))
(defcfun (memset "memset")
:pointer
(ptr (:pointer :void))
(c :int)
(len size-t))
#-mswindows
(defcfun (os-bzero "bzero")
:void
(ptr (:pointer :void))
(len size-t))
#+mswindows
(defun os-bzero (ptr len)
(memset ptr 0 len))
(defcfun (os-memcpy "memcpy")
:pointer
(dst :pointer)
(src :pointer)
(len size-t))