1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 11:25:08 +00:00

Lagom: Add LibGemini, LibGfx

They are dependencies of LibWeb and might be useful for
running test-web on GitHub actions one day.
This commit is contained in:
Nico Weber 2020-07-23 14:34:53 -04:00 committed by Andreas Kling
parent c4d9d5cc54
commit 3f45e9ab1e
8 changed files with 179 additions and 22 deletions

View file

@ -24,53 +24,137 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __serenity__
#if defined(__serenity__) || defined(__linux__)
#include <AK/SharedBuffer.h>
#include <AK/kmalloc.h>
#include <Kernel/API/Syscall.h>
#include <stdio.h>
#include <serenity.h>
# include <AK/SharedBuffer.h>
# include <AK/kmalloc.h>
# include <Kernel/API/Syscall.h>
# include <stdio.h>
# if defined(__serenity__)
# include <serenity.h>
# elif defined(__linux__)
# include <AK/String.h>
# include <fcntl.h>
# include <sys/mman.h>
static String shbuf_shm_name(int shbuf_id)
{
return String::format("/serenity-shm:%d", shbuf_id);
}
# endif
namespace AK {
RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
{
# if defined(__serenity__)
void* data;
int shbuf_id = shbuf_create(size, &data);
if (shbuf_id < 0) {
perror("shbuf_create");
return nullptr;
}
# elif defined(__linux__)
// Not atomic, so don't create shared buffers from many threads too hard under lagom.
static unsigned g_shm_id = 0;
int shbuf_id = (getpid() << 8) | (g_shm_id++);
int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd < 0) {
perror("shm_open");
return nullptr;
}
if (ftruncate(fd, size) < 0) {
perror("ftruncate");
return nullptr;
}
void* data = mmap(nullptr, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
return nullptr;
}
size_t* size_data = reinterpret_cast<size_t*>(data);
*size_data = size;
data = reinterpret_cast<u8*>(data) + sizeof(size_t);
if (close(fd) < 0) {
perror("close");
return nullptr;
}
# endif
return adopt(*new SharedBuffer(shbuf_id, size, data));
}
bool SharedBuffer::share_with(pid_t peer)
{
# if defined(__serenity__)
int ret = shbuf_allow_pid(shbuf_id(), peer);
if (ret < 0) {
perror("shbuf_allow_pid");
return false;
}
# else
(void)peer;
# endif
return true;
}
bool SharedBuffer::share_globally()
{
# if defined(__serenity__)
int ret = shbuf_allow_all(shbuf_id());
if (ret < 0) {
perror("shbuf_allow_all");
return false;
}
# endif
return true;
}
RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
{
# if defined(__serenity__)
size_t size = 0;
void* data = shbuf_get(shbuf_id, &size);
if (data == (void*)-1)
return nullptr;
# elif defined(__linux__)
int fd = shm_open(shbuf_shm_name(shbuf_id).characters(), O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
perror("shm_open");
return nullptr;
}
void* data = mmap(nullptr, sizeof(size_t), PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
return nullptr;
}
size_t* size_data = reinterpret_cast<size_t*>(data);
size_t size = *size_data;
if (munmap(data, sizeof(size_t)) < 0) {
perror("munmap");
return nullptr;
}
data = mmap(nullptr, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
return nullptr;
}
data = reinterpret_cast<u8*>(data) + sizeof(size_t);
if (close(fd) < 0) {
perror("close");
return nullptr;
}
# endif
return adopt(*new SharedBuffer(shbuf_id, size, data));
}
@ -84,36 +168,48 @@ SharedBuffer::SharedBuffer(int shbuf_id, int size, void* data)
SharedBuffer::~SharedBuffer()
{
if (m_shbuf_id >= 0) {
# if defined(__serenity__)
int rc = shbuf_release(m_shbuf_id);
if (rc < 0) {
perror("shbuf_release");
}
# elif defined(__linux__)
if (munmap(reinterpret_cast<u8*>(m_data) - sizeof(size_t), m_size + sizeof(size_t)) < 0)
perror("munmap");
if (shm_unlink(shbuf_shm_name(m_shbuf_id).characters()) < 0)
perror("unlink");
# endif
}
}
void SharedBuffer::seal()
{
# if defined(__serenity__)
int rc = shbuf_seal(m_shbuf_id);
if (rc < 0) {
perror("shbuf_seal");
ASSERT_NOT_REACHED();
}
# endif
}
void SharedBuffer::set_volatile()
{
# if defined(__serenity__)
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, true);
ASSERT(rc == 0);
# endif
}
bool SharedBuffer::set_nonvolatile()
{
# if defined(__serenity__)
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, false);
if (rc == 0)
return true;
if (rc == 1)
return false;
ASSERT_NOT_REACHED();
ASSERT(rc == 1);
# endif
return false;
}
}