diff options
author | Kacper Michajłow <kasper93@gmail.com> | 2023-11-24 02:01:10 +0100 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2023-11-28 10:46:16 +0100 |
commit | f2fdea9921d121cc2af20540db85c270ca90a3de (patch) | |
tree | 6275b7f9e7544f65a6b2b72b94fc2e4318483e45 /video/out/wayland_common.c | |
parent | 4e84ea22d770c5fc1c6d5d64be01a74f693daae7 (diff) | |
download | mpv-f2fdea9921d121cc2af20540db85c270ca90a3de.tar.bz2 mpv-f2fdea9921d121cc2af20540db85c270ca90a3de.tar.xz |
wayland: simplify reading data
- read directly to bstr
- use talloc for OOM checks
- don't parent temporary allocation
- don't check for NULL buffer after already writting to it
Diffstat (limited to 'video/out/wayland_common.c')
-rw-r--r-- | video/out/wayland_common.c | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/video/out/wayland_common.c b/video/out/wayland_common.c index 589135f45f..3aa535fc33 100644 --- a/video/out/wayland_common.c +++ b/video/out/wayland_common.c @@ -1410,28 +1410,23 @@ static void check_dnd_fd(struct vo_wayland_state *wl) return; if (fdp.revents & POLLIN) { - ptrdiff_t offset = 0; size_t data_read = 0; const size_t chunk_size = 1; - uint8_t *buffer = ta_zalloc_size(wl, chunk_size); - if (!buffer) - goto end; - - while ((data_read = read(wl->dnd_fd, buffer + offset, chunk_size)) > 0) { - offset += data_read; - buffer = ta_realloc_size(wl, buffer, offset + chunk_size); - memset(buffer + offset, 0, chunk_size); - if (!buffer) - goto end; + bstr file_list = { + .start = talloc_zero_size(NULL, chunk_size), + }; + + while ((data_read = read(wl->dnd_fd, file_list.start + file_list.len, chunk_size)) > 0) { + file_list.len += data_read; + file_list.start = talloc_realloc_size(NULL, file_list.start, file_list.len + chunk_size); + memset(file_list.start + file_list.len, 0, chunk_size); } - MP_VERBOSE(wl, "Read %td bytes from the DND fd\n", offset); + MP_VERBOSE(wl, "Read %zu bytes from the DND fd\n", file_list.len); - struct bstr file_list = bstr0(buffer); mp_event_drop_mime_data(wl->vo->input_ctx, wl->dnd_mime_type, file_list, wl->dnd_action); - talloc_free(buffer); -end: + talloc_free(file_list.start); if (wl->dnd_mime_type) talloc_free(wl->dnd_mime_type); |