memStream.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/stream/memStream.h"
  24. MemStream::MemStream( U32 growSize,
  25. bool allowRead,
  26. bool allowWrite )
  27. : mGrowSize( growSize ),
  28. mBufferSize( 0 ),
  29. mStreamSize( 0 ),
  30. mBufferBase( NULL ),
  31. mInstCaps( 0 ),
  32. mCurrentPosition( 0 )
  33. {
  34. AssertFatal( allowRead || allowWrite, "Either write or read must be allowed" );
  35. if ( allowRead )
  36. mInstCaps |= Stream::StreamRead;
  37. if ( allowWrite )
  38. mInstCaps |= Stream::StreamWrite;
  39. mOwnsMemory = true;
  40. setStatus( mGrowSize > 0 ? Ok : EOS );
  41. }
  42. MemStream::MemStream( U32 bufferSize,
  43. void *buffer,
  44. bool allowRead,
  45. bool allowWrite)
  46. : mGrowSize( 0 ),
  47. mBufferSize( bufferSize ),
  48. mStreamSize( bufferSize ),
  49. mBufferBase( buffer ),
  50. mInstCaps( 0 ),
  51. mOwnsMemory( false ),
  52. mCurrentPosition( 0 )
  53. {
  54. AssertFatal( bufferSize > 0, "Invalid buffer size");
  55. AssertFatal( allowRead || allowWrite, "Either write or read must be allowed");
  56. if ( allowRead )
  57. mInstCaps |= Stream::StreamRead;
  58. if ( allowWrite )
  59. mInstCaps |= Stream::StreamWrite;
  60. if ( !buffer )
  61. {
  62. mOwnsMemory = true;
  63. mBufferBase = dMalloc( mBufferSize );
  64. }
  65. setStatus( Ok );
  66. }
  67. MemStream::~MemStream()
  68. {
  69. if ( mOwnsMemory )
  70. dFree( mBufferBase );
  71. mBufferBase = NULL;
  72. mCurrentPosition = 0;
  73. setStatus( Closed );
  74. }
  75. U32 MemStream::getStreamSize()
  76. {
  77. AssertFatal( getStatus() != Closed, "Stream not open, size undefined" );
  78. return mStreamSize;
  79. }
  80. bool MemStream::hasCapability(const Capability in_cap) const
  81. {
  82. // Closed streams can't do anything
  83. //
  84. if (getStatus() == Closed)
  85. return false;
  86. U32 totalCaps = U32(Stream::StreamPosition) | mInstCaps;
  87. return (U32(in_cap) & totalCaps) != 0;
  88. }
  89. U32 MemStream::getPosition() const
  90. {
  91. AssertFatal(getStatus() != Closed, "Position of a closed stream is undefined");
  92. return mCurrentPosition;
  93. }
  94. bool MemStream::setPosition(const U32 in_newPosition)
  95. {
  96. AssertFatal(getStatus() != Closed, "SetPosition of a closed stream is not allowed");
  97. AssertFatal(in_newPosition <= mStreamSize, "Invalid position");
  98. mCurrentPosition = in_newPosition;
  99. if (mCurrentPosition > mStreamSize) {
  100. // Never gets here in debug version, this is for the release builds...
  101. //
  102. setStatus(UnknownError);
  103. return false;
  104. } else if (mCurrentPosition == mStreamSize) {
  105. setStatus(EOS);
  106. } else {
  107. setStatus(Ok);
  108. }
  109. return true;
  110. }
  111. bool MemStream::_read(const U32 in_numBytes, void *out_pBuffer)
  112. {
  113. AssertFatal(getStatus() != Closed, "Attempted read from a closed stream");
  114. if (in_numBytes == 0)
  115. return true;
  116. AssertFatal(out_pBuffer != NULL, "Invalid output buffer");
  117. if (hasCapability(StreamRead) == false) {
  118. AssertWarn(false, "Reading is disallowed on this stream");
  119. setStatus(IllegalCall);
  120. return false;
  121. }
  122. bool success = true;
  123. U32 actualBytes = in_numBytes;
  124. if ((mCurrentPosition + in_numBytes) > mStreamSize) {
  125. success = false;
  126. actualBytes = mStreamSize - mCurrentPosition;
  127. }
  128. // Obtain a current pointer, and do the copy
  129. const void* pCurrent = (const void*)((const U8*)mBufferBase + mCurrentPosition);
  130. dMemcpy(out_pBuffer, pCurrent, actualBytes);
  131. // Advance the stream position
  132. mCurrentPosition += actualBytes;
  133. if (!success)
  134. setStatus(EOS);
  135. else
  136. setStatus(Ok);
  137. return success;
  138. }
  139. bool MemStream::_write(const U32 in_numBytes, const void *in_pBuffer)
  140. {
  141. AssertFatal(getStatus() != Closed, "Attempted write to a closed stream");
  142. if (in_numBytes == 0)
  143. return true;
  144. if (hasCapability(StreamWrite) == false)
  145. {
  146. AssertWarn(0, "Writing is disallowed on this stream");
  147. setStatus(IllegalCall);
  148. return false;
  149. }
  150. bool success = true;
  151. U32 actualBytes = in_numBytes;
  152. if ((mCurrentPosition + in_numBytes) > mBufferSize)
  153. {
  154. if ( mGrowSize > 0 )
  155. {
  156. U32 growSize = (mCurrentPosition + in_numBytes) - mBufferSize;
  157. mBufferSize += growSize + ( mGrowSize - ( growSize % mGrowSize ) );
  158. mBufferBase = dRealloc( mBufferBase, mBufferSize );
  159. }
  160. else
  161. {
  162. success = false;
  163. actualBytes = mBufferSize - mCurrentPosition;
  164. }
  165. }
  166. AssertFatal(in_pBuffer != NULL, "Invalid input buffer");
  167. // Obtain a current pointer, and do the copy
  168. void* pCurrent = (void*)((U8*)mBufferBase + mCurrentPosition);
  169. dMemcpy(pCurrent, in_pBuffer, actualBytes);
  170. // Advance the stream position
  171. mCurrentPosition += actualBytes;
  172. if (mCurrentPosition > mStreamSize)
  173. mStreamSize = mCurrentPosition;
  174. if (mCurrentPosition == mStreamSize)
  175. setStatus(EOS);
  176. else
  177. setStatus(Ok);
  178. return success;
  179. }
  180. void *MemStream::takeBuffer()
  181. {
  182. void *buffer = mBufferBase;
  183. mBufferBase = NULL;
  184. mBufferSize = 0;
  185. mStreamSize = 0;
  186. mCurrentPosition = 0;
  187. setStatus(EOS);
  188. return buffer;
  189. }