Exemple #1
0
        /// <summary>
        /// Specialized Add for PFN cache
        /// </summary>
        /// <param name="Key">Must be pre-shifted, upper 16 bits are used for ref counting</param>
        /// <param name="Value">YOU MAY NOT ENTER FREEKING NULL PAGES INTO THE CACHE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!</param>
        /// <returns></returns>
        public static new bool TryAdd(long Key, long[] Value)
        {
            if (Value == null)
            {
                return(false);
            }

            // leave some wiggle room for other threads so maybe we don't block too much
            if ((Global.Count + RemovalRateBar) >= Capacity)
            {
                var next_remove = (from entry in Global.Keys
                                   where (((entry >> 48) & 0xffff) <= 2)                                                           // instead of 2 I should really keep an average ref count
                                   select entry).Take(RemovalRate).AsParallel().All(taken => Global.TryRemove(taken, out OutVar)); // 1% by default

                // might be a good idea to reduce the current count of all entries by 1/2 to ensure liveness
                // but this whole thing should be relatively short lived I don't foresee a huge population of max-aged items
            }

            if (UnsafeHelp.IsZero(Value))
            {
                return(false);
            }

            return(Global.TryAdd(Key, Value));
        }