mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 10:57:34 +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:
parent
02fcf3974e
commit
8ce7df73fb
2 changed files with 30 additions and 39 deletions
|
@ -24,39 +24,36 @@
|
||||||
* 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 <stdio.h>
|
||||||
|
|
||||||
# include <AK/SharedBuffer.h>
|
#if defined(__serenity__)
|
||||||
# include <AK/kmalloc.h>
|
|
||||||
# include <Kernel/API/Syscall.h>
|
# include <Kernel/API/Syscall.h>
|
||||||
# include <stdio.h>
|
# include <serenity.h>
|
||||||
|
#else
|
||||||
# if defined(__serenity__)
|
# include <AK/String.h>
|
||||||
# include <serenity.h>
|
# include <fcntl.h>
|
||||||
# elif defined(__linux__)
|
# include <sys/mman.h>
|
||||||
# include <AK/String.h>
|
|
||||||
# include <fcntl.h>
|
|
||||||
# include <sys/mman.h>
|
|
||||||
|
|
||||||
static String shbuf_shm_name(int shbuf_id)
|
static String shbuf_shm_name(int shbuf_id)
|
||||||
{
|
{
|
||||||
return String::formatted("/serenity-shm:{}", shbuf_id);
|
return String::formatted("/serenity-shm:{}", shbuf_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
# endif
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
|
RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
void* data;
|
void* data;
|
||||||
int shbuf_id = shbuf_create(size, &data);
|
int shbuf_id = shbuf_create(size, &data);
|
||||||
if (shbuf_id < 0) {
|
if (shbuf_id < 0) {
|
||||||
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;
|
||||||
|
|
||||||
|
@ -85,42 +82,42 @@ RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
|
||||||
perror("close");
|
perror("close");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
# endif
|
#endif
|
||||||
return adopt(*new SharedBuffer(shbuf_id, size, data));
|
return adopt(*new SharedBuffer(shbuf_id, size, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SharedBuffer::share_with([[maybe_unused]] pid_t peer)
|
bool SharedBuffer::share_with([[maybe_unused]] pid_t peer)
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
int ret = shbuf_allow_pid(shbuf_id(), peer);
|
int ret = shbuf_allow_pid(shbuf_id(), peer);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("shbuf_allow_pid");
|
perror("shbuf_allow_pid");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
# endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SharedBuffer::share_globally()
|
bool SharedBuffer::share_globally()
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
int ret = shbuf_allow_all(shbuf_id());
|
int ret = shbuf_allow_all(shbuf_id());
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
perror("shbuf_allow_all");
|
perror("shbuf_allow_all");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
# endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
|
RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
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");
|
||||||
|
@ -151,7 +148,7 @@ RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
|
||||||
perror("close");
|
perror("close");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
# endif
|
#endif
|
||||||
|
|
||||||
return adopt(*new SharedBuffer(shbuf_id, size, data));
|
return adopt(*new SharedBuffer(shbuf_id, size, data));
|
||||||
}
|
}
|
||||||
|
@ -166,50 +163,48 @@ SharedBuffer::SharedBuffer(int shbuf_id, int size, void* data)
|
||||||
SharedBuffer::~SharedBuffer()
|
SharedBuffer::~SharedBuffer()
|
||||||
{
|
{
|
||||||
if (m_shbuf_id >= 0) {
|
if (m_shbuf_id >= 0) {
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
int rc = shbuf_release(m_shbuf_id);
|
int rc = shbuf_release(m_shbuf_id);
|
||||||
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)
|
||||||
perror("unlink");
|
perror("unlink");
|
||||||
# endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SharedBuffer::seal()
|
void SharedBuffer::seal()
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
int rc = shbuf_seal(m_shbuf_id);
|
int rc = shbuf_seal(m_shbuf_id);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
perror("shbuf_seal");
|
perror("shbuf_seal");
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
# endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void SharedBuffer::set_volatile()
|
void SharedBuffer::set_volatile()
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, true);
|
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, true);
|
||||||
ASSERT(rc == 0);
|
ASSERT(rc == 0);
|
||||||
# endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SharedBuffer::set_nonvolatile()
|
bool SharedBuffer::set_nonvolatile()
|
||||||
{
|
{
|
||||||
# if defined(__serenity__)
|
#if defined(__serenity__)
|
||||||
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, false);
|
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, false);
|
||||||
if (rc == 0)
|
if (rc == 0)
|
||||||
return true;
|
return true;
|
||||||
ASSERT(rc == 1);
|
ASSERT(rc == 1);
|
||||||
# endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -26,10 +26,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if defined(__serenity__) || defined(__linux__)
|
#include <AK/RefCounted.h>
|
||||||
|
#include <AK/RefPtr.h>
|
||||||
# include <AK/RefCounted.h>
|
|
||||||
# include <AK/RefPtr.h>
|
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -73,5 +71,3 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
using AK::SharedBuffer;
|
using AK::SharedBuffer;
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue