bufferAPI.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 to 2020 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 "../math/scalar.h"
  27. #include "../base/text.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. explicit BufferImpl(int64_t newSize);
  39. BufferImpl(int64_t newSize, uint8_t *newData);
  40. ~BufferImpl();
  41. public:
  42. // No implicit copies, only pass using the Buffer handle
  43. BufferImpl(const BufferImpl&) = delete;
  44. BufferImpl& operator=(const BufferImpl&) = delete;
  45. };
  46. // Internal methods
  47. // buffer_alignment must be a power of two for buffer_alignment_mask to work
  48. static const int buffer_alignment = 16;
  49. static const uintptr_t buffer_alignment_mask = ~((uintptr_t)(buffer_alignment - 1));
  50. // If this C++ version additionally includes the C11 features then we may assume that aligned_alloc is available
  51. #ifdef _ISOC11_SOURCE
  52. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  53. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  54. uint8_t* allocation = (uint8_t*)aligned_alloc(buffer_alignment, newSize);
  55. targetDestructor = [](uint8_t *data) { free(data); };
  56. return allocation;
  57. }
  58. #else
  59. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  60. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  61. uintptr_t padding = buffer_alignment - 1;
  62. uint8_t* allocation = (uint8_t*)malloc(newSize + padding);
  63. uint8_t* aligned = (uint8_t*)(((uintptr_t)allocation + padding) & buffer_alignment_mask);
  64. uintptr_t offset = allocation - aligned;
  65. targetDestructor = [offset](uint8_t *data) { free(data - offset); };
  66. return aligned;
  67. }
  68. #endif
  69. BufferImpl::BufferImpl(int64_t newSize) :
  70. size(newSize),
  71. bufferSize(roundUp(newSize, buffer_alignment)) {
  72. this->data = buffer_allocate(this->bufferSize, this->destructor);
  73. memset(this->data, 0, this->bufferSize);
  74. }
  75. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  76. : size(newSize), bufferSize(newSize), data(newData), destructor([](uint8_t *data) { free(data); }) {}
  77. BufferImpl::~BufferImpl() {
  78. this->destructor(this->data);
  79. }
  80. // API
  81. Buffer buffer_clone(Buffer buffer) {
  82. if (!buffer_exists(buffer)) {
  83. return Buffer();
  84. } else {
  85. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size);
  86. memcpy(newBuffer->data, buffer->data, buffer->size);
  87. return newBuffer;
  88. }
  89. }
  90. Buffer buffer_create(int64_t newSize) {
  91. if (newSize <= 0) {
  92. throwError(U"buffer_create: Cannot create buffer of size ", newSize, U".\n");
  93. return Buffer();
  94. } else {
  95. return std::make_shared<BufferImpl>(newSize);
  96. }
  97. }
  98. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  99. if (newSize <= 0) {
  100. throwError(U"buffer_create: Cannot create buffer of size ", newSize, U" (with existing data).\n");
  101. return Buffer();
  102. } else {
  103. return std::make_shared<BufferImpl>(newSize, newData);
  104. }
  105. }
  106. void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor) {
  107. if (!buffer_exists(buffer)) {
  108. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  109. } else {
  110. buffer->destructor = newDestructor;
  111. }
  112. }
  113. int64_t buffer_getSize(Buffer buffer) {
  114. if (!buffer_exists(buffer)) {
  115. return 0;
  116. } else {
  117. return buffer->size;
  118. }
  119. }
  120. uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer) {
  121. if (!buffer_exists(buffer)) {
  122. return nullptr;
  123. } else {
  124. return buffer->data;
  125. }
  126. }
  127. void buffer_setBytes(Buffer buffer, uint8_t value) {
  128. if (!buffer_exists(buffer)) {
  129. throwError(U"buffer_setBytes: Cannot set bytes for a buffer that don't exist.\n");
  130. } else {
  131. memset(buffer->data, value, buffer->bufferSize);
  132. }
  133. }
  134. }