Browse Source

fix wraparound 32-bit cache size; robustly handle negative cache size

David Rose 19 years ago
parent
commit
51911fd6eb
2 changed files with 13 additions and 5 deletions
  1. 6 3
      panda/src/putil/bamCache.cxx
  2. 7 2
      panda/src/putil/bamCacheIndex.cxx

+ 6 - 3
panda/src/putil/bamCache.cxx

@@ -585,10 +585,13 @@ check_cache_size() {
     return;
   }
 
-  if (_index->_cache_size > _max_kbytes * 1024) {
-    while (_index->_cache_size > _max_kbytes * 1024) {
+  if (_index->_cache_size / 1024 > _max_kbytes) {
+    while (_index->_cache_size / 1024 > _max_kbytes) {
       PT(BamCacheRecord) record = _index->evict_old_file();
-      nassertv(record != (BamCacheRecord *)NULL);
+      if (record == NULL) {
+        // Never mind; the cache is empty.
+        break;
+      }
       Filename cache_pathname(_root, record->get_cache_filename());
       cache_pathname.unlink();
     }

+ 7 - 2
panda/src/putil/bamCacheIndex.cxx

@@ -118,13 +118,18 @@ release_records() {
 ////////////////////////////////////////////////////////////////////
 //     Function: BamCacheIndex::evict_old_file
 //       Access: Private
-//  Description: Evicts an old file from the cache.  Records the record.
+//  Description: Evicts an old file from the cache.  Records the
+//               record.  Returns NULL if the cache is empty.
 ////////////////////////////////////////////////////////////////////
 PT(BamCacheRecord) BamCacheIndex::
 evict_old_file() {
+  if (_next == this) {
+    // Nothing in the cache.
+    return NULL;
+  }
+
   // The first record in the linked list is the least-recently-used
   // one.
-  nassertr(_next != this, NULL);
   PT(BamCacheRecord) record = (BamCacheRecord *)_next;
   bool removed = remove_record(record->get_source_pathname());
   nassertr(removed, NULL);