bufferAPI.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <cstdlib>
  24. #include "bufferAPI.h"
  25. #include "../math/scalar.h"
  26. namespace dsr {
  27. // Hidden type
  28. class BufferImpl {
  29. public:
  30. // A Buffer cannot have a name, because each String contains a buffer
  31. const int64_t size; // The actually used data
  32. const int64_t bufferSize; // The accessible data
  33. uint8_t *data;
  34. std::function<void(uint8_t *)> destructor;
  35. public:
  36. explicit BufferImpl(int64_t newSize);
  37. BufferImpl(int64_t newSize, uint8_t *newData);
  38. ~BufferImpl();
  39. public:
  40. // No implicit copies, only pass using the Buffer handle
  41. BufferImpl(const BufferImpl&) = delete;
  42. BufferImpl& operator=(const BufferImpl&) = delete;
  43. };
  44. // Internal methods
  45. // buffer_alignment must be a power of two for buffer_alignment_mask to work
  46. static const int buffer_alignment = 16;
  47. static const uintptr_t buffer_alignment_mask = ~((uintptr_t)(buffer_alignment - 1));
  48. // If this C++ version additionally includes the C11 features then we may assume that aligned_alloc is available
  49. #ifdef _ISOC11_SOURCE
  50. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  51. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  52. uint8_t* allocation = (uint8_t*)aligned_alloc(buffer_alignment, newSize);
  53. targetDestructor = [](uint8_t *data) { free(data); };
  54. return allocation;
  55. }
  56. #else
  57. // Allocate data of newSize and write the corresponding destructor function to targetDestructor
  58. static uint8_t* buffer_allocate(int64_t newSize, std::function<void(uint8_t *)>& targetDestructor) {
  59. uintptr_t padding = buffer_alignment - 1;
  60. uint8_t* allocation = (uint8_t*)malloc(newSize + padding);
  61. uint8_t* aligned = (uint8_t*)(((uintptr_t)allocation + padding) & buffer_alignment_mask);
  62. uintptr_t offset = allocation - aligned;
  63. targetDestructor = [offset](uint8_t *data) { free(data - offset); };
  64. return aligned;
  65. }
  66. #endif
  67. BufferImpl::BufferImpl(int64_t newSize) :
  68. size(newSize),
  69. bufferSize(roundUp(newSize, buffer_alignment)) {
  70. this->data = buffer_allocate(this->bufferSize, this->destructor);
  71. memset(this->data, 0, this->bufferSize);
  72. }
  73. BufferImpl::BufferImpl(int64_t newSize, uint8_t *newData)
  74. : size(newSize), bufferSize(newSize), data(newData), destructor([](uint8_t *data) { free(data); }) {}
  75. BufferImpl::~BufferImpl() {
  76. this->destructor(this->data);
  77. }
  78. // API
  79. Buffer buffer_clone(Buffer buffer) {
  80. Buffer newBuffer = std::make_shared<BufferImpl>(buffer->size);
  81. memcpy(newBuffer->data, buffer->data, buffer->size);
  82. return newBuffer;
  83. }
  84. Buffer buffer_create(int64_t newSize) {
  85. return std::make_shared<BufferImpl>(newSize);
  86. }
  87. Buffer buffer_create(int64_t newSize, uint8_t *newData) {
  88. return std::make_shared<BufferImpl>(newSize, newData);
  89. }
  90. void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor) {
  91. buffer->destructor = newDestructor;
  92. }
  93. int64_t buffer_getSize(Buffer buffer) {
  94. return buffer->size;
  95. }
  96. uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer) {
  97. return buffer->data;
  98. }
  99. void buffer_setBytes(Buffer buffer, uint8_t value) {
  100. memset(buffer->data, value, buffer->bufferSize);
  101. }
  102. }