1 // $Id: tunemem.cpp 1282 2006-06-09 09:46:49Z alex $
2 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
3 ================================XARAHEADERSTART===========================
5 Xara LX, a vector drawing and manipulation program.
6 Copyright (C) 1993-2006 Xara Group Ltd.
7 Copyright on certain contributions may be held in joint with their
8 respective authors. See AUTHORS file for details.
10 LICENSE TO USE AND MODIFY SOFTWARE
11 ----------------------------------
13 This file is part of Xara LX.
15 Xara LX is free software; you can redistribute it and/or modify it
16 under the terms of the GNU General Public License version 2 as published
17 by the Free Software Foundation.
19 Xara LX and its component source files are distributed in the hope
20 that it will be useful, but WITHOUT ANY WARRANTY; without even the
21 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License along
25 with Xara LX (see the file GPL in the root directory of the
26 distribution); if not, write to the Free Software Foundation, Inc., 51
27 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 Conditional upon your continuing compliance with the GNU General Public
34 License described above, Xara Group Ltd grants to you certain additional
37 The additional rights are to use, modify, and distribute the software
38 together with the wxWidgets library, the wxXtra library, and the "CDraw"
39 library and any other such library that any version of Xara LX relased
40 by Xara Group Ltd requires in order to compile and execute, including
41 the static linking of that library to XaraLX. In the case of the
42 "CDraw" library, you may satisfy obligation under the GNU General Public
43 License to provide source code by providing a binary copy of the library
44 concerned and a copy of the license accompanying it.
46 Nothing in this section restricts any of the rights you have under
47 the GNU General Public License.
53 This license applies to this program (XaraLX) and its constituent source
54 files only, and does not necessarily apply to other Xara products which may
55 in part share the same code base, and are subject to their own licensing
58 This license does not apply to files in the wxXtra directory, which
59 are built into a separate library, and are subject to the wxWindows
60 license contained within that directory in the file "WXXTRA-LICENSE".
62 This license does not apply to the binary libraries (if any) within
63 the "libs" directory, which are subject to a separate license contained
64 within that directory in the file "LIBS-LICENSE".
67 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
68 ----------------------------------------------
70 Subject to the terms of the GNU Public License (see above), you are
71 free to do whatever you like with your modifications. However, you may
72 (at your option) wish contribute them to Xara's source tree. You can
73 find details of how to do this at:
74 http://www.xaraxtreme.org/developers/
76 Prior to contributing your modifications, you will need to complete our
77 contributor agreement. This can be found at:
78 http://www.xaraxtreme.org/developers/contribute/
80 Please note that Xara will not accept modifications which modify any of
81 the text between the start and end of this header (marked
82 XARAHEADERSTART and XARAHEADEREND).
88 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
89 designs are registered or unregistered trademarks, design-marks, and/or
90 service marks of Xara Group Ltd. All rights in these marks are reserved.
93 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
96 =================================XARAHEADEREND============================
98 // Tuned Memory manager
103 #include "camtypes.h"
105 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
106 //#include "fixmem.h" - in camtypes.h [AUTOMATICALLY REMOVED]
108 // An implement to match the Declare in the .h file.
109 CC_IMPLEMENT_MEMDUMP(TunedMemory
, CC_CLASS_MEMDUMP
)
111 // This will get Camelot to display the filename and linenumber of any memory allocations
112 // that are not released at program exit
113 // Declare smart memory handling in Debug builds
114 #define new CAM_DEBUG_NEW
116 //static BOOL ShowBandingRAM = FALSE;
118 // Defaults for smart banding
119 BOOL
TunedMemory::RecommendInfinite
= FALSE
;
120 INT32
TunedMemory::RecommendRAM
= 1024*768;
122 /********************************************************************************************
124 Preference: MaxRenderingRAM
127 Purpose: This pref is used to set the maximum amount of ram that can be used to
128 render with. If there is less than the screen needs here, the rendering
129 will be banded. Useful on Low memory machines.
131 ********************************************************************************************/
133 size_t TunedMemory::LimitRAM
= 1024*768;
134 size_t TunedMemory::TotalRAM
= 1024*768;
135 size_t TunedMemory::AvailableRAM
= 1024*768; // Make this the same!
139 /********************************************************************************************
141 Preference: IsRenderingRAMInfinte
144 Purpose: If this pref is TRUE then the MaxRenderingRAM preference is ignored and all
145 the ram we want is used. If it is FALSE the rendering ram will be limited
146 to the figure in MaxRenderingRAM
148 ********************************************************************************************/
150 BOOL
TunedMemory::IsInfinite
= TRUE
;
153 /********************************************************************************************
155 Preference: SmartRenderingRAM
158 Purpose: If this pref is TRUE then the MaxRenderingRAM preference is ignored and
159 the amount to use is calculated according to available memory.
160 If it is FALSE the rendering ram will be limited to the figure in
163 ********************************************************************************************/
165 BOOL
TunedMemory::SmartBanding
= TRUE
;
168 /********************************************************************************************
170 > TunedMemory::TunedMemory()
172 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
174 Purpose: Constructor - sets the memory thing up with a default value of 1Mb of
177 ********************************************************************************************/
179 TunedMemory::TunedMemory()
181 // make sure that the available ram is the same as the total
182 AvailableRAM
= TotalRAM
= LimitRAM
;
189 /********************************************************************************************
191 > TunedMemory::~TunedMemory()
193 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
195 Purpose: Destructor - TRACES an error out if there is still some ram left over
197 ********************************************************************************************/
199 TunedMemory::~TunedMemory()
201 if (AvailableRAM
!=TotalRAM
)
203 TRACEUSER( "Rik", wxT("At Exit the Available Ram did not equal the Total Ram in TunedMemory\n") );
204 TRACEUSER( "Rik", wxT("TotalRAM = %ld, Available=%ld\n"), TotalRAM
, AvailableRAM
);
212 /********************************************************************************************
214 > LPVOID TunedMemory::LimitedCCMalloc(UINT32 Size)
216 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
218 Inputs: Size - the ammount of ram needed
219 Returns: A pointer to the allocated ram or NULL if there is no more ram available
220 Purpose: Allocates ram out of the limited pool. The Available ram is decreased.
221 SeeAlso: LimitedCCFree
223 ********************************************************************************************/
225 LPVOID
TunedMemory::LimitedCCMalloc(size_t Size
)
228 LPVOID Memory
= CCMalloc(Size
);
230 // if we got it, make a note of its allocation
231 if ((Memory
!=NULL
) && (!IsInfinite
))
233 AvailableRAM
-= Size
;
236 // return what we got
240 /********************************************************************************************
242 > LPVOID TunedMemory::LimitedCCRealloc(LPVOID buf, UINT32 NewSize)
244 Author: Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
246 Inputs: Size - the new ammount of ram needed
247 Returns: A pointer to the allocated ram or NULL if there is no more ram available
248 Purpose: Re-Allocates ram out of the limited pool. The Available ram is decreased.
249 SeeAlso: LimitedCCFree
251 ********************************************************************************************/
253 LPVOID
TunedMemory::LimitedCCRealloc(LPVOID buf
, size_t NewSize
)
255 // Find out how big the block is at the moment
256 size_t OldSize
= CCGetBlockSize(buf
);
259 LPVOID Memory
= CCRealloc( buf
, NewSize
);
261 // If it got bigger, then decrease the available RAM,
262 // otherwise we increase it
263 if ((Memory
!=NULL
) && (!IsInfinite
))
265 AvailableRAM
-= (NewSize
- OldSize
);
268 // return what we got
275 /********************************************************************************************
277 > void TunedMemory::LimitedCCFree(LPVOID buf)
279 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
281 Inputs: buf - the previously allocated buffer
282 Purpose: Frees the memory allocated by LimitedCCMalloc
284 ********************************************************************************************/
286 void TunedMemory::LimitedCCFree(LPVOID buf
)
288 // Find out how big the block was
289 size_t BlockSize
= CCGetBlockSize(buf
);
291 // add it back into the pool
293 AvailableRAM
+= BlockSize
;
303 /********************************************************************************************
305 > INT32 TunedMemory::GetTotalTunedMem()
307 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
309 Returns: Total ram in the limited heap
310 Purpose: Finds out the total ram that can be allocated in the limited memory thing.
311 This is how much ram the user has deemed enough for Camelot to do its
312 rendering etc with. When this ammount has been allocated rendering will
313 have to wait for other regions to finish before they can start.
314 SeeAlso: TunedMemory::GetAvailableTunedMem
316 ********************************************************************************************/
318 size_t TunedMemory::GetTotalTunedMem()
327 /********************************************************************************************
329 > INT32 TunedMemory::GetAvailableTunedMem()
331 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
333 Returns: The amount of available ram in the limited pool
334 Purpose: This is the amount of the ram that could be allocated (GetTotal) that is
335 still left unallocated. If this amount is not enough for your job, then
336 you should wait until this function returns a bigger number. It would also
337 be worth checking to see if the TotalTunedRam would be enough as well.
339 ********************************************************************************************/
341 size_t TunedMemory::GetAvailableTunedMem()
348 /********************************************************************************************
350 > static INT32 TunedMemory::GetLimitedMemSize()
352 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
354 Returns: The current size of the limited memory thing
355 Purpose: To allows the Options Dialog to find out what the current setting for the
356 amount of RAM that can be used for rendering is at.
358 ********************************************************************************************/
360 size_t TunedMemory::GetLimitedMemSize()
367 /********************************************************************************************
369 > static void TunedMemory::SetLimitedMemSize(INT32 NewSize)
371 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
373 Inputs: NewSize - The new size of the limited memory heap
374 Returns: NewSize if ok or if the value is too low, a new minimum size.
375 Purpose: This function allows the options dialog to set the amount of ram to use
376 for drawing to a new value that the user chose. If the value is less than
377 a minimum useful value, at present 128k, then the value will be ignored and
380 ********************************************************************************************/
382 size_t TunedMemory::SetLimitedMemSize(size_t NewSize
)
384 // make sure it is not too small for safty
385 if (NewSize
<1024*128)
388 // Find out what the difference is
389 size_t Difference
= NewSize
- TotalRAM
;
391 // Set the total to the new size
395 // And change the available ram as well
396 AvailableRAM
+= Difference
;
403 /********************************************************************************************
405 > static BOOL TunedMemory::IsMemSizeInfinite()
407 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
409 Returns: TRUE if the Limited heap is not currently limited, FALSE if it is
410 Purpose: Lets the options dialog have a look at the Tuned Memorys IsInfinite state.
412 ********************************************************************************************/
414 BOOL
TunedMemory::IsMemSizeInfinite()
419 /********************************************************************************************
421 > static BOOL TunedMemory::IsAutomaticMemory()
423 Author: Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
425 Returns: TRUE if the memory limit will be calculated automatically.
426 FALSE if the user value is being used.
427 Purpose: Lets the options dialog have a look at the Smart banding state.
429 ********************************************************************************************/
431 BOOL
TunedMemory::IsAutomaticMemory()
433 // This routine has been changed to determine whether to use the
434 // recommended memory or not.
439 /********************************************************************************************
441 > static void TunedMemory::SetMemSizeInfinte(BOOL NewIsInfinite)
443 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
445 Inputs: NewIsInfinite - The new state of the Infinte flag
446 Purpose: Sets the infinte flag in the limited memory manager. TRUE means the limited
447 heap should be made infinte, FALSE means use the value found by
449 NOTE: This call now determines whether to use the recommended memory
452 ********************************************************************************************/
454 void TunedMemory::SetMemSizeInfinte(BOOL NewSmartBanding
)
456 // Set the member version
457 SmartBanding
= NewSmartBanding
;
461 // If 'Smart' banding enabled, then use the
462 // recommended memory (calculated on startup)
463 IsInfinite
= RecommendInfinite
;
467 // Set the recommended memory limit
469 // Find out what the difference is
470 size_t Difference
= RecommendRAM
- TotalRAM
;
472 // Set the total to the new size
473 TotalRAM
= RecommendRAM
;
475 // And change the available ram as well
476 AvailableRAM
+= Difference
;
481 // Use the user specified limited RAM
484 AvailableRAM
= LimitRAM
;
491 /********************************************************************************************
493 > static INT32 TunedMemory::GetScreenMemSize()
495 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
497 Returns: The mem required to do a full screen redraw with transparancy on
498 Purpose: This function gives a figure for the amount of ram needed to redraw
499 the full screen with transparency on. for 1024 x 768 this is about 3.5Meg
501 ********************************************************************************************/
503 INT32
TunedMemory::GetScreenMemSize()
505 INT32 Width
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
);
506 INT32 Height
= wxSystemSettings::GetMetric( wxSYS_SCREEN_Y
);
507 return Width
* Height
* 4;
513 /********************************************************************************************
517 Author: Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
519 Returns: TRUE if it worked, FALSE if not
520 Purpose: Declares the preferences for this class in the ini file.
522 ********************************************************************************************/
524 BOOL
TunedMemory::Init()
526 if( Camelot
.DeclareSection( wxT("Screen"), 10 ) )
528 Camelot
.DeclarePref( NULL
, wxT("MaxRenderingRAM"), (INT32
*)&LimitRAM
);
529 Camelot
.DeclarePref( NULL
, wxT("IsRenderingRAMInfinte"), &IsInfinite
, FALSE
, TRUE
);
530 Camelot
.DeclarePref( NULL
, wxT("SmartRenderingRAM"), &SmartBanding
, FALSE
, TRUE
);
532 // Camelot.DeclarePref(NULL, "ShowBandingRAM", &ShowBandingRAM, FALSE, TRUE);
535 // Find the available Physical memory,
536 // and work out a sensible band size to use
537 CalculateRecommendedMemory();
541 // Use the recommended memory
542 TotalRAM
= RecommendRAM
;
543 IsInfinite
= RecommendInfinite
;
547 // Use the user specified limited RAM
551 // Set available ram equal to the total ram
552 AvailableRAM
= TotalRAM
;
556 /********************************************************************************************
558 > void TunedMemory::CalculateRecommendedMemory()
560 Author: Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
562 Purpose: Calculates a sensible default Banding size given the Physical memory
565 ********************************************************************************************/
567 void TunedMemory::CalculateRecommendedMemory()
569 TRACE( _T("Warning - TunedMemory::CalculateRecommendedMemory called") );
572 MEMORYSTATUS MemStat
;
573 MemStat
.dwLength
= sizeof(MEMORYSTATUS
);
574 GlobalMemoryStatus(&MemStat
);
576 INT32 PhysicalMemory
= MemStat
.dwTotalPhys
;
577 TRACEALL( wxT("Total Physical Memory is %d\n"), PhysicalMemory
);
579 // INT32 ScreenMem = GetScreenMemSize();
580 // TRACEALL( _T("Screen Memory is %d\n"), ScreenMem);
582 if (PhysicalMemory
<= 8*1024*1024) // 8 Megs or less
584 RecommendInfinite
= FALSE
; // Use limited memory
585 RecommendRAM
= 512*1024; // Limit to max of 512 K
587 else if (PhysicalMemory
<= 12*1024*1024) // 8 to 12 Megs
589 RecommendInfinite
= FALSE
; // Use limited memory
590 RecommendRAM
= 1*1024*1024; // Limit to max of 1024 K
592 else if (PhysicalMemory
<= 16*1024*1024) // 12 to 16 Megs
594 RecommendInfinite
= FALSE
; // Use limited memory
595 RecommendRAM
= 2*1024*1024; // Limit to max of 2048 K
597 else // More than 16 Megs
599 RecommendInfinite
= TRUE
; // Unlimited memory
604 ERROR3_PF( ("Total Physical Memory is %d", PhysicalMemory
) );
608 ERROR3("Using unlimited banding RAM");
609 TRACEALL( wxT("Using unlimited Rendering RAM\n") );
613 ERROR3_PF( ("Rendering RAM set to %d",TotalRAM
) );
614 TRACEALL( wxT("Rendering RAM set to %d\n"), TotalRAM
);
618 RecommendInfinite
= TRUE
;