[mapguide-commits] r4419 - in trunk/MgDev/Common: CoordinateSystem Geometry/CoordinateSystem

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Fri Dec 11 20:28:01 EST 2009


Author: NormOlsen
Date: 2009-12-11 20:27:59 -0500 (Fri, 11 Dec 2009)
New Revision: 4419

Modified:
   trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.h
   trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.h
   trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.h
   trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.h
   trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.h
   trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.cpp
   trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.h
   trunk/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemGrids.h
Log:
The related MapGuide Trac ticket number is 1177, and is part of RFC 76.

This submission refactors the code which determines how much memory a Grid creation will consume and detects whether there is enough to continue.  We were attempting to calculate memory usage on an object by object basis.  It now does the checking in the same locations, but instead asks the system for actual memory usage by this process, and tracks a memory threshold at which the grid creation should not drop below.  This is proving to be much more reliable.

Since this code will only be used within the AutoCAD Map win32 environment at this time, the Linux memory check has not been implemented yet.

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -43,6 +43,10 @@
                        m_FrameBoundary (),
                        m_TheGrid       (0)
 {
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
 }
 CCoordinateSystemGridGeneric::CCoordinateSystemGridGeneric(MgCoordinateSystem* pSourceCs,
                                                            MgCoordinateSystem* pTargetCs,
@@ -60,6 +64,11 @@
 {
     m_pCsSource = SAFE_ADDREF (pSourceCs);      // Grid coordinate system
     m_pCsTarget = SAFE_ADDREF (pTargetCs);      // Viewport coordinate system
+
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
 }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 CCoordinateSystemGridGeneric::~CCoordinateSystemGridGeneric()
@@ -75,9 +84,8 @@
 void CCoordinateSystemGridGeneric::SetBoundary(MgCoordinateSystemGridBoundary* pFrameBoundary)
 {
     m_FrameBoundary = SAFE_ADDREF (pFrameBoundary);
-    m_TheGrid = new CCoordinateSystemOneGrid (m_FrameBoundary,m_pCsSource,m_pCsTarget);
-    m_TheGrid->SetGridLineExceptionLevel (m_GridLineExceptionLevel);
-    m_TheGrid->SetGridTickExceptionLevel (m_GridTickExceptionLevel);
+    m_TheGrid = new CCoordinateSystemOneGrid (m_FrameBoundary,m_pCsSource,m_pCsTarget,
+                                              m_GridLineMemoryThreshold,m_GridTickMemoryThreshold);
 }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 MgCoordinateSystemGridBoundary* CCoordinateSystemGridGeneric::GetBoundary(void)
@@ -163,11 +171,13 @@
     if (memoryUseMax > 0)
     {
         m_GridLineExceptionLevel = memoryUseMax;
-        if (m_TheGrid != 0)
-        {
-            m_TheGrid->SetGridLineExceptionLevel (m_GridLineExceptionLevel);
-        }
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+    if (m_TheGrid != 0)
+    {
+        m_TheGrid->ResetGridLineMemoryThreshold(m_GridLineMemoryThreshold);
+    }
     return rtnValue;
 }
 INT32 CCoordinateSystemGridGeneric::SetGridRegionExceptionLevel (INT32 memoryUseMax)
@@ -177,6 +187,8 @@
     {
         m_GridRegionExceptionLevel = memoryUseMax;
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
     return rtnValue;
 }
 INT32 CCoordinateSystemGridGeneric::SetGridTickExceptionLevel (INT32 memoryUseMax)
@@ -185,11 +197,13 @@
     if (memoryUseMax > 0)
     {
         m_GridTickExceptionLevel = memoryUseMax;
-        if (m_TheGrid != 0)
-        {
-            m_TheGrid->SetGridTickExceptionLevel (m_GridTickExceptionLevel);
-        }
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
+    if (m_TheGrid != 0)
+    {
+        m_TheGrid->ResetGridTickMemoryThreshold(m_GridTickMemoryThreshold);
+    }
     return rtnValue;
 }
 

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysGridGeneric.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -69,6 +69,9 @@
     INT32 m_GridLineExceptionLevel;
     INT32 m_GridRegionExceptionLevel;
     INT32 m_GridTickExceptionLevel;
+    INT64 m_GridLineMemoryThreshold;
+    INT64 m_GridRegionMemoryThreshold;
+    INT64 m_GridTickMemoryThreshold;
     INT32 m_nLastError;
     Ptr<MgCoordinateSystem> m_pCsSource;
     Ptr<MgCoordinateSystem> m_pCsTarget;

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -670,28 +670,6 @@
 {
     return SAFE_ADDREF(m_LineSegments.p);
 }
-INT32 CCoordinateSystemGridLine::GetMemoryUsage ()
-{
-    INT32 lineIndex;
-    INT32 lineCount;
-    INT32 memoryUse = sizeof (CCoordinateSystemGridLine) + kMgHeapOverhead;
-
-    lineCount = m_LineSegments->GetCount ();
-    for (lineIndex = 0;lineIndex < lineCount;lineIndex += 1)
-    {
-        Ptr<MgLineString> lineStringPtr = m_LineSegments->GetItem (lineIndex);
-        if (lineStringPtr != 0)
-        {
-            memoryUse += sizeof (MgLineString) + kMgHeapOverhead;
-            Ptr<MgCoordinateIterator> pointItr = lineStringPtr->GetCoordinates ();
-            while (pointItr->MoveNext ())
-            {
-                memoryUse += kMgSizeOfCoordinateXY;
-            }
-        }
-    }
-    return memoryUse;
-}
 void CCoordinateSystemGridLine::SetSegmentCollection (MgLineStringCollection* segmentCollection)
 {
     m_LineSegments = SAFE_ADDREF (segmentCollection);
@@ -814,26 +792,6 @@
 {
     return SAFE_ADDREF(m_WestLine.p);
 }
-INT32 CCoordinateSystemGridRegion::GetMemoryUsage ()
-{
-    INT32 memoryUse;
-
-    memoryUse  = sizeof (CCoordinateSystemGridRegion) + kMgHeapOverhead;
-    memoryUse += sizeof (STRING);
-    memoryUse += m_RegionLabel.capacity () * sizeof (wchar_t);
-    memoryUse += kMgSizeOfCoordinateXY;
-    memoryUse += PolygonMemoryUse (m_RegionBoundary.p);
-    memoryUse += LineStringCollectionMemoryUse (m_SouthLine.p);
-    memoryUse += LineStringCollectionMemoryUse (m_EastLine.p);
-    memoryUse += LineStringCollectionMemoryUse (m_NorthLine.p);
-    memoryUse += LineStringCollectionMemoryUse (m_WestLine.p);
-
-    // Testing has shown the above calculation to be about 20% low.
-    // This is assumed to be because collections have a capacity and a useage,
-    // and while we measure usage above, we can't measure capacity.
-    memoryUse += memoryUse / 5;
-    return memoryUse;
-}
 void CCoordinateSystemGridRegion::SetRegionBoundary (MgPolygon* boundary)
 {
     m_RegionBoundary = SAFE_ADDREF (boundary);
@@ -858,50 +816,6 @@
 {
     delete this;
 }
-INT32 CCoordinateSystemGridRegion::PolygonMemoryUse (MgPolygon* polygon)
-{
-    // While MgPolygon objects support holes, there are no holes in a Region
-    // (so far anyway); so we only evaluate the exterior ring.
-
-    INT32 memoryUse;
-
-    memoryUse = sizeof (MgPolygon) + kMgHeapOverhead;
-    if (polygon != 0)
-    {
-        Ptr<MgCoordinateIterator> pointItr = polygon->GetCoordinates ();
-        while (pointItr->MoveNext ())
-        {
-            memoryUse += kMgSizeOfCoordinateXY;
-        }
-    }
-    return memoryUse;
-}
-INT32 CCoordinateSystemGridRegion::LineStringCollectionMemoryUse (MgLineStringCollection* lineCollection)
-{
-    INT32 memoryUse (0);
-    INT32 lineCount;
-    INT32 lineIndex;
-
-    if (lineCollection != 0)
-    {
-        memoryUse += sizeof (MgLineStringCollection) + kMgHeapOverhead;
-        lineCount = lineCollection->GetCount ();
-        for (lineIndex = 0;lineIndex < lineCount;lineIndex += 1)
-        {
-            Ptr<MgLineString> lineStringPtr = lineCollection->GetItem (lineIndex);
-            if (lineStringPtr != 0)
-            {
-                memoryUse += sizeof (MgLineString) + kMgHeapOverhead;
-                Ptr<MgCoordinateIterator> pointItr = lineStringPtr->GetCoordinates ();
-                while (pointItr->MoveNext ())
-                {
-                    memoryUse += kMgSizeOfCoordinateXY;
-                }
-            }
-        }
-    }
-    return memoryUse;
-}
 //=============================================================================
 // CCoordinateSystemGridTick -- Defines the location of a grid tick mark.
 //
@@ -965,12 +879,6 @@
 {
     return SAFE_ADDREF(m_Direction.p);
 }
-INT32 CCoordinateSystemGridTick::GetMemoryUsage ()
-{
-    INT32 memoryUse = sizeof (CCoordinateSystemGridTick) + kMgHeapOverhead;
-    memoryUse += (kMgSizeOfCoordinateXY + kMgSizeOfCoordinateXY);
-    return memoryUse;
-}
 void CCoordinateSystemGridTick::Dispose ()
 {
     delete this;
@@ -981,14 +889,12 @@
 // appear first in ascending order by grid value (easting in this case);
 // followed by grid vertical lines in ascending order by grid value (northing
 // in this case).
-CCoordinateSystemGridLineCollection::CCoordinateSystemGridLineCollection (INT32 gridLineExceptionLevel)
+CCoordinateSystemGridLineCollection::CCoordinateSystemGridLineCollection (INT64 memoryThreshold)
                                         :
                                      MgCoordinateSystemGridLineCollection (),
-                                     m_MemoryUse                          (),
-                                     m_GridLineExceptionLevel             (gridLineExceptionLevel),
+                                     m_MemoryThreshold                    (memoryThreshold),
                                      m_GridLineCollection                 ()
 {
-    m_MemoryUse = sizeof (MgCoordinateSystemGridLineCollection) + sizeof (MgDisposableCollection);
     m_GridLineCollection = new MgDisposableCollection();
 }
 CCoordinateSystemGridLineCollection::~CCoordinateSystemGridLineCollection(void)
@@ -1038,67 +944,26 @@
 }
 void CCoordinateSystemGridLineCollection::RemoveAt (INT32 index)
 {
-    INT32 memoryUse (0);
-
     // The MgDisposableCollection object checks the index argument, and throws if appropriate.
     // The MgDisposableCollection object performs the "SAFE_RELEASE" operation.
-
-    if (index >= 0 && index < GetCount ())
-    {
-        // We don't want an invalid index exception thrown from this GetItem,
-        // we leave that to the RemoveAt function.  Also, this releases the
-        // pointer we get before the exception is thrown.
-        Ptr<MgCoordinateSystemGridLine> gridLinePtr = GetItem (index);
-        if (gridLinePtr != 0)
-        {
-            memoryUse = gridLinePtr->GetMemoryUsage ();
-        }
-    }
     m_GridLineCollection->RemoveAt (index);
-    m_MemoryUse -= memoryUse;
 }
 void CCoordinateSystemGridLineCollection::Clear()
 {
     // The MgDisposableCollection object performs the "SAFE_RELEASE" operation.
     m_GridLineCollection->Clear ();
-    m_MemoryUse = sizeof (MgCoordinateSystemGridLineCollection) + sizeof (MgDisposableCollection);
 }
 void CCoordinateSystemGridLineCollection::SetItem (INT32 index,MgCoordinateSystemGridLine* value)
 {
-    INT32 memoryUse (0);
-
-    memoryUse = value->GetMemoryUsage ();
-    if (index >= 0 && index < GetCount ())
-    {
-        Ptr<MgCoordinateSystemGridLine> oldGridLine = GetItem (index);
-        if (oldGridLine != 0)
-        {
-            memoryUse -= oldGridLine->GetMemoryUsage ();
-        }
-    }
-    if ((m_MemoryUse + memoryUse) < m_GridLineExceptionLevel)
-    {
-        m_GridLineCollection->SetItem (index,value);
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridLineCollection.SetItem", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+
+    m_GridLineCollection->SetItem (index,value);
 }
 void CCoordinateSystemGridLineCollection::Add (MgCoordinateSystemGridLine* value)
 {
-    INT32 memoryUse;
-
-    memoryUse = value->GetMemoryUsage ();
-    if ((memoryUse + m_MemoryUse) < m_GridLineExceptionLevel)
-    {
-        m_GridLineCollection->Add (value);
-        m_MemoryUse += memoryUse;
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridLineCollection.Add", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
 
     // The MgDIsposableCollection object does the "SAFE_ADDREF" operation.
     m_GridLineCollection->Add (value);
@@ -1107,16 +972,9 @@
 {
     INT32 index;
     INT32 toAddCount;
-    INT32 maxValue;
-    INT32 memoryUse;
     Ptr<MgCoordinateSystemGridLine> aGridLine;
 
-    maxValue = m_GridLineExceptionLevel - m_MemoryUse;
-    memoryUse = aGridLineCollection->GetMemoryUsage ();
-    if (memoryUse > maxValue)
-    {
-        throw new MgGridDensityException(L"CCoordinateSystemGridLineCollection.AddCollection", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+    // Memory check is done inside Add()
     MG_TRY ()
         toAddCount = aGridLineCollection->GetCount ();
         for (index = 0;index < toAddCount;index += 1)
@@ -1127,19 +985,6 @@
     MG_CATCH_AND_THROW(L"CCoordinateSystemGridLineCollection::AddCollection")
     return;
 }
-INT32 CCoordinateSystemGridLineCollection::SetGridLineExceptionLevel (INT32 memoryUseMax)
-{
-    INT32 rtnValue = m_GridLineExceptionLevel;
-    if (memoryUseMax > 0L)
-    {
-        m_GridLineExceptionLevel = memoryUseMax;
-    }
-    return rtnValue;
-}
-INT32 CCoordinateSystemGridLineCollection::GetMemoryUsage (void)
-{
-    return m_MemoryUse;
-}
 void CCoordinateSystemGridLineCollection::Dispose(void)
 {
     delete this;
@@ -1148,14 +993,12 @@
 //=============================================================================
 // A CCoordinateSystemGridRegionCollection is collection of
 // CCoordinateSystemGridRegion objects.
-CCoordinateSystemGridRegionCollection::CCoordinateSystemGridRegionCollection (INT32 gridRegionExceptionLevel)
+CCoordinateSystemGridRegionCollection::CCoordinateSystemGridRegionCollection (INT64 memoryThreshold)
                                          :
                                        MgCoordinateSystemGridRegionCollection (),
-                                       m_MemoryUse                            (),
-                                       m_GridRegionExceptionLevel             (gridRegionExceptionLevel),
+                                       m_MemoryThreshold                      (memoryThreshold),
                                        m_GridRegionCollection                 ()
 {
-    m_MemoryUse = sizeof (CCoordinateSystemGridRegionCollection) + sizeof (MgDisposableCollection);
     m_GridRegionCollection = new MgDisposableCollection();
 }
 CCoordinateSystemGridRegionCollection::~CCoordinateSystemGridRegionCollection (void)
@@ -1176,82 +1019,35 @@
 }
 void CCoordinateSystemGridRegionCollection::RemoveAt (INT32 index)
 {
-    INT32 memoryUse (0);
-
     // The MgDisposableCollection object checks the index argument, and throws if appropriate.
     // The MgDisposableCollection object performs the "SAFE_RELEASE" operation.
-
-    if (index >= 0 && index < GetCount ())
-    {
-        // We don't want an invalid index exception thrown from this GetItem,
-        // we leave that to the RemoveAt function.  Also, this releases the
-        // pointer we get before the exception is thrown.
-        Ptr<MgCoordinateSystemGridRegion> gridRegionPtr = GetItem (index);
-        if (gridRegionPtr != 0)
-        {
-            memoryUse = gridRegionPtr->GetMemoryUsage ();
-        }
-    }
     m_GridRegionCollection->RemoveAt (index);
-    m_MemoryUse -= memoryUse;
 }
 void CCoordinateSystemGridRegionCollection::Clear()
 {
     m_GridRegionCollection->Clear ();
-    m_MemoryUse = sizeof (CCoordinateSystemGridRegionCollection) + sizeof (MgDisposableCollection);
 }
 void CCoordinateSystemGridRegionCollection::SetItem (INT32 index, MgCoordinateSystemGridRegion* value)
 {
-    INT32 memoryUse;
-
-    memoryUse = value->GetMemoryUsage ();
-    if (index >= 0 && index < GetCount ())
-    {
-        Ptr<MgCoordinateSystemGridRegion> currentPtr = GetItem (index);
-        if (currentPtr != 0)
-        {
-            memoryUse -= currentPtr->GetMemoryUsage ();
-        }
-    }
-    if ((m_MemoryUse + memoryUse) < m_GridRegionExceptionLevel)
-    {
-        m_GridRegionCollection->SetItem (index,value);
-        m_MemoryUse += memoryUse;
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridRegionCollection.SetItem", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+
+    m_GridRegionCollection->SetItem (index,value);
 }
 void CCoordinateSystemGridRegionCollection::Add (MgCoordinateSystemGridRegion* value)
 {
-    INT32 memoryUse;
-
-    memoryUse = value->GetMemoryUsage ();
-    if ((memoryUse + m_MemoryUse) < m_GridRegionExceptionLevel)
-    {
-        m_GridRegionCollection->Add (value);
-        m_MemoryUse += memoryUse;
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridRegionCollection.Add", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+
+    m_GridRegionCollection->Add (value);
 }
 void CCoordinateSystemGridRegionCollection::AddCollection (MgCoordinateSystemGridRegionCollection* aGridRegionCollection)
 {
     INT32 index;
     INT32 toAddCount;
-    INT32 memoryUse;
-    INT32 maxValue;
     Ptr<MgCoordinateSystemGridRegion> aGridRegion;
 
-    maxValue = m_GridRegionExceptionLevel - m_MemoryUse;
-    memoryUse = aGridRegionCollection->GetMemoryUsage ();
-    if (memoryUse > maxValue)
-    {
-        throw new MgGridDensityException(L"CCoordinateSystemGridRegionCollection.AddCollection", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+    // Memory check is done inside Add()
     MG_TRY ()
         
         toAddCount = aGridRegionCollection->GetCount ();
@@ -1264,19 +1060,6 @@
     MG_CATCH_AND_THROW(L"CCoordinateSystemGridRegionCollection::AddCollection")
     return;
 }
-INT32 CCoordinateSystemGridRegionCollection::SetGridRegionExceptionLevel (INT32 memoryUseMax)
-{
-    INT32 rtnValue = m_GridRegionExceptionLevel;
-    if (memoryUseMax > 0L)
-    {
-        m_GridRegionExceptionLevel = memoryUseMax;
-    }
-    return rtnValue;
-}
-INT32 CCoordinateSystemGridRegionCollection::GetMemoryUsage (void)
-{
-    return m_MemoryUse;
-}
 void CCoordinateSystemGridRegionCollection::Dispose (void)
 {
     // Destructor deletes the contents of the collection.
@@ -1288,14 +1071,12 @@
 // CCoordinateSystemGridTick objects.  CCoordinateSystemGridTickCollection
 // objects will contain MgCoordinateSystemGridTick objects for the entire
 // boundary or for an individual grid line.
-CCoordinateSystemGridTickCollection::CCoordinateSystemGridTickCollection (INT32 gridTickExceptionLevel)
+CCoordinateSystemGridTickCollection::CCoordinateSystemGridTickCollection (INT64 memoryThreshold)
                                         :
                                      MgCoordinateSystemGridTickCollection (),
-                                     m_MemoryUse                          (),
-                                     m_GridTickExceptionLevel             (gridTickExceptionLevel),
+                                     m_MemoryThreshold                    (memoryThreshold),
                                      m_GridTickCollection                 ()
 {
-    m_MemoryUse = sizeof (CCoordinateSystemGridTickCollection) + sizeof (MgDisposableCollection);
     m_GridTickCollection = new MgDisposableCollection();
 }
 CCoordinateSystemGridTickCollection::~CCoordinateSystemGridTickCollection (void)
@@ -1314,83 +1095,35 @@
 }
 void CCoordinateSystemGridTickCollection::RemoveAt (INT32 index)
 {
-    INT32 memoryUse (0);
-
     // The MgDisposableCollection object checks the index argument, and throws if appropriate.
     // The MgDisposableCollection object performs the "SAFE_RELEASE" operation.
-
-    if (index >= 0 && index < GetCount ())
-    {
-        // We don't want an invalid index exception thrown from this GetItem,
-        // we leave that to the RemoveAt function.  Also, this releases the
-        // pointer we get before the exception is thrown.
-        Ptr<MgCoordinateSystemGridTick> gridTickPtr = GetItem (index);
-        if (gridTickPtr != 0)
-        {
-            memoryUse = gridTickPtr->GetMemoryUsage ();
-        }
-    }
     m_GridTickCollection->RemoveAt (index);
-    m_MemoryUse -= memoryUse;
 }
 void CCoordinateSystemGridTickCollection::Clear()
 {
     m_GridTickCollection->Clear ();
-    m_MemoryUse = sizeof (CCoordinateSystemGridTickCollection) + sizeof (MgDisposableCollection);
 }
 void CCoordinateSystemGridTickCollection::SetItem (INT32 index, MgCoordinateSystemGridTick* value)
 {
-    INT32 memoryUse;
-
-    memoryUse = value->GetMemoryUsage ();
-    if (index >= 0 && index < GetCount ())
-    {
-        Ptr<MgCoordinateSystemGridTick> currentPtr = GetItem (index);
-        if (currentPtr != 0)
-        {
-            memoryUse -= currentPtr->GetMemoryUsage ();
-        }
-    }
-    if ((m_MemoryUse + memoryUse) < m_GridTickExceptionLevel)
-    {
-        m_GridTickCollection->SetItem (index,value);
-        m_MemoryUse += memoryUse;
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridTickCollection.SetItem", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
 
+    m_GridTickCollection->SetItem (index,value);
 }
 void CCoordinateSystemGridTickCollection::Add (MgCoordinateSystemGridTick* value)
 {
-    INT32 memoryUse;
-
-    memoryUse = value->GetMemoryUsage ();
-    if ((memoryUse + m_MemoryUse) < m_GridTickExceptionLevel)
-    {
-        m_GridTickCollection->Add (value);
-        m_MemoryUse += memoryUse;
-    }
-    else
-    {
+    if (GetAvailableMemory() < m_MemoryThreshold)
         throw new MgGridDensityException(L"CCoordinateSystemGridTickCollection.Add", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+
+    m_GridTickCollection->Add (value);
 }
 void CCoordinateSystemGridTickCollection::AddCollection (MgCoordinateSystemGridTickCollection* aGridTickCollection)
 {
     INT32 index;
     INT32 toAddCount;
-    INT32 maxValue;
-    INT32 memoryUse;
     Ptr<MgCoordinateSystemGridTick> aGridTick;
 
-    maxValue = m_GridTickExceptionLevel - m_MemoryUse;
-    memoryUse = aGridTickCollection->GetMemoryUsage ();
-    if (memoryUse > maxValue)
-    {
-        throw new MgGridDensityException(L"CCoordinateSystemGridTickCollection.AddCollection", __LINE__, __WFILE__, NULL, L"", NULL);
-    }
+    // Memory check is done in Add()
     MG_TRY ()
         toAddCount = aGridTickCollection->GetCount ();
         for (index = 0;index < toAddCount;index += 1)
@@ -1401,19 +1134,6 @@
     MG_CATCH_AND_THROW(L"CCoordinateSystemGridTickCollection::AddCollection")
     return;
 }
-INT32 CCoordinateSystemGridTickCollection::SetGridTickExceptionLevel (INT32 memoryUseMax)
-{
-    INT32 rtnValue = m_GridTickExceptionLevel;
-    if (memoryUseMax > 0L)
-    {
-        m_GridTickExceptionLevel = memoryUseMax;
-    }
-    return rtnValue;
-}
-INT32 CCoordinateSystemGridTickCollection::GetMemoryUsage ()
-{
-    return m_MemoryUse;
-}
 void CCoordinateSystemGridTickCollection::Dispose (void)
 {
     delete this;

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysGrids.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -285,7 +285,6 @@
     INT32 GetCount (void);
     MgLineString* GetSegment (INT32 index);
     MgLineStringCollection* GetSegmentCollection (void);
-    INT32 GetMemoryUsage (void);
 
     void SetGridOrientation (INT32 orientation);
     void SetRealValue (double realValue);
@@ -327,7 +326,6 @@
     MgLineStringCollection* GetEastLine (void);
     MgLineStringCollection* GetNorthLine (void);
     MgLineStringCollection* GetWestLine (void);
-    INT32 GetMemoryUsage (void);
 
     void SetRegionBoundary (MgPolygon* boundary);
     void SetSouthLine (MgLineStringCollection* southLine);
@@ -346,8 +344,6 @@
     Ptr<MgLineStringCollection> m_WestLine;
 
 private:
-    INT32 PolygonMemoryUse (MgPolygon* polygon);
-    INT32 LineStringCollectionMemoryUse (MgLineStringCollection* lineCollection);
     // Not implemented
     CCoordinateSystemGridRegion (void);
     CCoordinateSystemGridRegion (const CCoordinateSystemGridRegion& source);
@@ -374,7 +370,6 @@
     double GetValue (void);
     MgCoordinate* GetPosition (void);
     MgCoordinate* GetDirectionVector (void);
-    INT32 GetMemoryUsage (void);
 
 protected:
     void Dispose (void);
@@ -399,7 +394,7 @@
 class CCoordinateSystemGridLineCollection : public MgCoordinateSystemGridLineCollection
 {
 public:
-    CCoordinateSystemGridLineCollection (INT32 gridLineExceptionLevel);
+    CCoordinateSystemGridLineCollection (INT64 memoryThreshold);
     ~CCoordinateSystemGridLineCollection(void);
 
     INT32 GetCount () const;
@@ -411,13 +406,10 @@
     void SetItem (INT32 index,MgCoordinateSystemGridLine* value);
     void Add (MgCoordinateSystemGridLine* value);
     void AddCollection (MgCoordinateSystemGridLineCollection* aGridLineCollection);
-    INT32 SetGridLineExceptionLevel (INT32 memoryUseMax);
-    INT32 GetMemoryUsage (void);
 
 protected:
     void Dispose(void);
-    INT32 m_MemoryUse;
-    INT32 m_GridLineExceptionLevel;
+    INT64 m_MemoryThreshold;
     Ptr<MgDisposableCollection> m_GridLineCollection;
 
 private:
@@ -432,7 +424,7 @@
 class CCoordinateSystemGridRegionCollection : public MgCoordinateSystemGridRegionCollection
 {
 public:
-    CCoordinateSystemGridRegionCollection (INT32 gridRegionExceptionLevel);
+    CCoordinateSystemGridRegionCollection (INT64 memoryThreshold);
     ~CCoordinateSystemGridRegionCollection (void);
 
     INT32 GetCount () const;
@@ -443,13 +435,10 @@
     void SetItem (INT32 index, MgCoordinateSystemGridRegion* value);
     void Add (MgCoordinateSystemGridRegion* value);
     void AddCollection (MgCoordinateSystemGridRegionCollection* aGridRegionCollection);
-    INT32 SetGridRegionExceptionLevel (INT32 memoryUseMax);
-    INT32 GetMemoryUsage (void);
 
 protected:
     void Dispose (void);
-    INT32 m_MemoryUse;
-    INT32 m_GridRegionExceptionLevel;
+    INT64 m_MemoryThreshold;
     Ptr<MgDisposableCollection> m_GridRegionCollection;
 
 private:
@@ -466,7 +455,7 @@
 class CCoordinateSystemGridTickCollection : public MgCoordinateSystemGridTickCollection 
 {
 public:
-    CCoordinateSystemGridTickCollection (INT32 gridLineExceptionLevel);
+    CCoordinateSystemGridTickCollection (INT64 memoryThreshold);
     ~CCoordinateSystemGridTickCollection (void);
 
     INT32 GetCount () const;
@@ -476,13 +465,10 @@
     void SetItem (INT32 index, MgCoordinateSystemGridTick* value);
     void Add (MgCoordinateSystemGridTick* value);
     void AddCollection (MgCoordinateSystemGridTickCollection* aGridTickCollection);
-    INT32 SetGridTickExceptionLevel (INT32 memoryUseMax);
-    INT32 GetMemoryUsage (void);
 
 protected:
     void Dispose (void);
-    INT32 m_MemoryUse;
-    INT32 m_GridTickExceptionLevel;
+    INT64 m_MemoryThreshold;
     Ptr<MgDisposableCollection> m_GridTickCollection;
 
 private:

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -81,6 +81,10 @@
                        m_GraticuleUpsNorth        (),
                        m_GraticuleUpsSouth        ()
 {
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
 }
 CCoordinateSystemMgrs::CCoordinateSystemMgrs(MgCoordinateSystem* pTargetCs,INT8 nLetteringScheme,
                                                                            bool bSetExceptionsOn)
@@ -102,6 +106,11 @@
 
 {
     m_pCsTarget = SAFE_ADDREF (pTargetCs);
+
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
 }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 CCoordinateSystemMgrs::~CCoordinateSystemMgrs()
@@ -274,7 +283,7 @@
             }
             else
             {
-                m_nLastError=m_nLastError=MgCoordinateSystemErrorCode::OutOfMemory;
+                m_nLastError=MgCoordinateSystemErrorCode::OutOfMemory;
                 return NULL;
             }
         }
@@ -388,7 +397,6 @@
     INT32 index;
     INT32 unitType;
     INT32 zoneCount;
-    INT32 zoneExceptionLevel;
     Ptr<CCoordinateSystemMgrsZone> mgrsZoneGrid;
     Ptr<MgCoordinateSystemGridLineCollection> aGridLineCollection;
     Ptr<CCoordinateSystemGridLineCollection> theGridLineCollection;
@@ -403,7 +411,7 @@
     }
 
     MG_TRY ()
-        theGridLineCollection = new CCoordinateSystemGridLineCollection (m_GridLineExceptionLevel);
+        theGridLineCollection = new CCoordinateSystemGridLineCollection (m_GridLineMemoryThreshold);
 
         // Determine the grid type.
         unitType = specification->GetUnitType();
@@ -419,14 +427,13 @@
         zoneCount = m_ZoneCollection->GetCount ();
         for (index = 0;index < zoneCount;index += 1)
         {
-            zoneExceptionLevel = m_GridLineExceptionLevel - theGridLineCollection->GetMemoryUsage ();
             mgrsZoneGrid = m_ZoneCollection->GetItem (index);
             if (specIsGrid)
             {
                 // The specification calls for a grid.  The following function
                 // is smart enough to deal with the special nature of zones
                 // 31 through 37 at the higher northern latitudes.
-                aGridLineCollection = mgrsZoneGrid->GetGridLines (m_GridBoundary,specification,zoneExceptionLevel);
+                aGridLineCollection = mgrsZoneGrid->GetGridLines (m_GridBoundary,specification);
             }
             else
             {
@@ -434,7 +441,7 @@
                 // smart enough to deal with band X (which is 12 degrees high)
                 // and the special nature of zones 31 through 37 at the higher
                 // northern latitudes.
-                aGridLineCollection = mgrsZoneGrid->GetGraticuleLines (m_GridBoundary,specification,zoneExceptionLevel);
+                aGridLineCollection = mgrsZoneGrid->GetGraticuleLines (m_GridBoundary,specification);
             }
             if (aGridLineCollection != 0)
             {
@@ -451,7 +458,6 @@
 {
     INT32 index;
     INT32 zoneCount;
-    INT32 zoneExceptionLevel;
     Ptr<CCoordinateSystemMgrsZone> mgrsZoneGrid;
     Ptr<CCoordinateSystemGridRegionCollection> theGridRegionCollection;
 
@@ -463,14 +469,13 @@
     }
 
     MG_TRY ()
-        theGridRegionCollection = new CCoordinateSystemGridRegionCollection (m_GridRegionExceptionLevel);
+        theGridRegionCollection = new CCoordinateSystemGridRegionCollection (m_GridRegionMemoryThreshold);
         zoneCount = m_ZoneCollection->GetCount ();
         for (index = 0;index < zoneCount;index += 1)
         {
-            zoneExceptionLevel = m_GridRegionExceptionLevel - theGridRegionCollection->GetMemoryUsage ();
             mgrsZoneGrid = m_ZoneCollection->GetItem (index);
             Ptr<MgCoordinateSystemGridRegionCollection> aGridRegionCollection;
-            aGridRegionCollection = mgrsZoneGrid->GetGridRegions (m_GridBoundary,specification,zoneExceptionLevel);
+            aGridRegionCollection = mgrsZoneGrid->GetGridRegions (m_GridBoundary,specification);
             theGridRegionCollection->AddCollection (aGridRegionCollection);
         }
     MG_CATCH_AND_THROW(L"MgCoordinateSystemMgrs::GetGridRegions")
@@ -496,7 +501,7 @@
     }
 
     MG_TRY ()
-        theGridTickCollection = new CCoordinateSystemGridTickCollection (m_GridTickExceptionLevel);
+        theGridTickCollection = new CCoordinateSystemGridTickCollection (m_GridTickMemoryThreshold);
         unitType = specification->GetUnitType();
         specIsGrid = (unitType ==  MgCoordinateSystemUnitType::Linear);
         if (specIsGrid)
@@ -618,6 +623,32 @@
     {
         m_GridLineExceptionLevel = memoryUseMax;
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridLineMemoryThreshold = (availableMemory > m_GridLineExceptionLevel) ? availableMemory - m_GridLineExceptionLevel : 0L;
+
+    if (m_ZoneCollection != 0)
+    {
+        INT32 index;
+        INT32 zoneCount;
+        zoneCount = m_ZoneCollection->GetCount ();
+        for (index = 0;index < zoneCount;index += 1)
+        {
+            Ptr<CCoordinateSystemMgrsZone> mgrsZoneGrid = m_ZoneCollection->GetItem (index);
+            mgrsZoneGrid->ResetGridLineMemoryThreshold(m_GridLineMemoryThreshold);
+        }
+    }
+    if (m_GraticuleUtm != 0)
+    {
+        m_GraticuleUtm->ResetGridLineMemoryThreshold(m_GridLineMemoryThreshold);
+    }
+    if (m_GraticuleUpsNorth != 0)
+    {
+        m_GraticuleUpsNorth->ResetGridLineMemoryThreshold(m_GridLineMemoryThreshold);
+    }
+    if (m_GraticuleUpsSouth != 0)
+    {
+        m_GraticuleUpsSouth->ResetGridLineMemoryThreshold(m_GridLineMemoryThreshold);
+    }
     return rtnValue;
 }
 INT32 CCoordinateSystemMgrs::SetGridRegionExceptionLevel (INT32 memoryUseMax)
@@ -627,6 +658,20 @@
     {
         m_GridRegionExceptionLevel = memoryUseMax;
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridRegionMemoryThreshold = (availableMemory > m_GridRegionExceptionLevel) ? availableMemory - m_GridRegionExceptionLevel : 0L;
+
+    if (m_ZoneCollection != 0)
+    {
+        INT32 index;
+        INT32 zoneCount;
+        zoneCount = m_ZoneCollection->GetCount ();
+        for (index = 0;index < zoneCount;index += 1)
+        {
+            Ptr<CCoordinateSystemMgrsZone> mgrsZoneGrid = m_ZoneCollection->GetItem (index);
+            mgrsZoneGrid->ResetGridRegionMemoryThreshold(m_GridRegionMemoryThreshold);
+        }
+    }
     return rtnValue;
 }
 INT32 CCoordinateSystemMgrs::SetGridTickExceptionLevel (INT32 memoryUseMax)
@@ -636,6 +681,32 @@
     {
         m_GridTickExceptionLevel = memoryUseMax;
     }
+    INT64 availableMemory = GetAvailableMemory();
+    m_GridTickMemoryThreshold = (availableMemory > m_GridTickExceptionLevel) ? availableMemory - m_GridTickExceptionLevel : 0L;
+    
+    if (m_ZoneCollection != 0)
+    {
+        INT32 index;
+        INT32 zoneCount;
+        zoneCount = m_ZoneCollection->GetCount ();
+        for (index = 0;index < zoneCount;index += 1)
+        {
+            Ptr<CCoordinateSystemMgrsZone> mgrsZoneGrid = m_ZoneCollection->GetItem (index);
+            mgrsZoneGrid->ResetGridTickMemoryThreshold(m_GridTickMemoryThreshold);
+        }
+    }
+    if (m_GraticuleUtm != 0)
+    {
+        m_GraticuleUtm->ResetGridTickMemoryThreshold(m_GridTickMemoryThreshold);
+    }
+    if (m_GraticuleUpsNorth != 0)
+    {
+        m_GraticuleUpsNorth->ResetGridTickMemoryThreshold(m_GridTickMemoryThreshold);
+    }
+    if (m_GraticuleUpsSouth != 0)
+    {
+        m_GraticuleUpsSouth->ResetGridTickMemoryThreshold(m_GridTickMemoryThreshold);
+    }
     return rtnValue;
 }
 
@@ -1010,11 +1081,13 @@
                 if (pPolygonIntersection != 0)
                 {
                     reducedFrameBoundary = csFactory->GridBoundary (pPolygonIntersection);
-                    mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme);
+                    mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme,
+                                                                  m_GridLineMemoryThreshold,m_GridTickMemoryThreshold,m_GridRegionMemoryThreshold);
                     zoneCollection->Add (mgrsZoneGrid);
 
                     // Construct the m_GraticuleUpsSouth member, it may be needed.
-                    m_GraticuleUpsSouth = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS);
+                    m_GraticuleUpsSouth = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS,
+                                                                        m_GridLineMemoryThreshold,m_GridTickMemoryThreshold);
                 }
             }
         }
@@ -1045,11 +1118,13 @@
                 if (pPolygonIntersection != 0)
                 {
                     reducedFrameBoundary = csFactory->GridBoundary (pPolygonIntersection);
-                    mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme);
+                    mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme,
+                                                                  m_GridLineMemoryThreshold,m_GridTickMemoryThreshold,m_GridRegionMemoryThreshold);
                     zoneCollection->Add (mgrsZoneGrid);
 
                     // Construct the m_GraticuleUpsNorth member, it may be needed.
-                    m_GraticuleUpsNorth = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS);
+                    m_GraticuleUpsNorth = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS,
+                                                                        m_GridLineMemoryThreshold,m_GridTickMemoryThreshold);
                 }
             }
         }
@@ -1106,7 +1181,8 @@
                     if (pPolygonIntersection != 0)
                     {
                         reducedFrameBoundary = csFactory->GridBoundary (pPolygonIntersection);
-                        mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme);
+                        mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme,
+                                                                      m_GridLineMemoryThreshold,m_GridTickMemoryThreshold,m_GridRegionMemoryThreshold);
                         zoneCollection->Add (mgrsZoneGrid);
                     }
                  }
@@ -1134,7 +1210,8 @@
                     if (pPolygonIntersection != 0)
                     {
                         reducedFrameBoundary = csFactory->GridBoundary (pPolygonIntersection);
-                        mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme);
+                        mgrsZoneGrid = new CCoordinateSystemMgrsZone (reducedFrameBoundary,zoneNbr,useFrameDatum,frameCRS,m_nLetteringScheme,
+                                                                      m_GridLineMemoryThreshold,m_GridTickMemoryThreshold,m_GridRegionMemoryThreshold);
                         zoneCollection->Add (mgrsZoneGrid);
                     }
                 }
@@ -1153,7 +1230,8 @@
             if (pPolygonIntersection != 0)
             {
                 reducedFrameBoundary = csFactory->GridBoundary (pPolygonIntersection);
-                m_GraticuleUtm = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS);
+                m_GraticuleUtm = new CCoordinateSystemOneGrid (reducedFrameBoundary,llCRS,frameCRS,
+                                                               m_GridLineMemoryThreshold,m_GridTickMemoryThreshold);
             }
         }
     MG_CATCH_AND_THROW(L"MgCoordinateSystemMgrs::FrameBoundaryToZones")

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysMgrs.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -130,6 +130,9 @@
     INT32 m_GridLineExceptionLevel;
     INT32 m_GridRegionExceptionLevel;
     INT32 m_GridTickExceptionLevel;
+    INT64 m_GridLineMemoryThreshold;
+    INT64 m_GridRegionMemoryThreshold;
+    INT64 m_GridTickMemoryThreshold;
     INT32 m_nLastError;
     Ptr<MgCoordinateSystem> m_pCsTarget;
     struct cs_Mgrs_* m_pCsMgrs;

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -43,11 +43,16 @@
                                                       INT32 utmZoneNbr,
                                                       bool useFrameDatum,
                                                       MgCoordinateSystem* frameCRS,
-                                                      INT8 letteringScheme)
+                                                      INT8 letteringScheme,
+                                                      INT64 gridLineMemoryThreshold,
+                                                      INT64 gridTickMemoryThreshold,
+                                                      INT64 gridRegionMemoryThreshold)
                                                         :
-                                                      CCoordinateSystemOneGrid (),
+                                                      CCoordinateSystemOneGrid (gridLineMemoryThreshold,
+                                                                                gridTickMemoryThreshold),
                                                       m_UtmZone                (utmZoneNbr),
-                                                      m_LetteringScheme        (letteringScheme)
+                                                      m_LetteringScheme        (letteringScheme),
+                                                      m_GridRegionMemoryThreshold(gridRegionMemoryThreshold)
 {
     MgCoordinateSystemFactory csFactory;
     Ptr<MgCoordinateSystem> utmZoneCRS;
@@ -62,19 +67,17 @@
 {
 }
 CCoordinateSystemGridRegionCollection* CCoordinateSystemMgrsZone::GetGridRegions (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                                                  MgCoordinateSystemGridSpecification* specification,
-                                                                                  INT32 exceptionLvl)
+                                                                                  MgCoordinateSystemGridSpecification* specification)
 {
     Ptr<CCoordinateSystemGridRegionCollection> regionCollection;
 
     MG_TRY ()
-        regionCollection = BuildRegionCollection (frameBoundary,specification,exceptionLvl);
+        regionCollection = BuildRegionCollection (frameBoundary,specification);
     MG_CATCH_AND_THROW(L"MgCoordinateSystemMgrsZone::GetGridRegions")
     return SAFE_ADDREF(regionCollection.p);
 }
 CCoordinateSystemGridLineCollection* CCoordinateSystemMgrsZone::GetGridLines (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                                              MgCoordinateSystemGridSpecification* specification,
-                                                                              INT32 exceptionLvl)
+                                                                              MgCoordinateSystemGridSpecification* specification)
 {
     // For now, we just call the generic OneGrid grid line function.
     // Later on we might need to add some special logic for zones 31
@@ -83,8 +86,7 @@
     return dynamic_cast<CCoordinateSystemGridLineCollection*>(gridLines);
 }
 CCoordinateSystemGridLineCollection* CCoordinateSystemMgrsZone::GetGraticuleLines (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                                                   MgCoordinateSystemGridSpecification* specification,
-                                                                                   INT32 exceptionLvl)
+                                                                                   MgCoordinateSystemGridSpecification* specification)
 {
     const INT32 maxPoints = 512;
 
@@ -111,7 +113,7 @@
 
     MgCoordinateSystemFactory csFactory;
 
-    Ptr<CCoordinateSystemGridLineCollection> gridLineCollection = new CCoordinateSystemGridLineCollection (exceptionLvl);
+    Ptr<CCoordinateSystemGridLineCollection> gridLineCollection = new CCoordinateSystemGridLineCollection (m_GridLineMemoryThreshold);
 
     MG_TRY ()
         fromPoint = new MgCoordinateXY ();
@@ -130,6 +132,10 @@
         // grid in geographic coordinate form, and work from there.
         GetGeographicExtents (lngMin,lngMax,latMin,latMax);
         
+        // MajorRegions do not do their own memory checks.  They do all their allocation
+        // in their ctor and it is estimated that the largest potential collection is about
+        // 40Mb.  The first following call to lineCollection->Add() will check, so it would
+        // be redundant to add an additional check here.
         regionCollection = new CCoordinateSystemMgrsMajorRegionCollection (m_UtmZone,latMin,latMax);
         if (regionCollection != 0)
         {
@@ -353,9 +359,12 @@
     // is the uninitialized/unknown/error value.
     return m_UtmZone;
 }
+void CCoordinateSystemMgrsZone::ResetGridRegionMemoryThreshold(INT64 memThreshold)
+{
+    m_GridRegionMemoryThreshold = memThreshold;
+}
 CCoordinateSystemGridRegionCollection* CCoordinateSystemMgrsZone::BuildRegionCollection (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                                                         MgCoordinateSystemGridSpecification* specification,
-                                                                                         INT32 exceptionLvl)
+                                                                                         MgCoordinateSystemGridSpecification* specification)
 {
     double curvePrecision;
     double eastingIncrement;
@@ -363,7 +372,7 @@
     Ptr<CCoordinateSystemGridRegionCollection> regionCollection;
 
     MG_TRY ()
-        regionCollection = new CCoordinateSystemGridRegionCollection (exceptionLvl);
+        regionCollection = new CCoordinateSystemGridRegionCollection (m_GridRegionMemoryThreshold);
         curvePrecision = specification->GetCurvePrecision ();
         if (specification->GetUnitType () == MgCoordinateSystemUnitType::Angular)
         {

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysMgrsZone.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -50,25 +50,26 @@
                                INT32 utmZoneNbr,
                                bool useFrameDatum,
                                MgCoordinateSystem* frameCS,
-                               INT8 letteringScheme);
+                               INT8 letteringScheme,
+                               INT64 gridLineMemoryThreshold,
+                               INT64 gridTickMemoryThreshold,
+                               INT64 gridRegionMemoryThreshold);
     ~CCoordinateSystemMgrsZone (void);
 
     CCoordinateSystemGridLineCollection* GetGridLines (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                       MgCoordinateSystemGridSpecification* specification,
-                                                       INT32 exceptionLvl);
+                                                       MgCoordinateSystemGridSpecification* specification);
     CCoordinateSystemGridLineCollection* GetGraticuleLines (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                            MgCoordinateSystemGridSpecification* specification,
-                                                            INT32 exceptionLvl);
+                                                            MgCoordinateSystemGridSpecification* specification);
     CCoordinateSystemGridRegionCollection* GetGridRegions (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                           MgCoordinateSystemGridSpecification* specification,
-                                                           INT32 exceptionLvl);
+                                                           MgCoordinateSystemGridSpecification* specification);
     INT32 ApproxGridRegionMemoryUsage (MgCoordinateSystemGridSpecification* specification);
     INT32 GetUtmZoneNbr (void);
 
