fix size of cache reporting

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/performance/trunk@38806 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2015-07-16 09:59:22 +00:00
parent 3c90224c87
commit 3859653420
2 changed files with 15 additions and 36 deletions

View file

@ -86,7 +86,12 @@
- (unsigned) currentObjects; - (unsigned) currentObjects;
/** /**
* Return the total size of the objects currently in the cache. * Return the total size of the objects currently in the cache.<br />
* NB. Object sizes are considered independently ... so where cached
* objects are containers with common content, the size of the cache
* may appear larger than is actually used.<br />
* Also, this figure does not consider memmory used by the cache itself
* or by the keys, only the memory used by the objects cached.
*/ */
- (NSUInteger) currentSize; - (NSUInteger) currentSize;

View file

@ -244,36 +244,7 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
- (NSUInteger) currentSize - (NSUInteger) currentSize
{ {
NSUInteger size = 0; return my->currentSize;
NSMapEnumerator e;
GSCacheItem *i;
id k;
[my->lock lock];
if (nil == my->exclude)
{
my->exclude = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
}
else
{
[my->exclude removeAllObjects];
}
e = NSEnumerateMapTable(my->contents);
while (NSNextMapEnumeratorPair(&e, (void**)&k, (void**)&i) != 0)
{
size += [i->object sizeInBytesExcluding: my->exclude];
}
NSEndMapTableEnumeration(&e);
if (my->maxSize > 0)
{
my->currentSize = size;
}
else
{
DESTROY(my->exclude);
}
[my->lock unlock];
return size;
} }
- (void) dealloc - (void) dealloc
@ -619,7 +590,7 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
id k; id k;
NSUInteger size = 0; NSUInteger size = 0;
if (my->exclude == nil) if (nil == my->exclude)
{ {
my->exclude my->exclude
= NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0); = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
@ -628,8 +599,8 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
{ {
if (i->size == 0) if (i->size == 0)
{ {
[my->exclude removeAllObjects];
i->size = [i->object sizeInBytesExcluding: my->exclude]; i->size = [i->object sizeInBytesExcluding: my->exclude];
[my->exclude removeAllObjects];
} }
if (i->size > max) if (i->size > max)
{ {
@ -703,13 +674,13 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
{ {
if (maxSize > 0) if (maxSize > 0)
{ {
if (my->exclude == nil) if (nil == my->exclude)
{ {
my->exclude my->exclude
= NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0); = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
} }
[my->exclude removeAllObjects];
addSize = [anObject sizeInBytesExcluding: my->exclude]; addSize = [anObject sizeInBytesExcluding: my->exclude];
[my->exclude removeAllObjects];
if (addSize > maxSize) if (addSize > maxSize)
{ {
addObjects = 0; // Object too big to cache. addObjects = 0; // Object too big to cache.
@ -807,8 +778,10 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
- (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude - (NSUInteger) sizeInBytesExcluding: (NSHashTable*)exclude
{ {
NSUInteger size = [super sizeInBytesExcluding: exclude]; NSUInteger size;
[my->lock lock];
size = [super sizeInBytesExcluding: exclude];
if (size > 0) if (size > 0)
{ {
size += sizeof(Item) size += sizeof(Item)
@ -817,6 +790,7 @@ static void removeItem(GSCacheItem *item, GSCacheItem **first)
+ [my->name sizeInBytesExcluding: exclude] + [my->name sizeInBytesExcluding: exclude]
+ [my->lock sizeInBytesExcluding: exclude]; + [my->lock sizeInBytesExcluding: exclude];
} }
[my->lock unlock];
return size; return size;
} }