[cig-commits] commit: Changing the way the Toolboxes manager handles remembering which Toolboxes it has loaded and unloaded.

Mercurial hg at geodynamics.org
Tue Mar 23 10:35:12 PDT 2010


changeset:   788:b4b09b400465
branch:      1.4.x
tag:         1.4.1
user:        JulianGiordani
date:        Fri Mar 12 15:41:54 2010 +1100
files:       Base/Extensibility/src/ToolboxesManager.c Base/Extensibility/src/ToolboxesManager.h
description:
Changing the way the Toolboxes manager handles remembering which Toolboxes it has loaded and unloaded.
Previously it was using a char* array, to flag things as initialised - this had a bug.
The char* array has been replaced with a Stg_ObjectList called initTB, any initialised toolboxes (initialised only via the ToolboxesManager) adds the toolbox to this list. Unloaded toolboxes remove themselves from the list.

The bug in the previous code was causing Finalisation issues, e.g PetscFinalize() wasn't been called, dangling pointers weren't being cleaned.


diff -r 0560f20624bd -r b4b09b400465 Base/Extensibility/src/ToolboxesManager.c
--- a/Base/Extensibility/src/ToolboxesManager.c	Tue Mar 16 12:40:44 2010 +1100
+++ b/Base/Extensibility/src/ToolboxesManager.c	Fri Mar 12 15:41:54 2010 +1100
@@ -85,30 +85,19 @@ void _ToolboxesManager_Init( void* toolb
 void _ToolboxesManager_Init( void* toolboxesManager, int* argc, char*** argv ) {
 	ToolboxesManager*         self = (ToolboxesManager*)toolboxesManager;
 	
+   self->initTB = Stg_ObjectList_New();
 	self->argc = argc;
 	self->argv = argv;
-	self->_initialisedSize = 8;
-	self->initialised = Memory_Alloc_Array( char*, self->_initialisedSize, ToolboxesManager_Type );
-	self->_initialisedCount = 0;
 }
 
 
 void _ToolboxesManager_Delete( void* toolboxesManager ) {
-	ToolboxesManager*         self = (ToolboxesManager*)toolboxesManager;
-  unsigned count;
+   ToolboxesManager*         self = (ToolboxesManager*)toolboxesManager;
+   int ii, originalListSize;
 
-  /* free all strings, defining loaded Toolboxes */
-  for( count = 0 ; count < self->_initialisedCount ; count++ ) {
-    Memory_Free( self->initialised[count] );
-  }
-	Memory_Free( self->initialised );
-	self->_initialisedSize = 0;
-	self->_initialisedCount = 0;
-	self->initialised = 0;
-	
 	Stg_ObjectList_DeleteAllObjects( self->codelets );
 	Stg_Class_Delete( self->codelets );
-	ModulesManager_Unload( self ); 
+	ModulesManager_Unload( self );  /* this will unload all toolboxes implicitly */
 	Stg_Class_Delete( self->modules );
 	
 	/* Delete parent */
@@ -121,13 +110,13 @@ void _ToolboxesManager_Print( void* tool
 	/* General info */
 	Journal_Printf( (void*) stream, "Toolboxes (ptr): %p\n", self );
 	
-	if( self->_initialisedCount > 0 ) {
+	if( Stg_ObjectList_Count(self->initTB) > 0 ) {
 		Index i;
 		
 		Journal_Printf( stream, "Initialised Modules:\n" );
 		Stream_Indent( stream );
-		for( i = 0; i < self->_initialisedCount; ++i ) {
-			Journal_Printf( stream, "%s\n", self->initialised[i] );
+		for( i = 0; i < Stg_ObjectList_Count(self->initTB) ; ++i ) {
+			Journal_Printf( stream, "%s\n", self->initTB->data[i]->name );
 		}
 		Stream_UnIndent( stream );
 	}
@@ -150,16 +139,26 @@ Bool _ToolboxesManager_LoadToolbox( void
 Bool _ToolboxesManager_LoadToolbox( void* toolboxesManager, Module* toolbox ) {
 	ToolboxesManager* self = (ToolboxesManager*)toolboxesManager;
 	
-	((Toolbox*)toolbox)->Initialise( self, self->argc, self->argv );
-	((Toolbox*)toolbox)->Register( self );
-    
+   /* if not Loaded call the Initialise() and Register() */
+   if( !Stg_ObjectList_Get( self->initTB, toolbox->name ) ) {
+
+      ((Toolbox*)toolbox)->Initialise( self, self->argc, self->argv );
+      ((Toolbox*)toolbox)->Register( self );
+
+      Stg_ObjectList_Append( self->initTB, toolbox );
+   }
 	return True;
 }
 
 Bool _ToolboxesManager_UnloadToolbox( void* toolboxesManager, Module* toolbox ) {
 	ToolboxesManager* self = (ToolboxesManager*)toolboxesManager;
 	
-	((Toolbox*)toolbox)->Finalise( self );
+   if( Stg_ObjectList_Get( self->initTB, toolbox->name ) ) {
+      ((Toolbox*)toolbox)->Finalise( self );
+
+      /* remove the toolbox from the initTB list, but don't actually Delete it's memory */
+      Stg_ObjectList_Remove( self->initTB, toolbox->name, KEEP );
+   }
     
 	return True;
 }
@@ -173,36 +172,14 @@ Name _ToolboxesManager_GetModuleName( vo
 	return Dictionary_Entry_Value_AsString( Dictionary_Entry_Value_GetElement( moduleVal, entry_I ) );
 }
 
-Index ToolboxesManager_SetInitialised( void* initRegister, char* label ) {
+Bool ToolboxesManager_IsInitialised( void* initRegister, char* label ) {
 	ToolboxesManager* self = (ToolboxesManager*)initRegister;
-	
-	if( self->_initialisedCount == self->_initialisedSize ) {
-		Index oldSize = self->_initialisedSize;
-		char** tmp;
-		
-		self->_initialisedSize += 8;
-		tmp = Memory_Alloc_Array( char*, self->_initialisedSize, ToolboxesManager_Type );
-		memcpy( tmp, self->initialised, sizeof( char* ) * oldSize );
-		Memory_Free( self->initialised );
-		self->initialised = tmp;
-	}
-	
-	self->initialised[self->_initialisedCount] = StG_Strdup( label );
-	self->_initialisedCount += 1;
-	return self->_initialisedCount - 1;
+
+   if( Stg_ObjectList_Get( self->initTB, label ) )
+      return True;
+   else
+      return False;
 }
 
-Bool ToolboxesManager_IsInitialised( void* initRegister, char* label ) {
-	ToolboxesManager* self = (ToolboxesManager*)initRegister;
-	Index i;
-	
-	for( i = 0; i < self->_initialisedCount; i++ ) {
-		if( strcmp( label, self->initialised[i] ) == 0 ) {
-			return True;
-		}
-	}
-	return False;
-}
 
 
-
diff -r 0560f20624bd -r b4b09b400465 Base/Extensibility/src/ToolboxesManager.h
--- a/Base/Extensibility/src/ToolboxesManager.h	Tue Mar 16 12:40:44 2010 +1100
+++ b/Base/Extensibility/src/ToolboxesManager.h	Fri Mar 12 15:41:54 2010 +1100
@@ -45,20 +45,18 @@
 	/* Textual name of this class */
 	extern const Type ToolboxesManager_Type;
 
-	
 	/* Toolboxes info */
-	#define __ToolboxesManager \
-		/* General info */ \
-		__ModulesManager \
-		\
-		/* Virtual info */ \
-		\
-		/* Toolboxes info */ \
-		int*       argc; \
-		char***    argv; \
-		char**     initialised; \
-		Index      _initialisedSize; \
-		Index      _initialisedCount;
+   #define __ToolboxesManager \
+      /* General info */ \
+      __ModulesManager \
+      \
+      /* Virtual info */ \
+      \
+      /* Toolboxes info */ \
+      Stg_ObjectList *initTB; /* list of initialised toolboxes */ \
+      int*       argc; \
+      char***    argv;
+		
 		
 	struct ToolboxesManager { __ToolboxesManager };
 	
@@ -110,10 +108,6 @@
 
 	#define ToolboxesManager_Submit ModulesManager_Submit
 
-	/** Let StGermain know that the "Init" function of a module has been called. This exists to handle the case where a module
-	   is linked into a binary and the user attempts to module load the module too. */
-	Index ToolboxesManager_SetInitialised( void* toolboxesManager, char* label );
-	
 	/** This exists to handle the case where a module is linked into a binary and the user attempts to module load the module too. 
 	   Its expected at modules will check to see if they have been inited already before doing initialisation work in the init 
 	   function. */



More information about the CIG-COMMITS mailing list