From 75f99b0abbb981d822ea33c020b06396ea3656be Mon Sep 17 00:00:00 2001 From: KUDr Date: Sat, 18 Nov 2006 19:20:47 +0000 Subject: (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) --- yapf/array.hpp | 2 ++ yapf/fixedsizearray.hpp | 13 +++++++++++-- yapf/hashtable.hpp | 6 ++++++ yapf/yapf_costcache.hpp | 18 +++++++----------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/yapf/array.hpp b/yapf/array.hpp index d6fe2a75f..e8eff1c8c 100644 --- a/yapf/array.hpp +++ b/yapf/array.hpp @@ -24,6 +24,8 @@ public: /** implicit constructor */ FORCEINLINE CArrayT() { } + /** Clear (destroy) all items */ + FORCEINLINE void Clear() {m_a.Clear();} /** Return actual number of items */ FORCEINLINE int Size() const { diff --git a/yapf/fixedsizearray.hpp b/yapf/fixedsizearray.hpp index 5d6c3b313..48b177f3c 100644 --- a/yapf/fixedsizearray.hpp +++ b/yapf/fixedsizearray.hpp @@ -51,12 +51,21 @@ struct CFixedSizeArrayT { // 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: diff --git a/yapf/hashtable.hpp b/yapf/hashtable.hpp index 71c4eac74..c6b52e50a 100644 --- a/yapf/hashtable.hpp +++ b/yapf/hashtable.hpp @@ -12,6 +12,9 @@ struct CHashTableSlotT 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 @@ public: /** 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 { diff --git a/yapf/yapf_costcache.hpp b/yapf/yapf_costcache.hpp index 8e99219cc..4d1f5ea0f 100644 --- a/yapf/yapf_costcache.hpp +++ b/yapf/yapf_costcache.hpp @@ -104,6 +104,9 @@ struct CSegmentCostCacheT 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 @@ protected: /// to access inherited path finder FORCEINLINE Tpf& Yapf() {return *static_cast(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 @@ protected: _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: -- cgit v1.2.3-70-g09d2