Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use munmap() correctly #213

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/background_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,12 @@ void BackgroundClient::Self::draw_screen(SurfaceInfo& info, bool draws_crash) co
info.output->output);
}

if (info.buffer)
if (info.buffer && info.buffer_size != stride * height)
{
wl_buffer_destroy(info.buffer);
info.reset_buffer();
}

if (!info.buffer)
{
auto const shm_pool = make_shm_pool(stride * height, &info.content_area);

Expand All @@ -433,6 +434,7 @@ void BackgroundClient::Self::draw_screen(SurfaceInfo& info, bool draws_crash) co
0,
width, height, stride,
WL_SHM_FORMAT_ARGB8888);
info.buffer_size = stride * height;
}

auto buffer = static_cast<unsigned char*>(info.content_area);
Expand Down
25 changes: 14 additions & 11 deletions src/egfullscreenclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ egmde::FullscreenClient::SurfaceInfo::~SurfaceInfo()

void egmde::FullscreenClient::SurfaceInfo::clear_window()
{
if (buffer)
wl_buffer_destroy(buffer);
reset_buffer();

if (shell_surface)
wl_shell_surface_destroy(shell_surface);
Expand All @@ -128,11 +127,22 @@ void egmde::FullscreenClient::SurfaceInfo::clear_window()
wl_surface_destroy(surface);


buffer = nullptr;
shell_surface = nullptr;
surface = nullptr;
}

void egmde::FullscreenClient::SurfaceInfo::reset_buffer()
{
if (buffer)
{
wl_buffer_destroy(buffer);
if (munmap(content_area, buffer_size))
mir::log_error("munmap() failed in %s: %s", __PRETTY_FUNCTION__, strerror(errno));
buffer = nullptr;
buffer_size = 0;
}
}

void egmde::FullscreenClient::Output::done(void* data, struct wl_output* /*wl_output*/)
{
auto const output = static_cast<Output*>(data);
Expand Down Expand Up @@ -261,12 +271,6 @@ void egmde::FullscreenClient::on_output_changed(Output const* output)
auto const p = outputs.find(output);
if (p != end(outputs))
{
if (auto& buffer = p->second.buffer)
{
wl_buffer_destroy(buffer);
buffer = nullptr;
}

draw_screen(p->second, should_draw_crash());
}

Expand Down Expand Up @@ -397,10 +401,9 @@ auto egmde::FullscreenClient::make_shm_pool(size_t size, void** data) const

return {
wl_shm_create_pool(shm, fd, size),
[size](auto* shm)
[](auto* shm)
{
wl_shm_pool_destroy(shm);
munmap(shm, size);
}};
}

Expand Down
2 changes: 2 additions & 0 deletions src/egfullscreenclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class FullscreenClient
~SurfaceInfo();

void clear_window();
void reset_buffer();

// Screen description
Output const* output;
Expand All @@ -140,6 +141,7 @@ class FullscreenClient
wl_surface* surface = nullptr;
wl_shell_surface* shell_surface = nullptr;
wl_buffer* buffer = nullptr;
size_t buffer_size = 0;
};

virtual void draw_screen(SurfaceInfo& info, bool draws_crash) const = 0;
Expand Down
Loading