diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-08 20:13:44 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-08 20:13:44 +0700 |
commit | c9444751604775d555ce8d200c2b5c083c3ab5cc (patch) | |
tree | b6215a862676e3a48f533859e0912514ab36927e | |
parent | New Spans methods (diff) | |
download | BouncyCastle.NET-ed25519-c9444751604775d555ce8d200c2b5c083c3ab5cc.tar.xz |
Tweak allocation strategy
-rw-r--r-- | crypto/src/tls/ByteQueue.cs | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs index 74a7104ba..e06ad6346 100644 --- a/crypto/src/tls/ByteQueue.cs +++ b/crypto/src/tls/ByteQueue.cs @@ -9,18 +9,9 @@ namespace Org.BouncyCastle.Tls public sealed class ByteQueue { /// <returns>The smallest number which can be written as 2^x which is bigger than i.</returns> - public static int NextTwoPow(int i) + private static int GetAllocationSize(int i) { - /* - * This code is based of a lot of code I found on the Internet which mostly - * referenced a book called "Hacking delight". - */ - i |= i >> 1; - i |= i >> 2; - i |= i >> 4; - i |= i >> 8; - i |= i >> 16; - return i + 1; + return Integers.HighestOneBit((256 | i) << 1); } /// <summary>The buffer where we store our data.</summary> @@ -68,14 +59,14 @@ namespace Org.BouncyCastle.Tls { if (len > m_databuf.Length) { - int desiredSize = NextTwoPow(len | 256); + int desiredSize = GetAllocationSize(len); m_databuf = new byte[desiredSize]; } m_skipped = 0; } else if ((m_skipped + m_available + len) > m_databuf.Length) { - int desiredSize = NextTwoPow(m_available + len); + int desiredSize = GetAllocationSize(m_available + len); if (desiredSize > m_databuf.Length) { byte[] tmp = new byte[desiredSize]; @@ -105,14 +96,14 @@ namespace Org.BouncyCastle.Tls { if (len > m_databuf.Length) { - int desiredSize = NextTwoPow(len | 256); + int desiredSize = GetAllocationSize(len); m_databuf = new byte[desiredSize]; } m_skipped = 0; } else if ((m_skipped + m_available + len) > m_databuf.Length) { - int desiredSize = NextTwoPow(m_available + len); + int desiredSize = GetAllocationSize(m_available + len); if (desiredSize > m_databuf.Length) { byte[] tmp = new byte[desiredSize]; @@ -259,7 +250,7 @@ namespace Org.BouncyCastle.Tls } else { - int desiredSize = NextTwoPow(m_available); + int desiredSize = GetAllocationSize(m_available); if (desiredSize < m_databuf.Length) { byte[] tmp = new byte[desiredSize]; |