+    void ResetGridRegionMemoryThreshold(INT64 memThreshold);
+
 protected:
     CCoordinateSystemGridRegionCollection* BuildRegionCollection (MgCoordinateSystemGridBoundary* frameBoundary,
-                                                                  MgCoordinateSystemGridSpecification* specification,
-                                                                  INT32 exceptionLvl);
+                                                                  MgCoordinateSystemGridSpecification* specification);
 private:
     void BuildMajorRegions (CCoordinateSystemGridRegionCollection* regionCollection,
                             MgCoordinateSystemGridBoundary* frameBoundary,
@@ -83,6 +84,7 @@
     // Zero is the uninitialized/unknown/error value.
     INT32 m_UtmZone;
     INT8 m_LetteringScheme;
+    INT64 m_GridRegionMemoryThreshold;
 //    Ptr<CCoordinateSystemGridRegionCollection> m_RegionCollection;
 
     // Not implemented

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -41,16 +41,17 @@
 //=============================================================================
 // CCoordinateSystemOneGrid
 const INT32 CCoordinateSystemOneGrid::MaxCurvePoints = 511;
-const INT32 CCoordinateSystemOneGrid::m_GridLineExceptionLevelK = 50000000L;   // 50MB
-const INT32 CCoordinateSystemOneGrid::m_GridTickExceptionLevelK = 20000000L;   // 20MB
 
-CCoordinateSystemOneGrid::CCoordinateSystemOneGrid () : MgGuardDisposable        (),
+CCoordinateSystemOneGrid::CCoordinateSystemOneGrid (INT64 gridLineMemoryThreshold,
+                                                    INT64 gridTickMemoryThreshold) 
+                                                        : 
+                                                        MgGuardDisposable        (),
                                                         m_GridFrameCrsSame       (false),
                                                         m_UserID                 (0),
                                                         m_MaxCurvePoints         (MaxCurvePoints),
                                                         m_Label                  (),
-                                                        m_GridLineExceptionLevel (m_GridLineExceptionLevelK),
-                                                        m_GridTickExceptionLevel (m_GridTickExceptionLevelK),
+                                                        m_GridLineMemoryThreshold(gridLineMemoryThreshold),
+                                                        m_GridTickMemoryThreshold(gridTickMemoryThreshold),
                                                         m_GridCRS                (),
                                                         m_FrameCRS               (),
                                                         m_ToFrameXform           (),
@@ -63,15 +64,17 @@
 }
 CCoordinateSystemOneGrid::CCoordinateSystemOneGrid (MgCoordinateSystemGridBoundary* frameBoundary,
                                                     MgCoordinateSystem* gridCRS,
-                                                    MgCoordinateSystem* frameCRS)
+                                                    MgCoordinateSystem* frameCRS,
+                                                    INT64 gridLineMemoryThreshold,
+                                                    INT64 gridTickMemoryThreshold)
                                                         :
                                                     MgGuardDisposable        (),
                                                     m_GridFrameCrsSame       (false),
                                                     m_UserID                 (0),
                                                     m_MaxCurvePoints         (MaxCurvePoints),
                                                     m_Label                  (),
