Oolite
Loading...
Searching...
No Matches
OOCache.m File Reference
import "OOCache.h"
import "OOStringParsing.h"
Include dependency graph for OOCache.m:

Go to the source code of this file.

Classes

protocol  <OOCacheComparable>
struct  OOCacheImpl
struct  OOCacheNode
category  OOCache(Private)

Macros

#define OOCACHE_PERFORM_INTEGRITY_CHECKS   0
#define CHECK_INTEGRITY(context)

Typedefs

typedef struct OOCacheImpl OOCacheImpl
typedef struct OOCacheNode OOCacheNode

Enumerations

enum  { kCountUnknown = -1U }

Functions

static OOCacheImpl * CacheAllocate (void)
static void CacheFree (OOCacheImpl *cache)
static BOOL CacheInsert (OOCacheImpl *cache, id key, id value)
static BOOL CacheRemove (OOCacheImpl *cache, id key)
static BOOL CacheRemoveOldest (OOCacheImpl *cache, NSString *logKey)
static id CacheRetrieve (OOCacheImpl *cache, id key)
static unsigned CacheGetCount (OOCacheImpl *cache)
static NSArray * CacheArrayOfContentsByAge (OOCacheImpl *cache)
static NSArray * CacheArrayOfNodesByAge (OOCacheImpl *cache)
static NSString * CacheGetName (OOCacheImpl *cache)
static void CacheSetName (OOCacheImpl *cache, NSString *name)
static OOCacheNode * CacheNodeAllocate (id< OOCacheComparable > key, id value)
static void CacheNodeFree (OOCacheImpl *cache, OOCacheNode *node)
static id CacheNodeGetValue (OOCacheNode *node)
static void CacheNodeSetValue (OOCacheNode *node, id value)
static OOCacheNode * TreeSplay (OOCacheNode **root, id< OOCacheComparable > key)
static OOCacheNode * TreeInsert (OOCacheImpl *cache, id< OOCacheComparable > key, id value)
static void AgeListMakeYoungest (OOCacheImpl *cache, OOCacheNode *node)
static void AgeListRemove (OOCacheImpl *cache, OOCacheNode *node)

Variables

static NSString *const kSerializedEntryKeyKey = @"@"key"
static NSString *const kSerializedEntryKeyValue = @"@"value"

Macro Definition Documentation

◆ CHECK_INTEGRITY

#define CHECK_INTEGRITY ( context)
Value:
do { \
} while (0)

Definition at line 172 of file OOCache.m.

172#define CHECK_INTEGRITY(context) \
173 do { \
174 } while (0)

Referenced by OOCache::dealloc, OOCache::objectForKey:, OOCache::removeObjectForKey:, and OOCache::setObject:forKey:.

◆ OOCACHE_PERFORM_INTEGRITY_CHECKS

#define OOCACHE_PERFORM_INTEGRITY_CHECKS   0

Definition at line 113 of file OOCache.m.

Typedef Documentation

◆ OOCacheImpl

typedef struct OOCacheImpl OOCacheImpl

Definition at line 122 of file OOCache.m.

◆ OOCacheNode

typedef struct OOCacheNode OOCacheNode

Definition at line 123 of file OOCache.m.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kCountUnknown 

Definition at line 148 of file OOCache.m.

148{ kCountUnknown = -1U };
@ kCountUnknown
Definition OOCache.m:148

Function Documentation

◆ AgeListMakeYoungest()

void AgeListMakeYoungest ( OOCacheImpl * cache,
OOCacheNode * node )
static

Definition at line 835 of file OOCache.m.

836{
837 if (cache == NULL || node == NULL)
838 return;
839
840 AgeListRemove(cache, node);
841 node->older = cache->youngest;
842 if (NULL != cache->youngest)
843 cache->youngest->younger = node;
844 cache->youngest = node;
845 if (cache->oldest == NULL)
846 cache->oldest = node;
847}
static void AgeListRemove(OOCacheImpl *cache, OOCacheNode *node)
Definition OOCache.m:850
OOCacheNode * youngest
Definition OOCache.m:130
OOCacheNode * oldest
Definition OOCache.m:130
OOCacheNode * older
Definition OOCache.m:145
OOCacheNode * younger
Definition OOCache.m:145

