bufferAPI.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. Buffer buffer_create(int64_t newSize);
  37. // Creates a new buffer of newSize bytes inheriting ownership of newData.
  38. // If the given data cannot be freed as a C allocation, replaceDestructor must be called with the special destructor.
  39. Buffer buffer_create(int64_t newSize, uint8_t *newData);
  40. // Sets the allocation's destructor, to be called when there are no more reference counted pointers to the buffer.
  41. void buffer_replaceDestructor(Buffer buffer, const std::function<void(uint8_t *)>& newDestructor);
  42. // Returns true iff the handle points to a buffer
  43. inline bool buffer_exists(Buffer buffer) {
  44. return buffer.get() != nullptr;
  45. }
  46. // Returns a clone of the buffer.
  47. Buffer buffer_clone(Buffer buffer);
  48. // Returns the buffer's size in bytes, as given when allocating it excluding allocation padding
  49. int64_t buffer_getSize(Buffer buffer);
  50. // Returns a raw pointer to the data.
  51. uint8_t* buffer_dangerous_getUnsafeData(Buffer buffer);
  52. // A wrapper for getting a bound-checked pointer of the correct element type.
  53. // Only cast to trivially packed types with power of two dimensions so that the compiler does not add padding.
  54. // The name must be an ansi encoded constant literal, because each String contains a Buffer which would cause a cyclic dependency.
  55. template <typename T>
  56. SafePointer<T> buffer_getSafeData(Buffer buffer, const char* name) {
  57. uint8_t *data = buffer_dangerous_getUnsafeData(buffer);
  58. return SafePointer<T>(name, (T*)data, buffer_getSize(buffer), (T*)data);
  59. }
  60. // Set all bytes to the same value
  61. void buffer_setBytes(Buffer buffer, uint8_t value);
  62. }
  63. #endif