-                                                    m_GridLineExceptionLevel (m_GridLineExceptionLevelK),
-                                                    m_GridTickExceptionLevel (m_GridTickExceptionLevelK),
+                                                    m_GridLineMemoryThreshold(gridLineMemoryThreshold),
+                                                    m_GridTickMemoryThreshold(gridTickMemoryThreshold),
                                                     m_GridCRS                (),
                                                     m_FrameCRS               (),
                                                     m_ToFrameXform           (),
@@ -166,7 +169,7 @@
     Ptr<MgLineStringCollection> lineStringCollection;
     Ptr<CCoordinateSystemGridLine> gridLine;
 
-    Ptr<CCoordinateSystemGridLineCollection> gridLineCollection = new CCoordinateSystemGridLineCollection (m_GridLineExceptionLevel);
+    Ptr<CCoordinateSystemGridLineCollection> gridLineCollection = new CCoordinateSystemGridLineCollection (m_GridLineMemoryThreshold);
 
     MG_TRY()
         gridCrsUnitCode = m_GridCRS->GetUnitCode ();
@@ -309,7 +312,7 @@
         gridCrsUnitCode = m_GridCRS->GetUnitCode ();
 
         CCoordinateSystemGridSpecification* mySpecPtr = dynamic_cast<CCoordinateSystemGridSpecification*>(specs);
