| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace System.Net.Security |
| | | 7 | | { |
| | | 8 | | // |
| | | 9 | | // Implements delayed SSPI handle release: |
| | | 10 | | // finalizable objects though the handles are kept alive until being pushed out by the newly incoming ones. |
| | | 11 | | // |
| | | 12 | | internal static class SSPIHandleCache |
| | | 13 | | { |
| | | 14 | | private const int MaxCacheSize = 0x1F; // must a (power of 2) - 1 |
| | 0 | 15 | | private static readonly SafeCredentialReference?[] s_cacheSlots = new SafeCredentialReference[MaxCacheSize + 1]; |
| | 0 | 16 | | private static int s_current = -1; |
| | | 17 | | |
| | | 18 | | internal static void CacheCredential(SafeFreeCredentials newHandle) |
| | 0 | 19 | | { |
| | | 20 | | try |
| | 0 | 21 | | { |
| | 0 | 22 | | SafeCredentialReference? newRef = SafeCredentialReference.CreateReference(newHandle); |
| | 0 | 23 | | if (newRef == null) |
| | 0 | 24 | | { |
| | 0 | 25 | | return; |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | int index = Interlocked.Increment(ref s_current) & MaxCacheSize; |
| | | 29 | | |
| | 0 | 30 | | Interlocked.Exchange(ref s_cacheSlots[index], newRef)?.Dispose(); |
| | 0 | 31 | | } |
| | 0 | 32 | | catch (Exception e) |
| | 0 | 33 | | { |
| | 0 | 34 | | if (NetEventSource.Log.IsEnabled() && !ExceptionCheck.IsFatal(e)) |
| | 0 | 35 | | { |
| | 0 | 36 | | NetEventSource.Error(null, $"Attempted to throw: {e}"); |
| | 0 | 37 | | } |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |