bufferAPI.cpp 3.5 KB

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