AnonymousBuffer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <LibCore/AnonymousBuffer.h>
  27. #include <LibIPC/Decoder.h>
  28. #include <LibIPC/Encoder.h>
  29. #include <LibIPC/File.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <sys/mman.h>
  33. #if defined(__serenity__)
  34. # include <serenity.h>
  35. #endif
  36. #if defined(__linux__) && !defined(MFD_CLOEXEC)
  37. # include <linux/memfd.h>
  38. # include <sys/syscall.h>
  39. static int memfd_create(const char* name, unsigned int flags)
  40. {
  41. return syscall(SYS_memfd_create, name, flags);
  42. }
  43. #endif
  44. namespace Core {
  45. AnonymousBuffer AnonymousBuffer::create_with_size(size_t size)
  46. {
  47. int fd = -1;
  48. #if defined(__serenity__)
  49. fd = anon_create(round_up_to_power_of_two(size, PAGE_SIZE), O_CLOEXEC);
  50. if (fd < 0) {
  51. perror("anon_create");
  52. return {};
  53. }
  54. #elif defined(__linux__)
  55. fd = memfd_create("", MFD_CLOEXEC);
  56. if (fd < 0) {
  57. perror("memfd_create");
  58. return {};
  59. }
  60. if (ftruncate(fd, size) < 0) {
  61. close(fd);
  62. perror("ftruncate");
  63. return {};
  64. }
  65. #endif
  66. if (fd < 0)
  67. return {};
  68. return create_from_anon_fd(fd, size);
  69. }
  70. RefPtr<AnonymousBufferImpl> AnonymousBufferImpl::create(int fd, size_t size)
  71. {
  72. auto* data = mmap(nullptr, round_up_to_power_of_two(size, PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  73. if (data == MAP_FAILED) {
  74. perror("mmap");
  75. return {};
  76. }
  77. return adopt(*new AnonymousBufferImpl(fd, size, data));
  78. }
  79. AnonymousBufferImpl::~AnonymousBufferImpl()
  80. {
  81. if (m_fd != -1) {
  82. auto rc = close(m_fd);
  83. VERIFY(rc == 0);
  84. }
  85. auto rc = munmap(m_data, round_up_to_power_of_two(m_size, PAGE_SIZE));
  86. VERIFY(rc == 0);
  87. }
  88. AnonymousBuffer AnonymousBuffer::create_from_anon_fd(int fd, size_t size)
  89. {
  90. auto impl = AnonymousBufferImpl::create(fd, size);
  91. if (!impl)
  92. return {};
  93. return AnonymousBuffer(impl.release_nonnull());
  94. }
  95. AnonymousBuffer::AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl> impl)
  96. : m_impl(move(impl))
  97. {
  98. }
  99. AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data)
  100. : m_fd(fd)
  101. , m_size(size)
  102. , m_data(data)
  103. {
  104. }
  105. AnonymousBuffer::~AnonymousBuffer()
  106. {
  107. }
  108. }