[10d2c0]: contrib / sb-bsd-sockets / malloc.lisp Maximize Restore History

Download this file

malloc.lisp    17 lines (14 with data), 936 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
(in-package :sb-bsd-sockets-internal)
(defun malloc (size)
"Allocate foreign memory in some way that allows the garbage collector to free it later. Note that memory allocated this way does not count as `consed' for the purposes of deciding when to gc, so explicitly calling EXT:GC occasionally would be a good idea if you use it a lot"
;; we can attach finalizers to any object, and they'll be called on
;; the next gc after the object no longer has references. We can't
;; however make the finalizer close over the object, or it'll never
;; have no references. I experimentally determined that (sap-alien
;; (alien-sap f)) is not EQ to f, so we can do it that way
(let* ((memory (make-alien (unsigned 8) size))
(alias (sap-alien (alien-sap memory)
(* (unsigned 8)))))
(sb-ext:finalize memory
(lambda ()
(free-alien alias)))))