1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:37:36 +00:00

AK: Enable AK::SharedBuffer for all platforms

A future patch could do some MacOS specific things for
set_volatile/set_nonvolatile. For now, swap out the defined(__linux__)
branches for simple not __serenity__ branches.
This commit is contained in:
Andrew Kaster 2020-12-27 19:09:59 -07:00 committed by Andreas Kling
parent 02fcf3974e
commit 8ce7df73fb
2 changed files with 30 additions and 39 deletions

View file

@ -24,16 +24,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#if defined(__serenity__) || defined(__linux__)
#include <AK/SharedBuffer.h> #include <AK/SharedBuffer.h>
# include <AK/kmalloc.h>
# include <Kernel/API/Syscall.h>
#include <stdio.h> #include <stdio.h>
#if defined(__serenity__) #if defined(__serenity__)
# include <Kernel/API/Syscall.h>
# include <serenity.h> # include <serenity.h>
# elif defined(__linux__) #else
# include <AK/String.h> # include <AK/String.h>
# include <fcntl.h> # include <fcntl.h>
# include <sys/mman.h> # include <sys/mman.h>
@ -56,7 +53,7 @@ RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
perror("shbuf_create"); perror("shbuf_create");
return nullptr; return nullptr;
} }
# elif defined(__linux__) #else
// Not atomic, so don't create shared buffers from many threads too hard under lagom. // Not atomic, so don't create shared buffers from many threads too hard under lagom.
static unsigned g_shm_id = 0; static unsigned g_shm_id = 0;
@ -120,7 +117,7 @@ RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
void* data = shbuf_get(shbuf_id, &size); void* data = shbuf_get(shbuf_id, &size);
if (data == (void*)-1) if (data == (void*)-1)
return nullptr; return nullptr;
# elif defined(__linux__) #else
int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR, S_IRUSR | S_IWUSR); int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) { if (fd < 0) {
perror("shm_open"); perror("shm_open");
@ -171,7 +168,7 @@ SharedBuffer::~SharedBuffer()
if (rc < 0) { if (rc < 0) {
perror("shbuf_release"); perror("shbuf_release");
} }
# elif defined(__linux__) #else
if (munmap(reinterpret_cast<u8*>(m_data) - sizeof(size_t), m_size + sizeof(size_t)) < 0) if (munmap(reinterpret_cast<u8*>(m_data) - sizeof(size_t), m_size + sizeof(size_t)) < 0)
perror("munmap"); perror("munmap");
if (shm_unlink(shbuf_shm_name(m_shbuf_id).characters()) < 0) if (shm_unlink(shbuf_shm_name(m_shbuf_id).characters()) < 0)
@ -211,5 +208,3 @@ bool SharedBuffer::set_nonvolatile()
} }
} }
#endif

View file

@ -26,8 +26,6 @@
#pragma once #pragma once
#if defined(__serenity__) || defined(__linux__)
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h> #include <AK/RefPtr.h>
@ -73,5 +71,3 @@ private:
} }
using AK::SharedBuffer; using AK::SharedBuffer;
#endif