changeset 5129:74056b129103 draft

(svn r7210) -CodeChange: [YAPF] the global cache object is now not destroyed/recreated whenever the cache is invalidated. It now supports Flush() method that is used instead. It should also fix mem-leak warning produced by valgrind (Tron)
author KUDr <KUDr@openttd.org>
date Sat, 18 Nov 2006 19:20:47 +0000
parents 315b071fad0d
children 5e4e6ca5d920
files yapf/array.hpp yapf/fixedsizearray.hpp yapf/hashtable.hpp yapf/yapf_costcache.hpp
diffstat 4 files changed, 26 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/yapf/array.hpp
+++ b/yapf/array.hpp
@@ -24,6 +24,8 @@
 
 	/** implicit constructor */
 	FORCEINLINE CArrayT() { }
+	/** Clear (destroy) all items */
+	FORCEINLINE void Clear() {m_a.Clear();}
 	/** Return actual number of items */
 	FORCEINLINE int Size() const
 	{
--- a/yapf/fixedsizearray.hpp
+++ b/yapf/fixedsizearray.hpp
@@ -51,12 +51,21 @@
 		// release one reference to the shared block
 		if ((--RefCnt()) > 0) return; // and return if there is still some owner
 
+		Clear();
+		// free the memory block occupied by items
+		free(((int8*)m_items) - ThdrSize);
+		m_items = NULL;
+	}
+
+	/** Clear (destroy) all items */
+	FORCEINLINE void Clear()
+	{
 		// walk through all allocated items backward and destroy them
 		for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) {
 			pItem->~Titem_();
 		}
-		free(((int8*)m_items) - ThdrSize);
-		m_items = NULL;
+		// number of items become zero
+		SizeRef() = 0;
 	}
 
 protected:
--- a/yapf/hashtable.hpp
+++ b/yapf/hashtable.hpp
@@ -12,6 +12,9 @@
 
 	CHashTableSlotT() : m_pFirst(NULL) {}
 
+	/** hash table slot helper - clears the slot by simple forgetting its items */
+	FORCEINLINE void Clear() {m_pFirst = NULL;}
+
 	/** hash table slot helper - linear search for item with given key through the given blob - const version */
 	FORCEINLINE const Titem_* Find(const Key& key) const
 	{
@@ -162,6 +165,9 @@
 	/** item count */
 	FORCEINLINE int Count() const {return m_num_items;}
 
+	/** simple clear - forget all items - used by CSegmentCostCacheT.Flush() */
+	FORCEINLINE void Clear() const {for (int i = 0; i < Tcapacity; i++) m_slots[i].Clear();}
+
 	/** const item search */
 	const Titem_* Find(const Tkey& key) const
 	{
--- a/yapf/yapf_costcache.hpp
+++ b/yapf/yapf_costcache.hpp
@@ -104,6 +104,9 @@
 
 	FORCEINLINE CSegmentCostCacheT() {}
 
+	/** flush (clear) the cache */
+	FORCEINLINE void Flush() {m_map.Clear(); m_heap.Clear();};
+
 	FORCEINLINE Tsegment& Get(Key& key, bool *found)
 	{
 		Tsegment* item = m_map.Find(key);
@@ -143,12 +146,11 @@
 	/// to access inherited path finder
 	FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
 
-	FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
-
 	FORCEINLINE static Cache& stGetGlobalCache()
 	{
 		static int last_rail_change_counter = 0;
 		static Date last_date = 0;
+		static Cache C;
 
 		// some statistics
 		if (last_date != _date) {
@@ -157,18 +159,12 @@
 			_total_pf_time_us = 0;
 		}
 
-		Cache*& pC = stGlobalCachePtr();
-
 		// delete the cache sometimes...
-		if (pC != NULL && last_rail_change_counter != Cache::s_rail_change_counter) {
+		if (last_rail_change_counter != Cache::s_rail_change_counter) {
 			last_rail_change_counter = Cache::s_rail_change_counter;
-			delete pC;
-			pC = NULL;
+			C.Flush();
 		}
-
-		if (pC == NULL)
-			pC = new Cache();
-		return *pC;
+		return C;
 	}
 
 public: