Audio plugin host https://kx.studio/carla
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.

142 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_ARRAYALLOCATIONBASE_H_INCLUDED
  21. #define WATER_ARRAYALLOCATIONBASE_H_INCLUDED
  22. #include "../memory/HeapBlock.h"
  23. #include "CarlaUtils.hpp"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Implements some basic array storage allocation functions.
  28. This class isn't really for public use - it's used by the other
  29. array classes, but might come in handy for some purposes.
  30. It inherits from a critical section class to allow the arrays to use
  31. the "empty base class optimisation" pattern to reduce their footprint.
  32. @see Array, OwnedArray, ReferenceCountedArray
  33. */
  34. template <class ElementType>
  35. class ArrayAllocationBase
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates an empty array. */
  40. ArrayAllocationBase() noexcept
  41. : numAllocated (0)
  42. {
  43. }
  44. /** Destructor. */
  45. ~ArrayAllocationBase() noexcept
  46. {
  47. }
  48. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  49. ArrayAllocationBase (ArrayAllocationBase<ElementType>&& other) noexcept
  50. : elements (static_cast<HeapBlock<ElementType>&&> (other.elements)),
  51. numAllocated (other.numAllocated)
  52. {
  53. }
  54. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType>&& other) noexcept
  55. {
  56. elements = static_cast<HeapBlock<ElementType>&&> (other.elements);
  57. numAllocated = other.numAllocated;
  58. return *this;
  59. }
  60. #endif
  61. //==============================================================================
  62. /** Changes the amount of storage allocated.
  63. This will retain any data currently held in the array, and either add or
  64. remove extra space at the end.
  65. @param numElements the number of elements that are needed
  66. */
  67. void setAllocatedSize (const int numElements)
  68. {
  69. if (numAllocated != numElements)
  70. {
  71. if (numElements > 0)
  72. elements.realloc ((size_t) numElements);
  73. else
  74. elements.free();
  75. numAllocated = numElements;
  76. }
  77. }
  78. /** Increases the amount of storage allocated if it is less than a given amount.
  79. This will retain any data currently held in the array, but will add
  80. extra space at the end to make sure there it's at least as big as the size
  81. passed in. If it's already bigger, no action is taken.
  82. @param minNumElements the minimum number of elements that are needed
  83. */
  84. void ensureAllocatedSize (const int minNumElements)
  85. {
  86. if (minNumElements > numAllocated)
  87. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  88. jassert (numAllocated <= 0 || elements != nullptr);
  89. }
  90. /** Minimises the amount of storage allocated so that it's no more than
  91. the given number of elements.
  92. */
  93. void shrinkToNoMoreThan (const int maxNumElements)
  94. {
  95. if (maxNumElements < numAllocated)
  96. setAllocatedSize (maxNumElements);
  97. }
  98. /** Swap the contents of two objects. */
  99. void swapWith (ArrayAllocationBase <ElementType>& other) noexcept
  100. {
  101. elements.swapWith (other.elements);
  102. std::swap (numAllocated, other.numAllocated);
  103. }
  104. //==============================================================================
  105. HeapBlock<ElementType> elements;
  106. int numAllocated;
  107. private:
  108. CARLA_DECLARE_NON_COPY_CLASS (ArrayAllocationBase)
  109. };
  110. }
  111. #endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED