Browse Source

Fixed .1 issue+GC cleanups.

Mark Sibly 8 years ago
parent
commit
2405983b50

+ 28 - 56
modules/monkey/native/bbgc.cpp

@@ -8,40 +8,6 @@
 // For testing only...
 // For testing only...
 // #define BBGC_DISABLED 1
 // #define BBGC_DISABLED 1
 
 
-//For future ref...
-#if BB_THREADED
-
-//fast but unpredictable
-//#define BBGC_LOCK while( bbGC::spinlock.test_and_set( std::memory_order_acquire ) ) std::this_thread::yield();
-//#define BBGC_UNLOCK bbGC::spinlock.clear( std::memory_order_release );
-
-//pretty slow...
-//#define BBGC_LOCK bbGC::mutex.lock();
-//#define BBGC_UNLOCK bbGC::mutex.unlock();
-
-//better...a 'Benaphore' apparently...
-#define BBGC_LOCK \
-	if( ++bbGC::locks>1 ){ \
-		std::unique_lock<std::mutex> lock( bbGC::mutex ); \
-		bbGC::cond_var.wait( lock,[]{ return bbGC::sem_count>0;} ); \
-		--bbGC::sem_count; \
-	}
-
-#define BBGC_UNLOCK \
-	if( --bbGC::locks>0 ){ \
-		std::unique_lock<std::mutex> lock( bbGC::mutex ); \
-		++bbGC::sem_count; \
-		bbGC::cond_var.notify_one(); \
-	}
-	
-	int sem_count;
-	std::mutex mutex;
-	std::atomic_int locks;
-	std::condition_variable cond_var;
-	std::atomic_flag spinlock=ATOMIC_FLAG_INIT;
-	
-#endif
-
 namespace bbGC{
 namespace bbGC{
 
 
 	size_t trigger=4*1024*1024;
 	size_t trigger=4*1024*1024;
@@ -72,16 +38,16 @@ namespace bbGC{
 	
 	
 	size_t allocedBytes;
 	size_t allocedBytes;
 	
 	
-	
 	void *pools[32];
 	void *pools[32];
 	
 	
 	unsigned char *poolBuf;
 	unsigned char *poolBuf;
 	size_t poolBufSize;
 	size_t poolBufSize;
 	
 	
+	bool inited;
+	
 	void init(){
 	void init(){
-		static bool done;
-		if( done ) return;
-		done=true;
+		if( inited ) return;
+		inited=true;
 
 
 		markedBit=1;
 		markedBit=1;
 		markedList=&markLists[0];
 		markedList=&markLists[0];
@@ -289,13 +255,27 @@ namespace bbGC{
 	
 	
 	void *malloc( size_t size ){
 	void *malloc( size_t size ){
 	
 	
-		size=(size+sizeof( size_t )+7)&~7;
+//		if( !inited ){ printf( "GC not inited!\n" );fflush( stdout ); }
+	
+		size=(size+sizeof(size_t)+7)&~7;
+		
+		if( size<256 && pools[size>>3] ){
+			void *p=pools[size>>3];
+			pools[size>>3]=*(void**)p;
+			allocedBytes+=size;
+			size_t *q=(size_t*)p;
+			*q++=size;
+			return q;
+		}
 		
 		
 		if( !suspended ){
 		if( !suspended ){
 			
 			
 			if( allocedBytes+size>=trigger ){
 			if( allocedBytes+size>=trigger ){
+				
 				sweep();
 				sweep();
+				
 			}else{
 			}else{
+			
 				markQueued( double( allocedBytes+size ) / double( trigger ) * double( unmarkedBytes + trigger ) );
 				markQueued( double( allocedBytes+size ) / double( trigger ) * double( unmarkedBytes + trigger ) );
 			}
 			}
 			
 			
@@ -305,32 +285,24 @@ namespace bbGC{
 		void *p;
 		void *p;
 		
 		
 		if( size<256 ){
 		if( size<256 ){
-			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 );
+			if( size>poolBufSize ){
+				if( poolBufSize ){
+					*(void**)poolBuf=pools[poolBufSize>>3];
+					pools[poolBufSize>>3]=poolBuf;
 				}
 				}
-				p=poolBuf;
-				poolBuf+=size;
-				poolBufSize-=size;
+				poolBufSize=65536;
+				poolBuf=(unsigned char*)::malloc( poolBufSize );
 			}
 			}
+			p=poolBuf;
+			poolBuf+=size;
+			poolBufSize-=size;
 		}else{
 		}else{
 			p=::malloc( size );
 			p=::malloc( size );
 		}
 		}
 		
 		
 		allocedBytes+=size;
 		allocedBytes+=size;
-		
 		size_t *q=(size_t*)p;
 		size_t *q=(size_t*)p;
-		
 		*q++=size;
 		*q++=size;
-		
 		return q;
 		return q;
 	}
 	}
 	
 	

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

@@ -542,19 +542,19 @@ bbString::operator bbUShort()const{
 }
 }
 
 
 bbString::operator bbUInt()const{
 bbString::operator bbUInt()const{
-	bbUInt n;
+	bbUInt n=0;
 	sscanf( c_str(),"%u",&n );
 	sscanf( c_str(),"%u",&n );
 	return n;
 	return n;
 }
 }
 
 
 bbString::operator bbLong()const{
 bbString::operator bbLong()const{
-	bbLong n;
+	bbLong n=0;
 	sscanf( c_str(),"%lld",&n );
 	sscanf( c_str(),"%lld",&n );
 	return n;
 	return n;
 }
 }
 
 
 bbString::operator bbULong()const{
 bbString::operator bbULong()const{
-	bbULong n;
+	bbULong n=0;
 	sscanf( c_str(),"%llu",&n );
 	sscanf( c_str(),"%llu",&n );
 	return n;
 	return n;
 }
 }
@@ -571,12 +571,12 @@ bbString::operator double()const{
 
 
 bbCString::bbCString( const bbString &str ){
 bbCString::bbCString( const bbString &str ){
 	int size=str.utf8Length()+1;
 	int size=str.utf8Length()+1;
-	_data=(char*)malloc( size );
+	_data=(char*)bbMalloc( size );
 	str.toCString( _data,size );
 	str.toCString( _data,size );
 }
 }
 
 
 bbCString::~bbCString(){
 bbCString::~bbCString(){
-	free( _data );
+	bbFree( _data );
 }
 }
 
 
 bbCString::operator char*()const{
 bbCString::operator char*()const{
@@ -595,15 +595,14 @@ bbCString::operator unsigned char*()const{
 
 
 bbWString::bbWString( const bbString &str ){
 bbWString::bbWString( const bbString &str ){
 	int size=(str.length()+1)*sizeof(wchar_t);
 	int size=(str.length()+1)*sizeof(wchar_t);
-	_data=(wchar_t*)malloc( size );
+	_data=(wchar_t*)bbMalloc( size );
 	str.toWString( _data,size );
 	str.toWString( _data,size );
 }
 }
 
 
 bbWString::~bbWString(){
 bbWString::~bbWString(){
-	free( _data );
+	bbFree( _data );
 }
 }
 
 
 bbWString::operator wchar_t*()const{
 bbWString::operator wchar_t*()const{
 	return _data;
 	return _data;
 }
 }
-

+ 13 - 0
modules/monkey/native/bbstring.h

@@ -6,6 +6,11 @@
 #include "bbassert.h"
 #include "bbassert.h"
 #include "bbmemory.h"
 #include "bbmemory.h"
 
 
+namespace bbGC{
+	void *malloc( size_t size );
+	void free( void *p );
+}
+
 class bbCString;
 class bbCString;
 
 
 class bbString{
 class bbString{
@@ -15,7 +20,15 @@ class bbString{
 		int length;
 		int length;
 		bbChar data[0];
 		bbChar data[0];
 		
 		
+//		Rep():refs( $80000000 ),length( 0 ){
+//		}
+		
+//		Rep( int length ):refs( 1 ),length( length ){
+//		}
+		
 		static Rep *alloc( int length );
 		static Rep *alloc( int length );
+//			return new( bbGC::malloc( sizeof( Rep )+length*sizeof( bbChar ) ) ) Rep( length );
+//		}
 		
 		
 		template<class C> static Rep *create( const C *p,int length ){
 		template<class C> static Rep *create( const C *p,int length ){
 			Rep *rep=alloc( length );
 			Rep *rep=alloc( length );