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.

109 lines
3.1KB

  1. #pragma once
  2. #include <cstdlib>
  3. #include <utility>
  4. class Allocator
  5. {
  6. public:
  7. Allocator(void);
  8. Allocator(const Allocator&) = delete;
  9. ~Allocator(void);
  10. void *alloc_mem(size_t mem_size);
  11. void dealloc_mem(void *memory);
  12. template <typename T, typename... Ts>
  13. T *alloc(Ts&&... ts)
  14. {
  15. void *data = alloc_mem(sizeof(T));
  16. if(!data)
  17. return nullptr;
  18. return new (data) T(std::forward<Ts>(ts)...);
  19. }
  20. template <typename T, typename... Ts>
  21. T *valloc(size_t len, Ts&&... ts)
  22. {
  23. T *data = (T*)alloc_mem(len*sizeof(T));
  24. if(!data)
  25. return nullptr;
  26. for(unsigned i=0; i<len; ++i)
  27. new ((void*)&data[i]) T(std::forward<Ts>(ts)...);
  28. return data;
  29. }
  30. template <typename T>
  31. void dealloc(T*&t)
  32. {
  33. if(t) {
  34. t->~T();
  35. dealloc_mem((void*)t);
  36. t = nullptr;
  37. }
  38. }
  39. //Destructor Free Version
  40. template <typename T>
  41. void devalloc(T*&t)
  42. {
  43. if(t) {
  44. dealloc_mem(t);
  45. t = nullptr;
  46. }
  47. }
  48. template <typename T>
  49. void devalloc(size_t elms, T*&t)
  50. {
  51. if(t) {
  52. for(size_t i=0; i<elms; ++i)
  53. (t+i)->~T();
  54. dealloc_mem(t);
  55. t = nullptr;
  56. }
  57. }
  58. void addMemory(void *, size_t mem_size);
  59. //Return true if the current pool cannot allocate n chunks of chunk_size
  60. bool lowMemory(unsigned n, size_t chunk_size);
  61. bool memFree(void *pool);
  62. //returns number of pools
  63. int memPools();
  64. int freePools();
  65. unsigned long long totalAlloced();
  66. struct AllocatorImpl *impl;
  67. };
  68. extern Allocator DummyAlloc;
  69. /**
  70. * General notes on Memory Allocation Within ZynAddSubFX
  71. * -----------------------------------------------------
  72. *
  73. * - Parameter Objects Are never allocated within the realtime thread
  74. * - Effects, notes and note subcomponents must be allocated with an allocator
  75. * - 5M Chunks are used to give the allocator the memory it wants
  76. * - If there are 3 chunks that are unused then 1 will be deallocated
  77. * - The system will request more allocated space if 5x 1MB chunks cannot be
  78. * allocated at any given time (this is likely huge overkill, but if this is
  79. * satisfied, then a lot of note spamming would be needed to run out of
  80. * space)
  81. *
  82. * - Things will get a bit weird around the effects due to how pointer swaps
  83. * occur
  84. * * When a new Part instance is provided it may or may not come with some
  85. * instrument effects
  86. * * Merging blocks is an option, but one that is not going to likely be
  87. * implmented too soon, thus all effects need to be reallocated when the
  88. * pointer swap occurs
  89. * * The old effect is extracted from the manager
  90. * * A new one is constructed with a deep copy
  91. * * The old one is returned to middleware for deallocation
  92. */