WHERE
-TUPLE: F-destructor alien disposed ;
+TUPLE: F-destructor < disposable alien ;
-: <F-destructor> ( alien -- destructor ) f F-destructor boa ; inline
+: <F-destructor> ( alien -- destructor )
+ F-destructor new-disposable swap >>alien ; inline
M: F-destructor dispose* alien>> F N ndrop ;
"tools.crossref"
"tools.errors"
"tools.deploy"
+ "tools.destructors"
"tools.disassembler"
"tools.memory"
"tools.profiler"
USING: kernel assocs math accessors destructors fry sequences ;
IN: cache
-TUPLE: cache-assoc assoc max-age disposed ;
+TUPLE: cache-assoc < disposable assoc max-age ;
: <cache-assoc> ( -- cache )
- H{ } clone 10 f cache-assoc boa ;
+ cache-assoc new-disposable H{ } clone >>assoc 10 >>max-age ;
<PRIVATE
debugger debugger.threads locals fry ;\r
IN: concurrency.mailboxes\r
\r
-TUPLE: mailbox threads data disposed ;\r
+TUPLE: mailbox < disposable threads data ;\r
\r
M: mailbox dispose* threads>> notify-all ;\r
\r
: <mailbox> ( -- mailbox )\r
- <dlist> <dlist> f mailbox boa ;\r
+ mailbox new-disposable <dlist> >>threads <dlist> >>data ;\r
\r
: mailbox-empty? ( mailbox -- bool )\r
data>> deque-empty? ;\r
}
"cdecl" [ (master-event-source-callback) ] alien-callback ;
-TUPLE: event-stream info handle disposed ;
+TUPLE: event-stream < disposable info handle ;
: <event-stream> ( quot paths latency flags -- event-stream )
[
- add-event-source-callback dup
- [ master-event-source-callback ] dip
+ add-event-source-callback
+ [ master-event-source-callback ] keep
] 3dip <FSEventStream>
dup enable-event-stream
- f event-stream boa ;
+ event-stream new-disposable swap >>handle swap >>info ;
M: event-stream dispose*
{
CTLineCreateWithAttributedString
] with-destructors ;
-TUPLE: line line metrics image loc dim disposed ;
+TUPLE: line < disposable line metrics image loc dim ;
: typographic-bounds ( line -- width ascent descent leading )
0 <CGFloat> 0 <CGFloat> 0 <CGFloat>
:: <line> ( font string -- line )
[
+ line new-disposable
+
[let* | open-font [ font cache-font ]
line [ string open-font font foreground>> <CTLine> |CFRelease ]
ext [ (loc) (dim) [ + ceiling ] 2map ]
dim [ ext loc [ - >integer ] 2map ]
metrics [ open-font line compute-line-metrics ] |
- line metrics
+
+ line >>line
+
+ metrics >>metrics
+
dim [
{
[ font dim fill-background ]
[ loc set-text-position ]
[ [ line ] dip CTLineDraw ]
} cleave
- ] make-bitmap-image
- metrics loc dim line-loc
- metrics metrics>dim
+ ] make-bitmap-image >>image
+
+ metrics loc dim line-loc >>loc
+
+ metrics metrics>dim >>dim
]
- f line boa
] with-destructors ;
M: line dispose* line>> CFRelease ;
{ $heading "Debugging" }
{ $subsection "prettyprint" }
{ $subsection "inspector" }
+{ $subsection "tools.inference" }
{ $subsection "tools.annotations" }
{ $subsection "tools.deprecation" }
-{ $subsection "tools.inference" }
{ $heading "Browsing" }
{ $subsection "see" }
{ $subsection "tools.crossref" }
{ $subsection "profiling" }
{ $subsection "tools.memory" }
{ $subsection "tools.threads" }
+{ $subsection "tools.destructors" }
{ $subsection "tools.disassembler" }
{ $heading "Deployment" }
{ $subsection "tools.deploy" } ;
kernel.private math io.ports sequences strings sbufs threads
unix vectors io.buffers io.backend io.encodings math.parser
continuations system libc namespaces make io.timeouts
-io.encodings.utf8 destructors accessors summary combinators
-locals unix.time fry io.backend.unix.multiplexers ;
+io.encodings.utf8 destructors destructors.private accessors
+summary combinators locals unix.time fry
+io.backend.unix.multiplexers ;
QUALIFIED: io
IN: io.backend.unix
GENERIC: handle-fd ( handle -- fd )
-TUPLE: fd fd disposed ;
+TUPLE: fd < disposable fd ;
: init-fd ( fd -- fd )
[
#! since on OS X 10.3, this operation fails from init-io
#! when running the Factor.app (presumably because fd 0 and
#! 1 are closed).
- f fd boa ;
+ fd new-disposable swap >>fd ;
M: fd dispose
dup disposed>> [ drop ] [
- [ cancel-operation ]
- [ t >>disposed drop ]
- [ fd>> close-file ]
- tri
+ {
+ [ cancel-operation ]
+ [ t >>disposed drop ]
+ [ unregister-disposable ]
+ [ fd>> close-file ]
+ } cleave
] if ;
M: fd handle-fd dup check-disposed fd>> ;
! pipe to non-blocking, and read from it instead of the real
! stdin. Very crufty, but it will suffice until we get native
! threading support at the language level.
-TUPLE: stdin control size data disposed ;
+TUPLE: stdin < disposable control size data ;
M: stdin dispose*
[
: data-read-fd ( -- fd ) &: stdin_read *uint ;
: <stdin> ( -- stdin )
- stdin new
+ stdin new-disposable
control-write-fd <fd> <output-port> >>control
size-read-fd <fd> init-fd <input-port> >>size
data-read-fd <fd> >>data ;
splitting continuations math.bitwise accessors init sets assocs ;
IN: io.backend.windows
-: win32-handles ( -- assoc )
- \ win32-handles [ H{ } clone ] initialize-alien ;
-
-TUPLE: win32-handle < identity-tuple handle disposed ;
-
-M: win32-handle hashcode* handle>> hashcode* ;
+TUPLE: win32-handle < disposable handle ;
: set-inherit ( handle ? -- )
[ handle>> HANDLE_FLAG_INHERIT ] dip
>BOOLEAN SetHandleInformation win32-error=0/f ;
: new-win32-handle ( handle class -- win32-handle )
- new swap >>handle
- dup f set-inherit
- dup win32-handles conjoin ;
+ new-disposable swap >>handle
+ dup f set-inherit ;
: <win32-handle> ( handle -- win32-handle )
win32-handle new-win32-handle ;
-ERROR: disposing-twice ;
-
-: unregister-handle ( handle -- )
- win32-handles delete-at*
- [ t >>disposed drop ] [ disposing-twice ] if ;
-
M: win32-handle dispose* ( handle -- )
- [ unregister-handle ] [ handle>> CloseHandle win32-error=0/f ] bi ;
+ handle>> CloseHandle win32-error=0/f ;
TUPLE: win32-file < win32-handle ptr ;
<win32-file> |dispose
dup add-completion ;
-: share-mode ( -- fixnum )
+: share-mode ( -- n )
{
FILE_SHARE_READ
FILE_SHARE_WRITE
math ;
IN: io.mmap
-TUPLE: mapped-file address handle length disposed ;
+TUPLE: mapped-file < disposable address handle length ;
HOOK: (mapped-file-reader) os ( path length -- address handle )
HOOK: (mapped-file-r/w) os ( path length -- address handle )
-ERROR: bad-mmap-size path size ;
+ERROR: bad-mmap-size n ;
<PRIVATE
-: prepare-mapped-file ( path -- path' n )
- [ normalize-path ] [ file-info size>> ] bi
- dup 0 <= [ bad-mmap-size ] when ;
+: prepare-mapped-file ( path quot -- mapped-file path' length )
+ [
+ [ normalize-path ] [ file-info size>> ] bi
+ [ dup 0 <= [ bad-mmap-size ] [ 2drop ] if ]
+ [ nip mapped-file new-disposable swap >>length ]
+ ] dip 2tri [ >>address ] [ >>handle ] bi* ; inline
PRIVATE>
: <mapped-file-reader> ( path -- mmap )
- prepare-mapped-file
- [ (mapped-file-reader) ] keep
- f mapped-file boa ;
+ [ (mapped-file-reader) ] prepare-mapped-file ;
: <mapped-file> ( path -- mmap )
- prepare-mapped-file
- [ (mapped-file-r/w) ] keep
- f mapped-file boa ;
+ [ (mapped-file-r/w) ] prepare-mapped-file ;
HOOK: close-mapped-file io-backend ( mmap -- )
SYMBOL: inotify
-TUPLE: linux-monitor < monitor wd inotify watches disposed ;
+TUPLE: linux-monitor < monitor wd inotify watches ;
: <linux-monitor> ( wd path mailbox -- monitor )
linux-monitor new-monitor
[ dispose-monitors ] [ ] cleanup
] with-scope ; inline
-TUPLE: monitor < identity-tuple path queue timeout ;
+TUPLE: monitor < disposable path queue timeout ;
M: monitor hashcode* path>> hashcode* ;
M: monitor set-timeout (>>timeout) ;
: new-monitor ( path mailbox class -- monitor )
- new
+ new-disposable
swap >>queue
swap >>path ; inline
! Simulate recursive monitors on platforms that don't have them
-TUPLE: recursive-monitor < monitor children thread ready disposed ;
+TUPLE: recursive-monitor < monitor children thread ready ;
: notify? ( -- ? ) monitor tget ready>> promise-fulfilled? ;
SYMBOL: default-buffer-size
64 1024 * default-buffer-size set-global
-TUPLE: port handle timeout disposed ;
+TUPLE: port < disposable handle timeout ;
M: port timeout timeout>> ;
M: port set-timeout (>>timeout) ;
: <port> ( handle class -- port )
- new swap >>handle ; inline
+ new-disposable swap >>handle ; inline
TUPLE: buffered-port < port { buffer buffer } ;
SSL_CTX_set_verify_depth
] [ drop ] if ;
-TUPLE: bio handle disposed ;
+TUPLE: bio < disposable handle ;
-: <bio> ( handle -- bio ) f bio boa ;
+: <bio> ( handle -- bio ) bio new-disposable swap >>handle ;
M: bio dispose* handle>> BIO_free ssl-error ;
SSL_CTX_set_tmp_dh ssl-error
] [ drop ] if ;
-TUPLE: rsa handle disposed ;
+TUPLE: rsa < disposable handle ;
-: <rsa> ( handle -- rsa ) f rsa boa ;
+: <rsa> ( handle -- rsa ) rsa new-disposable swap >>handle ;
M: rsa dispose* handle>> RSA_free ;
SSL_CTX_set_tmp_rsa ssl-error ;
: <openssl-context> ( config ctx -- context )
- openssl-context new
+ openssl-context new-disposable
swap >>handle
swap >>config
V{ } clone >>aliens
[ handle>> SSL_CTX_free ]
tri ;
-TUPLE: ssl-handle file handle connected disposed ;
+TUPLE: ssl-handle < disposable file handle connected ;
SYMBOL: default-secure-context
] unless* ;
: <ssl-handle> ( fd -- ssl )
- current-secure-context handle>> SSL_new dup ssl-error
- f f ssl-handle boa ;
+ ssl-handle new-disposable
+ current-secure-context handle>> SSL_new
+ dup ssl-error >>handle
+ swap >>file ;
M: ssl-handle dispose*
[ handle>> SSL_free ] [ file>> dispose ] bi ;
"vocab:openssl/cacert.pem" >>ca-file
t >>verify ;
-TUPLE: secure-context config handle disposed ;
+TUPLE: secure-context < disposable config handle ;
HOOK: <secure-context> secure-socket-backend ( config -- context )
! See what happens if other end is closed
[ ] [ <promise> "port" set ] unit-test
+[ ] [ "datagram3" get dispose ] unit-test
+
[ ] [
[
"127.0.0.1" 0 <inet4> utf8 <server>
[ "hello" f ] [
"port" get ?promise utf8 [
+ 1 seconds input-stream get set-timeout
+ 1 seconds output-stream get set-timeout
"hi\n" write flush readln readln
] with-client
] unit-test
<PRIVATE
-TUPLE: single-texture image dim loc texture-coords texture display-list disposed ;
+TUPLE: single-texture < disposable image dim loc texture-coords texture display-list ;
: adjust-texture-dim ( dim -- dim' )
non-power-of-2-textures? get [
GL_COMPILE [ [ dim>> ] keep draw-textured-rect ] make-dlist ;
: <single-texture> ( image loc -- texture )
- single-texture new swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
+ single-texture new-disposable swap >>loc swap [ >>image ] [ dim>> >>dim ] bi
dup image>> dim>> product 0 = [
dup texture-coords >>texture-coords
dup image>> make-texture >>texture
dup texture>> [ draw-textured-rect ] [ 2drop ] if
] if ;
-TUPLE: multi-texture grid display-list loc disposed ;
+TUPLE: multi-texture < disposable grid display-list loc ;
: image-locs ( image-grid -- loc-grid )
[ first [ dim>> first ] map ] [ [ first dim>> second ] map ] bi
: <multi-texture> ( image-grid loc -- multi-texture )
[
- [
- <texture-grid> dup
- make-textured-grid-display-list
- ] keep
- f multi-texture boa
+ [ multi-texture new-disposable ] 2dip
+ [ nip >>loc ] [ <texture-grid> >>grid ] 2bi
+ dup grid>> make-textured-grid-display-list >>display-list
] with-destructors ;
M: multi-texture draw-scaled-texture nip draw-texture ;
DESTRUCTOR: pango_layout_iter_free
-TUPLE: layout font string selection layout metrics ink-rect logical-rect image disposed ;
+TUPLE: layout < disposable font string selection layout metrics ink-rect logical-rect image ;
SYMBOL: dpi
: <layout> ( font string -- line )
[
- layout new
+ layout new-disposable
swap unpack-selection
swap >>font
dup [ string>> ] [ font>> ] bi <PangoLayout> >>layout
: strip-init-hooks ( -- )
"Stripping startup hooks" show
{
+ "alien.strings"
"command-line"
"cpu.x86"
+ "destructors"
"environment"
"libc"
- "alien.strings"
}
[ init-hooks get delete-at ] each
deploy-threads? get [
run-file
] when ;
+: strip-destructors ( -- )
+ "libc" vocab [
+ "Stripping destructor debug code" show
+ "vocab:tools/deploy/shaker/strip-destructors.factor"
+ run-file
+ ] when ;
+
: strip-call ( -- )
"Stripping stack effect checking from call( and execute(" show
"vocab:tools/deploy/shaker/strip-call.factor" run-file ;
"mallocs" "libc.private" lookup ,
+ "disposables" "destructors" lookup ,
+
deploy-threads? [
"initial-thread" "threads" lookup ,
] unless
: strip ( -- )
init-stripper
strip-libc
+ strip-destructors
strip-call
strip-cocoa
strip-debugger
--- /dev/null
+USE: kernel
+IN: destructors.private
+
+: register-disposable ( obj -- ) drop ; inline
+
+: unregister-disposable ( obj -- ) drop ; inline
--- /dev/null
+Slava Pestov
--- /dev/null
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: help.markup help.syntax quotations ;
+IN: tools.destructors
+
+HELP: disposables.
+{ $description "Print the number of disposable objects of each class." } ;
+
+HELP: leaks
+{ $values
+ { "quot" quotation }
+}
+{ $description "Runs a quotation, printing any increases in the number of disposable objects after the quotation returns." } ;
+
+ARTICLE: "tools.destructors" "Destructor tools"
+"The " { $vocab-link "tools.destructors" } " vocabulary provides words for tracking down resource leaks."
+{ $subsection disposables. }
+{ $subsection leaks }
+{ $see-also "destructors" } ;
+
+ABOUT: "tools.destructors"
--- /dev/null
+! Copyright (C) 2009 Slava Pestov.
+! See http://factorcode.org/license.txt for BSD license.
+USING: assocs classes destructors fry kernel math namespaces
+prettyprint sequences sets sorting ;
+IN: tools.destructors
+
+<PRIVATE
+
+: disposable-tally ( -- assoc )
+ disposables get
+ H{ } clone [ [ keys ] dip '[ class _ inc-at ] each ] keep ;
+
+: subtract-values ( assoc1 assoc2 -- assoc )
+ [ [ keys ] bi@ append prune ] 2keep
+ H{ } clone [
+ '[
+ [ _ _ [ at 0 or ] bi-curry@ bi - ] keep _ set-at
+ ] each
+ ] keep ;
+
+: (disposables.) ( assoc -- )
+ >alist sort-keys simple-table. ;
+
+PRIVATE>
+
+: disposables. ( -- )
+ disposable-tally (disposables.) ;
+
+: leaks ( quot -- )
+ disposable-tally [ call disposable-tally ] dip subtract-values
+ (disposables.) ; inline
specialized-arrays.alien specialized-arrays.direct.alien ;
IN: windows.com.wrapper
-TUPLE: com-wrapper callbacks vtbls disposed ;
+TUPLE: com-wrapper < disposable callbacks vtbls ;
<PRIVATE
[ +live-wrappers+ get adjoin ] bi ;
: <com-wrapper> ( implementations -- wrapper )
- (make-callbacks) f f com-wrapper boa
+ com-wrapper new-disposable swap (make-callbacks) >>vtbls
dup allocate-wrapper ;
M: com-wrapper dispose*
windows.fonts opengl.textures locals windows.errors ;
IN: windows.uniscribe
-TUPLE: script-string font string metrics ssa size image disposed ;
+TUPLE: script-string < disposable font string metrics ssa size image ;
: line-offset>x ( n script-string -- x )
2dup string>> length = [
TEXTMETRIC>metrics ;
: <script-string> ( font string -- script-string )
- [ script-string new ] 2dip
+ [ script-string new-disposable ] 2dip
[ >>font ] [ >>string ] bi*
[
{
$nl
"No further operations can be performed on a disposable object after this call."
$nl
-"Disposing an object which has already been disposed should have no effect, and in particular it should not fail with an error. To help implement this pattern, add a " { $slot "disposed" } " slot to your object and implement the " { $link dispose* } " method instead." }
-{ $notes "You must close disposable objects after you are finished working with them, to avoid leaking operating system resources. A convenient way to automate this is by using the " { $link with-disposal } " word."
+"Disposing an object which has already been disposed should have no effect, and in particular it should not fail with an error. To help implement this pattern, inherit from the " { $link disposable } " class and implement the " { $link dispose* } " method instead." }
+{ $notes "You must dispose of disposable objects after you are finished working with them, to avoid leaking operating system resources. A convenient way to automate this is by using the " { $link with-disposal } " word."
$nl
"The default implementation assumes the object has a " { $snippet "disposed" } " slot. If the slot is set to " { $link f } ", it calls " { $link dispose* } " and sets the slot to " { $link t } "." } ;
{ "seq" sequence } }
{ $description "Attempts to dispose of each element of a sequence and collects all of the errors into a sequence. If any errors are thrown during disposal, the last error is rethrown after all objects have been disposed." } ;
+HELP: disposables
+{ $var-description "Global variable holding all disposable objects which have not been disposed of yet. The " { $link new-disposable } " word adds objects here, and the " { $link dispose } " method on disposables removes them. The " { $link "tools.destructors" } " vocabulary provides some words for working with this data." } ;
+
ARTICLE: "destructors-anti-patterns" "Resource disposal anti-patterns"
"Words which create objects corresponding to external resources should always be used with " { $link with-disposal } ". The following code is wrong:"
{ $code
}
"The reason being that if " { $snippet "do stuff" } " throws an error, the resource will not be disposed of. The most important case where this can occur is with I/O streams, and the correct solution is to always use " { $link with-input-stream } " and " { $link with-output-stream } "; see " { $link "stdio" } " for details." ;
-ARTICLE: "destructors" "Deterministic resource disposal"
-"Operating system resources such as streams, memory mapped files, and so on are not managed by Factor's garbage collector and must be released when you are done with them. Failing to release a resource can lead to reduced performance and instability."
-$nl
-"Disposable object protocol:"
+ARTICLE: "destructors-using" "Using destructors"
+"Disposing of an object:"
{ $subsection dispose }
-{ $subsection dispose* }
"Utility word for scoped disposal:"
{ $subsection with-disposal }
"Utility word for disposing multiple objects:"
"Utility words for more complex disposal patterns:"
{ $subsection with-destructors }
{ $subsection &dispose }
-{ $subsection |dispose }
-{ $subsection "destructors-anti-patterns" } ;
+{ $subsection |dispose } ;
+
+ARTICLE: "destructors-extending" "Writing new destructors"
+"Superclass for disposable objects:"
+{ $subsection disposable }
+"Parametrized constructor for disposable objects:"
+{ $subsection new-disposable }
+"Generic disposal word:"
+{ $subsection dispose* }
+"Global set of disposable objects:"
+{ $subsection disposables } ;
+
+ARTICLE: "destructors" "Deterministic resource disposal"
+"Operating system resources such as streams, memory mapped files, and so on are not managed by Factor's garbage collector and must be released when you are done with them. Failing to release a resource can lead to reduced performance and instability."
+{ $subsection "destructors-using" }
+{ $subsection "destructors-extending" }
+{ $subsection "destructors-anti-patterns" }
+{ $see-also "tools.destructors" } ;
ABOUT: "destructors"
-! Copyright (C) 2007, 2008 Doug Coleman, Slava Pestov.
+! Copyright (C) 2007, 2009 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors continuations kernel namespaces make
-sequences vectors ;
+sequences vectors sets assocs init ;
IN: destructors
-TUPLE: disposable disposed ;
+SYMBOL: disposables
+
+[ H{ } clone disposables set-global ] "destructors" add-init-hook
+
+<PRIVATE
+
+: register-disposable ( obj -- )
+ disposables get conjoin ;
+
+: unregister-disposable ( obj -- )
+ disposables get delete-at ;
+
+PRIVATE>
+
+TUPLE: disposable < identity-tuple disposed id ;
+
+M: disposable hashcode* nip id>> ;
+
+: new-disposable ( class -- disposable )
+ new \ disposable counter >>id
+ dup register-disposable ; inline
GENERIC: dispose* ( disposable -- )
M: object dispose
dup disposed>> [ drop ] [ t >>disposed dispose* ] if ;
+M: disposable dispose
+ [ unregister-disposable ] [ call-next-method ] bi ;
+
: dispose-each ( seq -- )
[
[ [ dispose ] curry [ , ] recover ] each
accessors combinators ;
IN: io.streams.c
-TUPLE: c-stream handle disposed ;
+TUPLE: c-stream < disposable handle ;
+
+: new-c-stream ( handle class -- c-stream )
+ new-disposable swap >>handle ; inline
M: c-stream dispose* handle>> fclose ;
TUPLE: c-writer < c-stream ;
-: <c-writer> ( handle -- stream ) f c-writer boa ;
+: <c-writer> ( handle -- stream ) c-writer new-c-stream ;
M: c-writer stream-element-type drop +byte+ ;
TUPLE: c-reader < c-stream ;
-: <c-reader> ( handle -- stream ) f c-reader boa ;
+: <c-reader> ( handle -- stream ) c-reader new-c-stream ;
M: c-reader stream-element-type drop +byte+ ;