malloc the NSFastEnumeration buffers because the class implementation might store pointers into the buffers in the enumeration state

db4
Joe Groff 2008-07-13 12:48:59 -07:00
parent 8174a0967c
commit 310acd52c5
1 changed files with 13 additions and 11 deletions

View File

@ -1,28 +1,30 @@
USING: kernel cocoa cocoa.types alien.c-types locals math sequences
vectors fry ;
vectors fry libc ;
IN: cocoa.enumeration
: NS-EACH-BUFFER-SIZE 16 ; inline
: (allocate-enumeration-buffers) ( -- state stackbuf count )
"NSFastEnumerationState" <c-object>
NS-EACH-BUFFER-SIZE "id" <c-array>
NS-EACH-BUFFER-SIZE ;
: (with-enumeration-buffers) ( quot -- )
"NSFastEnumerationState" heap-size swap '[
NS-EACH-BUFFER-SIZE "id" heap-size * [
NS-EACH-BUFFER-SIZE @
] with-malloc
] with-malloc ; inline
:: (NSFastEnumeration-each) ( object quot state stackbuf count -- )
object state stackbuf count -> countByEnumeratingWithState:objects:count:
dup zero? [ drop ] [
[
state NSFastEnumerationState-itemsPtr dup stackbuf ?
void*-nth quot call
] each object quot state stackbuf count (NSFastEnumeration-each)
state NSFastEnumerationState-itemsPtr [ stackbuf ] unless*
'[ , void*-nth quot call ] each
object quot state stackbuf count (NSFastEnumeration-each)
] if ; inline
: NSFastEnumeration-each ( object quot -- )
(allocate-enumeration-buffers) (NSFastEnumeration-each) ; inline
[ (NSFastEnumeration-each) ] (with-enumeration-buffers) ; inline
: NSFastEnumeration-map ( object quot -- vector )
NS-EACH-BUFFER-SIZE <vector> [ '[ @ , push ] NSFastEnumeration-each ] keep ; inline
NS-EACH-BUFFER-SIZE <vector>
[ '[ @ , push ] NSFastEnumeration-each ] keep ; inline
: NSFastEnumeration>vector ( object -- vector )
[ ] NSFastEnumeration-map ;