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#pragma once
28
29#include <AK/OwnPtr.h>
30#include <AK/WeakPtr.h>
31#include <Kernel/VM/MemoryManager.h>
32#include <Kernel/VM/PurgeableVMObject.h>
33
34namespace Kernel {
35
36class SharedBuffer {
37private:
38 struct Reference {
39 Reference(pid_t pid)
40 : pid(pid)
41 {
42 }
43
44 pid_t pid;
45 unsigned count { 0 };
46 WeakPtr<Region> region;
47 };
48
49public:
50 SharedBuffer(int id, int size)
51 : m_shbuf_id(id)
52 , m_vmobject(PurgeableVMObject::create_with_size(size))
53 {
54#ifdef SHARED_BUFFER_DEBUG
55 dbg() << "Created shared buffer " << m_shbuf_id << " of size " << size;
56#endif
57 }
58
59 ~SharedBuffer()
60 {
61#ifdef SHARED_BUFFER_DEBUG
62 dbg() << "Destroyed shared buffer " << m_shbuf_id << " of size " << size();
63#endif
64 }
65
66 void sanity_check(const char* what);
67 bool is_shared_with(pid_t peer_pid);
68 void* ref_for_process_and_get_address(Process& process);
69 void share_with(pid_t peer_pid);
70 void share_globally() { m_global = true; }
71 void deref_for_process(Process& process);
72 void disown(pid_t pid);
73 size_t size() const { return m_vmobject->size(); }
74 void destroy_if_unused();
75 void seal();
76 PurgeableVMObject& vmobject() { return m_vmobject; }
77 const PurgeableVMObject& vmobject() const { return m_vmobject; }
78 int id() const { return m_shbuf_id; }
79
80 int m_shbuf_id { -1 };
81 bool m_writable { true };
82 bool m_global { false };
83 NonnullRefPtr<PurgeableVMObject> m_vmobject;
84 Vector<Reference, 2> m_refs;
85 unsigned m_total_refs { 0 };
86};
87
88Lockable<HashMap<int, NonnullOwnPtr<SharedBuffer>>>& shared_buffers();
89
90}