ソースを参照

Added Finalizers;Fixes for resources.

Mark Sibly 8 年 前
コミット
c6be108efb

+ 2 - 0
modules/std/collections/map.monkey2

@@ -46,6 +46,8 @@ Class Map<K,V>
 		#end
 		Property Value:V()
 			Return _value
+		Setter( value:V )
+			_value=value
 		End
 	
 		Private	

+ 42 - 1
modules/std/graphics/color.monkey2

@@ -97,10 +97,51 @@ Namespace std.graphics
 	#end
 	Const Pink:=New Color( 1,.75,.8 )
 	
-	#rem monkeydoc HotPink!
+	#rem monkeydoc HotPink.
 	#end
 	Const HotPink:=New Color( 1,.41,.71 )
 	
+	#rem monkeydoc Silver.
+	#end
+	Const Silver:=New Color( .971519,.959915,.915324,1 )
+	
+	#rem monkeydoc Aluminum.
+	#end
+	Const Aluminum:=New Color( .913183,.921494,.924524,1 )
+	
+	#rem monkeydoc Gold.
+	#end
+	Const Gold:=New Color( 1,.765557,.336057,1 )
+	
+	#rem monkeydoc Copper.
+	#end
+	Const Copper:=New Color( .955008,.637427,.538163,1 )
+	
+	#rem monkeydoc Chromium.
+	#end
+	Const Chromium:=New Color( .549585,.556114,.554256,1 )
+	
+	#rem monkeydoc Nickel.
+	#end
+	Const Nickel:=New Color( .659777,.608679,.525649,1 )
+	
+	#rem monkeydoc Titanium.
+	#end
+	Const Titanium:=New Color( .541931,.496791,.449419,1 )
+	
+	#rem monkeydoc Cobalt.
+	#end
+	Const Cobalt:=New Color( .662124,.654864,.633732,1 )
+	
+	#rem monkeydoc Platinum.
+	#end
+	Const Platinum:=New Color( .672411,.637331,.585456,1 )
+	
+	
+	#rem monkeydoc SeaGreen.
+	#end
+	Const SeaGreen:=New Color( .031372,.301960,.247058,1 )
+	
 #rem
 	#rem monkeydoc Transparent black.
 	#end

+ 19 - 8
modules/std/graphics/pixmap.monkey2

@@ -31,19 +31,15 @@ Class Pixmap Extends Resource
 
 		Local depth:=PixelFormatDepth( format )
 		Local pitch:=width*depth
-		Local data:=Cast<UByte Ptr>( libc.malloc( pitch*height ) )
+		Local data:=Cast<UByte Ptr>( GCMalloc( pitch*height ) )
 		
 		_width=width
 		_height=height
 		_format=format
 		_depth=depth
+		_gcdata=data
 		_data=data
 		_pitch=pitch
-		
-		OnDiscarded+=Lambda()
-			libc.free( _data )
-			_data=Null
-		End
 	End
 	
 	Method New( width:Int,height:Int,format:PixelFormat,data:UByte Ptr,pitch:Int )
@@ -467,8 +463,6 @@ Class Pixmap Extends Resource
 		
 		Local pixmap:=New Pixmap( width,height,_format,PixelPtr( x,y ),_pitch )
 		
-		AddDependancy( pixmap )
-		
 		Return pixmap
 	End
 	
@@ -500,6 +494,20 @@ Class Pixmap Extends Resource
 		
 		Return pixmap
 	End
+	
+	Protected
+	
+	Method OnDiscard() Override
+		
+		GCFree( _gcdata )
+		_gcdata=Null
+		_data=null
+	End
+	
+	Method Finalize() Override
+		
+		GCFree( _gcdata )
+	End
 
 	Private
 	
@@ -507,6 +515,7 @@ Class Pixmap Extends Resource
 	Field _height:Int
 	Field _format:PixelFormat
 	Field _depth:Int
+	Field _gcdata:UByte Ptr
 	Field _data:UByte Ptr
 	Field _pitch:Int
 
@@ -522,8 +531,10 @@ Class ResourceManager Extension
 		If pixmap Return pixmap
 		
 		pixmap=Pixmap.Load( path,format,pmAlpha )
+		If Not pixmap Return Null
 		
 		AddResource( slug,pixmap )
+		
 		Return pixmap
 	End
 	

+ 26 - 6
modules/std/graphics/pixmaploader.monkey2

@@ -25,6 +25,31 @@ Function stbi_eof:Int( user:Void Ptr )
 	Return stream.Eof
 End
 
