0.7.12.28
[sbcl/lichteblau.git] / contrib / sb-bsd-sockets / malloc.lisp
blob0b6ca39f3a853dedc6a97137842dcea588da95c7
1 (in-package :sb-bsd-sockets-internal)
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)))))