bufferAPI.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2020 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 <stdint.h>
  26. #include <memory>
  27. #include <functional>
  28. #include "../base/SafePointer.h"
  29. namespace dsr {
  30. // A safer replacement for raw memory allocation when you don't need to resize the content.
  31. // Guarantees that internal addresses will not be invalidated during its lifetime.
  32. // Just remember to always keep a handle together with any pointers to the data to prevent the buffer from being freed.
  33. class BufferImpl;
  34. using Buffer = std::shared_ptr<BufferImpl>;
  35. // Creates a new buffer of newSize bytes.
  36. // Pre-condition: newSize > 0
  37. Buffer buffer_create(int64_t newSize);
  38. // Creates a new buffer of newSize bytes inheriting ownership of newData.
  39. // If the given data cannot be freed as a C allocation, replaceDestructor must be called with the special destructor.
  40. // Pre-condition: newSize > 0
  41. Buffer buffer_create(int64_t newSize, uint8_t *newData);
  42. // Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
  43. // Pre-condition: buffer exists
  44. void buffer_replaceDestructor(const Buffer &buffer, const std::function<void(uint8_t *)>& newDestructor);
  45. // Returns true iff buffer exists
  46. inline bool buffer_exists(Buffer buffer) {
  47. return buffer.get() != nullptr;
  48. }
  49. // Returns a clone of the buffer.
  50. // Giving an empty handle returns an empty handle.
  51. Buffer buffer_clone(const Buffer &buffer);
  52. // Returns the buffer's size in bytes, as given when allocating it excluding allocation padding.
  53. // Returns zero if buffer doesn't exist.
  54. int64_t buffer_getSize(const Buffer &buffer);
  55. // Returns the number of reference counted handles to the buffer, or 0 if the buffer does not exist.
  56. int64_t buffer_getUseCount(const Buffer &buffer);
  57. // Returns a raw pointer to the data.
  58. // An empty handle will return nullptr.
  59. uint8_t* buffer_dangerous_getUnsafeData(const Buffer &buffer);
  60. // A wrapper for getting a bound-checked pointer of the correct element type.
  61. // Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
  62. // The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
  63. // Returns a safe null pointer if buffer does not exist.
  64. template <typename T>
  65. SafePointer<T> buffer_getSafeData(const Buffer &buffer, const char* name) {
  66. if (!buffer_exists(buffer)) {
  67. return SafePointer<T>();
  68. } else {
  69. uint8_t *data = buffer_dangerous_getUnsafeData(buffer);
  70. return SafePointer<T>(name, (T*)data, buffer_getSize(buffer), (T*)data);
  71. }
  72. }
  73. // Set all bytes to the same value.
  74. // Pre-condition: buffer exists, or else an exception is thrown to warn you.
  75. void buffer_setBytes(const Buffer &buffer, uint8_t value);
  76. }
  77. #endif