bufferAPI.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 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. #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. #include "../base/Handle.h"
  31. // The types of buffers to consider when designing algorithms:
  32. // * Null handle suggesting that there is nothing, such as when loading a file failed.
  33. // Size does not exist, but is substituted with zero when asked.
  34. // buffer_exists(Buffer()) == false
  35. // buffer_dangerous_getUnsafeData(Buffer()) == nullptr
  36. // buffer_getSize(Buffer()) == 0
  37. // * Empty head, used when loading a file worked but the file itself contained no data.
  38. // Size equals zero, but stored in the head.
  39. // Empty buffer heads will be reused when cloning, because they do not share any side-effects
  40. // when there is no shared data allocation and replacing the destructor will be blocked.
  41. // buffer_exists(buffer_create(0)) == true
  42. // buffer_dangerous_getUnsafeData(buffer_create(0)) == nullptr
  43. // buffer_getSize(buffer_create(0)) == 0
  44. // * Buffer containing data, when the file contained data.
  45. // When bytes is greater than zero.
  46. // buffer_exists(buffer_create(bytes)) == true
  47. // buffer_dangerous_getUnsafeData(buffer_create(x)) == zeroedData
  48. // buffer_getSize(buffer_create(bytes)) == bytes
  49. namespace dsr {
  50. using Buffer = Handle<uint8_t>;
  51. // Allocate a Buffer without padding,
  52. // The newSize argument should not include any padding.
  53. // The memory is allocated in whole aligned blocks of DSR_MAXIMUM_ALIGNMENT and buffer_getSafeData padds out the SafePointer region to the maximum alignment.
  54. // Side-effect: Creates a new buffer containing newSize bytes.
  55. // Post-condition: Returns the new buffer, which is initialized to zeroes.
  56. Buffer buffer_create(intptr_t newSize);
  57. // Allocate a Buffer with padding.
  58. // The buffer always align the start with heap alignment, but this function makes sure that paddToAlignment does not exceed heap alignment.
  59. // Pre-condition: paddToAlignment <= heap_getHeapAlignment()
  60. Buffer buffer_create(intptr_t newSize, uintptr_t paddToAlignment);
  61. // Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
  62. // The destructor is not responsible for freeing the memory allocation itself, only calling destructors in the content.
  63. // Pre-condition: The buffer exists.
  64. void buffer_replaceDestructor(Buffer &buffer, const HeapDestructor& newDestructor);
  65. // Returns true iff buffer exists, even if it is empty without any data allocation.
  66. inline bool buffer_exists(const Buffer &buffer) {
  67. return buffer.isNotNull();
  68. }
  69. // Returns a clone of the buffer.
  70. // Giving an empty handle returns an empty handle.
  71. // If the old buffer's alignment exceeds DSR_DEFAULT_ALIGNMENT, the alignment will be inherited.
  72. // The resulting buffer will always be aligned by at least DSR_DEFAULT_ALIGNMENT, even if the old buffer had no alignment.
  73. Buffer buffer_clone(const Buffer &buffer);
  74. // Returns the buffer's size in bytes, as given when allocating it excluding allocation padding.
  75. // Returns zero if buffer doesn't exist or has no data allocated.
  76. intptr_t buffer_getSize(const Buffer &buffer);
  77. // Returns the number of reference counted handles to the buffer, or 0 if the buffer does not exist.
  78. intptr_t buffer_getUseCount(const Buffer &buffer);
  79. // Returns a raw pointer to the data.
  80. // An empty handle or buffer of length zero without data will return nullptr.
  81. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer);
  82. // A wrapper for getting a bound-checked pointer of the correct element type.
  83. // The name must be an ascii encoded constant literal.
  84. // Returns a safe null pointer if buffer does not exist or there is no data allocation.
  85. template <typename T>
  86. SafePointer<T> buffer_getSafeData(const Buffer &buffer, const char* name) { return buffer.getSafe<T>(name); }
  87. // Set all bytes to the same value.
  88. // Pre-condition: buffer exists, or else an exception is thrown to warn you.
  89. // If the buffer has a head but no data allocation, the command will be ignored because there are no bytes to set.
  90. void buffer_setBytes(const Buffer &buffer, uint8_t value);
  91. }
  92. #endif