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.

127 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. #pragma once
  18. //==============================================================================
  19. /**
  20. Implements some basic array storage allocation functions.
  21. This class isn't really for public use - it's used by the other
  22. array classes, but might come in handy for some purposes.
  23. It inherits from a critical section class to allow the arrays to use
  24. the "empty base class optimisation" pattern to reduce their footprint.
  25. @see Array, OwnedArray, ReferenceCountedArray
  26. */
  27. template <class ElementType, class TypeOfCriticalSectionToUse>
  28. class ArrayAllocationBase : public TypeOfCriticalSectionToUse
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates an empty array. */
  33. ArrayAllocationBase() noexcept
  34. : numAllocated (0)
  35. {
  36. }
  37. /** Destructor. */
  38. ~ArrayAllocationBase() noexcept
  39. {
  40. }
  41. ArrayAllocationBase (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  42. : elements (static_cast<HeapBlock<ElementType>&&> (other.elements)),
  43. numAllocated (other.numAllocated)
  44. {
  45. }
  46. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  47. {
  48. elements = static_cast<HeapBlock<ElementType>&&> (other.elements);
  49. numAllocated = other.numAllocated;
  50. return *this;
  51. }
  52. //==============================================================================
  53. /** Changes the amount of storage allocated.
  54. This will retain any data currently held in the array, and either add or
  55. remove extra space at the end.
  56. @param numElements the number of elements that are needed
  57. */
  58. void setAllocatedSize (const int numElements)
  59. {
  60. if (numAllocated != numElements)
  61. {
  62. if (numElements > 0)
  63. elements.realloc ((size_t) numElements);
  64. else
  65. elements.free();
  66. numAllocated = numElements;
  67. }
  68. }
  69. /** Increases the amount of storage allocated if it is less than a given amount.
  70. This will retain any data currently held in the array, but will add
  71. extra space at the end to make sure there it's at least as big as the size
  72. passed in. If it's already bigger, no action is taken.
  73. @param minNumElements the minimum number of elements that are needed
  74. */
  75. void ensureAllocatedSize (const int minNumElements)
  76. {
  77. if (minNumElements > numAllocated)
  78. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  79. jassert (numAllocated <= 0 || elements != nullptr);
  80. }
  81. /** Minimises the amount of storage allocated so that it's no more than
  82. the given number of elements.
  83. */
  84. void shrinkToNoMoreThan (const int maxNumElements)
  85. {
  86. if (maxNumElements < numAllocated)
  87. setAllocatedSize (maxNumElements);
  88. }
  89. /** Swap the contents of two objects. */
  90. void swapWith (ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse>& other) noexcept
  91. {
  92. elements.swapWith (other.elements);
  93. std::swap (numAllocated, other.numAllocated);
  94. }
  95. //==============================================================================
  96. HeapBlock<ElementType> elements;
  97. int numAllocated;
  98. private:
  99. JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
  100. };