0.7.12.24
[sbcl.git] / contrib / bsd-sockets / malloc.lisp
1 (in-package :bsd-sockets-internal)
2
3 (defun malloc (size)
4   "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"
5   ;; we can attach finalizers to any object, and they'll be called on
6   ;; the next gc after the object no longer has references.  We can't
7   ;; however make the finalizer close over the object, or it'll never
8   ;; have no references.  I experimentally determined that (sap-alien
9   ;; (alien-sap f)) is not EQ to f, so we can do it that way
10   (let* ((memory (make-alien (unsigned 8) size))
11          (alias (sap-alien (alien-sap memory)
12                                  (* (unsigned 8)))))
13     (sb-ext:finalize memory
14                      (lambda ()
15                        (free-alien alias)))))
16