-        tickCollection = new CCoordinateSystemGridTickCollection (m_GridTickExceptionLevel);
+        tickCollection = new CCoordinateSystemGridTickCollection (m_GridTickMemoryThreshold);
 
         // Get the grid extents.
         curvePrecision = mySpecPtr->GetCurvePrecision();
@@ -512,24 +515,13 @@
 {
     return 10000;
 }
-
-INT32 CCoordinateSystemOneGrid::SetGridLineExceptionLevel (INT32 memoryUseMax)
+void CCoordinateSystemOneGrid::ResetGridLineMemoryThreshold(INT64 memThreshold)
 {
-    INT32 rtnValue = m_GridLineExceptionLevel;
-    if (memoryUseMax > 0L)
-    {
-        m_GridLineExceptionLevel = memoryUseMax;
-    }
-    return rtnValue;
+    m_GridLineMemoryThreshold = memThreshold;
 }
-INT32 CCoordinateSystemOneGrid::SetGridTickExceptionLevel (INT32 memoryUseMax)
+void CCoordinateSystemOneGrid::ResetGridTickMemoryThreshold(INT64 memThreshold)
 {
-    INT32 rtnValue = m_GridTickExceptionLevel;
-    if (memoryUseMax > 0L)
-    {
-        m_GridTickExceptionLevel = memoryUseMax;
-    }
-    return rtnValue;
+    m_GridTickMemoryThreshold = memThreshold;
 }
 MgCoordinateSystemGridBoundary* CCoordinateSystemOneGrid::GetFrameBoundary (void)
 {

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysOneGrid.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -40,14 +40,15 @@
 
 class CCoordinateSystemOneGrid : public MgGuardDisposable
 {
-    static const INT32 m_GridLineExceptionLevelK;
-    static const INT32 m_GridTickExceptionLevelK;
 public:
     static const INT32 MaxCurvePoints;
-    CCoordinateSystemOneGrid (void);
+    CCoordinateSystemOneGrid (INT64 gridLineMemoryThreshold,
+                              INT64 gridTickMemoryThreshold);
     CCoordinateSystemOneGrid (MgCoordinateSystemGridBoundary* frameBoundary,
                               MgCoordinateSystem* gridCRS,
-                              MgCoordinateSystem* frameCRS);
+                              MgCoordinateSystem* frameCRS,
+                              INT64 gridLineMemoryThreshold,
+                              INT64 gridTickMemoryThreshold);
     ~CCoordinateSystemOneGrid (void);
 
     void SetUp (MgCoordinateSystemGridBoundary* frameBoundary,
@@ -71,8 +72,8 @@
     INT32 ApproxGridLineMemoryUsage (MgCoordinateSystemGridSpecification* specification);
     INT32 ApproxGridTickMemoryUsage (MgCoordinateSystemGridSpecification* specification);
 
-    INT32 SetGridLineExceptionLevel (INT32 memoryUseMax);
-    INT32 SetGridTickExceptionLevel (INT32 memoryUseMax);
+    void ResetGridLineMemoryThreshold(INT64 memThreshold);
+    void ResetGridTickMemoryThreshold(INT64 memThreshold);
 
 protected:
     MgCoordinateSystemGridBoundary* GetFrameBoundary (void);
@@ -89,8 +90,8 @@
     INT32 m_UserID;                                      // For user convenience (i.e. UTM zone)
     INT32 m_MaxCurvePoints;
     STRING m_Label;                                      // For user conveinence (i.e. MGRS)
-    INT32 m_GridLineExceptionLevel;
-    INT32 m_GridTickExceptionLevel;
+    INT64 m_GridLineMemoryThreshold;
+    INT64 m_GridTickMemoryThreshold;
     Ptr<MgCoordinateSystem> m_GridCRS;                   // The grid coordinate system
     Ptr<MgCoordinateSystem> m_FrameCRS;                  // The frame coordinate system
     Ptr<MgCoordinateSystemTransform> m_ToFrameXform;     // Converts grid coordinates to frame coordinates

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.cpp
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.cpp	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.cpp	2009-12-12 01:27:59 UTC (rev 4419)
@@ -680,4 +680,24 @@
     }
 }
 
