bufferAPI.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. uint8_t *data;
  37. std::function<void(uint8_t *)> destructor;
  38. public:
  39. // Create head without data.
  40. BufferImpl();
  41. // Create head with newly allocated data.
  42. explicit BufferImpl(int64_t newSize);
  43. // Create head with inherited data.
  44. BufferImpl(int64_t newSize, uint8_t *newData);
  45. ~BufferImpl();
  46. public:
  47. // No implicit copies, only pass using the Buffer handle
  48. BufferImpl(const BufferImpl&) = delete;
  49. BufferImpl& operator=(const BufferImpl&) = delete;
  50. };
  51. // Internal methods
  52. // Buffers are aligned and padded for the deafult SIMD vector size, so that vectorization can be efficient.
  53. static const int buffer_alignment = DSR_DEFAULT_ALIGNMENT;
  54. // If this C++ version additionally includes the C11 features then we may assume that aligned_alloc is available
  55. #ifdef _ISOC11_SOURCE
  56. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  57. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  58. uint8_t* allocation = (uint8_t*)aligned_alloc(buffer_alignment, newSize);
  59. targetDestructor = [](uint8_t *data) { free(data); };
  60. return allocation;
  61. }
  62. #else
  63. static const uintptr_t buffer_alignment_mask = ~((uintptr_t)(buffer_alignment - 1));
  64. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  65. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  66. uintptr_t padding = buffer_alignment - 1;
  67. uint8_t* allocation = (uint8_t*)malloc(newSize + padding);
  68. uint8_t* aligned = (uint8_t*)(((uintptr_t)allocation + padding) & buffer_alignment_mask);
  69. uintptr_t offset = allocation - aligned;
  70. targetDestructor = [offset](uint8_t *data) { free(data - offset); };
  71. return aligned;
  72. }
  73. #endif
  74. BufferImpl::BufferImpl() : size(0), bufferSize(0), data(nullptr) {}
  75. BufferImpl::BufferImpl(int64_t newSize) :
  76. size(newSize),
  77. bufferSize(roundUp(newSize, buffer_alignment)) {
  78. this->data = buffer_allocate(this->bufferSize, this->destructor);
  79. if (this->data == nullptr) {
  80. throwError(U"Failed to allocate buffer of ", newSize, " bytes!\n");
  81. }
  82. memset(this->data, 0, this->bufferSize);
  83. }
  84. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  85. : size(newSize), bufferSize(newSize), data(newData), destructor([](uint8_t *data) { free(data); }) {}
  86. BufferImpl::~BufferImpl() {
  87. if (this->data) {
  88. this->destructor(this->data);
  89. }
  90. }
  91. // API
  92. Buffer buffer_clone(const Buffer &buffer) {
  93. if (!buffer_exists(buffer)) {
  94. // If the original buffer does not exist, just return another null handle.
  95. return Buffer();
  96. } else {
  97. if (buffer->size <= 0) {
  98. // No need to clone when there is no shared data.
  99. return buffer;
  100. } else {
  101. // Clone the data so that the allocations can be modified individually.
  102. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size);
  103. memcpy(newBuffer->data, buffer->data, buffer->size);
  104. return newBuffer;
  105. }
  106. }
  107. }
  108. Buffer buffer_create(int64_t newSize) {
  109. if (newSize < 0) newSize = 0;
  110. if (newSize == 0) {
  111. // Allocate empty head to indicate that an empty buffer exists.
  112. return std::make_shared<BufferImpl>();
  113. } else {
  114. // Allocate head and data.
  115. return std::make_shared<BufferImpl>(newSize);
  116. }
  117. }
  118. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  119. if (newSize < 0) newSize = 0;
  120. return std::make_shared<BufferImpl>(newSize, newData);
  121. }
  122. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor) {
  123. if (!buffer_exists(buffer)) {
  124. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  125. } else if (buffer->bufferSize > 0) {
  126. buffer->destructor = newDestructor;
  127. }
  128. }
  129. int64_t buffer_getSize(const Buffer &buffer) {
  130. if (!buffer_exists(buffer)) {
  131. return 0;
  132. } else {
  133. return buffer->size;
  134. }
  135. }
  136. int64_t buffer_getUseCount(const Buffer &buffer) {
  137. if (!buffer_exists(buffer)) {
  138. return 0;
  139. } else {
  140. return buffer.use_count();
  141. }
  142. }
  143. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
  144. if (!buffer_exists(buffer)) {
  145. return nullptr;
  146. } else {
  147. return buffer->data;
  148. }
  149. }
  150. void buffer_setBytes(const Buffer &buffer, uint8_t value) {
  151. if (!buffer_exists(buffer)) {
  152. throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
  153. } else if (buffer->bufferSize > 0) {
  154. memset(buffer->data, value, buffer->bufferSize);
  155. }
  156. }
  157. }