mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-21 06:45:39 -06:00
Core: ValueCache
git-svn-id: svn://ultimatepp.org/upp/trunk@14573 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
0eaae761a1
commit
9a82b14059
6 changed files with 83 additions and 21 deletions
|
|
@ -366,6 +366,8 @@ class JsonIO;
|
|||
|
||||
#include "Huge.h"
|
||||
|
||||
#include "ValueCache.h"
|
||||
|
||||
#ifdef PLATFORM_WIN32
|
||||
NTL_MOVEABLE(POINT)
|
||||
NTL_MOVEABLE(SIZE)
|
||||
|
|
|
|||
|
|
@ -163,6 +163,8 @@ file
|
|||
Topic.cpp,
|
||||
CoWork.h,
|
||||
CoWork.cpp,
|
||||
ValueCache.h,
|
||||
ValueCache.cpp,
|
||||
Hash.h,
|
||||
MD5.cpp,
|
||||
SHA1.cpp,
|
||||
|
|
|
|||
|
|
@ -155,21 +155,6 @@ void GetSystemMemoryStatus(uint64& total, uint64& available)
|
|||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef PLATFORM_FREEBSD
|
||||
int64 page_size;
|
||||
struct vmtotal vmt;
|
||||
size_t vmt_size, uint_size;
|
||||
|
||||
vmt_size = sizeof(vmt);
|
||||
uint_size = sizeof(page_size);
|
||||
|
||||
if(sysctlbyname("vm.vmtotal", &vmt, &vmt_size, NULL, 0) >= 0 &&
|
||||
sysctlbyname("vm.stats.vm.v_page_size", &page_size, &uint_size, NULL, 0) >= 0) {
|
||||
available = vmt.t_free * page_size;
|
||||
total = vmt.t_avm * page_size;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef PLATFORM_MACOS
|
||||
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
|
||||
vm_statistics_data_t vmstat;
|
||||
|
|
@ -191,6 +176,21 @@ void GetSystemMemoryStatus(uint64& total, uint64& available)
|
|||
total = physical_memory;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef PLATFORM_FREEBSD
|
||||
int64 page_size;
|
||||
struct vmtotal vmt;
|
||||
size_t vmt_size, uint_size;
|
||||
|
||||
vmt_size = sizeof(vmt);
|
||||
uint_size = sizeof(page_size);
|
||||
|
||||
if(sysctlbyname("vm.vmtotal", &vmt, &vmt_size, NULL, 0) >= 0 &&
|
||||
sysctlbyname("vm.stats.vm.v_page_size", &page_size, &uint_size, NULL, 0) >= 0) {
|
||||
available = vmt.t_free * page_size;
|
||||
total = vmt.t_avm * page_size;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
total = 256*1024*1024;
|
||||
available = 16*1024*1024;
|
||||
|
|
|
|||
|
|
@ -349,10 +349,10 @@ private:
|
|||
|
||||
struct Key : Moveable<Key> {
|
||||
K key;
|
||||
const void *type;
|
||||
String type;
|
||||
|
||||
bool operator==(const Key& b) const { return key == b.key && type == b.type; }
|
||||
hash_t GetHashValue() const { return CombineHash(key, (uintptr_t)type); }
|
||||
hash_t GetHashValue() const { return CombineHash(key, type); }
|
||||
};
|
||||
|
||||
Index<Key> key;
|
||||
|
|
@ -366,6 +366,7 @@ private:
|
|||
int newsize;
|
||||
bool flag;
|
||||
|
||||
const int InternalSize = 3 * (sizeof(Item) + sizeof(Key) + 24) / 2;
|
||||
|
||||
void Unlink(int i);
|
||||
void LinkHead(int i);
|
||||
|
|
@ -464,7 +465,7 @@ void LRUCache<T, K>::AdjustSize(P getsize)
|
|||
if(!key.IsUnlinked(i)) {
|
||||
int sz = getsize(*data[i].data);
|
||||
if(sz >= 0)
|
||||
data[i].size = sz;
|
||||
data[i].size = sz + InternalSize;
|
||||
size += data[i].size;
|
||||
count++;
|
||||
}
|
||||
|
|
@ -540,12 +541,12 @@ T& LRUCache<T, K>::Get(const Maker& m)
|
|||
{
|
||||
Key k;
|
||||
k.key = m.Key();
|
||||
k.type = &typeid(m);
|
||||
k.type = typeid(m).name();
|
||||
int q = key.Find(k);
|
||||
if(q < 0) {
|
||||
q = key.Put(k);
|
||||
Item& t = data.At(q);
|
||||
t.size = m.Make(t.data.Create());
|
||||
t.size = m.Make(t.data.Create()) + InternalSize;
|
||||
size += t.size;
|
||||
newsize += t.size;
|
||||
t.flag = flag;
|
||||
|
|
|
|||
37
uppsrc/Core/ValueCache.cpp
Normal file
37
uppsrc/Core/ValueCache.cpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#include "Core.h"
|
||||
|
||||
namespace Upp {
|
||||
|
||||
bool sFinished;
|
||||
|
||||
struct ValueMakeCacheClass : LRUCache<Value> {
|
||||
~ValueMakeCacheClass() { sFinished = true; }
|
||||
};
|
||||
|
||||
LRUCache<Value>& TheValueCache()
|
||||
{
|
||||
static ValueMakeCacheClass m;
|
||||
return m;
|
||||
}
|
||||
|
||||
int sMaxSize = 1000000;
|
||||
|
||||
void ShrinkValueCache()
|
||||
{
|
||||
TheValueCache().Shrink(sMaxSize, 2000);
|
||||
}
|
||||
|
||||
void ShrinkValueCache(int maxsize)
|
||||
{
|
||||
sMaxSize = maxsize;
|
||||
ShrinkValueCache();
|
||||
}
|
||||
|
||||
Value MakeValue(ValueMaker& m)
|
||||
{
|
||||
Value v = TheValueCache().Get(m);
|
||||
ShrinkValueCache();
|
||||
return v;
|
||||
}
|
||||
|
||||
};
|
||||
20
uppsrc/Core/ValueCache.h
Normal file
20
uppsrc/Core/ValueCache.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
typedef LRUCache<Value>::Maker ValueMaker;
|
||||
|
||||
LRUCache<Value>& TheValueCache();
|
||||
|
||||
Value MakeValue(ValueMaker& m);
|
||||
|
||||
void ShrinkValueCache();
|
||||
void ShrinkValueCache(int maxsize);
|
||||
|
||||
template <class P>
|
||||
int ValueCacheRemove(P what)
|
||||
{
|
||||
return TheValueCache().Remove(what);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
int ValueCacheRemoveOne(P what)
|
||||
{
|
||||
return TheValueCache().Remove(what);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue