AnonymousBuffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Noncopyable.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/RefPtr.h>
  30. #include <AK/Types.h>
  31. #include <LibIPC/Forward.h>
  32. namespace Core {
  33. class AnonymousBufferImpl final : public RefCounted<AnonymousBufferImpl> {
  34. public:
  35. static RefPtr<AnonymousBufferImpl> create(int fd, size_t);
  36. ~AnonymousBufferImpl();
  37. int fd() const { return m_fd; }
  38. size_t size() const { return m_size; }
  39. void* data() { return m_data; }
  40. const void* data() const { return m_data; }
  41. private:
  42. AnonymousBufferImpl(int fd, size_t, void*);
  43. int m_fd { -1 };
  44. size_t m_size { 0 };
  45. void* m_data { nullptr };
  46. };
  47. class AnonymousBuffer {
  48. public:
  49. static AnonymousBuffer create_with_size(size_t);
  50. static AnonymousBuffer create_from_anon_fd(int fd, size_t);
  51. AnonymousBuffer() { }
  52. ~AnonymousBuffer();
  53. bool is_valid() const { return m_impl; }
  54. int fd() const { return m_impl ? m_impl->fd() : -1; }
  55. size_t size() const { return m_impl ? m_impl->size() : 0; }
  56. template<typename T>
  57. T* data()
  58. {
  59. static_assert(IsVoid<T>::value || is_trivial<T>());
  60. if (!m_impl)
  61. return nullptr;
  62. return (T*)m_impl->data();
  63. }
  64. template<typename T>
  65. const T* data() const
  66. {
  67. static_assert(IsVoid<T>::value || is_trivial<T>());
  68. if (!m_impl)
  69. return nullptr;
  70. return (const T*)m_impl->data();
  71. }
  72. private:
  73. explicit AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl>);
  74. RefPtr<AnonymousBufferImpl> m_impl;
  75. };
  76. }
  77. namespace IPC {
  78. bool encode(Encoder&, const Core::AnonymousBuffer&);
  79. bool decode(Decoder&, Core::AnonymousBuffer&);
  80. }