2
0

BufferCache.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. internal static object[] Cache = new object[MaxBuffers];
  23. private static int _freeBuffers;
  24. public static byte[] GetBuffer()
  25. {
  26. lock (Cache)
  27. {
  28. byte[] buf;
  29. if (_freeBuffers > 0)
  30. {
  31. for (int i = 0; i < MaxBuffers; i++)
  32. {
  33. if (Cache[i] != null)
  34. {
  35. buf = (byte[])Cache[i];
  36. Cache[i] = null;
  37. _freeBuffers--;
  38. return buf;
  39. }
  40. }
  41. }
  42. buf = new byte[SmbComTransaction.TransactionBufSize];
  43. return buf;
  44. }
  45. }
  46. internal static void GetBuffers(SmbComTransaction req, SmbComTransactionResponse rsp)
  47. {
  48. lock (Cache)
  49. {
  50. req.TxnBuf = GetBuffer();
  51. rsp.TxnBuf = GetBuffer();
  52. }
  53. }
  54. public static void ReleaseBuffer(byte[] buf)
  55. {
  56. lock (Cache)
  57. {
  58. if (_freeBuffers < MaxBuffers)
  59. {
  60. for (int i = 0; i < MaxBuffers; i++)
  61. {
  62. if (Cache[i] == null)
  63. {
  64. Cache[i] = buf;
  65. _freeBuffers++;
  66. return;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }