ByteBlock.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class ByteBlock
  9. {
  10. DECLARE_OBJECT(ByteBlock)
  11. private:
  12. Field(uint) m_contentSize = 0; // Length of block, in bytes
  13. __declspec(align(4)) // Align the buffer to sizeof(uint32) to improve GetHashCode() perf.
  14. Field(byte*) m_content = nullptr; // The block's content
  15. static ByteBlock* New(Recycler* alloc, const byte * initialContent, int initialContentSize, ScriptContext * requestContext);
  16. public:
  17. ByteBlock(uint size, byte * content)
  18. : m_contentSize(size), m_content(content)
  19. { }
  20. ByteBlock(uint size, Recycler *alloc) : m_contentSize(size), m_content(nullptr)
  21. {
  22. // The New function below will copy over a buffer into this so
  23. // we don't need to zero it out
  24. m_content = RecyclerNewArrayLeaf(alloc, byte, size);
  25. }
  26. ByteBlock(uint size, ArenaAllocator* alloc) : m_contentSize(size), m_content(nullptr)
  27. {
  28. m_content = AnewArray(alloc, byte, size);
  29. }
  30. static DWORD GetBufferOffset() { return offsetof(ByteBlock, m_content); }
  31. static ByteBlock* New(Recycler* alloc, const byte * initialContent, int initialContentSize);
  32. // This is needed just for the debugger since it allocates
  33. // the byte block on a separate thread, which the recycler doesn't like.
  34. // To remove when the recycler supports multi-threaded allocation.
  35. static ByteBlock* NewFromArena(ArenaAllocator* alloc, const byte * initialContent, int initialContentSize);
  36. uint GetLength() const;
  37. byte* GetBuffer();
  38. const byte* GetBuffer() const;
  39. const byte operator[](uint itemIndex) const;
  40. byte& operator[] (uint itemIndex);
  41. };
  42. } // namespace Js