2
0

bufferAPI.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2019 to 2025 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/SafePointer.h"
  28. namespace dsr {
  29. Buffer buffer_create(intptr_t newSize) {
  30. if (newSize < 0) newSize = 0;
  31. // Allocate head and data.
  32. return handle_createArray<uint8_t>(AllocationInitialization::Zeroed, (uintptr_t)newSize);
  33. }
  34. Buffer buffer_create(intptr_t newSize, int paddToAlignment) {
  35. if (newSize < 0) newSize = 0;
  36. if (paddToAlignment > DSR_MAXIMUM_ALIGNMENT) {
  37. throwError(U"Maximum alignment exceeded when creating a buffer!\n");
  38. return Handle<uint8_t>();
  39. } else {
  40. return handle_createArray<uint8_t>(AllocationInitialization::Zeroed, memory_getPaddedSize((uintptr_t)newSize, paddToAlignment));
  41. }
  42. }
  43. void buffer_replaceDestructor(Buffer &buffer, const HeapDestructor& newDestructor) {
  44. if (!buffer_exists(buffer)) {
  45. throwError(U"buffer_replaceDestructor: Cannot replace destructor for a buffer that don't exist.\n");
  46. } else {
  47. heap_setAllocationDestructor(buffer.getUnsafe(), newDestructor);
  48. }
  49. }
  50. // TODO: Create clone and reallocation methods in heap.h to handle object lifetime in a reusable way.
  51. Buffer buffer_clone(const Buffer &buffer) {
  52. if (!buffer_exists(buffer)) {
  53. return Handle<uint8_t>();
  54. } else {
  55. uintptr_t size = buffer.getUsedSize();
  56. if (size == 0) {
  57. // Buffers of zero elements are reused with reference counting.
  58. return buffer;
  59. } else {
  60. // Allocate new memory without setting it to zero, before cloning data into it.
  61. Buffer result = handle_createArray<uint8_t>(AllocationInitialization::Uninitialized, size);
  62. SafePointer<const uint8_t> source = buffer_getSafeData<const uint8_t>(buffer, "Buffer cloning source");
  63. SafePointer<uint8_t> target = buffer_getSafeData<uint8_t>(result, "Buffer cloning target");
  64. safeMemoryCopy(target, source, size);
  65. return result;
  66. }
  67. }
  68. }
  69. intptr_t buffer_getSize(const Buffer &buffer) {
  70. return buffer.getUsedSize();
  71. }
  72. intptr_t buffer_getUseCount(const Buffer &buffer) {
  73. return buffer.getUseCount();
  74. }
  75. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer) {
  76. return buffer.getUnsafe();
  77. }
  78. void buffer_setBytes(const Buffer &buffer, uint8_t value) {
  79. if (buffer.isNull()) {
  80. throwError(U"buffer_setBytes: Can not set bytes for a buffer that does not exist.\n");
  81. } else {
  82. uintptr_t size = buffer.getUsedSize();
  83. if (size > 0) {
  84. SafePointer<uint8_t> target = buffer_getSafeData<uint8_t>(buffer, "Buffer set target");
  85. safeMemorySet(target, value, size);
  86. }
  87. }
  88. }
  89. }