[Avida-cvs] [avida-svn] r415 - in development: Avida.xcodeproj source/cpu source/tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Dec 8 13:04:32 PST 2005


Author: brysonda
Date: 2005-12-08 16:04:32 -0500 (Thu, 08 Dec 2005)
New Revision: 415

Added:
   development/source/tools/tManagedPointerArray.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/cpu/cHardwareSMT.h
   development/source/tools/cDataEntry.h
Log:
Implement tManagedPointerArray.  tManagedPointerArray maintains the same interface as tArray, however internally it uses an array of pointers to objects, rather than an array of objects.  The goal of this structural change is to reduce the cost of resizing the array, since it only needs to copy pointers rather than objects.

This new array template is now used within cHardwareSMT for the memory array, in the hope that it will reduce pathological memory allocation behavior that occurs due to the frequent expansion of memory spaces (for example, in any organism the genome would be copied as soon as an organism called SetMemory to create its child memory space).

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2005-12-07 23:44:39 UTC (rev 414)
+++ development/Avida.xcodeproj/project.pbxproj	2005-12-08 21:04:32 UTC (rev 415)
@@ -563,6 +563,7 @@
 		70B08B8F08FB2E5500FC65FE /* tVector.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = tVector.h; sourceTree = "<group>"; };
 		70B08B9008FB2E6B00FC65FE /* cTools.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cTools.cc; sourceTree = "<group>"; };
 		70B08B9108FB2E6B00FC65FE /* cWeightedIndex.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cWeightedIndex.cc; sourceTree = "<group>"; };
+		70B3984E0947B29D0018F09D /* tManagedPointerArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tManagedPointerArray.h; sourceTree = "<group>"; };
 		70C1EF4608C393BA00F50912 /* cCodeLabel.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cCodeLabel.cc; sourceTree = "<group>"; };
 		70C1EF4708C393BA00F50912 /* cCodeLabel.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cCodeLabel.h; sourceTree = "<group>"; };
 		70C1EF5808C3948C00F50912 /* cCPUMemory.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = cCPUMemory.cc; sourceTree = "<group>"; };
@@ -1295,6 +1296,7 @@
 				DCC315B1076253A5008F7A48 /* tVerifierSuiteRecurser.hh */,
 				DCC315B5076253A5008F7A48 /* win32_mkdir_hack.hh */,
 				700E28CF0859FFD700CF158A /* tObjectFactory.h */,
+				70B3984E0947B29D0018F09D /* tManagedPointerArray.h */,
 			);
 			path = tools;
 			sourceTree = "<group>";

Modified: development/source/cpu/cHardwareSMT.h
===================================================================
--- development/source/cpu/cHardwareSMT.h	2005-12-07 23:44:39 UTC (rev 414)
+++ development/source/cpu/cHardwareSMT.h	2005-12-08 21:04:32 UTC (rev 415)
@@ -36,8 +36,8 @@
 #ifndef cString_h
 #include "cString.h"
 #endif
-#ifndef tArray_h
-#include "tArray.h"
+#ifndef tManagedPointerArray_h
+#include "tManagedPointerArray.h"
 #endif
 #ifndef tInstLib_h
 #include "tInstLib.h"
@@ -74,7 +74,7 @@
   cCPUStack m_global_stacks[nHardwareSMT::NUM_GLOBAL_STACKS];
 	
   // Memory
-  tArray<cCPUMemory> m_mem_array;
+  tManagedPointerArray<cCPUMemory> m_mem_array;
   tHashTable<int, int> m_mem_lbls;
 
   // Threads
@@ -89,10 +89,10 @@
 #endif
 
   
-  cHardwareSMT(const cHardwareSMT &); // @DMB - disabled...  can't (easily) copy m_mem_lbls
+  cHardwareSMT(const cHardwareSMT&); // @DMB - disabled...  can't (easily) copy m_mem_lbls
 
-  bool SingleProcess_PayCosts(const cInstruction & cur_inst);
-  bool SingleProcess_ExecuteInst(const cInstruction & cur_inst);
+  bool SingleProcess_PayCosts(const cInstruction& cur_inst);
+  bool SingleProcess_ExecuteInst(const cInstruction& cur_inst);
   	
 
   // --------  Stack Manipulation...  --------

Modified: development/source/tools/cDataEntry.h
===================================================================
--- development/source/tools/cDataEntry.h	2005-12-07 23:44:39 UTC (rev 414)
+++ development/source/tools/cDataEntry.h	2005-12-08 21:04:32 UTC (rev 415)
@@ -39,7 +39,7 @@
   virtual bool Print(std::ostream& fp) const { (void) fp;  return false; }
 };
 
-std::ostream& operator << (std::ostream& out, cDataEntry & entry)
+inline std::ostream& operator << (std::ostream& out, cDataEntry & entry)
 {
   entry.Print(out);
   return out;

Added: development/source/tools/tManagedPointerArray.h
===================================================================
--- development/source/tools/tManagedPointerArray.h	2005-12-07 23:44:39 UTC (rev 414)
+++ development/source/tools/tManagedPointerArray.h	2005-12-08 21:04:32 UTC (rev 415)
@@ -0,0 +1,141 @@
+/*
+ *  tManagedPointerArray.h
+ *  Avida
+ *
+ *  Created by David on 12/7/05.
+ *  Copyright 2005 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef tManagedPointerArray_h
+#define tManagedPointerArray_h
+
+#include <assert.h>
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
+template <class T> class tManagedPointerArray {  
+protected:
+  T** data;  // Data Elements
+  int size;  // Number of Elements
+  
+public:
+  explicit tManagedPointerArray(const int _size = 0) : data(NULL), size(0) { ResizeClear(_size); }
+  tManagedPointerArray(const tManagedPointerArray& rhs) : data(NULL), size(0) { this->operator=(rhs); }
+  tManagedPointerArray(const tArray<T>& rhs) : data(NULL), size(0) { this->operator=(rhs); }
+
+  ~tManagedPointerArray()
+  { 
+    for (int i = 0; i < size; i++) delete data[i];
+    delete [] data;
+  }
+  
+  tManagedPointerArray& operator=(const tManagedPointerArray& rhs)
+  {
+    if (size != rhs.GetSize()) Resize(rhs.GetSize());
+    for(int i = 0; i < size; i++) *data[i] = rhs[i];
+    return *this;
+  }
+  tManagedPointerArray& operator=(const tArray<T>& rhs)
+  {
+    if (size != rhs.GetSize()) Resize(rhs.GetSize());
+    for(int i = 0; i < size; i++) *data[i] = rhs[i];
+    return *this;
+  }
+  
+  bool Good() const { return (data != NULL); }
+  int GetSize() const { return size; }
+  
+  void ResizeClear(const int in_size)
+  {
+    for (int i = 0; i < size; i++) delete data[i];
+    delete [] data;  // remove old data if exists
+    
+    size = in_size;
+    assert(size >= 0);  // Invalid size specified for array intialization
+    
+    if (size > 0) {
+      data = new T*[size];   // Allocate block for data
+      assert(data != NULL); // Memory allocation error: Out of Memory?
+      for (int i = 0; i < size; i++) data[i] = new T;
+    } else
+      data = NULL;
+  }
+  
+  void Resize(int new_size)
+  {
+    assert(new_size >= 0);
+    
+    // If we're already at the size we want, don't bother doing anything.
+    if (size == new_size) return;
+    
+    // If new size is 0, clean up and go!
+    if (new_size == 0) {
+      for (int i = 0; i < size; i++) delete data[i];
+      delete [] data;
+      data = NULL;
+      size = 0;
+      return;
+    }
+    
+    T** new_data = new T*[new_size];
+    assert(new_data != NULL); // Memory Allocation Error: Out of Memory?
+    
+    if (size < new_size) {
+      // Fill out the new portion of the array, if needed
+      for (int i = size; i < new_size; i++) new_data[i] = new T;
+    } else if (new_size < size) {
+      // Clean up old portion of the array, if needed
+      for (int i = new_size; i < size; i++) delete data[i];
+    }
+    
+    // Copy over old data...
+    for (int i = 0; i < size && i < new_size; i++) {
+      new_data[i] = data[i];
+    }
+    delete [] data;  // remove old data if exists
+    data = new_data;
+    
+    size = new_size;
+  }
+  
+  void Resize(int new_size, const T& empty_value)
+  {
+    assert(new_size >= 0);
+    int old_size = size;
+    Resize(new_size);
+    if (new_size > old_size)
+      for (int i = old_size; i < new_size; i++) *data[i] = empty_value;
+  }
+  
+  T& ElementAt(const int index)
+  {
+    assert(index >= 0);    // Lower Bounds Error
+    assert(index < size);  // Upper Bounds Error
+    return *data[index];   // in range, so return element
+  }  
+  const T& ElementAt(const int index) const
+  {
+    assert(index >= 0);    // Lower Bounds Error
+    assert(index < size);  // Upper Bounds Error
+    return *data[index];   // in range, so return element
+  }
+  
+  T& operator[](const int index) { return ElementAt(index); }
+  const T& operator[](const int index) const { return ElementAt(index); }
+  
+  void Push(const T& value)
+  {
+    Resize(size + 1);
+    *data[size - 1] = value;
+  }
+  
+  void SetAll(const T& value)
+  {
+    for (int i = 0; i < size; i++) *data[i] = value;
+  }
+};
+
+#endif


Property changes on: development/source/tools/tManagedPointerArray.h
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the Avida-cvs mailing list