bufferAPI.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 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. #ifndef DFPSR_API_BUFFER
  24. #define DFPSR_API_BUFFER
  25. #include <cstdint>
  26. #include <memory>
  27. #include <functional>
  28. #include "../base/SafePointer.h"
  29. // The types of buffer handles to consider when designing algorithms:
  30. // * Null handle suggesting that there is nothing, such as when loading a file failed.
  31. // Size does not exist, but is substituted with zero when asked.
  32. // buffer_exists(Buffer()) == false
  33. // buffer_dangerous_getUnsafeData(Buffer()) == nullptr
  34. // buffer_getSize(Buffer()) == 0
  35. // * Empty head, used when loading a file worked but the file itself contained no data.
  36. // Size equals zero, but stored in the head.
  37. // Empty buffer heads will be reused when cloning, because they do not share any side-effects
  38. // when there is no shared data allocation and replacing the destructor will be blocked.
  39. // buffer_exists(buffer_create(0)) == true
  40. // buffer_dangerous_getUnsafeData(buffer_create(0)) == nullptr
  41. // buffer_getSize(buffer_create(0)) == 0
  42. // * Buffer containing data, when the file contained data.
  43. // When bytes is greater than zero.
  44. // buffer_exists(buffer_create(bytes)) == true
  45. // buffer_dangerous_getUnsafeData(buffer_create(x)) == zeroedData
  46. // buffer_getSize(buffer_create(bytes)) == bytes
  47. namespace dsr {
  48. // A safer replacement for raw memory allocation when you don't need to resize the content.
  49. // Guarantees that internal addresses will not be invalidated during its lifetime.
  50. // Just remember to always keep a handle together with any pointers to the data to prevent the buffer from being freed.
  51. class BufferImpl;
  52. using Buffer = std::shared_ptr<BufferImpl>;
  53. // Pre-condition:
  54. // minimumAlignment must be a power of two, otherwise an exception will be thrown.
  55. // Side-effect: Creates a new buffer head regardless of newSize, but only allocates a zeroed data allocation if newSize > 0.
  56. // Post-condition: Returns a handle to the new buffer.
  57. // Creating a buffer without a size will only allocate the buffer's head referring to null data with size zero.
  58. // The minimumAlignment argument represents the number of bytes the allocation must be aligned with in memory when DSR_DEFAULT_ALIGNMENT is not enough.
  59. // Useful for when using a longer SIMD vector that only exists for a certain type (such as AVX without AVX2) or you use a signal processor.
  60. // Any minimumAlignment smaller than DSR_DEFAULT_ALIGNMENT will be ignored, because then DSR_DEFAULT_ALIGNMENT is larger than the minimum.
  61. Buffer buffer_create(int64_t newSize, int minimumAlignment = 1);
  62. // Pre-conditions:
  63. // newSize may not be larger than the size of newData in bytes.
  64. // Breaking this pre-condition may cause crashes, so only provide a newData pointer if you know what you are doing.
  65. // Side-effect: Creates a new buffer of newSize bytes inheriting ownership of newData.
  66. // If the given data cannot be freed as a C allocation, replaceDestructor must be called with the special destructor.
  67. // Post-condition: Returns a handle to the manually constructed buffer.
  68. Buffer buffer_create(int64_t newSize, uint8_t *newData);
  69. // Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
  70. // Pre-condition: The buffer exists.
  71. // If the buffer has a head but no data allocation, the command will be ignored because there is no allocation to delete.
  72. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor);
  73. // Returns true iff buffer exists, even if it is empty without any data allocation.
  74. inline bool buffer_exists(Buffer buffer) {
  75. return buffer.get() != nullptr;
  76. }
  77. // Returns a clone of the buffer.
  78. // Giving an empty handle returns an empty handle.
  79. // If the old buffer's alignment exceeds DSR_DEFAULT_ALIGNMENT, the alignment will be inherited.
  80. // The resulting buffer will always be aligned by at least DSR_DEFAULT_ALIGNMENT, even if the old buffer had no alignment.
  81. Buffer buffer_clone(const Buffer &buffer);
  82. // Returns the buffer's size in bytes, as given when allocating it excluding allocation padding.
  83. // Returns zero if buffer doesn't exist or has no data allocated.
  84. int64_t buffer_getSize(const Buffer &buffer);
  85. // Returns the number of reference counted handles to the buffer, or 0 if the buffer does not exist.
  86. int64_t buffer_getUseCount(const Buffer &buffer);
  87. // Returns a raw pointer to the data.
  88. // An empty handle or buffer of length zero without data will return nullptr.
  89. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer);
  90. // A wrapper for getting a bound-checked pointer of the correct element type.
  91. // Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
  92. // The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
  93. // Returns a safe null pointer if buffer does not exist or there is no data allocation.
  94. template <typename T>
  95. SafePointer<T> buffer_getSafeData(const Buffer &buffer, const char* name) {
  96. if (!buffer_exists(buffer)) {
  97. return SafePointer<T>();
  98. } else {
  99. uint8_t *data = buffer_dangerous_getUnsafeData(buffer);
  100. return SafePointer<T>(name, (T*)data, buffer_getSize(buffer), (T*)data);
  101. }
  102. }
  103. // Set all bytes to the same value.
  104. // Pre-condition: buffer exists, or else an exception is thrown to warn you.
  105. // If the buffer has a head but no data allocation, the command will be ignored because there are no bytes to set.
  106. void buffer_setBytes(const Buffer &buffer, uint8_t value);
  107. }
  108. #endif