tunemem.cpp

Go to the documentation of this file.
00001 // $Id: tunemem.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // Tuned Memory manager
00099 
00100 /*
00101 */
00102 
00103 #include "camtypes.h"
00104 #include "tunemem.h"
00105 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 //#include "fixmem.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 
00108 // An implement to match the Declare in the .h file.
00109 CC_IMPLEMENT_MEMDUMP(TunedMemory, CC_CLASS_MEMDUMP)
00110 
00111 // This will get Camelot to display the filename and linenumber of any memory allocations
00112 // that are not released at program exit
00113 // Declare smart memory handling in Debug builds
00114 #define new CAM_DEBUG_NEW
00115 
00116 //static BOOL ShowBandingRAM = FALSE;
00117 
00118 // Defaults for smart banding
00119 BOOL TunedMemory::RecommendInfinite = FALSE;
00120 INT32 TunedMemory::RecommendRAM = 1024*768;
00121 
00122 /********************************************************************************************
00123 
00124     Preference: MaxRenderingRAM
00125     Section:    
00126     Range:      >128K
00127     Purpose:    This pref is used to set the maximum amount of ram that can be used to
00128                 render with. If there is less than the screen needs here, the rendering
00129                 will be banded. Useful on Low memory machines.
00130 
00131 ********************************************************************************************/
00132 
00133 size_t TunedMemory::LimitRAM        = 1024*768;
00134 size_t TunedMemory::TotalRAM        = 1024*768;
00135 size_t TunedMemory::AvailableRAM    = 1024*768; // Make this the same!
00136 
00137 
00138 
00139 /********************************************************************************************
00140 
00141     Preference: IsRenderingRAMInfinte
00142     Section:    
00143     Range:      TRUE, FALSE
00144     Purpose:    If this pref is TRUE then the MaxRenderingRAM preference is ignored and all
00145                 the ram we want is used. If it is FALSE the rendering ram will be limited
00146                 to the figure in MaxRenderingRAM
00147 
00148 ********************************************************************************************/
00149 
00150 BOOL TunedMemory::IsInfinite = TRUE;
00151 
00152 
00153 /********************************************************************************************
00154 
00155     Preference: SmartRenderingRAM
00156     Section:    
00157     Range:      TRUE, FALSE
00158     Purpose:    If this pref is TRUE then the MaxRenderingRAM preference is ignored and 
00159                 the amount to use is calculated according to available memory. 
00160                 If it is FALSE the rendering ram will be limited to the figure in 
00161                 MaxRenderingRAM
00162 
00163 ********************************************************************************************/
00164 
00165 BOOL TunedMemory::SmartBanding = TRUE;
00166 
00167 
00168 /********************************************************************************************
00169 
00170 >   TunedMemory::TunedMemory()
00171 
00172     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00173     Created:    1/5/95
00174     Purpose:    Constructor - sets the memory thing up with a default value of 1Mb of
00175                 available ram
00176 
00177 ********************************************************************************************/
00178 
00179 TunedMemory::TunedMemory()
00180 {
00181     // make sure that the available ram is the same as the total
00182     AvailableRAM = TotalRAM = LimitRAM;
00183 }
00184 
00185 
00186 
00187 
00188 
00189 /********************************************************************************************
00190 
00191 >   TunedMemory::~TunedMemory()
00192 
00193     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00194     Created:    1/5/95
00195     Purpose:    Destructor - TRACES an error out if there is still some ram left over
00196 
00197 ********************************************************************************************/
00198 
00199 TunedMemory::~TunedMemory()
00200 {
00201     if (AvailableRAM!=TotalRAM)
00202     {
00203         TRACEUSER( "Rik", wxT("At Exit the Available Ram did not equal the Total Ram in TunedMemory\n") );
00204         TRACEUSER( "Rik", wxT("TotalRAM = %ld, Available=%ld\n"), TotalRAM, AvailableRAM );
00205     }
00206 }
00207 
00208 
00209 
00210 
00211 
00212 /********************************************************************************************
00213 
00214 >   LPVOID TunedMemory::LimitedCCMalloc(UINT32 Size)
00215 
00216     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00217     Created:    1/5/95
00218     Inputs:     Size - the ammount of ram needed
00219     Returns:    A pointer to the allocated ram or NULL if there is no more ram available
00220     Purpose:    Allocates ram out of the limited pool. The Available ram is decreased.
00221     SeeAlso:    LimitedCCFree
00222 
00223 ********************************************************************************************/
00224 
00225 LPVOID TunedMemory::LimitedCCMalloc(size_t Size)
00226 {
00227     // Get some ram
00228     LPVOID Memory = CCMalloc(Size);
00229 
00230     // if we got it, make a note of its allocation
00231     if ((Memory!=NULL) && (!IsInfinite))
00232     {
00233         AvailableRAM -= Size;
00234     }
00235 
00236     // return what we got
00237     return Memory;
00238 }
00239 
00240 /********************************************************************************************
00241 
00242 >   LPVOID TunedMemory::LimitedCCRealloc(LPVOID buf, UINT32 NewSize)
00243 
00244     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00245     Created:    5/7/95
00246     Inputs:     Size - the new ammount of ram needed
00247     Returns:    A pointer to the allocated ram or NULL if there is no more ram available
00248     Purpose:    Re-Allocates ram out of the limited pool. The Available ram is decreased.
00249     SeeAlso:    LimitedCCFree
00250 
00251 ********************************************************************************************/
00252 
00253 LPVOID TunedMemory::LimitedCCRealloc(LPVOID buf, size_t NewSize)
00254 {
00255     // Find out how big the block is at the moment
00256     size_t OldSize = CCGetBlockSize(buf); 
00257 
00258     // Resize the block
00259     LPVOID Memory = CCRealloc( buf, NewSize );
00260 
00261     // If it got bigger, then decrease the available RAM,
00262     // otherwise we increase it
00263     if ((Memory!=NULL) && (!IsInfinite))
00264     {
00265         AvailableRAM -= (NewSize - OldSize);
00266     }
00267 
00268     // return what we got
00269     return Memory;
00270 }
00271 
00272 
00273 
00274 
00275 /********************************************************************************************
00276 
00277 >   void TunedMemory::LimitedCCFree(LPVOID buf)
00278 
00279     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00280     Created:    1/5/95
00281     Inputs:     buf - the previously allocated buffer
00282     Purpose:    Frees the memory allocated by LimitedCCMalloc
00283 
00284 ********************************************************************************************/
00285 
00286 void TunedMemory::LimitedCCFree(LPVOID buf)
00287 {
00288     // Find out how big the block was
00289     size_t BlockSize = CCGetBlockSize(buf); 
00290 
00291     // add it back into the pool
00292     if (!IsInfinite)
00293         AvailableRAM += BlockSize;
00294 
00295     // Free the memory
00296     CCFree(buf);
00297 }
00298 
00299 
00300 
00301 
00302 
00303 /********************************************************************************************
00304 
00305 >   INT32 TunedMemory::GetTotalTunedMem()
00306 
00307     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00308     Created:    1/5/95
00309     Returns:    Total ram in the limited heap
00310     Purpose:    Finds out the total ram that can be allocated in the limited memory thing.
00311                 This is how much ram the user has deemed enough for Camelot to do its
00312                 rendering etc with. When this ammount has been allocated rendering will
00313                 have to wait for other regions to finish before they can start.
00314     SeeAlso:    TunedMemory::GetAvailableTunedMem
00315 
00316 ********************************************************************************************/
00317 
00318 size_t TunedMemory::GetTotalTunedMem()
00319 {
00320     return TotalRAM;
00321 }
00322 
00323 
00324 
00325 
00326 
00327 /********************************************************************************************
00328 
00329 >   INT32 TunedMemory::GetAvailableTunedMem()
00330 
00331     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00332     Created:    1/5/95
00333     Returns:    The amount of available ram in the limited pool
00334     Purpose:    This is the amount of the ram that could be allocated (GetTotal) that is
00335                 still left unallocated. If this amount is not enough for your job, then
00336                 you should wait until this function returns a bigger number. It would also
00337                 be worth checking to see if the TotalTunedRam would be enough as well.
00338 
00339 ********************************************************************************************/
00340 
00341 size_t TunedMemory::GetAvailableTunedMem()
00342 {
00343     return AvailableRAM;
00344 }
00345 
00346 
00347 
00348 /********************************************************************************************
00349 
00350 >   static INT32 TunedMemory::GetLimitedMemSize()
00351 
00352     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00353     Created:    4/5/95
00354     Returns:    The current size of the limited memory thing
00355     Purpose:    To allows the Options Dialog to find out what the current setting for the
00356                 amount of RAM that can be used for rendering is at.
00357 
00358 ********************************************************************************************/
00359 
00360 size_t TunedMemory::GetLimitedMemSize()
00361 {
00362     return LimitRAM;
00363 }
00364 
00365 
00366 
00367 /********************************************************************************************
00368 
00369 >   static void TunedMemory::SetLimitedMemSize(INT32 NewSize)
00370 
00371     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00372     Created:    4/5/95
00373     Inputs:     NewSize - The new size of the limited memory heap
00374     Returns:    NewSize if ok or if the value is too low, a new minimum size. 
00375     Purpose:    This function allows the options dialog to set the amount of ram to use
00376                 for drawing to a new value that the user chose. If the value is less than
00377                 a minimum useful value, at present 128k, then the value will be ignored and
00378                 128k will be used.
00379 
00380 ********************************************************************************************/
00381 
00382 size_t TunedMemory::SetLimitedMemSize(size_t NewSize)
00383 {
00384     // make sure it is not too small for safty
00385     if (NewSize<1024*128)
00386         NewSize = 1024*128;
00387 
00388     // Find out what the difference is
00389     size_t Difference = NewSize - TotalRAM;
00390 
00391     // Set the total to the new size
00392     TotalRAM = NewSize;
00393     LimitRAM = NewSize;
00394 
00395     // And change the available ram as well
00396     AvailableRAM += Difference;
00397     
00398     return NewSize;
00399 }
00400 
00401 
00402 
00403 /********************************************************************************************
00404 
00405 >   static BOOL TunedMemory::IsMemSizeInfinite()
00406 
00407     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00408     Created:    4/5/95
00409     Returns:    TRUE if the Limited heap is not currently limited, FALSE if it is
00410     Purpose:    Lets the options dialog have a look at the Tuned Memorys IsInfinite state.
00411 
00412 ********************************************************************************************/
00413 
00414 BOOL TunedMemory::IsMemSizeInfinite()
00415 {
00416     return IsInfinite;
00417 }
00418 
00419 /********************************************************************************************
00420 
00421 >   static BOOL TunedMemory::IsAutomaticMemory()
00422 
00423     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00424     Created:    16/6/95
00425     Returns:    TRUE if the memory limit will be calculated automatically.
00426                 FALSE if the user value is being used.
00427     Purpose:    Lets the options dialog have a look at the Smart banding state.
00428 
00429 ********************************************************************************************/
00430 
00431 BOOL TunedMemory::IsAutomaticMemory()
00432 {
00433     // This routine has been changed to determine whether to use the
00434     // recommended memory or not.
00435     return SmartBanding;
00436 }
00437 
00438 
00439 /********************************************************************************************
00440 
00441 >   static void TunedMemory::SetMemSizeInfinte(BOOL NewIsInfinite)
00442 
00443     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00444     Created:    4/5/95
00445     Inputs:     NewIsInfinite - The new state of the Infinte flag
00446     Purpose:    Sets the infinte flag in the limited memory manager. TRUE means the limited
00447                 heap should be made infinte, FALSE means use the value found by
00448                 GetLimitedMemSize
00449                 NOTE: This call now determines whether to use the recommended memory
00450                       or not.
00451 
00452 ********************************************************************************************/
00453 
00454 void TunedMemory::SetMemSizeInfinte(BOOL NewSmartBanding)
00455 {
00456     // Set the member version
00457     SmartBanding = NewSmartBanding;
00458 
00459     if (SmartBanding)
00460     {
00461         // If 'Smart' banding enabled, then use the 
00462         // recommended memory (calculated on startup)
00463         IsInfinite  = RecommendInfinite;
00464 
00465         if (!IsInfinite)
00466         {
00467             // Set the recommended memory limit
00468 
00469             // Find out what the difference is
00470             size_t      Difference = RecommendRAM - TotalRAM;
00471 
00472             // Set the total to the new size
00473             TotalRAM = RecommendRAM;
00474 
00475             // And change the available ram as well
00476             AvailableRAM += Difference;
00477         }
00478     }
00479     else
00480     {
00481         // Use the user specified limited RAM
00482         IsInfinite = FALSE;
00483         TotalRAM = LimitRAM;
00484         AvailableRAM = LimitRAM;
00485     }
00486 }
00487 
00488 
00489 
00490 
00491 /********************************************************************************************
00492 
00493 >   static INT32 TunedMemory::GetScreenMemSize()
00494 
00495     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00496     Created:    4/5/95
00497     Returns:    The mem required to do a full screen redraw with transparancy on
00498     Purpose:    This function gives a figure for the amount of ram needed to redraw
00499                 the full screen with transparency on. for 1024 x 768 this is about 3.5Meg
00500 
00501 ********************************************************************************************/
00502 
00503 INT32 TunedMemory::GetScreenMemSize()
00504 {
00505     INT32                   Width  = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
00506     INT32                   Height = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
00507     return Width * Height * 4;
00508 }
00509 
00510 
00511 
00512 
00513 /********************************************************************************************
00514 
00515 >   static BOOL Init()
00516 
00517     Author:     Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
00518     Created:    4/5/95
00519     Returns:    TRUE if it worked, FALSE if not
00520     Purpose:    Declares the preferences for this class in the ini file.
00521 
00522 ********************************************************************************************/
00523 
00524 BOOL TunedMemory::Init()
00525 {
00526     if( Camelot.DeclareSection( wxT("Screen"), 10 ) )
00527     { 
00528         Camelot.DeclarePref( NULL, wxT("MaxRenderingRAM"), (INT32*)&LimitRAM );
00529         Camelot.DeclarePref( NULL, wxT("IsRenderingRAMInfinte"), &IsInfinite, FALSE, TRUE );
00530         Camelot.DeclarePref( NULL, wxT("SmartRenderingRAM"), &SmartBanding, FALSE, TRUE );
00531 
00532 //      Camelot.DeclarePref(NULL, "ShowBandingRAM", &ShowBandingRAM, FALSE, TRUE);
00533     }
00534 
00535     // Find the available Physical memory,
00536     // and work out a sensible band size to use
00537     CalculateRecommendedMemory();
00538 
00539     if (SmartBanding)
00540     {
00541         // Use the recommended memory
00542         TotalRAM    = RecommendRAM;
00543         IsInfinite  = RecommendInfinite;
00544     }
00545     else
00546     {
00547         // Use the user specified limited RAM
00548         TotalRAM    = LimitRAM;
00549     }
00550 
00551     // Set available ram equal to the total ram
00552     AvailableRAM = TotalRAM;
00553     return TRUE;
00554 }
00555 
00556 /********************************************************************************************
00557 
00558 >   void TunedMemory::CalculateRecommendedMemory()
00559 
00560     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00561     Created:    16/6/95
00562     Purpose:    Calculates a sensible default Banding size given the Physical memory
00563                 available.
00564 
00565 ********************************************************************************************/
00566 
00567 void TunedMemory::CalculateRecommendedMemory()
00568 {
00569     TRACE( _T("Warning - TunedMemory::CalculateRecommendedMemory called") );
00570 
00571 #if defined(__WXMSW)
00572     MEMORYSTATUS MemStat;
00573     MemStat.dwLength = sizeof(MEMORYSTATUS);
00574     GlobalMemoryStatus(&MemStat);
00575 
00576     INT32 PhysicalMemory = MemStat.dwTotalPhys;
00577     TRACEALL( wxT("Total Physical Memory is %d\n"), PhysicalMemory);
00578 
00579 //  INT32 ScreenMem = GetScreenMemSize();
00580 //  TRACEALL( _T("Screen Memory is %d\n"), ScreenMem);
00581 
00582     if (PhysicalMemory <= 8*1024*1024)          // 8 Megs or less
00583     {
00584         RecommendInfinite = FALSE;              // Use limited memory
00585         RecommendRAM = 512*1024;                // Limit to max of 512 K
00586     }
00587     else if (PhysicalMemory <= 12*1024*1024)    // 8 to 12 Megs
00588     {
00589         RecommendInfinite = FALSE;              // Use limited memory
00590         RecommendRAM = 1*1024*1024;             // Limit to max of 1024 K
00591     }
00592     else if (PhysicalMemory <= 16*1024*1024)    // 12 to 16 Megs
00593     {
00594         RecommendInfinite = FALSE;              // Use limited memory
00595         RecommendRAM = 2*1024*1024;             // Limit to max of 2048 K
00596     }
00597     else                                        // More than 16 Megs
00598     {
00599         RecommendInfinite = TRUE;               // Unlimited memory
00600     }
00601 
00602     if (ShowBandingRAM)
00603     {
00604         ERROR3_PF( ("Total Physical Memory is %d", PhysicalMemory) );
00605 
00606         if (IsInfinite)
00607         {
00608             ERROR3("Using unlimited banding RAM");
00609             TRACEALL( wxT("Using unlimited Rendering RAM\n") );
00610         }
00611         else
00612         {
00613             ERROR3_PF( ("Rendering RAM set to %d",TotalRAM) );
00614             TRACEALL( wxT("Rendering RAM set to %d\n"), TotalRAM );
00615         }
00616     }
00617 #else
00618     RecommendInfinite = TRUE;
00619 #endif
00620 }
00621 

Generated on Sat Nov 10 03:48:57 2007 for Camelot by  doxygen 1.4.4