MemoryBlock.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Memory block helper used with ZLib
  9. #ifndef CRYINCLUDE_EDITOR_UTIL_MEMORYBLOCK_H
  10. #define CRYINCLUDE_EDITOR_UTIL_MEMORYBLOCK_H
  11. #pragma once
  12. #include "RefCountBase.h"
  13. #include "Include/EditorCoreAPI.h"
  14. struct IEditor;
  15. class CArchive;
  16. class EDITOR_CORE_API CMemoryBlock
  17. : public CRefCountBase
  18. {
  19. public:
  20. CMemoryBlock();
  21. CMemoryBlock(const CMemoryBlock& mem);
  22. ~CMemoryBlock();
  23. CMemoryBlock& operator=(const CMemoryBlock& mem);
  24. //! Allocate or reallocate memory for this block.
  25. //! @param size Amount of memory in bytes to allocate.
  26. //! @return true if the allocation succeeded.
  27. bool Allocate(int size, int uncompressedSize = 0);
  28. //! Frees memory allocated in this block (if owned).
  29. //! Just clears internal references (if unowned).
  30. void Free();
  31. //! Attach memory buffer to this block.
  32. //! Ownership is not transferred; this buffer will not be deleted by CMemoryBlock
  33. void Attach(void* buffer, int size, int uncompressedSize = 0);
  34. //! Detach memory buffer that was previously attached.
  35. //! Note: Implemented as Free()
  36. void Detach();
  37. //! Returns amount of allocated memory in this block.
  38. int GetSize() const { return m_size; }
  39. //! Returns amount of allocated memory in this block.
  40. int GetUncompressedSize() const { return m_uncompressedSize; }
  41. void* GetBuffer() const { return m_buffer; };
  42. //! Copy memory range to memory block.
  43. void Copy(void* src, int size);
  44. //! Compress this memory block to specified memory block.
  45. //! @param toBlock target memory block where compressed result will be stored.
  46. void Compress(CMemoryBlock& toBlock) const;
  47. //! Uncompress this memory block to specified memory block.
  48. //! @param toBlock target memory block where compressed result will be stored.
  49. void Uncompress(CMemoryBlock& toBlock) const;
  50. //! Serialize memory block to archive.
  51. void Serialize(CArchive& ar);
  52. //! Is MemoryBlock is empty.
  53. bool IsEmpty() const { return m_buffer == 0; }
  54. private:
  55. void* m_buffer;
  56. int m_size;
  57. //! If not 0, memory block is compressed.
  58. int m_uncompressedSize;
  59. //! True if memory block owns its memory.
  60. bool m_owns;
  61. };
  62. #endif // CRYINCLUDE_EDITOR_UTIL_MEMORYBLOCK_H