Serenity Operating System
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#if defined(__serenity__) || defined(__OpenBSD__)
28
29#include <AK/SharedBuffer.h>
30#include <AK/kmalloc.h>
31#ifdef __serenity__
32#include <Kernel/Syscall.h>
33#endif
34#include <stdio.h>
35#include <serenity.h>
36
37namespace AK {
38
39RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
40{
41 void* data;
42 int shbuf_id = shbuf_create(size, &data);
43 if (shbuf_id < 0) {
44 perror("shbuf_create");
45 return nullptr;
46 }
47 return adopt(*new SharedBuffer(shbuf_id, size, data));
48}
49
50bool SharedBuffer::share_with(pid_t peer)
51{
52 int ret = shbuf_allow_pid(shbuf_id(), peer);
53 if (ret < 0) {
54 perror("shbuf_allow_pid");
55 return false;
56 }
57 return true;
58}
59
60bool SharedBuffer::share_globally()
61{
62 int ret = shbuf_allow_all(shbuf_id());
63 if (ret < 0) {
64 perror("shbuf_allow_all");
65 return false;
66 }
67 return true;
68}
69
70RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
71{
72 size_t size = 0;
73 void* data = shbuf_get(shbuf_id, &size);
74 if (data == (void*)-1) {
75 perror("shbuf_get");
76 return nullptr;
77 }
78 return adopt(*new SharedBuffer(shbuf_id, size, data));
79}
80
81SharedBuffer::SharedBuffer(int shbuf_id, int size, void* data)
82 : m_shbuf_id(shbuf_id)
83 , m_size(size)
84 , m_data(data)
85{
86}
87
88SharedBuffer::~SharedBuffer()
89{
90 if (m_shbuf_id >= 0) {
91 int rc = shbuf_release(m_shbuf_id);
92 if (rc < 0) {
93 perror("shbuf_release");
94 }
95 }
96}
97
98void SharedBuffer::seal()
99{
100 int rc = shbuf_seal(m_shbuf_id);
101 if (rc < 0) {
102 perror("shbuf_seal");
103 ASSERT_NOT_REACHED();
104 }
105}
106
107void SharedBuffer::set_volatile()
108{
109#ifdef __serenity__
110 u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, true);
111 ASSERT(rc == 0);
112#endif
113}
114
115bool SharedBuffer::set_nonvolatile()
116{
117#ifdef __serenity__
118 u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, false);
119 if (rc == 0)
120 return true;
121 if (rc == 1)
122 return false;
123 ASSERT_NOT_REACHED();
124#else
125 return true;
126#endif
127}
128
129}
130
131#endif