GrowingArray.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Contains a class which will provide a uint32 array which can grow dynamically
  6. // It behaves almost same as regex::List<> except it has less members, is customized for being used in SmallSpanSequence of FunctionBody
  7. #pragma once
  8. #ifdef DIAG_MEM
  9. extern int listFreeAmount;
  10. #endif
  11. namespace JsUtil
  12. {
  13. template <class ValueType, class TAllocator>
  14. class GrowingArray
  15. {
  16. public:
  17. typedef Field(ValueType, TAllocator) TValue;
  18. typedef typename AllocatorInfo<TAllocator, TValue>::AllocatorType AllocatorType;
  19. static GrowingArray* Create(uint32 _length);
  20. GrowingArray(AllocatorType* allocator, uint32 _length)
  21. : buffer(nullptr),
  22. alloc(allocator),
  23. count(0),
  24. length(_length)
  25. {
  26. EnsureArray();
  27. }
  28. ~GrowingArray()
  29. {
  30. if (buffer != nullptr)
  31. {
  32. AllocatorFree(alloc, (TypeAllocatorFunc<AllocatorType, int>::GetFreeFunc()), buffer, UInt32Math::Mul(length, sizeof(TValue)));
  33. }
  34. }
  35. TValue ItemInBuffer(uint32 index) const
  36. {
  37. if (index >= count)
  38. {
  39. return (TValue)0;
  40. }
  41. return buffer[index];
  42. }
  43. void ItemInBuffer(uint32 index, TValue item)
  44. {
  45. EnsureArray();
  46. Assert(index < count);
  47. buffer[index] = item;
  48. }
  49. void Add(TValue item)
  50. {
  51. EnsureArray();
  52. buffer[count] = item;
  53. count++;
  54. }
  55. uint32 Count() const { return count; }
  56. void SetCount(uint32 _count) { count = _count; }
  57. uint32 GetLength() const { return length; }
  58. TValue* GetBuffer() const { return buffer; }
  59. GrowingArray * Clone()
  60. {
  61. GrowingArray * pNewArray = AllocatorNew(AllocatorType, alloc, GrowingArray, alloc, length);
  62. pNewArray->count = count;
  63. if (buffer)
  64. {
  65. pNewArray->buffer = AllocateArray<AllocatorType, TValue, false>(
  66. TRACK_ALLOC_INFO(alloc, TValue, AllocatorType, 0, length),
  67. TypeAllocatorFunc<AllocatorType, TValue>::GetAllocFunc(),
  68. length);
  69. CopyArray<TValue, TValue, TAllocator>(pNewArray->buffer, length, buffer, length);
  70. }
  71. return pNewArray;
  72. }
  73. private:
  74. Field(TValue*, TAllocator) buffer;
  75. Field(uint32) count;
  76. Field(uint32) length;
  77. FieldNoBarrier(AllocatorType*) alloc;
  78. void EnsureArray()
  79. {
  80. if (buffer == nullptr)
  81. {
  82. buffer = AllocateArray<AllocatorType, TValue, false>(
  83. TRACK_ALLOC_INFO(alloc, TValue, AllocatorType, 0, length),
  84. TypeAllocatorFunc<AllocatorType, TValue>::GetAllocFunc(),
  85. length);
  86. count = 0;
  87. }
  88. else if (count == length)
  89. {
  90. uint32 newLength = UInt32Math::AddMul<1, 2>(length);
  91. TValue * newbuffer = AllocateArray<AllocatorType, TValue, false>(
  92. TRACK_ALLOC_INFO(alloc, TValue, AllocatorType, 0, newLength),
  93. TypeAllocatorFunc<AllocatorType, TValue>::GetAllocFunc(),
  94. newLength);
  95. CopyArray<TValue, TValue, TAllocator>(newbuffer, newLength, buffer, length);
  96. #ifdef DIAG_MEM
  97. listFreeAmount += length;
  98. #endif
  99. if (length != 0)
  100. {
  101. const size_t lengthByteSize = UInt32Math::Mul(length, sizeof(TValue));
  102. AllocatorFree(alloc, (TypeAllocatorFunc<AllocatorType, int>::GetFreeFunc()), buffer, lengthByteSize);
  103. }
  104. length = newLength;
  105. buffer = newbuffer;
  106. }
  107. }
  108. };
  109. typedef GrowingArray<uint32, HeapAllocator> GrowingUint32HeapArray;
  110. }