bufferAPI.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. uint32_t requestedAlignment = 1 << e;
  59. if (1 << e == largestAlignment) return largestAlignment;
  60. }
  61. return -1;
  62. }
  63. // If this C++ version additionally includes the C11 features then we may assume that aligned_alloc is available
  64. #ifdef _ISOC11_SOURCE
  65. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  66. static uint8_t* buffer_allocate(int64_t newSize, int alignment, std::function<void(uint8_t *)>& targetDestructor) {
  67. uint8_t* allocation = (uint8_t*)aligned_alloc(alignment, newSize);
  68. targetDestructor = [](uint8_t *data) { free(data); };
  69. return allocation;
  70. }
  71. #else
  72. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  73. static uint8_t* buffer_allocate(int64_t newSize, int alignment, std::function<void(uint8_t *)>& targetDestructor) {
  74. uintptr_t padding = alignment - 1;
  75. uint8_t* allocation = (uint8_t*)malloc(newSize + padding);
  76. uintptr_t buffer_alignment_mask = ~((uintptr_t)(alignment - 1));
  77. uint8_t* aligned = (uint8_t*)(((uintptr_t)allocation + padding) & buffer_alignment_mask);
  78. uintptr_t offset = allocation - aligned;
  79. targetDestructor = [offset](uint8_t *data) { free(data - offset); };
  80. return aligned;
  81. }
  82. #endif
  83. BufferImpl::BufferImpl() : size(0), bufferSize(0), alignment(DSR_DEFAULT_ALIGNMENT), data(nullptr) {}
  84. BufferImpl::BufferImpl(int64_t newSize, int alignment) :
  85. size(newSize),
  86. bufferSize(roundUp(newSize, alignment)),
  87. alignment(alignment) {
  88. this->data = buffer_allocate(this->bufferSize, alignment, this->destructor);
  89. if (this->data == nullptr) {
  90. throwError(U"Failed to allocate buffer of ", newSize, " bytes!\n");
  91. }
  92. memset(this->data, 0, this->bufferSize);
  93. }
  94. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  95. : size(newSize), bufferSize(newSize), alignment(1), data(newData), destructor([](uint8_t *data) { free(data); }) {}
  96. BufferImpl::~BufferImpl() {
  97. if (this->data) {
  98. this->destructor(this->data);
  99. }
  100. }
  101. // API
  102. Buffer buffer_clone(const Buffer &buffer) {
  103. if (!buffer_exists(buffer)) {
  104. // If the original buffer does not exist, just return another null handle.
  105. return Buffer();
  106. } else {
  107. if (buffer->size <= 0) {
  108. // No need to clone when there is no shared data.
  109. return buffer;
  110. } else {
  111. // Clone the data so that content of the allocations can be modified individually without affecting each other.
  112. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size, max(buffer->alignment, DSR_DEFAULT_ALIGNMENT));
  113. memcpy(newBuffer->data, buffer->data, buffer->size);
  114. return newBuffer;
  115. }
  116. }
  117. }
  118. Buffer buffer_create(int64_t newSize, int minimumAlignment) {
  119. if (newSize < 0) newSize = 0;
  120. if (newSize == 0) {
  121. // Allocate empty head to indicate that an empty buffer exists.
  122. return std::make_shared<BufferImpl>();
  123. } else {
  124. // Allocate head and data.
  125. int finalAlignment = getFinalAlignment(minimumAlignment);
  126. if (finalAlignment != -1) {
  127. return std::make_shared<BufferImpl>(newSize, finalAlignment);
  128. } else {
  129. throwError(U"buffer_create: Minimum alignment ", minimumAlignment, " is not a power of two!\n");
  130. return Buffer(); // Invalid alignment argument was not a power of two.
  131. }
  132. }
  133. }
  134. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  135. if (newSize < 0) newSize = 0;
  136. return std::make_shared<BufferImpl>(newSize, newData);
  137. }
  138. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor) {
  139. if (!buffer_exists(buffer)) {
  140. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  141. } else if (buffer->bufferSize > 0) {
  142. buffer->destructor = newDestructor;
  143. }
  144. }
  145. int64_t buffer_getSize(const Buffer &buffer) {
  146. if (!buffer_exists(buffer)) {
  147. return 0;
  148. } else {
  149. return buffer->size;
  150. }
  151. }
  152. int64_t buffer_getUseCount(const Buffer &buffer) {
  153. if (!buffer_exists(buffer)) {
  154. return 0;
  155. } else {
  156. return buffer.use_count();
  157. }
  158. }
  159. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
  160. if (!buffer_exists(buffer)) {
  161. return nullptr;
  162. } else {
  163. return buffer->data;
  164. }
  165. }
  166. void buffer_setBytes(const Buffer &buffer, uint8_t value) {
  167. if (!buffer_exists(buffer)) {
  168. throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
  169. } else if (buffer->bufferSize > 0) {
  170. memset(buffer->data, value, buffer->bufferSize);
  171. }
  172. }
  173. }