bufferAPI.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "bufferAPI.h"
  25. #include "stringAPI.h"
  26. #include "../math/scalar.h"
  27. namespace dsr {
  28. // Hidden type
  29. class BufferImpl {
  30. public:
  31. // A Buffer cannot have a name, because each String contains a buffer
  32. const int64_t size; // The actually used data
  33. const int64_t bufferSize; // The accessible data
  34. uint8_t *data;
  35. std::function<void(uint8_t *)> destructor;
  36. public:
  37. // Create head without data.
  38. BufferImpl();
  39. // Create head with newly allocated data.
  40. explicit BufferImpl(int64_t newSize);
  41. // Create head with inherited data.
  42. BufferImpl(int64_t newSize, uint8_t *newData);
  43. ~BufferImpl();
  44. public:
  45. // No implicit copies, only pass using the Buffer handle
  46. BufferImpl(const BufferImpl&) = delete;
  47. BufferImpl& operator=(const BufferImpl&) = delete;
  48. };
  49. // Internal methods
  50. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  51. uint8_t* allocation = heap_allocate(newSize).data;
  52. targetDestructor = [](uint8_t *data) { heap_free(data); };
  53. return allocation;
  54. }
  55. BufferImpl::BufferImpl() : size(0), bufferSize(0), data(nullptr) {}
  56. BufferImpl::BufferImpl(int64_t newSize) :
  57. size(newSize),
  58. bufferSize(roundUp(newSize, DSR_MAXIMUM_ALIGNMENT)) {
  59. this->data = buffer_allocate(this->bufferSize, this->destructor);
  60. if (this->data == nullptr) {
  61. throwError(U"Failed to allocate buffer of ", newSize, " bytes!\n");
  62. }
  63. memset(this->data, 0, this->bufferSize);
  64. }
  65. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  66. : size(newSize), bufferSize(newSize), data(newData), destructor([](uint8_t *data) { heap_free(data); }) {}
  67. BufferImpl::~BufferImpl() {
  68. if (this->data) {
  69. this->destructor(this->data);
  70. }
  71. }
  72. // API
  73. Buffer buffer_clone(const Buffer &buffer) {
  74. if (!buffer_exists(buffer)) {
  75. // If the original buffer does not exist, just return another null handle.
  76. return Buffer();
  77. } else {
  78. if (buffer->size <= 0) {
  79. // No need to clone when there is no shared data.
  80. return buffer;
  81. } else {
  82. // Clone the data so that content of the allocations can be modified individually without affecting each other.
  83. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size);
  84. memcpy(newBuffer->data, buffer->data, buffer->size);
  85. return newBuffer;
  86. }
  87. }
  88. }
  89. Buffer buffer_create(int64_t newSize) {
  90. if (newSize < 0) newSize = 0;
  91. if (newSize == 0) {
  92. // Allocate empty head to indicate that an empty buffer exists.
  93. return std::make_shared<BufferImpl>();
  94. } else {
  95. // Allocate head and data.
  96. return std::make_shared<BufferImpl>(newSize);
  97. }
  98. }
  99. Buffer buffer_create(int64_t newSize, int minimumAlignment) {
  100. if (newSize < 0) newSize = 0;
  101. if (newSize == 0) {
  102. // Allocate empty head to indicate that an empty buffer exists.
  103. return std::make_shared<BufferImpl>();
  104. } else if (minimumAlignment > DSR_MAXIMUM_ALIGNMENT) {
  105. throwError(U"Maximum alignment exceeded when creating a buffer!\n");
  106. return Buffer();
  107. } else {
  108. // Allocate head and data.
  109. return std::make_shared<BufferImpl>(newSize);
  110. }
  111. }
  112. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  113. if (newSize < 0) newSize = 0;
  114. return std::make_shared<BufferImpl>(newSize, newData);
  115. }
  116. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor) {
  117. if (!buffer_exists(buffer)) {
  118. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  119. } else if (buffer->bufferSize > 0) {
  120. buffer->destructor = newDestructor;
  121. }
  122. }
  123. int64_t buffer_getSize(const Buffer &buffer) {
  124. if (!buffer_exists(buffer)) {
  125. return 0;
  126. } else {
  127. return buffer->size;
  128. }
  129. }
  130. int64_t buffer_getUseCount(const Buffer &buffer) {
  131. if (!buffer_exists(buffer)) {
  132. return 0;
  133. } else {
  134. return buffer.use_count();
  135. }
  136. }
  137. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
  138. if (!buffer_exists(buffer)) {
  139. return nullptr;
  140. } else {
  141. return buffer->data;
  142. }
  143. }
  144. void buffer_setBytes(const Buffer &buffer, uint8_t value) {
  145. if (!buffer_exists(buffer)) {
  146. throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
  147. } else if (buffer->bufferSize > 0) {
  148. memset(buffer->data, value, buffer->bufferSize);
  149. }
  150. }
  151. }