BufferCache.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. namespace SharpCifs.Smb
  18. {
  19. public class BufferCache
  20. {
  21. private static readonly int MaxBuffers = Config.GetInt("jcifs.smb.maxBuffers", 16
  22. );
  23. internal static object[] Cache = new object[MaxBuffers];
  24. private static int _freeBuffers;
  25. public static byte[] GetBuffer()
  26. {
  27. lock (Cache)
  28. {
  29. byte[] buf;
  30. if (_freeBuffers > 0)
  31. {
  32. for (int i = 0; i < MaxBuffers; i++)
  33. {
  34. if (Cache[i] != null)
  35. {
  36. buf = (byte[])Cache[i];
  37. Cache[i] = null;
  38. _freeBuffers--;
  39. return buf;
  40. }
  41. }
  42. }
  43. buf = new byte[SmbComTransaction.TransactionBufSize];
  44. return buf;
  45. }
  46. }
  47. internal static void GetBuffers(SmbComTransaction req, SmbComTransactionResponse
  48. rsp)
  49. {
  50. lock (Cache)
  51. {
  52. req.TxnBuf = GetBuffer();
  53. rsp.TxnBuf = GetBuffer();
  54. }
  55. }
  56. public static void ReleaseBuffer(byte[] buf)
  57. {
  58. lock (Cache)
  59. {
  60. if (_freeBuffers < MaxBuffers)
  61. {
  62. for (int i = 0; i < MaxBuffers; i++)
  63. {
  64. if (Cache[i] == null)
  65. {
  66. Cache[i] = buf;
  67. _freeBuffers++;
  68. return;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }