[cig-commits] r4823 - in long/3D/Gale/trunk/src/StGermain: . Base/Container/src

walter at geodynamics.org walter at geodynamics.org
Wed Oct 11 13:46:07 PDT 2006


Author: walter
Date: 2006-10-11 13:46:07 -0700 (Wed, 11 Oct 2006)
New Revision: 4823

Added:
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.c
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.h
Modified:
   long/3D/Gale/trunk/src/StGermain/
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/Container.h
   long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h
Log:
 r2880 at earth:  boo | 2006-10-11 13:42:29 -0700
  r2796 at earth (orig r3784):  RaquibulHassan | 2006-09-06 03:01:39 -0700
  Implementing a Memory pool which can be used to create temporary object caches. Memory pools can be used to speed up code by removing expensive calls to "malloc" in critical sections of the code.
  
  
 



Property changes on: long/3D/Gale/trunk/src/StGermain
___________________________________________________________________
Name: svk:merge
   - 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2879
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3783
   + 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2880
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3784

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/Container.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/Container.h	2006-10-11 20:46:04 UTC (rev 4822)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/Container.h	2006-10-11 20:46:07 UTC (rev 4823)
@@ -64,6 +64,7 @@
 	#include "MaxHeap.h"
 	#include "MPIRoutines.h"
 	#include "UIntMap.h"
+	#include "MemoryPool.h"
 	#include "RangeSet.h"
 	#include "Init.h"
 	#include "Finalise.h"

Added: long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.c	2006-10-11 20:46:04 UTC (rev 4822)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.c	2006-10-11 20:46:07 UTC (rev 4823)
@@ -0,0 +1,176 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+**
+** Copyright (C), 2003, Victorian Partnership for Advanced Computing (VPAC) Ltd, 110 Victoria Street, Melbourne, 3053, Australia.
+**
+** Authors:
+**	Stevan M. Quenette, Senior Software Engineer, VPAC. (steve at vpac.org)
+**	Patrick D. Sunter, Software Engineer, VPAC. (pds at vpac.org)
+**	Luke J. Hodkinson, Computational Engineer, VPAC. (lhodkins at vpac.org)
+**	Siew-Ching Tan, Software Engineer, VPAC. (siew at vpac.org)
+**	Alan H. Lo, Computational Engineer, VPAC. (alan at vpac.org)
+**	Raquibul Hassan, Computational Engineer, VPAC. (raq at vpac.org)
+**
+**  This library is free software; you can redistribute it and/or
+**  modify it under the terms of the GNU Lesser General Public
+**  License as published by the Free Software Foundation; either
+**  version 2.1 of the License, or (at your option) any later version.
+**
+**  This library is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+**  Lesser General Public License for more details.
+**
+**  You should have received a copy of the GNU Lesser General Public
+**  License along with this library; if not, write to the Free Software
+**  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+**
+** $Id: List.c 2038 2004-11-15 07:11:01Z RaquibulHassan $
+**
+**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+#include <Base/Foundation/Foundation.h>
+#include <Base/IO/IO.h>
+
+#include "types.h"
+#include "MemoryPool.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+const Type MemoryPool_Type = "MemoryPool";
+
+/*----------------------------------------------------------------------------------------------------------------------------------
+** Constructors
+*/
+MemoryPool* _MemoryPool_New(
+			SizeT							_sizeOfSelf,
+			Type							type,
+			Stg_Class_DeleteFunction*			_delete,
+			Stg_Class_PrintFunction*			_print,
+			Stg_Class_CopyFunction*				_copy,
+			int									elementSize,
+			int									numElements
+			)
+{
+	MemoryPool *self = NULL;
+
+	self = (MemoryPool*)_Stg_Class_New(
+							_sizeOfSelf,
+							type,
+							_delete,
+							_print,
+							_copy);
+
+	self->elementSize = elementSize;
+	self->numElements = numElements;
+	self->numElementsFree = numElements;
+
+	return self;
+}
+
+MemoryPool* MemoryPool_NewFunc( SizeT elementSize, int numElements )
+{
+	
+	MemoryPool* self;
+	
+	self = _MemoryPool_New( 
+		   					sizeof(MemoryPool),
+							MemoryPool_Type,
+							_MemoryPool_DeleteFunc,
+							_MemoryPool_PrintFunc,
+							NULL,
+							elementSize,
+							numElements);
+
+	/* Virtual functions */
+	_MemoryPool_Init( self );
+	return self;
+}
+
+void _MemoryPool_Init( MemoryPool* self ){
+	int i = 0;
+	
+	/* General info */
+	
+	assert( self );
+	_Stg_Class_Init ((Stg_Class*) self);
+	
+	self->elements = (char*)malloc( self->elementSize * self->numElements );
+	memset( self->elements, 0, self->elementSize * self->numElements );
+
+	self->pool = (char**)malloc( sizeof( char* ) * self->numElements );
+	memset( self->pool, 0, sizeof(char*) * self->numElements );
+	
+	for( i=0; i<self->numElements; i++ ){
+		self->pool[i] = &(self->elements[i*self->elementSize]);
+	}
+}
+
+void *MemoryPool_Init( MemoryPool *self )
+{
+	return NULL;
+}
+
+/*----------------------------------------------------------------------------------------------------------------------------------
+** Virtual Functions
+*/
+void _MemoryPool_PrintFunc ( void *memPool, Stream *stream )
+{
+	MemoryPool *self = NULL;
+	
+	self = (MemoryPool*) memPool;
+	assert( self );
+	assert( stream );
+
+	/* print parent */
+	_Stg_Class_Print( (void*) self, stream );
+
+	/* general info */
+	Journal_Printf( stream, "MemoryPool (ptr): (%p)\n", self );
+
+	/* Virtual Info */
+
+	/* MemoryPool Info */
+	Journal_Printf( stream, "\tElementSize\t\t - %d\n", self->elementSize );
+	Journal_Printf( stream, "\tNumElememts\t\t - %d\n", self->numElements );
+	Journal_Printf( stream, "\tNumElememtsFree\t\t - %d\n", self->numElementsFree );
+}
+
+void _MemoryPool_DeleteFunc( void *memPool )
+{
+	MemoryPool *self = NULL;
+	
+	self = (MemoryPool*)memPool;
+	assert (self);
+	
+	free( self->elements );
+	free( self->pool );
+	
+	_Stg_Class_Delete( self );
+}
+
+/*----------------------------------------------------------------------------------------------------------------------------------
+** Public Functions
+*/
+
+void *MemoryPool_NewObjectFunc( SizeT elementSize, MemoryPool *memPool )
+{
+	int index = 0;
+	
+	assert( elementSize == memPool->elementSize );
+	
+	index = memPool->numElementsFree - 1;
+
+	if( index < 0 ) return NULL;
+
+	return (void*)(memPool->pool[--memPool->numElementsFree]);
+}
+		
+void MemoryPool_DeleteObject( MemoryPool *memPool, void *object )
+{
+	memset( (char*)object, 0, memPool->elementSize );
+	memPool->pool[memPool->numElementsFree++] = (char*)object;
+}
+

Added: long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.h	2006-10-11 20:46:04 UTC (rev 4822)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/MemoryPool.h	2006-10-11 20:46:07 UTC (rev 4823)
@@ -0,0 +1,104 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+**
+** Copyright (C), 2003, Victorian Partnership for Advanced Computing (VPAC) Ltd, 110 Victoria Street, Melbourne, 3053, Australia.
+**
+** Authors:
+**	Stevan M. Quenette, Senior Software Engineer, VPAC. (steve at vpac.org)
+**	Patrick D. Sunter, Software Engineer, VPAC. (pds at vpac.org)
+**	Luke J. Hodkinson, Computational Engineer, VPAC. (lhodkins at vpac.org)
+**	Siew-Ching Tan, Software Engineer, VPAC. (siew at vpac.org)
+**	Alan H. Lo, Computational Engineer, VPAC. (alan at vpac.org)
+**	Raquibul Hassan, Computational Engineer, VPAC. (raq at vpac.org)
+**
+**  This library is free software; you can redistribute it and/or
+**  modify it under the terms of the GNU Lesser General Public
+**  License as published by the Free Software Foundation; either
+**  version 2.1 of the License, or (at your option) any later version.
+**
+**  This library is distributed in the hope that it will be useful,
+**  but WITHOUT ANY WARRANTY; without even the implied warranty of
+**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+**  Lesser General Public License for more details.
+**
+**  You should have received a copy of the GNU Lesser General Public
+**  License along with this library; if not, write to the Free Software
+**  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+/** \file
+** <b>Role:</b>
+**	Binary Tree class for objects.
+**
+** <b>Assumptions:</b>
+**	None
+**
+** <b>Comments:</b>
+**	None
+**
+** $Id: MemoryPool.h 2087 2005-2-25 02:28:44Z RaquibulHassan $
+**
+**/
+
+#ifndef __MemoryPool_h__
+#define __MemoryPool_h__
+
+	/** Textual name for List class. */
+	extern const Type MemoryPool_Type;
+	
+	/** \def __List See __List */
+	#define __MemoryPool \
+		/* General info */ \
+		__Stg_Class \
+		\
+		/* Virtual info */ \
+		\
+		/* MemoryPool info */ \
+		SizeT	elementSize; \
+		int		numElements; \
+		int		numElementsFree; \
+		char	*elements; \
+		char	**pool;
+
+	struct MemoryPool { __MemoryPool };
+	
+	/** Constructor interface. */
+	#define MemoryPool_New( type, numElements )\
+		MemoryPool_NewFunc( sizeof(type), numElements )
+	
+	MemoryPool* MemoryPool_NewFunc( SizeT elementSize, int numElements );
+
+	MemoryPool* _MemoryPool_New(
+				SizeT							_sizeOfSelf,
+				Type							type,
+				Stg_Class_DeleteFunction*			_delete,
+				Stg_Class_PrintFunction*			_print,
+				Stg_Class_CopyFunction*				_copy,
+				int									elementSize,
+				int									numElements
+				);
+
+	
+	/** Init interface. */
+	void *MemoryPool_Init( MemoryPool* self );
+	
+	void _MemoryPool_Init( MemoryPool* self );
+	
+	/** Stg_Class_Delete interface. */
+		/** Stg_Class delete function */
+	void _MemoryPool_DeleteFunc ( void *memPool );
+	
+	/** Print interface. */
+		/** Stg_Class print function */
+	void _MemoryPool_PrintFunc ( void *memPool, Stream* stream );
+
+	/** Public functions */
+#define MemoryPool_NewObject( type, memPool ) \
+	(type*)MemoryPool_NewObjectFunc( sizeof(type), memPool )
+	
+	void *MemoryPool_NewObjectFunc( SizeT elementSize, MemoryPool *memPool );
+		
+	void MemoryPool_DeleteObject( MemoryPool *memPool, void *object );
+	
+	/** Private Functions */
+	
+#endif /* __MemoryPool_h__ */
+

Modified: long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h	2006-10-11 20:46:04 UTC (rev 4822)
+++ long/3D/Gale/trunk/src/StGermain/Base/Container/src/types.h	2006-10-11 20:46:07 UTC (rev 4823)
@@ -64,11 +64,9 @@
 	typedef struct _Heap			_Heap;
 	typedef struct MaxHeap			MaxHeap;
 	typedef struct UIntMap			UIntMap;
+	typedef struct MemoryPool		MemoryPool;
 	typedef struct RangeSet			RangeSet;
-
-
 	typedef unsigned char		Byte;
-	
 	typedef char					BitField;
 	
 	typedef struct BTreeNode			BTreeNode;



More information about the cig-commits mailing list