summaryrefslogtreecommitdiff
path: root/Kernel/SharedBuffer.h
blob: c2187f106800b1b605c425c107af7108a5f776c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once

#include <Kernel/VM/MemoryManager.h>
#include <AK/OwnPtr.h>

struct SharedBuffer {
private:
    struct Reference {
        Reference(pid_t pid)
            : pid(pid)
        {
        }

        pid_t pid;
        unsigned count { 1 };
        Region* region { nullptr };
    };
public:
    SharedBuffer(int id, int size)
        : m_shared_buffer_id(id)
        , m_vmo(VMObject::create_anonymous(size))
    {
#ifdef SHARED_BUFFER_DEBUG
        dbgprintf("Created shared buffer %d of size %d\n", m_shared_buffer_id, size);
#endif
    }

    ~SharedBuffer()
    {
#ifdef SHARED_BUFFER_DEBUG
        dbgprintf("Destroyed shared buffer %d of size %d\n", m_shared_buffer_id, size());
#endif
    }

    bool is_shared_with(pid_t peer_pid);
    void* get_address(Process& process);
    void share_with(pid_t peer_pid);
    void release(Process& process);
    void disown(pid_t pid);
    size_t size() const { return m_vmo->size(); }
    void destroy_if_unused();
    void seal();

    int m_shared_buffer_id { -1 };
    bool m_writable { true };
    NonnullRefPtr<VMObject> m_vmo;
    Vector<Reference, 2> m_refs;
};

Lockable<HashMap<int, OwnPtr<SharedBuffer>>>& shared_buffers();