mirror of https://github.com/OpenTTD/OpenTTD
(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)
parent
3f64e50fc9
commit
9b81f084af
|
@ -24,6 +24,8 @@ public:
|
||||||
|
|
||||||
/** implicit constructor */
|
/** implicit constructor */
|
||||||
FORCEINLINE CArrayT() { }
|
FORCEINLINE CArrayT() { }
|
||||||
|
/** Clear (destroy) all items */
|
||||||
|
FORCEINLINE void Clear() {m_a.Clear();}
|
||||||
/** Return actual number of items */
|
/** Return actual number of items */
|
||||||
FORCEINLINE int Size() const
|
FORCEINLINE int Size() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -51,12 +51,21 @@ struct CFixedSizeArrayT {
|
||||||
// release one reference to the shared block
|
// release one reference to the shared block
|
||||||
if ((--RefCnt()) > 0) return; // and return if there is still some owner
|
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
|
// walk through all allocated items backward and destroy them
|
||||||
for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) {
|
for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) {
|
||||||
pItem->~Titem_();
|
pItem->~Titem_();
|
||||||
}
|
}
|
||||||
free(((int8*)m_items) - ThdrSize);
|
// number of items become zero
|
||||||
m_items = NULL;
|
SizeRef() = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -12,6 +12,9 @@ struct CHashTableSlotT
|
||||||
|
|
||||||
CHashTableSlotT() : m_pFirst(NULL) {}
|
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 */
|
/** 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
|
FORCEINLINE const Titem_* Find(const Key& key) const
|
||||||
{
|
{
|
||||||
|
@ -162,6 +165,9 @@ public:
|
||||||
/** item count */
|
/** item count */
|
||||||
FORCEINLINE int Count() const {return m_num_items;}
|
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 item search */
|
||||||
const Titem_* Find(const Tkey& key) const
|
const Titem_* Find(const Tkey& key) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -104,6 +104,9 @@ struct CSegmentCostCacheT
|
||||||
|
|
||||||
FORCEINLINE CSegmentCostCacheT() {}
|
FORCEINLINE CSegmentCostCacheT() {}
|
||||||
|
|
||||||
|
/** flush (clear) the cache */
|
||||||
|
FORCEINLINE void Flush() {m_map.Clear(); m_heap.Clear();};
|
||||||
|
|
||||||
FORCEINLINE Tsegment& Get(Key& key, bool *found)
|
FORCEINLINE Tsegment& Get(Key& key, bool *found)
|
||||||
{
|
{
|
||||||
Tsegment* item = m_map.Find(key);
|
Tsegment* item = m_map.Find(key);
|
||||||
|
@ -143,12 +146,11 @@ protected:
|
||||||
/// to access inherited path finder
|
/// to access inherited path finder
|
||||||
FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
|
FORCEINLINE Tpf& Yapf() {return *static_cast<Tpf*>(this);}
|
||||||
|
|
||||||
FORCEINLINE static Cache*& stGlobalCachePtr() {static Cache* pC = NULL; return pC;}
|
|
||||||
|
|
||||||
FORCEINLINE static Cache& stGetGlobalCache()
|
FORCEINLINE static Cache& stGetGlobalCache()
|
||||||
{
|
{
|
||||||
static int last_rail_change_counter = 0;
|
static int last_rail_change_counter = 0;
|
||||||
static Date last_date = 0;
|
static Date last_date = 0;
|
||||||
|
static Cache C;
|
||||||
|
|
||||||
// some statistics
|
// some statistics
|
||||||
if (last_date != _date) {
|
if (last_date != _date) {
|
||||||
|
@ -157,18 +159,12 @@ protected:
|
||||||
_total_pf_time_us = 0;
|
_total_pf_time_us = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cache*& pC = stGlobalCachePtr();
|
|
||||||
|
|
||||||
// delete the cache sometimes...
|
// 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;
|
last_rail_change_counter = Cache::s_rail_change_counter;
|
||||||
delete pC;
|
C.Flush();
|
||||||
pC = NULL;
|
|
||||||
}
|
}
|
||||||
|
return C;
|
||||||
if (pC == NULL)
|
|
||||||
pC = new Cache();
|
|
||||||
return *pC;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
Loading…
Reference in New Issue