bufferAPI.h 6.3 KB

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