The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Implements some basic array storage allocation functions.
  22. This class isn't really for public use - it used to be part of the
  23. container classes but has since been superseded by ArrayBase. Eventually
  24. it will be removed from the API.
  25. @tags{Core}
  26. */
  27. template <class ElementType, class TypeOfCriticalSectionToUse>
  28. class ArrayAllocationBase : public TypeOfCriticalSectionToUse
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty array. */
  33. ArrayAllocationBase() = default;
  34. /** Destructor. */
  35. ~ArrayAllocationBase() = default;
  36. ArrayAllocationBase (ArrayAllocationBase&& other) noexcept
  37. : elements (std::move (other.elements)),
  38. numAllocated (other.numAllocated)
  39. {
  40. }
  41. ArrayAllocationBase& operator= (ArrayAllocationBase&& other) noexcept
  42. {
  43. elements = std::move (other.elements);
  44. numAllocated = other.numAllocated;
  45. return *this;
  46. }
  47. //==============================================================================
  48. /** Changes the amount of storage allocated.
  49. This will retain any data currently held in the array, and either add or
  50. remove extra space at the end.
  51. @param numElements the number of elements that are needed
  52. */
  53. void setAllocatedSize (int numElements)
  54. {
  55. if (numAllocated != numElements)
  56. {
  57. if (numElements > 0)
  58. elements.realloc ((size_t) numElements);
  59. else
  60. elements.free();
  61. numAllocated = numElements;
  62. }
  63. }
  64. /** Increases the amount of storage allocated if it is less than a given amount.
  65. This will retain any data currently held in the array, but will add
  66. extra space at the end to make sure there it's at least as big as the size
  67. passed in. If it's already bigger, no action is taken.
  68. @param minNumElements the minimum number of elements that are needed
  69. */
  70. void ensureAllocatedSize (int minNumElements)
  71. {
  72. if (minNumElements > numAllocated)
  73. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  74. jassert (numAllocated <= 0 || elements != nullptr);
  75. }
  76. /** Minimises the amount of storage allocated so that it's no more than
  77. the given number of elements.
  78. */
  79. void shrinkToNoMoreThan (int maxNumElements)
  80. {
  81. if (maxNumElements < numAllocated)
  82. setAllocatedSize (maxNumElements);
  83. }
  84. /** Swap the contents of two objects. */
  85. void swapWith (ArrayAllocationBase& other) noexcept
  86. {
  87. elements.swapWith (other.elements);
  88. std::swap (numAllocated, other.numAllocated);
  89. }
  90. //==============================================================================
  91. HeapBlock<ElementType> elements;
  92. int numAllocated = 0;
  93. private:
  94. JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
  95. };
  96. } // namespace juce