Skip to content

Commit

Permalink
Kernel+LibC: Rename shared buffer syscalls to use a prefix
Browse files Browse the repository at this point in the history
This feels a lot more consistent and Unixy:

    create_shared_buffer()   => shbuf_create()
    share_buffer_with()      => shbuf_allow_pid()
    share_buffer_globally()  => shbuf_allow_all()
    get_shared_buffer()      => shbuf_get()
    release_shared_buffer()  => shbuf_release()
    seal_shared_buffer()     => shbuf_seal()
    get_shared_buffer_size() => shbuf_get_size()

Also, "shared_buffer_id" is shortened to "shbuf_id" all around.
  • Loading branch information
awesomekling committed Feb 28, 2020
1 parent 8460d02 commit f72e5bb
Show file tree
Hide file tree
Showing 36 changed files with 581 additions and 581 deletions.
46 changes: 23 additions & 23 deletions AK/SharedBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,84 +37,84 @@ namespace AK {
RefPtr<SharedBuffer> SharedBuffer::create_with_size(int size)
{
void* data;
int shared_buffer_id = create_shared_buffer(size, &data);
if (shared_buffer_id < 0) {
perror("create_shared_buffer");
int shbuf_id = shbuf_create(size, &data);
if (shbuf_id < 0) {
perror("shbuf_create");
return nullptr;
}
return adopt(*new SharedBuffer(shared_buffer_id, size, data));
return adopt(*new SharedBuffer(shbuf_id, size, data));
}

bool SharedBuffer::share_with(pid_t peer)
{
int ret = share_buffer_with(shared_buffer_id(), peer);
int ret = shbuf_allow_pid(shbuf_id(), peer);
if (ret < 0) {
perror("share_buffer_with");
perror("shbuf_allow_pid");
return false;
}
return true;
}

bool SharedBuffer::share_globally()
{
int ret = share_buffer_globally(shared_buffer_id());
int ret = shbuf_allow_all(shbuf_id());
if (ret < 0) {
perror("share_buffer_globally");
perror("shbuf_allow_all");
return false;
}
return true;
}

RefPtr<SharedBuffer> SharedBuffer::create_from_shared_buffer_id(int shared_buffer_id)
RefPtr<SharedBuffer> SharedBuffer::create_from_shbuf_id(int shbuf_id)
{
void* data = get_shared_buffer(shared_buffer_id);
void* data = shbuf_get(shbuf_id);
if (data == (void*)-1) {
perror("get_shared_buffer");
perror("shbuf_get");
return nullptr;
}
int size = get_shared_buffer_size(shared_buffer_id);
int size = shbuf_get_size(shbuf_id);
if (size < 0) {
perror("get_shared_buffer_size");
perror("shbuf_get_size");
return nullptr;
}
return adopt(*new SharedBuffer(shared_buffer_id, size, data));
return adopt(*new SharedBuffer(shbuf_id, size, data));
}

SharedBuffer::SharedBuffer(int shared_buffer_id, int size, void* data)
: m_shared_buffer_id(shared_buffer_id)
SharedBuffer::SharedBuffer(int shbuf_id, int size, void* data)
: m_shbuf_id(shbuf_id)
, m_size(size)
, m_data(data)
{
}

SharedBuffer::~SharedBuffer()
{
if (m_shared_buffer_id >= 0) {
int rc = release_shared_buffer(m_shared_buffer_id);
if (m_shbuf_id >= 0) {
int rc = shbuf_release(m_shbuf_id);
if (rc < 0) {
perror("release_shared_buffer");
perror("shbuf_release");
}
}
}

void SharedBuffer::seal()
{
int rc = seal_shared_buffer(m_shared_buffer_id);
int rc = shbuf_seal(m_shbuf_id);
if (rc < 0) {
perror("seal_shared_buffer");
perror("shbuf_seal");
ASSERT_NOT_REACHED();
}
}

void SharedBuffer::set_volatile()
{
u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, true);
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, true);
ASSERT(rc == 0);
}

bool SharedBuffer::set_nonvolatile()
{
u32 rc = syscall(SC_set_shared_buffer_volatile, m_shared_buffer_id, false);
u32 rc = syscall(SC_shbuf_set_volatile, m_shbuf_id, false);
if (rc == 0)
return true;
if (rc == 1)
Expand Down
8 changes: 4 additions & 4 deletions AK/SharedBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ namespace AK {
class SharedBuffer : public RefCounted<SharedBuffer> {
public:
static RefPtr<SharedBuffer> create_with_size(int);
static RefPtr<SharedBuffer> create_from_shared_buffer_id(int);
static RefPtr<SharedBuffer> create_from_shbuf_id(int);
~SharedBuffer();

bool share_globally();
bool share_with(pid_t);
int shared_buffer_id() const { return m_shared_buffer_id; }
int shbuf_id() const { return m_shbuf_id; }
void seal();
int size() const { return m_size; }
void* data() { return m_data; }
Expand All @@ -50,9 +50,9 @@ class SharedBuffer : public RefCounted<SharedBuffer> {
[[nodiscard]] bool set_nonvolatile();

private:
SharedBuffer(int shared_buffer_id, int size, void*);
SharedBuffer(int shbuf_id, int size, void*);

int m_shared_buffer_id { -1 };
int m_shbuf_id { -1 };
int m_size { 0 };
void* m_data;
};
Expand Down
4 changes: 2 additions & 2 deletions Applications/SoundPlayer/PlaybackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ void PlaybackManager::remove_dead_buffers()
int id = m_connection->get_playing_buffer();
int current_id = -1;
if (m_current_buffer)
current_id = m_current_buffer->shared_buffer_id();
current_id = m_current_buffer->shbuf_id();

if (id >= 0 && id != current_id) {
while (!m_buffers.is_empty()) {
--m_next_ptr;
auto buffer = m_buffers.take_first();

if (buffer->shared_buffer_id() == id) {
if (buffer->shbuf_id() == id) {
m_current_buffer = buffer;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/ProcessModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ GUI::Variant ProcessModel::data(const GUI::ModelIndex& index, Role role) const
switch (index.column()) {
case Column::Icon:
if (thread.current_state.icon_id != -1) {
auto icon_buffer = SharedBuffer::create_from_shared_buffer_id(thread.current_state.icon_id);
auto icon_buffer = SharedBuffer::create_from_shbuf_id(thread.current_state.icon_id);
if (icon_buffer) {
auto icon_bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *icon_buffer, { 16, 16 });
if (icon_bitmap)
Expand Down
2 changes: 1 addition & 1 deletion Applications/Taskbar/TaskbarWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void TaskbarWindow::wm_event(GUI::WMEvent& event)
changed_event.icon_buffer_id());
#endif
if (auto* window = WindowList::the().window(identifier)) {
auto buffer = SharedBuffer::create_from_shared_buffer_id(changed_event.icon_buffer_id());
auto buffer = SharedBuffer::create_from_shbuf_id(changed_event.icon_buffer_id());
ASSERT(buffer);
window->button()->set_icon(Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *buffer, changed_event.icon_size()));
}
Expand Down
27 changes: 0 additions & 27 deletions Base/usr/share/man/man2/create_shared_buffer.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
## Name

share\_buffer\_with - allow another process to map a shareable buffer
shbuf\_allow\_pid - allow another process to map a shareable buffer

## Synopsis
```**c++
#include <SharedBuffer.h>
int share_buffer_with(int shared_buffer_id, pid_t peer_pid);
int shbuf_allow_pid(int shbuf_id, pid_t peer_pid);
```

## Description

Gives the process with PID `peer_pid` permission to map the shareable buffer with ID `shared_buffer_id`.
Gives the process with PID `peer_pid` permission to map the shareable buffer with ID `shbuf_id`.

## Return value

On success, returns 0. Otherwise, returns -1 and `errno` is set.

## Errors

* `EINVAL`: `peer_pid` is invalid, or `shared_buffer_id` is not a valid ID.
* `EPERM`: The calling process does not have access to the buffer with `shared_buffer_id`.
* `EINVAL`: `peer_pid` is invalid, or `shbuf_id` is not a valid ID.
* `EPERM`: The calling process does not have access to the buffer with `shbuf_id`.
* `ESRCH`: No process with PID `peer_pid` is found.

## See also

* [`create_shared_buffer`(2)](create_shared_buffer.md)
* [`shbuf_create`(2)](shbuf_create.md)
27 changes: 27 additions & 0 deletions Base/usr/share/man/man2/shbuf_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Name

shbuf\_create - create a shareable memory buffer

## Synopsis
```**c++
#include <SharedBuffer.h>
int shbuf_create(int size, void** buffer);
```

## Description

Creates a new memory region that can be shared with other processes. The region is only accessible to the calling process by default.

## Return value

If a region is successfully created, `shbuf_create()` stores a pointer to the memory in `buffer` and returns a buffer ID. Otherwise, it returns -1 and sets `errno` to describe the error.

## Errors

* `EINVAL`: `size` is zero or negative.
* `EFAULT`: `buffer` is not a valid address.

## See also

* [`shbuf_allow_pid`(2)](shbuf_allow_pid.md)
Loading

0 comments on commit f72e5bb

Please sign in to comment.