+'this is a bit nasty but meh...
+Class StbPixmap Extends Pixmap
+	
+	Method New( width:Int,height:Int,format:PixelFormat,data:UByte Ptr,pitch:Int )
+		Super.New( width,height,format,data,pitch )
+		
+		_data=data
+	End
+	
+	Protected
+	
+	Method OnDiscard() Override
+		stbi_image_free( _data )
+		_data=Null
+	End
+	
+	Method Finalize() Override
+		stbi_image_free( _data )
+	End
+	
+	Private
+	
+	Field _data:UByte Ptr
+End
+
 Public
 
 #rem monkeydoc @hidden
@@ -62,12 +87,7 @@ Function LoadPixmap:Pixmap( path:String,format:PixelFormat )
 		End
 	End
 	
-	Local pixmap:=New Pixmap( x,y,format,data,x*PixelFormatDepth( format ) )
-	
-	pixmap.OnDiscarded+=Lambda()
-		stbi_image_free( data )
-	End
+	Local pixmap:=New StbPixmap( x,y,format,data,x*PixelFormatDepth( format ) )
 	
 	Return pixmap
-
 End

+ 16 - 9
modules/std/memory/databuffer.monkey2

@@ -43,16 +43,11 @@ Class DataBuffer Extends std.resource.Resource
 	
 	#end
 	Method New( length:Int,byteOrder:ByteOrder=std.memory.ByteOrder.LittleEndian )
-	
+		
 		_length=length
-		_data=Cast<UByte Ptr>( libc.malloc( length ) )
+		_data=Cast<UByte Ptr>( GCMalloc( length ) )
 		_byteOrder=byteOrder
 		_swapEndian=False
-		
-		OnDiscarded+=Lambda()
-			libc.free( _data )
-			_data=Null
-		End
 	End
 	
 	#rem monkeydoc A raw pointer to the databuffer's internal memory.
@@ -87,9 +82,9 @@ Class DataBuffer Extends std.resource.Resource
 	
 	#end	
 	Method Resize( length:Int )
-		Local data:=Cast<UByte Ptr>( libc.malloc( length ) )
+		Local data:=Cast<UByte Ptr>( GCMalloc( length ) )
 		libc.memcpy( data,_data,Min( length,_length ) )
-		libc.free( _data )
+		GCFree( _data )
 		_data=data
 	End
 	
@@ -523,7 +518,19 @@ Class DataBuffer Extends std.resource.Resource
 		
 		Return data
 	End
+	
+	Protected
+	
+	Method OnDiscard() Override
+		
+		GCFree( _data )
+	End
 
+	Method Finalize() Override
+		
+		GCFree( _data )
+	End
+	
 	Private
 	
 	Field _data:UByte Ptr

+ 41 - 149
modules/std/resource/resource.monkey2

@@ -1,187 +1,64 @@
 
 Namespace std.resource
 
-#rem
-
-Ok, the basic rules for resource management are:
-
-* If you create a resource using 'New' or 'Load', you must either Retain() it, Discard() it or add it as a dependancy of another resource (same as retaining it really).
-
-* If you Retain() a resource, you must eventually Release() it.
-
-* If you open a resource from a resource manager using an OpenBlah method, it will be managed for you.
-
-* Discarding() a resource manager releases any resources it is managing.
-
-Note:
-
-AddDependancy( r1,r2 ) is pretty much the same as:
-
-r2.Retain()
-r1.OnDiscarded+=Lambda()
-	r2.Release()
-End
-
-Implemented as a stack for now so I can debug it.
-
-#end
-
 Class Resource
-
-	#rem monkeydoc Invoked when the resource is discarded.
-	#end
-	Field OnDiscarded:Void()
 	
-	#rem monkeydoc Creates a new resource.
+	Field Discarded:Void()
 	
-	The reference count for a resource is initially 0.
-	
-	#end
-	Method New()
-
-		_live.Push( Self )
+	Property Refs:Int()
+		
+		Return _refs
 	End
 	
-	#rem monkeydoc True if resource has been discarded.
-	#end
-	Property Discarded:Bool()
-	
-		Return _refs=-1
-	End
-
-	#rem monkeydoc Retains the resource.
-	
-	Increments the resource's reference count by 1.
-	
-	Resources with a reference counter >0 will not be discarded.
-	
-	#end
 	Method Retain()
-		DebugAssert( _refs>=0 )
+		
+		If Not _refs Return
 		
 		_refs+=1
 	End
 	
-	#rem monkeydoc Releases the resource.
-	
-	Decrements the resource's reference count by 1.
-	
-	If the reference count becomes 0, the resource is discarded.
-	
-	#end
 	Method Release()
-
-		DebugAssert( _refs>0 )
 		
-		_refs-=1
+		If Not _refs Return
 		
-		If Not _refs Discard()
+		If _refs=1 Discard() Else _refs-=1
 	End
 	
-	#rem monkeydoc Discards the resource.
-	
-	If the resource's reference count is >0 or the resource has already been discarded, nothing happens.
-	
-	If the resource's reference count is 0, the resource is discarded. First, OnDiscard() is called, then OnDiscarded() and finally any dependancies are released.
-	
-	#end
 	Method Discard()
-		If _refs Return
-		
-		_refs=-1
-		
-		_live.Remove( Self )
-	
-		OnDiscard()
 		
-		OnDiscarded()
+		If Not _refs Return
 		
-		If Not _depends Return
+		_refs=0
 		
-		For Local r:=Eachin _depends
-			r.Release()
-		Next
+		OnDiscard()
 		
-		_depends=Null
+		Discarded()
 	End
 	
-	#rem monkeydoc Adds a dependancy to the resource.
-	
-	Adds `resource` to the list of dependancies for this resource and retains it.
-	
-	When this resource is eventually discarded, `resource` will be automatically released.
-	
-	#end
-	Method AddDependancy( resource:Resource )
-		DebugAssert( _refs>=0 And resource._refs>=0 )
-		
-		If Not _depends _depends=New Stack<Resource>
+	Method AddDependancy( r:Resource )
 		
-		_depends.Add( resource )
+		If Not r Return
 		
-		resource.Retain()
-	End
+		r.Retain()
 
-	#rem monkeydoc @hidden
-	#end	
-	Function NumLive:Int()
-	
-		Return _live.Length
+		Discarded+=r.Release
 	End
 	
 	Protected
 	
-	#rem monkeydoc Called when resource is discarded.
-	#end
 	Method OnDiscard() Virtual
 	End
 	
 	Private
 	
-	Global _live:=New Stack<Resource>
-	
-	Field _refs:Int
-	
-	Field _depends:Stack<Resource>
+	Field _refs:=1
 End
 
 Class ResourceManager Extends Resource
 
 	Method New()
-		If Not _managers
-			_managers=New Stack<ResourceManager>
-		Endif
-		_managers.Push( Self )
-	End
-	
-	Function DebugDeps( r:Resource,indent:String )
-	
-		If Not r._depends Return
-	
-		indent+="  "
-		For Local d:=Eachin r._depends
-			Print indent+String.FromCString( d.typeName() )+", refs="+d._refs
-			DebugDeps( d,indent )
-		Next
-		indent=indent.Slice( 0,-2 )
-		
-	End
-	
-	Function DebugAll()
-	
-		For Local manager:=Eachin _managers
-		
-			For Local it:=Eachin manager._retained
-			
-				Print it.Key+", refs="+it.Value._refs
-				DebugDeps( it.Value,"" )
 
-			Next
-		Next
-		
-		For Local r:=Eachin _live
-			'If Not r._slug Print String.FromCString( r.typeName() )+", ref="+r._refs+", slug="+r._slug
-		End
+		_managers.Push( Self )
 	End
 	
 	Method OpenResource:Resource( slug:String )
@@ -189,6 +66,7 @@ Class ResourceManager Extends Resource
 		For Local manager:=Eachin _managers
 		
 			Local r:=manager._retained[slug]
+
 			If Not r Continue
 			
 			If manager<>Self AddResource( slug,r )
@@ -200,15 +78,10 @@ Class ResourceManager Extends Resource
 	End
 	
 	Method AddResource( slug:String,r:Resource )
-		If Not r Return
-		
-		DebugAssert( Not r.Discarded,"Can't add discarded resource to resource manager" )
 
-		If _retained.Contains( slug ) Return
+		If Not r Or _retained.Contains( slug ) Return
 		
 		_retained[slug]=r
-		
-		AddDependancy( r )
 	End
 	
 	Protected
@@ -216,14 +89,33 @@ Class ResourceManager Extends Resource
 	Method OnDiscard() Override
 	
 		_managers.Remove( Self )
-	
+		
+		For Local it:=Eachin _retained
+			
+			it.Value.Release()
+				
+			it.Value=Null
+		Next
+		
 		_retained=Null
 	End
 	
 	Private
 	
-	Global _managers:Stack<ResourceManager>
+	Global _managers:=New Stack<ResourceManager>
+	
+	Global _refs:=New StringMap<Int>
 	
 	Field _retained:=New StringMap<Resource>
 
 End
+
+Function SafeRetain( r:Resource )
+	
+	If r r.Retain()
+End
+
+Function SafeRelease( r:Resource )
+	
+	If r r.Release()
+End

+ 7 - 0
modules/std/stream/filestream.monkey2

@@ -127,6 +127,13 @@ Class FileStream Extends Stream
 		Return New FileStream( file )
 	End
 	
+	Protected
+	
+	Method Finalize() Override
+		
+		If _file fclose( _file )
+	End
+	
 	Private
 	
 	Field _file:FILE Ptr