diff --git a/cffi-epoll.lisp b/cffi-epoll.lisp
index b4bdea0..5b78c62 100644
--- a/cffi-epoll.lisp
+++ b/cffi-epoll.lisp
@@ -77,22 +77,23 @@
(timeout :int))
(defmacro wait ((events-var fd-var epfd &optional
- (maxevents 1024)
+ (max-events 1024)
(timeout -1))
&body body)
(let ((events (gensym "EVENTS-"))
- (e (gensym "E-"))
+ (evt (gensym "EVT-"))
(e-data (gensym "E-DATA-"))
+ (n (gensym "N-"))
(i (gensym "I-"))
- (m (gensym "M-")))
- `(let ((,m ,maxevents))
- (with-foreign-object (,events '(:struct epoll-event) ,m)
- (let ((,i (c-epoll-wait ,epfd ,events ,m ,timeout)))
- (when (< ,i 0)
+ (g-max-events (gensym "MAX-EVENTS-")))
+ `(let ((,g-max-events ,max-events))
+ (with-foreign-object (,events '(:struct epoll-event) ,g-max-events)
+ (let ((,n (c-epoll-wait ,epfd ,events ,g-max-events ,timeout)))
+ (when (< ,n 0)
(error-errno "epoll_wait"))
- (dotimes (,i ,i)
- (let* ((,e (mem-aref ,events '(:struct epoll-event) ,i))
- (,events-var (foreign-slot-value ,e '(:struct epoll-event) 'events))
- (,e-data (foreign-slot-value ,e '(:struct epoll-event) 'data))
+ (dotimes (,i ,n)
+ (let* ((,evt (mem-aptr ,events '(:struct epoll-event) ,i))
+ (,events-var (foreign-slot-value ,evt '(:struct epoll-event) 'events))
+ (,e-data (foreign-slot-value ,evt '(:struct epoll-event) 'data))
(,fd-var (foreign-slot-value ,e-data '(:union epoll-data) 'fd)))
,@body)))))))