References AgeListRemove(), OOCacheNode::older, OOCacheImpl::oldest, OOCacheNode::younger, and OOCacheImpl::youngest.

Referenced by CacheInsert(), and CacheRetrieve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AgeListRemove()

void AgeListRemove ( OOCacheImpl * cache,
OOCacheNode * node )
static

Definition at line 850 of file OOCache.m.

851{
852 OOCacheNode* younger = NULL;
853 OOCacheNode* older = NULL;
854
855 if (node == NULL)
856 return;
857
858 younger = node->younger;
859 older = node->older;
860
861 if (cache->youngest == node)
862 cache->youngest = older;
863 if (cache->oldest == node)
864 cache->oldest = younger;
865
866 node->younger = NULL;
867 node->older = NULL;
868
869 if (younger != NULL)
870 younger->older = older;
871 if (older != NULL)
872 older->younger = younger;
873}

References OOCacheNode::older, OOCacheImpl::oldest, OOCacheNode::younger, and OOCacheImpl::youngest.

Referenced by AgeListMakeYoungest(), CacheNodeFree(), and CacheRemove().

Here is the caller graph for this function:

◆ CacheAllocate()

OOCacheImpl * CacheAllocate ( void )
static

Definition at line 414 of file OOCache.m.

415{
416 return calloc(sizeof(OOCacheImpl), 1);
417}

Referenced by OOCache::initWithPList:.

Here is the caller graph for this function:

◆ CacheArrayOfContentsByAge()

NSArray * CacheArrayOfContentsByAge ( OOCacheImpl * cache)
static

Definition at line 497 of file OOCache.m.

498{
499 OOCacheNode* node = NULL;
500 NSMutableArray* result = nil;
501
502 if (cache == NULL || cache->count == 0)
503 return nil;
504
505 result = [NSMutableArray arrayWithCapacity:cache->count];
506
507 for (node = cache->youngest; node != NULL; node = node->older) {
508 [result addObject:node->value];
509 }
510 return result;
511}
return nil
unsigned count
Definition OOCache.m:132

References OOCacheImpl::count, nil, OOCacheNode::older, and OOCacheImpl::youngest.

Referenced by OOCache::objectsByAge.

Here is the caller graph for this function:

◆ CacheArrayOfNodesByAge()

NSArray * CacheArrayOfNodesByAge ( OOCacheImpl * cache)
static

Definition at line 513 of file OOCache.m.

514{
515 OOCacheNode* node = NULL;
516 NSMutableArray* result = nil;
517
518 if (cache == NULL || cache->count == 0)
519 return nil;
520
521 result = [NSMutableArray arrayWithCapacity:cache->count];
522
523 for (node = cache->oldest; node != NULL; node = node->younger) {
524 [result addObject:[NSDictionary dictionaryWithObjectsAndKeys:node->key, kSerializedEntryKeyKey, node->value, kSerializedEntryKeyValue, nil]];
525 }
526 return result;
527}
static NSString *const kSerializedEntryKeyKey
Definition OOCache.m:150
static NSString *const kSerializedEntryKeyValue
Definition OOCache.m:151
id< OOCacheComparable > key
Definition OOCache.m:138

References OOCacheImpl::count, nil, OOCacheImpl::oldest, and OOCacheNode::younger.

Referenced by OOCache::pListRepresentation.

Here is the caller graph for this function:

◆ CacheFree()

void CacheFree ( OOCacheImpl * cache)
static

Definition at line 419 of file OOCache.m.

420{
421 if (cache == NULL)
422 return;
423
424 CacheNodeFree(cache, cache->root);
425 [cache->name autorelease];
426 free(cache);
427}
static void CacheNodeFree(OOCacheImpl *cache, OOCacheNode *node)
Definition OOCache.m:586
OOCacheNode * root
Definition OOCache.m:127
NSString * name
Definition OOCache.m:133

References CacheNodeFree(), and OOCacheImpl::root.

Referenced by OOCache::dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheGetCount()

unsigned CacheGetCount ( OOCacheImpl * cache)
static

Definition at line 540 of file OOCache.m.

541{
542 return cache->count;
543}

References OOCacheImpl::count.

Referenced by OOCache::prune.

Here is the caller graph for this function:

◆ CacheGetName()

NSString * CacheGetName ( OOCacheImpl * cache)
static

Definition at line 529 of file OOCache.m.

530{
531 return cache->name;
532}

References OOCacheImpl::name.

Referenced by OOCache::name, and OOCache::prune.

Here is the caller graph for this function:

◆ CacheInsert()

BOOL CacheInsert ( OOCacheImpl * cache,
id key,
id value )
static

Definition at line 429 of file OOCache.m.

430{
431 OOCacheNode* node = NULL;
432
433 if (cache == NULL || key == nil || value == nil)
434 return NO;
435
436 node = TreeInsert(cache, key, value);
437 if (node != NULL) {
438 AgeListMakeYoungest(cache, node);
439 return YES;
440 } else
441 return NO;
442}
static void AgeListMakeYoungest(OOCacheImpl *cache, OOCacheNode *node)
Definition OOCache.m:835
static OOCacheNode * TreeInsert(OOCacheImpl *cache, id< OOCacheComparable > key, id value)
Definition OOCache.m:721

References AgeListMakeYoungest(), nil, and TreeInsert().

Referenced by OOCache::setObject:forKey:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheNodeAllocate()

OOCacheNode * CacheNodeAllocate ( id< OOCacheComparable > key,
id value )
static

Definition at line 569 of file OOCache.m.

570{
571 OOCacheNode* result = NULL;
572
573 if (key == nil || value == nil)
574 return NULL;
575
576 result = calloc(sizeof *result, 1);
577 if (result != NULL) {
578 result->key = [key copy];
579 result->value = [value retain];
580 }
581
582 return result;
583}

References OOCacheNode::key, nil, and OOCacheNode::value.

Referenced by TreeInsert().

Here is the caller graph for this function:

◆ CacheNodeFree()

void CacheNodeFree ( OOCacheImpl * cache,
OOCacheNode * node )
static

Definition at line 586 of file OOCache.m.

587{
588 id key, value;
589
590 if (node == NULL)
591 return;
592
593 AgeListRemove(cache, node);
594
595 key = node->key;
596 node->key = nil;
597 [key release];
598
599 value = node->value;
600 node->value = nil;
601 [value release];
602
603 CacheNodeFree(cache, node->leftChild);
604 CacheNodeFree(cache, node->rightChild);
605
606 free(node);
607}
OOCacheNode * rightChild
Definition OOCache.m:142
OOCacheNode * leftChild
Definition OOCache.m:142

References AgeListRemove(), CacheNodeFree(), OOCacheNode::key, OOCacheNode::leftChild, nil, OOCacheNode::rightChild, and OOCacheNode::value.

Referenced by CacheFree(), CacheNodeFree(), CacheRemove(), and TreeInsert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheNodeGetValue()

id CacheNodeGetValue ( OOCacheNode * node)
static

Definition at line 610 of file OOCache.m.

611{
612 if (node == NULL)
613 return nil;
614
615 return node->value;
616}

References nil, and OOCacheNode::value.

Referenced by CacheRetrieve().

Here is the caller graph for this function:

◆ CacheNodeSetValue()

void CacheNodeSetValue ( OOCacheNode * node,
id value )
static

Definition at line 619 of file OOCache.m.

620{
621 if (node == NULL)
622 return;
623
624 id tmp = node->value;
625 node->value = [value retain];
626 [tmp release];
627}

References OOCacheNode::value.

Referenced by TreeInsert().

Here is the caller graph for this function:

◆ CacheRemove()

BOOL CacheRemove ( OOCacheImpl * cache,
id key )
static

Definition at line 444 of file OOCache.m.

