//Debugging method to log whatever action was taken in the cache. This will //include lookups, adding after cache misses, and times when we clear the //cache. [Conditional("_LOGGING")] private void LogAction(CacheAction action, CacheObjType cacheType) { #if _LOGGING BCLDebug.Trace("CACHE", "Took action ", action, " in cache named ", m_cacheName, " on object ", cacheType, " on thread ", Thread.CurrentThread.GetHashCode()); #endif }
private int FindObjectPosition(InternalCacheItem[] cache, int itemCount, CacheObjType cacheType, bool findEmpty) { if (cache == null) { return(-1); } //This helps us in the case where we grabbed the cache and then //somebody added an item, forced a reallocation, and hence made //itemCount greater than the length of cache. if (itemCount > cache.Length) { itemCount = cache.Length; } for (int i = 0; i < itemCount; i++) { if (cacheType == cache[i].Key) { return(i); } } if (findEmpty) { if (itemCount < (cache.Length - 1)) { return(itemCount + 1); } } return(-1); }
internal object this[CacheObjType cacheType] { [SecurityCritical] get { InternalCacheItem[] cache = this.m_cache; int numItems = this.m_numItems; int index = this.FindObjectPosition(cache, numItems, cacheType, false); if (index >= 0) { bool flag1 = BCLDebug.m_loggingNotEnabled; return(cache[index].Value); } bool loggingNotEnabled = BCLDebug.m_loggingNotEnabled; return(null); } [SecurityCritical] set { bool loggingNotEnabled = BCLDebug.m_loggingNotEnabled; lock (this) { int index = this.FindObjectPosition(this.m_cache, this.m_numItems, cacheType, true); if (index > 0) { this.m_cache[index].Value = value; this.m_cache[index].Key = cacheType; if (index == this.m_numItems) { this.m_numItems++; } } else if (this.m_cache == null) { bool flag2 = BCLDebug.m_loggingNotEnabled; this.m_cache = new InternalCacheItem[2]; this.m_cache[0].Value = value; this.m_cache[0].Key = cacheType; this.m_numItems = 1; } else { bool flag3 = BCLDebug.m_loggingNotEnabled; InternalCacheItem[] itemArray = new InternalCacheItem[this.m_numItems * 2]; for (int i = 0; i < this.m_numItems; i++) { itemArray[i] = this.m_cache[i]; } itemArray[this.m_numItems].Value = value; itemArray[this.m_numItems].Key = cacheType; this.m_cache = itemArray; this.m_numItems++; } } } }
internal object this[CacheObjType cacheType] { [SecurityCritical] get { InternalCacheItem[] cache = this.m_cache; int numItems = this.m_numItems; int index = this.FindObjectPosition(cache, numItems, cacheType, false); if (index >= 0) { bool flag1 = BCLDebug.m_loggingNotEnabled; return cache[index].Value; } bool loggingNotEnabled = BCLDebug.m_loggingNotEnabled; return null; } [SecurityCritical] set { bool loggingNotEnabled = BCLDebug.m_loggingNotEnabled; lock (this) { int index = this.FindObjectPosition(this.m_cache, this.m_numItems, cacheType, true); if (index > 0) { this.m_cache[index].Value = value; this.m_cache[index].Key = cacheType; if (index == this.m_numItems) { this.m_numItems++; } } else if (this.m_cache == null) { bool flag2 = BCLDebug.m_loggingNotEnabled; this.m_cache = new InternalCacheItem[2]; this.m_cache[0].Value = value; this.m_cache[0].Key = cacheType; this.m_numItems = 1; } else { bool flag3 = BCLDebug.m_loggingNotEnabled; InternalCacheItem[] itemArray = new InternalCacheItem[this.m_numItems * 2]; for (int i = 0; i < this.m_numItems; i++) { itemArray[i] = this.m_cache[i]; } itemArray[this.m_numItems].Value = value; itemArray[this.m_numItems].Key = cacheType; this.m_cache = itemArray; this.m_numItems++; } } } }
//Debugging method to log whatever action was taken in the cache. This will //include lookups, adding after cache misses, and times when we clear the //cache. [Conditional("_LOGGING")] private void LogAction(CacheAction action, CacheObjType cacheType, Object obj) { #if _LOGGING BCLDebug.Trace("CACHE", "Took action ", action, " in cache named ", m_cacheName, " on object ", cacheType, " on thread ", Thread.CurrentThread.GetHashCode(), " Object was ", obj); if (action == CacheAction.AddItem) { BCLDebug.DumpStack("CACHE"); } #endif }
private int FindObjectPosition(InternalCacheItem[] cache, int itemCount, CacheObjType cacheType, bool findEmpty) { if (cache != null) { if (itemCount > cache.Length) { itemCount = cache.Length; } for (int i = 0; i < itemCount; i++) { if (cacheType == cache[i].Key) { return(i); } } if (findEmpty && (itemCount < (cache.Length - 1))) { return(itemCount + 1); } } return(-1); }
private int FindObjectPosition(InternalCacheItem[] cache, int itemCount, CacheObjType cacheType, bool findEmpty) { if (cache != null) { if (itemCount > cache.Length) { itemCount = cache.Length; } for (int i = 0; i < itemCount; i++) { if (cacheType == cache[i].Key) { return i; } } if (findEmpty && (itemCount < (cache.Length - 1))) { return (itemCount + 1); } } return -1; }
private void LogAction(CacheAction action, CacheObjType cacheType, object obj) { }
private void LogAction(CacheAction action, CacheObjType cacheType) { }
internal Object this[CacheObjType cacheType] { [System.Security.SecurityCritical] // auto-generated get { //Let's snapshot a reference to the array up front so that //we don't have to worry about any writers. It's important //to grab the cache first and then numItems. In the event that //the cache gets cleared, m_numItems will be set to 0 before //we actually release the cache. Getting it second will cause //us to walk only 0 elements, but not to fault. InternalCacheItem[] cache = m_cache; int numItems = m_numItems; int position = FindObjectPosition(cache, numItems, cacheType, false); if (position>=0) { if (!BCLDebug.m_loggingNotEnabled) LogAction(CacheAction.LookupItemHit, cacheType, cache[position].Value); return cache[position].Value; } //Couldn't find it -- oh, well. if (!BCLDebug.m_loggingNotEnabled) LogAction(CacheAction.LookupItemMiss, cacheType); return null; } [System.Security.SecurityCritical] // auto-generated set { int position; if (!BCLDebug.m_loggingNotEnabled) LogAction(CacheAction.AddItem, cacheType, value); lock(this) { position = FindObjectPosition(m_cache, m_numItems, cacheType, true); if (position>0) { m_cache[position].Value = value; m_cache[position].Key = cacheType; if (position==m_numItems) { m_numItems++; } return; } if (m_cache==null) { if (!BCLDebug.m_loggingNotEnabled) LogAction(CacheAction.AllocateCache, cacheType); // m_copying = true; m_cache = new InternalCacheItem[MinCacheSize]; m_cache[0].Value = value; m_cache[0].Key = cacheType; m_numItems = 1; // m_copying = false; } else { if (!BCLDebug.m_loggingNotEnabled) LogAction(CacheAction.GrowCache, cacheType); // m_copying = true; InternalCacheItem[] tempCache = new InternalCacheItem[m_numItems * 2]; for (int i=0; i<m_numItems; i++) { tempCache[i] = m_cache[i]; } tempCache[m_numItems].Value = value; tempCache[m_numItems].Key = cacheType; m_cache = tempCache; m_numItems++; // m_copying = false; } } } }
//This is a debugging-only function which verifies that the object is of the //the correct type and follows some arbitrary set of constraints. Please //add any validation code which you require to the switch statement below. // //Note to Testing: These are not localized strings because this error checking //occurs only in the debug build and I don't want to push extra resources //into the build which are not going to be used for customers. /* [Conditional("_DEBUG")] private void OnValidate(InternalCacheItem item) { switch (item.Key) { case CacheObjType.ParameterInfo: if (!(item.Value is System.Reflection.ParameterInfo)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a ParameterInfo"); } break; case CacheObjType.TypeName: if (!(item.Value is String)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a non-null String"); } break; case CacheObjType.AssemblyName: if (!(item.Value is String)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a non-null String"); } break; case CacheObjType.RemotingData: if (!(item.Value is RemotingCachedData)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a RemotingCacheData"); } break; case CacheObjType.SerializableAttribute: if (!(item.Value is SerializableAttribute)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a SerializableAttribute"); } break; case CacheObjType.FieldName: if (!(item.Value is String)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a non-null String"); } break; case CacheObjType.FieldType: if (!(item.Value is Type)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a non-null Type"); } break; case CacheObjType.DefaultMember: if (!(item.Value is String)) { throw new ArgumentException("Invalid type for the internal cache. " + item.Value + " requires a non-null String"); } break; default: throw new ArgumentException("Invalid caching type. Please add " + item.Value + " to the validated types in InternalCache."); } } */ //Debugging method to log whatever action was taken in the cache. This will //include lookups, adding after cache misses, and times when we clear the //cache. [Conditional("_LOGGING")] private void LogAction(CacheAction action, CacheObjType cacheType) { #if _LOGGING BCLDebug.Trace("CACHE", "Took action ", action, " in cache named ", m_cacheName, " on object ", cacheType, " on thread ", Thread.CurrentThread.GetHashCode()); #endif }
private int FindObjectPosition(InternalCacheItem[] cache, int itemCount, CacheObjType cacheType, bool findEmpty) { if (cache==null) { return -1; } //This helps us in the case where we grabbed the cache and then //somebody added an item, forced a reallocation, and hence made //itemCount greater than the length of cache. if (itemCount > cache.Length) { itemCount = cache.Length; } for (int i=0; i<itemCount; i++) { if (cacheType==cache[i].Key) { return i; } } if (findEmpty) { if (itemCount<(cache.Length-1)) { return itemCount + 1; } } return -1; }
internal Object this[CacheObjType cacheType] { get { //Let's snapshot a reference to the array up front so that //we don't have to worry about any writers. It's important //to grab the cache first and then numItems. In the event that //the cache gets cleared, m_numItems will be set to 0 before //we actually release the cache. Getting it second will cause //us to walk only 0 elements, but not to fault. InternalCacheItem[] cache = m_cache; int numItems = m_numItems; int position = FindObjectPosition(cache, numItems, cacheType, false); if (position >= 0) { LogAction(CacheAction.LookupItemHit, cacheType, cache[position].Value); return(cache[position].Value); } //Couldn't find it -- oh, well. LogAction(CacheAction.LookupItemMiss, cacheType); return(null); } set { int position; LogAction(CacheAction.AddItem, cacheType, value); lock (this) { position = FindObjectPosition(m_cache, m_numItems, cacheType, true); if (position > 0) { m_cache[position].Value = value; m_cache[position].Key = cacheType; if (position == m_numItems) { m_numItems++; } return; } if (m_cache == null) { LogAction(CacheAction.AllocateCache, cacheType); // m_copying = true; m_cache = new InternalCacheItem[MinCacheSize]; m_cache[0].Value = value; m_cache[0].Key = cacheType; m_numItems = 1; // m_copying = false; } else { LogAction(CacheAction.GrowCache, cacheType); // m_copying = true; InternalCacheItem[] tempCache = new InternalCacheItem[m_numItems * 2]; for (int i = 0; i < m_numItems; i++) { tempCache[i] = m_cache[i]; } tempCache[m_numItems].Value = value; tempCache[m_numItems].Key = cacheType; m_cache = tempCache; m_numItems++; // m_copying = false; } } } }