Przeglądaj źródła

GC fixes and cleanups.

Mark Sibly 8 lat temu
rodzic
commit
be4914435f

+ 8 - 0
modules/monkey/native/bbarray.h

@@ -79,6 +79,14 @@ template<class T,int D> struct bbArray{
 		bbGC::endCtor( _rep );
 	}
 	
+	void retain()const{
+		bbGC::retain( _rep );
+	}
+	
+	void release()const{
+		bbGC::release( _rep );
+	}
+	
 	void discard(){
 		_rep=nullptr;
 	}

+ 59 - 91
modules/monkey/native/bbgc.cpp

@@ -1,13 +1,6 @@
 
-//v1001
-
-#include <utility>
-
 #include "bbgc.h"
 
-// For testing only...
-// #define BBGC_DISABLED 1
-
 namespace bbDB{
 
 	void stop();
@@ -23,8 +16,6 @@ namespace bbGC{
 	
 	int malloced;
 
-	bool debug;
-	
 	size_t trigger=4*1024*1024;
 
 	int suspended=1;
@@ -82,11 +73,6 @@ namespace bbGC{
 		suspended=0;
 	}
 	
-	void setDebug( bool debug ){
-	
-		bbGC::debug=debug;
-	}
-	
 	void setTrigger( size_t size ){
 	
 		trigger=size;
@@ -102,43 +88,11 @@ namespace bbGC{
 		--suspended;
 	}
 	
-	void destroy( bbGCNode *p ){
-	
-		if( p->flags & 1 ){
-			
-			//Run finalizer
-			//printf( "finalizing: %s %p\n",p->typeName(),p );fflush( stdout );
-			
-			++suspended;
-			
-			p->state=unmarkedBit;
-			
-			p->gcFinalize();
-			
-			if( p->state==markedBit ) bbRuntimeError( "Object resurrected in finalizer" );
-				
-			--suspended;
-		}
-		
-		//printf( "destroying: %s %p\n",p->typeName(),p );fflush( stdout );
-		
-#if BBGC_DEBUG
-//		printf( "destroying: %s %p\n",p->typeName(),p );
-		p->state=3;
-		p->flags=0;
-#else
-		
-		p->~bbGCNode();
-			
-		bbGC::free( p );
-#endif
-	}
-	
 	void reclaim( size_t size=0x7fffffff ){
 	
 		size_t freed=0;
 	
-		while( freeList.succ!=&freeList ){
+		while( freeList.succ!=&freeList && freed<size ){
 		
 			bbGCNode *p=freeList.succ;
 			
@@ -146,26 +100,42 @@ namespace bbGC{
 			
 			remove( p );
 			
-			destroy( p );
+			if( p->flags & 1 ){
+				
+				//printf( "finalizing: %s %p\n",p->typeName(),p );fflush( stdout );
+				
+				++suspended;
+				
+				p->state=unmarkedBit;
+				
+				p->gcFinalize();
+				
+				if( p->state==markedBit ) bbRuntimeError( "Object resurrected in finalizer" );
+					
+				--suspended;
+			}
+			
+			p->~bbGCNode();
 			
-			if( freed>=size ) break;
+			bbGC::free( p );
 		}
 	}
 	
-	void mark( bbGCNode *p ){
-
-		if( !p || p->state==markedBit ) return;
-		
-		remove( p );
-		insert( p,markedList );
-		
-		markedBytes+=mallocSize( p );
+	void markQueued( size_t tomark=0x7fffffff ){
+	
+		while( markQueue && markedBytes<tomark ){
 
-		p->state=markedBit;
-		
-		p->gcMark();
+			bbGCNode *p=markQueue;
+			markQueue=p->succ;
+			
+			insert( p,markedList );
+			
+			markedBytes+=mallocSize( p );
+			
+			p->gcMark();
+		}
 	}
-	
+
 	void markRoots(){
 	
 		for( bbGCRoot *root=roots;root;root=root->succ ){
@@ -178,7 +148,7 @@ namespace bbGC{
 	
 		for( bbGCTmp *tmp=retained;tmp;tmp=tmp->succ ){
 		
-			tmp->node->gcMark();
+			enqueue( tmp->node );
 		}
 	}
 	
@@ -202,7 +172,7 @@ namespace bbGC{
 			
 			for( bbGCTmp *tmp=fiber->tmps;tmp;tmp=tmp->succ ){
 			
-				if( tmp->node ) tmp->node->gcMark();
+				enqueue( tmp->node );
 			}
 			
 			fiber=fiber->succ;
@@ -211,21 +181,6 @@ namespace bbGC{
 		}
 	}
 	
-	void markQueued( size_t tomark=0x7fffffff ){
-	
-		while( markQueue && markedBytes<tomark ){
-
-			bbGCNode *p=markQueue;
-			markQueue=p->succ;
-			
-			insert( p,markedList );
-			
-			markedBytes+=mallocSize( p );
-			
-			p->gcMark();
-		}
-	}
-
 	void sweep(){
 	
 		markRetained();
@@ -259,6 +214,8 @@ namespace bbGC{
 	}
 	
 	void retain( bbGCNode *node ){
+		BBGC_VALIDATE( node );
+
 		if( !node ) return;
 		
 		bbGCTmp *tmp=freeTmps;
@@ -290,6 +247,7 @@ namespace bbGC{
 		
 		memused+=size;
 		
+		/*
 		if( size<256 && pools[size>>3] ){
 			void *p=pools[size>>3];
 			pools[size>>3]=*(void**)p;
@@ -298,6 +256,7 @@ namespace bbGC{
 			*q++=size;
 			return q;
 		}
+		*/
 		
 		if( !suspended ){
 
@@ -316,18 +275,27 @@ namespace bbGC{
 		void *p;
 		
 		if( size<256 ){
-			if( size>poolBufSize ){
-				if( poolBufSize ){
-					*(void**)poolBuf=pools[poolBufSize>>3];
-					pools[poolBufSize>>3]=poolBuf;
+			
+			if( pools[size>>3] ){
+				
+				p=pools[size>>3];
+				pools[size>>3]=*(void**)p;
+				
+			}else{
+			
+				if( size>poolBufSize ){
+					if( poolBufSize ){
+						*(void**)poolBuf=pools[poolBufSize>>3];
+						pools[poolBufSize>>3]=poolBuf;
+					}
+					poolBufSize=65536;
+					poolBuf=(unsigned char*)::malloc( poolBufSize );
+					malloced+=poolBufSize;
 				}
-				poolBufSize=65536;
-				poolBuf=(unsigned char*)::malloc( poolBufSize );
-				malloced+=poolBufSize;
+				p=poolBuf;
+				poolBuf+=size;
+				poolBufSize-=size;
 			}
-			p=poolBuf;
-			poolBuf+=size;
-			poolBufSize-=size;
 		}else{
 			p=::malloc( size );
 			malloced+=size;
@@ -355,9 +323,9 @@ namespace bbGC{
 		
 		size_t size=*q;
 		
-		if( debug ){
-//			printf( "bbGC::free %p size=%i\n",q,size );fflush( stdout );
-		}
+		#ifndef NDEBUG
+		memset( q,0xa5,size );
+		#endif
 		
 		memused-=size;
 		

+ 8 - 9
modules/monkey/native/bbgc.h

@@ -7,16 +7,13 @@
 #include "bbmemory.h"
 #include "bbfunction.h"
 
-//check for use of deleted objects, MUCH leakier...
-//#define BBGC_DEBUG 1
-
-#if BBGC_DEBUG
+#ifndef NDEBUG
 #define BBGC_VALIDATE( P ) \
-	if( (P) && (P)->state==3 ){ \
-		printf( "Attempt to use deleted object %p of type '%s'\n",(P),(P)->typeName() ); \
-		fflush( stdout ); \
-		abort(); \
-	}
+if( (P) && ((P)->state!=0 && (P)->state!=1 && (P)->state!=2) ){ \
+	printf( "BBGC_VALIDATE failed: %p %s %i\n",(P),(P)->typeName(),(P)->state ); \
+	fflush( stdout ); \
+	abort(); \
+}
 #else
 #define BBGC_VALIDATE( P )
 #endif
@@ -181,6 +178,8 @@ namespace bbGC{
 	}
 	
 	inline void pushTmp( bbGCNode *p ){
+		BBGC_VALIDATE( p );
+		
 		bbGCTmp *tmp=freeTmps;
 		if( !tmp ) tmp=new bbGCTmp;
 		tmp->node=p;

+ 21 - 7
modules/monkey/native/bbstring.cpp

@@ -298,10 +298,17 @@ bbString bbString::fromChar( int chr ){
 bbArray<bbString> bbString::split( bbString sep )const{
 
 	if( !sep.length() ){
+		
 		bbArray<bbString> bits=bbArray<bbString>( length() );
+		
+		bits.retain();
+		
 		for( int i=0;i<length();++i ){
 			bits[i]=bbString( &data()[i],1 );
 		}
+		
+		bits.release();
+		
 		return bits;
 	}
 	
@@ -310,17 +317,24 @@ bbArray<bbString> bbString::split( bbString sep )const{
 		++n;
 		i=i2+sep.length();
 	}
+	
 	bbArray<bbString> bits=bbArray<bbString>( n );
+	
+	bits.retain();
+	
 	if( n==1 ){
 		bits[0]=*this;
-		return bits;
-	}
-	i=0;n=0;
-	while( (i2=find( sep,i ))!=-1 ){
-		bits[n++]=slice( i,i2 );
-		i=i2+sep.length();
+	}else{
+		i=0;n=0;
+		while( (i2=find( sep,i ))!=-1 ){
+			bits[n++]=slice( i,i2 );
+			i=i2+sep.length();
+		}
+		bits[n]=slice( i );
 	}
-	bits[n]=slice( i );
+	
+	bits.release();
+	
 	return bits;
 }