445{
446 OOCacheNode *node = NULL, *newRoot = NULL;
447
448 node = TreeSplay(&cache->root, key);
449 if (node != NULL) {
450 if (node->leftChild == NULL)
451 newRoot = node->rightChild;
452 else {
453 newRoot = node->leftChild;
454 TreeSplay(&newRoot, key);
455 newRoot->rightChild = node->rightChild;
456 }
457 node->leftChild = NULL;
458 node->rightChild = NULL;
459
460 cache->root = newRoot;
461 --cache->count;
462
463 AgeListRemove(cache, node);
464 CacheNodeFree(cache, node);
465
466 return YES;
467 } else
468 return NO;
469}
static OOCacheNode * TreeSplay(OOCacheNode **root, id< OOCacheComparable > key)
Definition OOCache.m:649

References AgeListRemove(), CacheNodeFree(), OOCacheImpl::count, OOCacheNode::leftChild, OOCacheNode::rightChild, OOCacheImpl::root, and TreeSplay().

Referenced by CacheRemoveOldest(), and OOCache::removeObjectForKey:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheRemoveOldest()

BOOL CacheRemoveOldest ( OOCacheImpl * cache,
NSString * logKey )
static

Definition at line 471 of file OOCache.m.

472{
473 // This could be more efficient, but does it need to be?
474 if (cache == NULL || cache->oldest == NULL)
475 return NO;
476
477 OOLog(logKey, @"Pruning cache \"%@\": removing %@", cache->name, cache->oldest->key);
478 return CacheRemove(cache, cache->oldest->key);
479}
static BOOL CacheRemove(OOCacheImpl *cache, id key)
Definition OOCache.m:444
#define OOLog(class, format,...)
Definition OOLogging.h:84

References CacheRemove(), OOCacheNode::key, OOCacheImpl::name, OOCacheImpl::oldest, and OOLog.

Referenced by OOCache::prune.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheRetrieve()

id CacheRetrieve ( OOCacheImpl * cache,
id key )
static

Definition at line 481 of file OOCache.m.

482{
483 OOCacheNode* node = NULL;
484 id result = nil;
485
486 if (cache == NULL || key == NULL)
487 return nil;
488
489 node = TreeSplay(&cache->root, key);
490 if (node != NULL) {
491 result = CacheNodeGetValue(node);
492 AgeListMakeYoungest(cache, node);
493 }
494 return result;
495}
static id CacheNodeGetValue(OOCacheNode *node)
Definition OOCache.m:610

References AgeListMakeYoungest(), CacheNodeGetValue(), nil, OOCacheImpl::root, and TreeSplay().

Referenced by OOCache::objectForKey:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CacheSetName()

void CacheSetName ( OOCacheImpl * cache,
NSString * name )
static

Definition at line 534 of file OOCache.m.

535{
536 [cache->name autorelease];
537 cache->name = [name copy];
538}

References OOCacheImpl::name.

Referenced by OOCache::setName:.

Here is the caller graph for this function:

◆ TreeInsert()

OOCacheNode * TreeInsert ( OOCacheImpl * cache,
id< OOCacheComparable > key,
id value )
static

Definition at line 721 of file OOCache.m.

722{
723 OOCacheNode *closest = NULL,
724 *node = NULL;
725 NSComparisonResult order;
726
727 if (cache == NULL || key == nil || value == nil)
728 return NULL;
729
730 if (cache->root == NULL) {
731 node = CacheNodeAllocate(key, value);
732 cache->root = node;
733 cache->count = 1;
734 } else {
735 node = TreeSplay(&cache->root, key);
736 if (node != NULL) {
737 // Exact match: key already exists, reuse its node
738 CacheNodeSetValue(node, value);
739 } else {
740 closest = cache->root;
741 node = CacheNodeAllocate(key, value);
742 if (EXPECT_NOT(node == NULL))
743 return NULL;
744
745 order = [key compare:closest->key];
746
747 if (order == NSOrderedAscending) {
748 // Insert to left
749 node->leftChild = closest->leftChild;
750 node->rightChild = closest;
751 closest->leftChild = NULL;
752 cache->root = node;
753 ++cache->count;
754 } else if (order == NSOrderedDescending) {
755 // Insert to right
756 node->rightChild = closest->rightChild;
757 node->leftChild = closest;
758 closest->rightChild = NULL;
759 cache->root = node;
760 ++cache->count;
761 } else {
762 // Key already exists, which we should have caught above
763 OOLog(@"dataCache.inconsistency", @"%s() internal inconsistency for cache \"%@\", insertion failed.", __PRETTY_FUNCTION__, cache->name);
764 CacheNodeFree(cache, node);
765 return NULL;
766 }
767 }
768 }
769
770 return node;
771}
static void CacheNodeSetValue(OOCacheNode *node, id value)
Definition OOCache.m:619
static OOCacheNode * CacheNodeAllocate(id< OOCacheComparable > key, id value)
Definition OOCache.m:569
#define EXPECT_NOT(x)

