AnonymousBuffer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <Kernel/API/Syscall.h>
  35. # include <serenity.h>
  36. #endif
  37. namespace Core {
  38. AnonymousBuffer AnonymousBuffer::create_with_size(size_t size)
  39. {
  40. int fd = -1;
  41. #if defined(__serenity__)
  42. fd = anon_create(round_up_to_power_of_two(size, PAGE_SIZE), O_CLOEXEC);
  43. if (fd < 0) {
  44. perror("anon_create");
  45. return {};
  46. }
  47. #elif defined(__linux__)
  48. fd = memfd_create("", MFD_CLOEXEC);
  49. if (fd < 0) {
  50. perror("memfd_create");
  51. return {};
  52. }
  53. if (ftruncate(fd, size) < 0) {
  54. perror("ftruncate");
  55. return {};
  56. }
  57. #endif
  58. if (fd < 0)
  59. return {};
  60. return create_from_anon_fd(fd, size);
  61. }
  62. RefPtr<AnonymousBufferImpl> AnonymousBufferImpl::create(int fd, size_t size)
  63. {
  64. auto* data = mmap(nullptr, round_up_to_power_of_two(size, PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
  65. if (data == MAP_FAILED) {
  66. perror("mmap");
  67. return {};
  68. }
  69. return adopt(*new AnonymousBufferImpl(fd, size, data));
  70. }
  71. AnonymousBufferImpl::~AnonymousBufferImpl()
  72. {
  73. if (m_fd != -1) {
  74. auto rc = close(m_fd);
  75. ASSERT(rc == 0);
  76. }
  77. auto rc = munmap(m_data, round_up_to_power_of_two(m_size, PAGE_SIZE));
  78. ASSERT(rc == 0);
  79. }
  80. AnonymousBuffer AnonymousBuffer::create_from_anon_fd(int fd, size_t size)
  81. {
  82. auto impl = AnonymousBufferImpl::create(fd, size);
  83. if (!impl)
  84. return {};
  85. return AnonymousBuffer(impl.release_nonnull());
  86. }
  87. AnonymousBuffer::AnonymousBuffer(NonnullRefPtr<AnonymousBufferImpl> impl)
  88. : m_impl(move(impl))
  89. {
  90. }
  91. AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data)
  92. : m_fd(fd)
  93. , m_size(size)
  94. , m_data(data)
  95. {
  96. }
  97. AnonymousBuffer::~AnonymousBuffer()
  98. {
  99. }
  100. }