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.

233 lines
6.0KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Allocator.cpp - RT-Safe Memory Allocator
  4. Copyright (C) 2016 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #include <cstddef>
  11. #include <cstdlib>
  12. #include <cassert>
  13. #include <utility>
  14. #include <cstdio>
  15. #include "tlsf/tlsf.h"
  16. #include "Allocator.h"
  17. //Used for dummy allocations
  18. DummyAllocator DummyAlloc;
  19. //recursive type class to avoid void *v = *(void**)v style casting
  20. struct next_t
  21. {
  22. next_t *next;
  23. size_t pool_size;
  24. };
  25. void *data(next_t *n)
  26. {
  27. return n+sizeof(next_t);
  28. }
  29. struct AllocatorImpl
  30. {
  31. void *tlsf = 0;
  32. //singly linked list of memory pools
  33. //XXX this may violate alignment on some platforms if malloc doesn't return
  34. //nice values
  35. next_t *pools = 0;
  36. unsigned long long totalAlloced = 0;
  37. };
  38. Allocator::Allocator(void) : transaction_active()
  39. {
  40. impl = new AllocatorImpl;
  41. size_t default_size = 10*1024*1024;
  42. impl->pools = (next_t*)malloc(default_size);
  43. impl->pools->next = 0x0;
  44. impl->pools->pool_size = default_size;
  45. size_t off = tlsf_size() + tlsf_pool_overhead() + sizeof(next_t);
  46. //printf("Generated Memory Pool with '%p'\n", impl->pools);
  47. impl->tlsf = tlsf_create_with_pool(((char*)impl->pools)+off, default_size-2*off);
  48. //printf("Allocator(%p)\n", impl);
  49. }
  50. Allocator::~Allocator(void)
  51. {
  52. next_t *n = impl->pools;
  53. while(n) {
  54. next_t *nn = n->next;
  55. free(n);
  56. n = nn;
  57. }
  58. delete impl;
  59. }
  60. void *AllocatorClass::alloc_mem(size_t mem_size)
  61. {
  62. impl->totalAlloced += mem_size;
  63. void *mem = tlsf_malloc(impl->tlsf, mem_size);
  64. //printf("Allocator.malloc(%p, %d) = %p\n", impl, mem_size, mem);
  65. //void *mem = malloc(mem_size);
  66. //printf("Allocator result = %p\n", mem);
  67. return mem;
  68. }
  69. void AllocatorClass::dealloc_mem(void *memory)
  70. {
  71. //printf("dealloc_mem(%d)\n", tlsf_block_size(memory));
  72. tlsf_free(impl->tlsf, memory);
  73. //free(memory);
  74. }
  75. bool AllocatorClass::lowMemory(unsigned n, size_t chunk_size) const
  76. {
  77. //This should stay on the stack
  78. void *buf[n];
  79. for(unsigned i=0; i<n; ++i)
  80. buf[i] = tlsf_malloc(impl->tlsf, chunk_size);
  81. bool outOfMem = false;
  82. for(unsigned i=0; i<n; ++i)
  83. outOfMem |= (buf[i] == nullptr);
  84. for(unsigned i=0; i<n; ++i)
  85. if(buf[i])
  86. tlsf_free(impl->tlsf, buf[i]);
  87. return outOfMem;
  88. }
  89. void AllocatorClass::addMemory(void *v, size_t mem_size)
  90. {
  91. next_t *n = impl->pools;
  92. while(n->next) n = n->next;
  93. n->next = (next_t*)v;
  94. n->next->next = 0x0;
  95. n->next->pool_size = mem_size;
  96. //printf("Inserting '%p'\n", v);
  97. off_t off = sizeof(next_t) + tlsf_pool_overhead();
  98. void *result =
  99. tlsf_add_pool(impl->tlsf, ((char*)n->next)+off,
  100. //0x0eadbeef);
  101. mem_size-off-sizeof(size_t));
  102. if(!result)
  103. printf("FAILED TO INSERT MEMORY POOL\n");
  104. };//{(void)mem_size;};
  105. #ifndef INCLUDED_tlsfbits
  106. //From tlsf internals
  107. typedef struct block_header_t
  108. {
  109. /* Points to the previous physical block. */
  110. struct block_header_t* prev_phys_block;
  111. /* The size of this block, excluding the block header. */
  112. size_t size;
  113. /* Next and previous free blocks. */
  114. struct block_header_t* next_free;
  115. struct block_header_t* prev_free;
  116. } block_header_t;
  117. static const size_t block_header_free_bit = 1 << 0;
  118. #endif
  119. void Allocator::beginTransaction() {
  120. // TODO: log about unsupported nested transaction when a RT compliant
  121. // logging is available and transaction_active == true
  122. transaction_active = true;
  123. transaction_alloc_index = 0;
  124. }
  125. void Allocator::endTransaction() {
  126. // TODO: log about invalid end of transaction when a RT copmliant logging
  127. // is available and transaction_active == false
  128. transaction_active = false;
  129. }
  130. bool Allocator::memFree(void *pool) const
  131. {
  132. size_t bh_shift = sizeof(next_t)+sizeof(size_t);
  133. //Assume that memory is free to start with
  134. bool isFree = true;
  135. //Get the block header from the pool
  136. block_header_t &bh = *(block_header_t*)((char*)pool+bh_shift);
  137. //The first block must be free
  138. if((bh.size&block_header_free_bit) == 0)
  139. isFree = false;
  140. block_header_t &bhn = *(block_header_t*)
  141. (((char*)&bh)+((bh.size&~0x3)+bh_shift-2*sizeof(size_t)));
  142. //The next block must be 'non-free' and zero length
  143. if((bhn.size&block_header_free_bit) != 0)
  144. isFree = false;
  145. if((bhn.size&~0x3) != 0)
  146. isFree = false;
  147. return isFree;
  148. }
  149. int Allocator::memPools() const
  150. {
  151. int i = 1;
  152. next_t *n = impl->pools;
  153. while(n->next) {
  154. i++;
  155. n = n->next;
  156. }
  157. return i;
  158. }
  159. int Allocator::freePools() const
  160. {
  161. int i = 0;
  162. next_t *n = impl->pools->next;
  163. while(n) {
  164. if(memFree(n))
  165. i++;
  166. n = n->next;
  167. }
  168. return i;
  169. }
  170. unsigned long long Allocator::totalAlloced() const
  171. {
  172. return impl->totalAlloced;
  173. }
  174. void Allocator::rollbackTransaction() {
  175. // if a transaction is active
  176. if (transaction_active) {
  177. // deallocate all allocated memory within this transaction
  178. for (size_t temp_idx = 0;
  179. temp_idx < transaction_alloc_index; ++temp_idx) {
  180. dealloc_mem(transaction_alloc_content[temp_idx]);
  181. }
  182. }
  183. transaction_active = false;
  184. }
  185. /*
  186. * Notes on tlsf internals
  187. * - TLSF consists of blocks linked by block headers and these form a doubly
  188. * linked list of free segments
  189. * - Original memory is [control_t pool]
  190. * base sentinal
  191. * Pools are [block_t block_t blocks ...]
  192. * Blocks are [memory block_t](??)
  193. * - These are stored in the control_t structure in an order dependent on the
  194. * size that they are
  195. * it's a bit unclear how collisions are handled here, but the basic premise
  196. * makes sense
  197. * - Additional structure is added before the start of each pool to define the
  198. * pool size and the next pool in the list as this information is not
  199. * accessible in O(good) time
  200. */