References CacheNodeAllocate(), CacheNodeFree(), CacheNodeSetValue(), OOCacheImpl::count, EXPECT_NOT, OOCacheNode::leftChild, OOCacheImpl::name, nil, OOLog, OOCacheNode::rightChild, OOCacheImpl::root, and TreeSplay().

Referenced by CacheInsert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ TreeSplay()

OOCacheNode * TreeSplay ( OOCacheNode ** root,
id< OOCacheComparable > key )
static

Definition at line 649 of file OOCache.m.

650{
651 NSComparisonResult order;
652 OOCacheNode N = { .leftChild = NULL, .rightChild = NULL };
653 OOCacheNode *node = NULL, *temp = NULL, *l = &N, *r = &N;
654 BOOL exact = NO;
655
656 if (root == NULL || *root == NULL || key == nil)
657 return NULL;
658
659 node = *root;
660
661 for (;;) {
662#ifndef NDEBUG
663 if (node == NULL) {
664 OOLog(@"node.error", @"%@", @"node is NULL");
665 } else if (node->key == NULL) {
666 OOLog(@"node.error", @"%@", @"node->key is NULL");
667 }
668#endif
669 order = [key compare:node->key];
670 if (order == NSOrderedAscending) {
671 // Closest match is in left subtree
672 if (node->leftChild == NULL)
673 break;
674 if ([key compare:node->leftChild->key] == NSOrderedAscending) {
675 // Rotate right
676 temp = node->leftChild;
677 node->leftChild = temp->rightChild;
678 temp->rightChild = node;
679 node = temp;
680 if (node->leftChild == NULL)
681 break;
682 }
683 // Link right
684 r->leftChild = node;
685 r = node;
686 node = node->leftChild;
687 } else if (order == NSOrderedDescending) {
688 // Closest match is in right subtree
689 if (node->rightChild == NULL)
690 break;
691 if ([key compare:node->rightChild->key] == NSOrderedDescending) {
692 // Rotate left
693 temp = node->rightChild;
694 node->rightChild = temp->leftChild;
695 temp->leftChild = node;
696 node = temp;
697 if (node->rightChild == NULL)
698 break;
699 }
700 // Link left
701 l->rightChild = node;
702 l = node;
703 node = node->rightChild;
704 } else {
705 // Found exact match
706 exact = YES;
707 break;
708 }
709 }
710
711 // Assemble
712 l->rightChild = node->leftChild;
713 r->leftChild = node->rightChild;
714 node->leftChild = N.rightChild;
715 node->rightChild = N.leftChild;
716
717 *root = node;
718 return exact ? node : NULL;
719}

References OOCacheNode::key, OOCacheNode::leftChild, nil, OOLog, and OOCacheNode::rightChild.

Referenced by CacheRemove(), CacheRetrieve(), and TreeInsert().

Here is the caller graph for this function:

Variable Documentation

◆ kSerializedEntryKeyKey

NSString* const kSerializedEntryKeyKey = @"@"key"
static

Definition at line 150 of file OOCache.m.

◆ kSerializedEntryKeyValue

NSString* const kSerializedEntryKeyValue = @"@"value"
static

Definition at line 151 of file OOCache.m.