Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions src/iffi/alloc.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@
;;; INSTANCE
;;;
(defun make-intricate-instance (name &rest args)
(let* ((record (find-intricate-record name))
(ptr (intricate-alloc name)))
(let* ((record (find-intricate-record name)))
(unless record
(error "Record with name ~A not found" name))
(if-let ((ctor (constructor-of record)))
(handler-case
(apply (constructor-of record) `(:pointer ,name) ptr args)
(serious-condition (condi) (intricate-free ptr) (error condi)))
(error "Constructor not found for record ~A" name))
ptr))
(let ((ptr (intricate-alloc name)))
(if-let ((ctor (constructor-of record)))
(handler-case
(apply (constructor-of record) `(:pointer ,name) ptr args)
(serious-condition (condi) (intricate-free ptr) (error condi)))
(error "Constructor not found for record ~A" name))
ptr)))
Comment on lines 54 to +64

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes a minor memory leak (if the record name isn't found).



(define-compiler-macro make-intricate-instance (&whole whole name &rest args)
Expand All @@ -73,15 +73,20 @@
((not record) (warn "Record with name ~A not found" quoted-name))
((not ctor) (warn "Constructor not found for record ~A" quoted-name))))
(if ctor
(with-gensyms (ptr condi)
`(let ((,ptr (intricate-alloc ',quoted-name)))
(handler-case
;; FIXME: here we actually a break funcall protocol a bit
;; because if during args evaluation condition is raised it is
;; going to be consumed here with stack being unwound
(,ctor '(:pointer ,quoted-name) ,ptr ,@args)
(serious-condition (,condi) (intricate-free ,ptr) (error ,condi)))
,ptr))
(with-gensyms (ptr)
(let ((arg-bindings (mapcar (lambda (arg)
(if (and (consp arg) (not (eq (car arg) 'quote)))
(list :bind (gensym "ARG-") arg)
(list :nobind arg)))
args)))
;; Evaluate the argument expressions first, to prevent a leak
;; if one takes a nonlocal exit.
`(let (,@(mapcar #'cdr (remove-if-not (lambda (b) (eq (car b) :bind))
arg-bindings))
(,ptr (intricate-alloc ',quoted-name)))
(,ctor '(:pointer ,quoted-name) ,ptr
,@(mapcar #'cadr arg-bindings))
,ptr)))
Comment on lines -76 to +89

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes the FIXME, while being careful not to introduce a leak.

whole)))


Expand Down Expand Up @@ -120,4 +125,31 @@
`((with-intricate-instance ,(first declarations)
,@(expand-with-intricate-instances (rest declarations) body)))
`(,@body))))
(first (expand-with-intricate-instances declarations body))))
`(progn ,@(expand-with-intricate-instances declarations body))))
Comment on lines -123 to +128

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor.



(defun initialize-intricate-instance (name ptr &rest args)
"Initializes an instance of type `name' at `ptr' by calling a constructor.
Use very carefully -- the allocation at `ptr' must be large enough to hold
an instance of type `name'."
(let* ((record (find-intricate-record name)))
(unless record
(error "Record with name ~A not found" name))
(if-let ((ctor (constructor-of record)))
(handler-case
(apply (constructor-of record) `(:pointer ,name) ptr args)
(serious-condition (condi) (intricate-free ptr) (error condi)))
(error "Constructor not found for record ~A" name))
ptr))

(define-compiler-macro initialize-intricate-instance (&whole whole name ptr &rest args)
(let* ((quoted-name (find-quoted name))
(record (find-intricate-record quoted-name))
(ctor (and record (constructor-of record))))
(when quoted-name
(cond
((not record) (warn "Record with name ~A not found" quoted-name))
((not ctor) (warn "Constructor not found for record ~A" quoted-name))))
(if ctor
`(,ctor '(:pointer ,quoted-name) ,ptr ,@args)
whole)))
Comment on lines +131 to +155

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used this yet, but at some point I plan to introduce potential stack allocation of temporaries, at which point it will be useful.

1 change: 1 addition & 0 deletions src/iffi/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#:with-intricate-slots

#:make-intricate-instance
#:initialize-intricate-instance
#:destroy-intricate-instance
#:with-intricate-instance
#:with-intricate-instances
Expand Down