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.

ArrayAllocationBase.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. @see Array, OwnedArray, ReferenceCountedArray
  31. */
  32. template <class ElementType>
  33. class ArrayAllocationBase
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty array. */
  38. ArrayAllocationBase() noexcept
  39. : elements(),
  40. numAllocated(0)
  41. {
  42. }
  43. /** Destructor. */
  44. ~ArrayAllocationBase() noexcept
  45. {
  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 numNewElements the number of elements that are needed
  52. */
  53. bool setAllocatedSize (const size_t numNewElements) noexcept
  54. {
  55. if (numAllocated != numNewElements)
  56. {
  57. if (numNewElements > 0)
  58. {
  59. if (! elements.realloc ((size_t) numNewElements))
  60. return false;
  61. }
  62. else
  63. {
  64. elements.free();
  65. }
  66. numAllocated = numNewElements;
  67. }
  68. return true;
  69. }
  70. /** Increases the amount of storage allocated if it is less than a given amount.
  71. This will retain any data currently held in the array, but will add
  72. extra space at the end to make sure there it's at least as big as the size
  73. passed in. If it's already bigger, no action is taken.
  74. @param minNumElements the minimum number of elements that are needed
  75. */
  76. bool ensureAllocatedSize (const size_t minNumElements) noexcept
  77. {
  78. if (minNumElements > numAllocated)
  79. return setAllocatedSize ((minNumElements + minNumElements / 2U + 8U) & ~7U);
  80. return true;
  81. }
  82. /** Minimises the amount of storage allocated so that it's no more than
  83. the given number of elements.
  84. */
  85. bool shrinkToNoMoreThan (const size_t maxNumElements) noexcept
  86. {
  87. if (maxNumElements < numAllocated)
  88. return setAllocatedSize (maxNumElements);
  89. return true;
  90. }
  91. /** Swap the contents of two objects. */
  92. void swapWith (ArrayAllocationBase <ElementType>& other) noexcept
  93. {
  94. elements.swapWith (other.elements);
  95. std::swap (numAllocated, other.numAllocated);
  96. }
  97. void moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
  98. {
  99. CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
  100. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  101. CARLA_SAFE_ASSERT_RETURN(target != source,);
  102. CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
  103. std::memmove (target, source, ((size_t) numElements) * sizeof (ElementType));
  104. }
  105. //==============================================================================
  106. HeapBlock<ElementType> elements;
  107. size_t numAllocated;
  108. CARLA_DECLARE_NON_COPYABLE (ArrayAllocationBase)
  109. };
  110. }
  111. #endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED