bufferAPI.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 to 2023 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include <fstream>
  24. #include <cstdlib>
  25. #include "bufferAPI.h"
  26. #include "stringAPI.h"
  27. #include "../math/scalar.h"
  28. #include "../base/simd.h"
  29. namespace dsr {
  30. // Hidden type
  31. class BufferImpl {
  32. public:
  33. // A Buffer cannot have a name, because each String contains a buffer
  34. const int64_t size; // The actually used data
  35. const int64_t bufferSize; // The accessible data
  36. const int alignment;
  37. uint8_t *data;
  38. std::function<void(uint8_t *)> destructor;
  39. public:
  40. // Create head without data.
  41. BufferImpl();
  42. // Create head with newly allocated data.
  43. explicit BufferImpl(int64_t newSize, int alignment);
  44. // Create head with inherited data.
  45. BufferImpl(int64_t newSize, uint8_t *newData);
  46. ~BufferImpl();
  47. public:
  48. // No implicit copies, only pass using the Buffer handle
  49. BufferImpl(const BufferImpl&) = delete;
  50. BufferImpl& operator=(const BufferImpl&) = delete;
  51. };
  52. // Internal methods
  53. // Get the largest alignment and confirm that it is a power of two.
  54. static int getFinalAlignment(int requestedAlignment) {
  55. // Find any power of two alignment divisible by both requestedAlignment and DSR_DEFAULT_ALIGNMENT
  56. int largestAlignment = max(requestedAlignment, DSR_DEFAULT_ALIGNMENT);
  57. for (uint32_t e = 0; e < 32; e++) {
  58. if (1 << e == largestAlignment) return largestAlignment;
  59. }
  60. return -1;
  61. }
  62. // If this C++ version additionally includes the C11 features then we may assume that aligned_alloc is available
  63. #ifdef _ISOC11_SOURCE
  64. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  65. static uint8_t* buffer_allocate(int64_t newSize, int alignment, std::function<void(uint8_t *)>& targetDestructor) {
  66. uint8_t* allocation = (uint8_t*)aligned_alloc(alignment, newSize);
  67. targetDestructor = [](uint8_t *data) { free(data); };
  68. return allocation;
  69. }
  70. #else
  71. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  72. static uint8_t* buffer_allocate(int64_t newSize, int alignment, std::function<void(uint8_t *)>& targetDestructor) {
  73. uintptr_t padding = alignment - 1;
  74. uint8_t* allocation = (uint8_t*)malloc(newSize + padding);
  75. uintptr_t buffer_alignment_mask = ~((uintptr_t)(alignment - 1));
  76. uint8_t* aligned = (uint8_t*)(((uintptr_t)allocation + padding) & buffer_alignment_mask);
  77. uintptr_t offset = allocation - aligned;
  78. targetDestructor = [offset](uint8_t *data) { free(data - offset); };
  79. return aligned;
  80. }
  81. #endif
  82. BufferImpl::BufferImpl() : size(0), bufferSize(0), alignment(DSR_DEFAULT_ALIGNMENT), data(nullptr) {}
  83. BufferImpl::BufferImpl(int64_t newSize, int alignment) :
  84. size(newSize),
  85. bufferSize(roundUp(newSize, alignment)),
  86. alignment(alignment) {
  87. this->data = buffer_allocate(this->bufferSize, alignment, this->destructor);
  88. if (this->data == nullptr) {
  89. throwError(U"Failed to allocate buffer of ", newSize, " bytes!\n");
  90. }
  91. memset(this->data, 0, this->bufferSize);
  92. }
  93. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  94. : size(newSize), bufferSize(newSize), alignment(1), data(newData), destructor([](uint8_t *data) { free(data); }) {}
  95. BufferImpl::~BufferImpl() {
  96. if (this->data) {
  97. this->destructor(this->data);
  98. }
  99. }
  100. // API
  101. Buffer buffer_clone(const Buffer &buffer) {
  102. if (!buffer_exists(buffer)) {
  103. // If the original buffer does not exist, just return another null handle.
  104. return Buffer();
  105. } else {
  106. if (buffer->size <= 0) {
  107. // No need to clone when there is no shared data.
  108. return buffer;
  109. } else {
  110. // Clone the data so that content of the allocations can be modified individually without affecting each other.
  111. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size, max(buffer->alignment, DSR_DEFAULT_ALIGNMENT));
  112. memcpy(newBuffer->data, buffer->data, buffer->size);
  113. return newBuffer;
  114. }
  115. }
  116. }
  117. Buffer buffer_create(int64_t newSize, int minimumAlignment) {
  118. if (newSize < 0) newSize = 0;
  119. if (newSize == 0) {
  120. // Allocate empty head to indicate that an empty buffer exists.
  121. return std::make_shared<BufferImpl>();
  122. } else {
  123. // Allocate head and data.
  124. int finalAlignment = getFinalAlignment(minimumAlignment);
  125. if (finalAlignment != -1) {
  126. return std::make_shared<BufferImpl>(newSize, finalAlignment);
  127. } else {
  128. throwError(U"buffer_create: Minimum alignment ", minimumAlignment, " is not a power of two!\n");
  129. return Buffer(); // Invalid alignment argument was not a power of two.
  130. }
  131. }
  132. }
  133. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  134. if (newSize < 0) newSize = 0;
  135. return std::make_shared<BufferImpl>(newSize, newData);
  136. }
  137. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor) {
  138. if (!buffer_exists(buffer)) {
  139. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  140. } else if (buffer->bufferSize > 0) {
  141. buffer->destructor = newDestructor;
  142. }
  143. }
  144. int64_t buffer_getSize(const Buffer &buffer) {
  145. if (!buffer_exists(buffer)) {
  146. return 0;
  147. } else {
  148. return buffer->size;
  149. }
  150. }
  151. int64_t buffer_getUseCount(const Buffer &buffer) {
  152. if (!buffer_exists(buffer)) {
  153. return 0;
  154. } else {
  155. return buffer.use_count();
  156. }
  157. }
  158. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
  159. if (!buffer_exists(buffer)) {
  160. return nullptr;
  161. } else {
  162. return buffer->data;
  163. }
  164. }
  165. void buffer_setBytes(const Buffer &buffer, uint8_t value) {
  166. if (!buffer_exists(buffer)) {
  167. throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
  168. } else if (buffer->bufferSize > 0) {
  169. memset(buffer->data, value, buffer->bufferSize);
  170. }
  171. }
  172. }