Serenity Operating System
at portability 127 lines 3.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, 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 27#ifdef __serenity__ 28 29#include <AK/SharedBuffer.h> 30#include <AK/kmalloc.h> 31#include <Kernel/Syscall.h> 32#include <stdio.h> 33#include <unistd.h> 34 35namespace AK { 36 37RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size) 38{ 39 void* data; 40 int shared_buffer_id = create_shared_buffer(size, &data); 41 if (shared_buffer_id < 0) { 42 perror("create_shared_buffer"); 43 return nullptr; 44 } 45 return adopt(*new SharedBuffer(shared_buffer_id, size, data)); 46} 47 48bool SharedBuffer::share_with(pid_t peer) 49{ 50 int ret = share_buffer_with(shared_buffer_id(), peer); 51 if (ret < 0) { 52 perror("share_buffer_with"); 53 return false; 54 } 55 return true; 56} 57 58bool SharedBuffer::share_globally() 59{ 60 int ret = share_buffer_globally(shared_buffer_id()); 61 if (ret < 0) { 62 perror("share_buffer_globally"); 63 return false; 64 } 65 return true; 66} 67 68RefPtr<SharedBuffer> SharedBuffer::create_from_shared_buffer_id(int shared_buffer_id) 69{ 70 void* data = get_shared_buffer(shared_buffer_id); 71 if (data == (void*)-1) { 72 perror("get_shared_buffer"); 73 return nullptr; 74 } 75 int size = get_shared_buffer_size(shared_buffer_id); 76 if (size < 0) { 77 perror("get_shared_buffer_size"); 78 return nullptr; 79 } 80 return adopt(*new SharedBuffer(shared_buffer_id, size, data)); 81} 82 83SharedBuffer::SharedBuffer(int shared_buffer_id, int size, void* data) 84 : m_shared_buffer_id(shared_buffer_id) 85 , m_size(size) 86 , m_data(data) 87{ 88} 89 90SharedBuffer::~SharedBuffer() 91{ 92 if (m_shared_buffer_id >= 0) { 93 int rc = release_shared_buffer(m_shared_buffer_id); 94 if (rc < 0) { 95 perror("release_shared_buffer"); 96 } 97 } 98} 99 100void SharedBuffer::seal() 101{ 102 int rc = seal_shared_buffer(m_shared_buffer_id); 103 if (rc < 0) { 104 perror("seal_shared_buffer"); 105 ASSERT_NOT_REACHED(); 106 } 107} 108 109void SharedBuffer::set_volatile() 110{ 111 u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, true); 112 ASSERT(rc == 0); 113} 114 115bool SharedBuffer::set_nonvolatile() 116{ 117 u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, false); 118 if (rc == 0) 119 return true; 120 if (rc == 1) 121 return false; 122 ASSERT_NOT_REACHED(); 123} 124 125} 126 127#endif