BufferQueue.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/BufferQueue.h"
  24. #include "../DebugNew.h"
  25. // This BufferQueue implementation is based on the BufferedSoundStream.
  26. //
  27. // It is a Deserializer and a Serializer. However, it should only be written
  28. // to in large chunks because some writes cause allocation. Reads should
  29. // perform well reguardless of the read chunk sizes.
  30. namespace Atomic
  31. {
  32. BufferQueue::BufferQueue(Context* context) :
  33. Object(context),
  34. position_(0)
  35. {
  36. }
  37. BufferQueue::~BufferQueue()
  38. {
  39. }
  40. unsigned BufferQueue::Read(void* dest_, unsigned numBytes)
  41. {
  42. char* dest((char*)dest_);
  43. MutexLock lock(bufferMutex_);
  44. unsigned outBytes = 0;
  45. while (numBytes && buffers_.Size())
  46. {
  47. // Copy as much from the front buffer as possible, then discard it and move to the next
  48. List<Pair<SharedArrayPtr<signed char>, unsigned> >::Iterator front = buffers_.Begin();
  49. unsigned copySize = front->second_ - position_;
  50. if (copySize > numBytes)
  51. copySize = numBytes;
  52. memcpy(dest, front->first_.Get() + position_, copySize);
  53. position_ += copySize;
  54. if (position_ >= front->second_)
  55. {
  56. buffers_.PopFront();
  57. position_ = 0;
  58. }
  59. dest += copySize;
  60. outBytes += copySize;
  61. numBytes -= copySize;
  62. }
  63. size_ -= outBytes;
  64. return outBytes;
  65. }
  66. unsigned BufferQueue::Write(const void* data, unsigned numBytes)
  67. {
  68. if (data && numBytes)
  69. {
  70. MutexLock lock(bufferMutex_);
  71. SharedArrayPtr<signed char> newBuffer(new signed char[numBytes]);
  72. memcpy(newBuffer.Get(), data, numBytes);
  73. buffers_.Push(MakePair(newBuffer, numBytes));
  74. size_ += numBytes;
  75. return numBytes;
  76. }
  77. return 0;
  78. }
  79. void BufferQueue::Write(SharedArrayPtr<signed char> data, unsigned numBytes)
  80. {
  81. if (data && numBytes)
  82. {
  83. MutexLock lock(bufferMutex_);
  84. buffers_.Push(MakePair(data, numBytes));
  85. size_ += numBytes;
  86. }
  87. }
  88. void BufferQueue::Write(SharedArrayPtr<unsigned char> data, unsigned numBytes)
  89. {
  90. if (data && numBytes)
  91. {
  92. MutexLock lock(bufferMutex_);
  93. buffers_.Push(MakePair(ReinterpretCast<signed char>(data), numBytes));
  94. size_ += numBytes;
  95. }
  96. }
  97. void BufferQueue::Clear()
  98. {
  99. MutexLock lock(bufferMutex_);
  100. buffers_.Clear();
  101. position_ = 0;
  102. size_ = 0;
  103. }
  104. }