+//*****************************************************************************
+INT64 GetAvailableMemory()
+{
+#ifdef _WIN32
+    // Size of available memory in bytes, with an additional 
+    // 200Mb subtracted to take into account heap fragmentation,
+    // and a 100Mb buffer for smaller allocations that may happen in 
+    // between checks.
+    MEMORYSTATUSEX  statex;
+    statex.dwLength = sizeof (statex);
+    INT64 maxAvailable = 0L;  
+    if(GlobalMemoryStatusEx (&statex)) {
+        maxAvailable = min(statex.ullAvailPhys + statex.ullAvailPageFile, statex.ullAvailVirtual);
+        maxAvailable = (maxAvailable > 300000000L) ? maxAvailable - 300000000L : 0L;
+    }
+    return maxAvailable;
+#else // LINUX
+    throw new MgNotImplementedException(L"GetAvailableMemory", __LINE__, __WFILE__, NULL, L"", NULL);
+#endif
+}
 //End of file.

Modified: trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.h
===================================================================
--- trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/CoordinateSystem/CoordSysUtil.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -229,4 +229,7 @@
     char *m_pszCurLocale;
 };
 
+// Returns approximate remaining memory for the current process
+INT64 GetAvailableMemory();
+
 #endif //_CCOORDINATESYSTEMUTIL_H_

Modified: trunk/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemGrids.h
===================================================================
--- trunk/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemGrids.h	2009-12-11 23:21:02 UTC (rev 4418)
+++ trunk/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemGrids.h	2009-12-12 01:27:59 UTC (rev 4419)
@@ -776,7 +776,6 @@
     virtual MgLineStringCollection* GetSegmentCollection(void)= 0;
     virtual void SetSegmentCollection (MgLineStringCollection* segmentCollection) = 0;
 INTERNAL_API:
-    virtual INT32 GetMemoryUsage (void) = 0;
 protected:
     INT32 GetClassId(){return m_cls_id;};
 CLASS_ID:
@@ -804,7 +803,6 @@
     virtual MgLineStringCollection* GetWestLine (void) = 0;
 
 INTERNAL_API:
-    virtual INT32 GetMemoryUsage (void) = 0;
 protected:
     INT32 GetClassId(){return m_cls_id;};
 CLASS_ID:
@@ -828,7 +826,6 @@
     virtual MgCoordinate* GetPosition () = 0;
     virtual MgCoordinate* GetDirectionVector () = 0;
 INTERNAL_API:
-    virtual INT32 GetMemoryUsage (void) = 0;
 protected:
     INT32 GetClassId(){return m_cls_id;};
 CLASS_ID:
@@ -853,7 +850,6 @@
     virtual void SetItem (INT32 index, MgCoordinateSystemGridLine* value)=0;
     virtual void Add (MgCoordinateSystemGridLine* value)=0;
     virtual void AddCollection (MgCoordinateSystemGridLineCollection* aGridLineCollection)=0;
-    virtual INT32 GetMemoryUsage (void) = 0;
 
 protected:
     INT32 GetClassId(){return m_cls_id;};
@@ -874,7 +870,6 @@
 INTERNAL_API:
     virtual void SetItem (INT32 index, MgCoordinateSystemGridRegion* value)=0;
     virtual void Add (MgCoordinateSystemGridRegion* value)=0;
-    virtual INT32 GetMemoryUsage (void) = 0;
 
 protected:
     INT32 GetClassId(){return m_cls_id;};
@@ -897,7 +892,6 @@
 INTERNAL_API:
     virtual void SetItem (INT32 index, MgCoordinateSystemGridTick* value)=0;
     virtual void Add (MgCoordinateSystemGridTick* value)=0;
-    virtual INT32 GetMemoryUsage (void) = 0;
 
 protected:
     INT32 GetClassId(){return m_cls_id;};



More information about the mapguide-commits mailing list