bufferAPI.cpp 5.2 KB

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