AkBytesMem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. // AkBytesMem.h
  21. /// \file
  22. /// IReadBytes / IWriteBytes implementation on a growing memory buffer. This
  23. /// version uses the AK::MemoryMgr allocator.
  24. #pragma once
  25. #include <AK/IBytes.h>
  26. #include <AK/SoundEngine/Common/AkSoundEngineExport.h>
  27. #include <AK/Tools/Common/AkBankReadHelpers.h>
  28. namespace AK
  29. {
  30. class ReadBytesMem
  31. : public AK::IReadBytes
  32. {
  33. public:
  34. AKSOUNDENGINE_API ReadBytesMem();
  35. AKSOUNDENGINE_API ReadBytesMem(
  36. const void * in_pBytes,
  37. AkInt32 in_cBytes
  38. );
  39. AKSOUNDENGINE_API virtual ~ReadBytesMem();
  40. // IReadBytes implementation
  41. AKSOUNDENGINE_API virtual bool ReadBytes(
  42. void * in_pData,
  43. AkInt32 in_cBytes,
  44. AkInt32 & out_cRead
  45. );
  46. // Public methods
  47. AKSOUNDENGINE_API void Attach(
  48. const void * in_pBytes,
  49. AkInt32 in_cBytes
  50. );
  51. private:
  52. AkInt32 m_cBytes;
  53. const AkUInt8 * m_pBytes;
  54. AkInt32 m_cPos;
  55. };
  56. class WriteBytesMem
  57. : public AK::IWriteBuffer
  58. {
  59. public:
  60. AKSOUNDENGINE_API WriteBytesMem();
  61. AKSOUNDENGINE_API virtual ~WriteBytesMem();
  62. // IWriteBytes implementation
  63. AKSOUNDENGINE_API virtual bool WriteBytes(
  64. const void * in_pData,
  65. AkInt32 in_cBytes,
  66. AkInt32& out_cWritten);
  67. // IWriteBuffer implementation
  68. AKSOUNDENGINE_API virtual bool Reserve(
  69. AkInt32 in_cBytes
  70. );
  71. AKSOUNDENGINE_API virtual AkInt32 Count() const;
  72. AKSOUNDENGINE_API virtual void SetCount(
  73. AkInt32 in_cBytes
  74. );
  75. AKSOUNDENGINE_API AkInt32 Size() const;
  76. AKSOUNDENGINE_API virtual AkUInt8 * Bytes() const;
  77. AKSOUNDENGINE_API virtual AkUInt8 * Detach();
  78. AKSOUNDENGINE_API virtual void Clear();
  79. // Public methods
  80. AKSOUNDENGINE_API void SetMemPool( AkMemPoolId in_pool );
  81. AKSOUNDENGINE_API bool HasValidMemPool();
  82. // Reserves space for writing in_cBytes and returns the location, advancing the write pointer.
  83. AkUInt8* GetWritePtr(AkInt32 in_cBytes)
  84. {
  85. AkInt32 cPos = m_cPos;
  86. AkInt32 cNewPos = cPos + in_cBytes;
  87. if ((m_cBytes >= cNewPos) || Grow(cNewPos))
  88. {
  89. m_cPos = cNewPos;
  90. return m_pBytes + cPos;
  91. }
  92. else
  93. return nullptr;
  94. }
  95. template<class T>
  96. T* GetWritePtr()
  97. {
  98. static_assert(alignof(T) == 1, "T must have an alignment of 1 to avoid crashes due to unaligned writes on 32-bit ARM targets. Use Write<T> instead.");
  99. return reinterpret_cast<T*>(GetWritePtr(sizeof(T)));
  100. }
  101. template<class T>
  102. bool Write(const T & in_data)
  103. {
  104. if (AkUInt8* pWrite = GetWritePtr(sizeof(T)))
  105. {
  106. AK::WriteUnaligned<T>(pWrite, in_data);
  107. return true;
  108. }
  109. else
  110. return false;
  111. }
  112. private:
  113. bool Grow(
  114. AkInt32 in_cBytes
  115. );
  116. AkInt32 m_cBytes;
  117. AkUInt8 * m_pBytes;
  118. AkInt32 m_cPos;
  119. AkMemPoolId m_pool;
  120. };
  121. class WriteBytesBuffer
  122. : public AK::IWriteBytes
  123. {
  124. public:
  125. AKSOUNDENGINE_API WriteBytesBuffer();
  126. AKSOUNDENGINE_API virtual ~WriteBytesBuffer();
  127. AKSOUNDENGINE_API void SetBuffer(AkUInt8* in_pBytes, AkInt32 in_cSize);
  128. // IWriteBytes implementation
  129. AKSOUNDENGINE_API virtual bool WriteBytes(
  130. const void* in_pData,
  131. AkInt32 in_cBytes,
  132. AkInt32& out_cWritten);
  133. AKSOUNDENGINE_API AkInt32 GetPos() const;
  134. AKSOUNDENGINE_API void Clear();
  135. private:
  136. AkUInt8* m_pBytes;
  137. AkInt32 m_cSize;
  138. AkInt32 m_cPos;
  139. };
  140. }