00001 // $Id: objcache.cpp 751 2006-03-31 15:43: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 00099 /* 00100 */ 00101 00102 #include "camtypes.h" 00103 #include "objcache.h" 00104 #include "cache.h" 00105 #include <time.h> 00106 #include <stdlib.h> 00107 00108 00109 DECLARE_SOURCE( "$Revision: 751 $" ); 00110 00111 00112 CC_IMPLEMENT_DYNCREATE(ObjectCache, CCObject) 00113 00114 // Declare smart memory handling in Debug builds 00115 #define new CAM_DEBUG_NEW 00116 00117 00118 #define MYTRACE if(IsUserName("Olivier")) TRACE 00119 00120 /********************************************************************************************* 00121 00122 > ObjectCache::ObjectCache() 00123 00124 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00125 Created: 20/01/97 00126 Purpose: default constructor for ObjectCache 00127 Note: 00128 SeeAlso: 00129 **********************************************************************************************/ 00130 ObjectCache::ObjectCache() 00131 { 00132 // Compute the available amount of memory 00133 /*TunedMemory* pTuneMem = GetTunedMemManager(); 00134 if (pTuneMem != NULL) 00135 Ceiling = pTuneMem->GetAvailableTunedMem();*/ 00136 00137 m_NumObjects = 0; 00138 m_HandleFactory = 1; 00139 m_CurrentSize = 0; 00140 } 00141 00142 /********************************************************************************************* 00143 00144 > ObjectCache::ObjectCache(UINT32 ceiling) 00145 00146 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00147 Created: 20/01/97 00148 Purpose: constructor for ObjectCache, set the cache size. 00149 Inputs: ceiling: the amount of memory available for the cache 00150 Note: 00151 SeeAlso: 00152 **********************************************************************************************/ 00153 ObjectCache::ObjectCache(UINT32 ceiling) 00154 { 00155 m_Ceiling = ceiling; 00156 m_NumObjects = 0; 00157 m_HandleFactory = 1; // we don't start from 0 00158 m_CurrentSize = 0; 00159 } 00160 00161 /********************************************************************************************* 00162 00163 > ObjectCache::~ObjectCache() 00164 00165 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00166 Created: 20/01/97 00167 Purpose: destructor for ObjectCache 00168 Note: 00169 SeeAlso: 00170 **********************************************************************************************/ 00171 ObjectCache::~ObjectCache() 00172 {} 00173 00174 00175 /********************************************************************************************* 00176 00177 > void ObjectCache::SetMaxCacheSize(UINT32 NewCeiling) 00178 00179 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00180 Created: 20/01/97 00181 Inputs: NewCeiling: The size of the new ceiling 00182 Purpose: dynamically change the size (in bytes) of the Cache. 00183 Note: 00184 SeeAlso: 00185 **********************************************************************************************/ 00186 00187 BOOL ObjectCache::SetMaxCacheSize(UINT32 NewCeiling) 00188 { 00189 // MYTRACE( _T("ObjectCache::SetMaxCacheSize()\n")); 00190 while (m_CurrentSize > NewCeiling) 00191 { 00192 if(!DeleteObject()) 00193 break; 00194 } 00195 m_Ceiling = NewCeiling; 00196 return TRUE; 00197 } 00198 00199 00200 /********************************************************************************************* 00201 00202 > CACHE_HANDLE ObjectCache::Add(CachedObject *pObj) 00203 00204 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00205 Created: 21/01/97 00206 Inputs: pObj: the Object to cache 00207 Purpose: Add an object in the Hash table of the cache 00208 Note: 00209 SeeAlso: 00210 **********************************************************************************************/ 00211 00212 CACHE_HANDLE ObjectCache::Add(CachedObject *pObj) 00213 { 00214 ERROR3IF(pObj==NULL, "Error: the object to cache can't be NULL"); 00215 00216 if (pObj == NULL) 00217 return 0; 00218 00219 // MYTRACE( _T("ObjectCache::Add()\n")); 00220 00221 // if we can make room for the object then assign the object a unique handle and cache it 00222 if (MakeRoomFor(pObj)) 00223 { 00224 CACHE_HANDLE hObj = m_HandleFactory++; 00225 00226 pObj->StoreHandle(hObj); // store the CachedObject Handle 00227 m_NumObjects++; // Increase the number of objects 00228 m_CurrentSize += pObj->GetSize(); // Increase the space used 00229 m_ObjectMap[hObj] = pObj; // set the object in the hash table 00230 return hObj; 00231 } 00232 00233 return 0; 00234 } 00235 00236 00237 /********************************************************************************************* 00238 00239 > BOOL ObjectCache::MakeRoomFor(CachedObject* pObj) 00240 00241 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00242 Created: 21/01/97 00243 Inputs: pObj: The object to add in the cache 00244 Returns: TRUE if succesful, FALSE otherwise 00245 Purpose: make room for another object by deleting old objects if the cache is full 00246 Note: 00247 SeeAlso: 00248 **********************************************************************************************/ 00249 00250 BOOL ObjectCache::MakeRoomFor(CachedObject* pObj) 00251 { 00252 ERROR3IF(pObj==NULL, "Error: the object to cache can't be NULL"); 00253 00254 if (pObj == NULL) 00255 return FALSE; 00256 00257 // is there room in the cache or do we have to free some memory ? 00258 if (pObj->GetSize() + m_CurrentSize <= m_Ceiling) 00259 return TRUE; // enough room 00260 00261 // is the cache big enough ? 00262 if (pObj->GetSize() > m_Ceiling) 00263 return FALSE; // the object is bigger than the cache size 00264 00265 // Scan the cache deleting objects until there is room 00266 BOOL EnoughRoom = (pObj->GetSize() + m_CurrentSize <= m_Ceiling); 00267 do 00268 { 00269 BOOL ObjectDeleted = DeleteObject(); 00270 00271 EnoughRoom = (pObj->GetSize() + m_CurrentSize <= m_Ceiling); 00272 00273 if(!ObjectDeleted && !EnoughRoom) 00274 return FALSE; // no room and no deleted objects 00275 } 00276 while (!EnoughRoom); 00277 00278 return TRUE; 00279 } 00280 00281 00282 /********************************************************************************************* 00283 00284 > CachedObject *ObjectCache::LookUp(CACHE_HANDLE hObj) 00285 00286 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00287 Created: 21/01/97 00288 Purpose: LookUp in the hash table 00289 Returns: return a pointer to the object found, NULL otherwise 00290 Note: 00291 SeeAlso: 00292 **********************************************************************************************/ 00293 00294 CachedObject *ObjectCache::LookUp(CACHE_HANDLE hObj) 00295 { 00296 // void* pObjMap = NULL; 00297 00298 std::map<WORD, PVOID>::iterator iter = m_ObjectMap.find( hObj ); 00299 if( m_ObjectMap.end() != iter ) 00300 return (CachedObject *)iter->second; 00301 00302 return NULL; 00303 } 00304 00305 00306 /********************************************************************************************* 00307 00308 > BOOL ObjectCache::Remove(CACHE_HANDLE hObj) 00309 00310 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00311 Created: 21/01/97 00312 Inputs: hObj: the object handle 00313 Returns: TRUE if success, FALSE otherwise 00314 Purpose: Remove a key from the hash table 00315 Note: 00316 SeeAlso: 00317 **********************************************************************************************/ 00318 00319 BOOL ObjectCache::Remove(CACHE_HANDLE hObj) 00320 { 00321 if( m_ObjectMap.erase( hObj ) ) 00322 return TRUE; 00323 00324 return FALSE; 00325 } 00326 00327 00328 /********************************************************************************************* 00329 00330 > BOOL ObjectCache::DeleteObject() 00331 00332 Author: Olivier_Gascoin (Xara Group Ltd) <camelotdev@xara.com> 00333 Created: 21/01/97 00334 Purpose: virtual function. This is the base class delete function. All algorithms 00335 are implemented in the derived classes such as ObjectCacheRandom...etc. 00336 Note: 00337 SeeAlso: 00338 **********************************************************************************************/ 00339 00340 BOOL ObjectCache::DeleteObject() 00341 { 00342 return TRUE; 00343 } 00344 00345