examples: fix mapoffset in examples

The mapoffset should be using in the mmap call as the offset.

Mapping the whole memory before the offset and then ignoring the part
before it seems like it can work but it actually has some problems:

1. some drivers (v4l2) use the mapoffset to calculate the buffer to map
2. we waste resources for mapped but unused pages.

The problem with the mapoffset is that it needs to be page aligned and
that used to be a problem in the past. Nowadays PipeWire no longer set
the mapoffset for any of the memory that it allocates.
This commit is contained in:
Wim Taymans 2024-06-13 10:11:29 +02:00
parent eed7eb1556
commit 9592b0af2a
5 changed files with 18 additions and 19 deletions

View file

@ -190,11 +190,11 @@ static int on_source_ready(void *_data, int status)
sdata = datas[0].data;
if (datas[0].type == SPA_DATA_MemFd ||
datas[0].type == SPA_DATA_DmaBuf) {
map = mmap(NULL, datas[0].maxsize + datas[0].mapoffset, PROT_READ,
MAP_PRIVATE, datas[0].fd, 0);
map = mmap(NULL, datas[0].maxsize, PROT_READ,
MAP_PRIVATE, datas[0].fd, datas[0].mapoffset);
if (map == MAP_FAILED)
return -errno;
sdata = SPA_PTROFF(map, datas[0].mapoffset, uint8_t);
sdata = map;
} else if (datas[0].type == SPA_DATA_MemPtr) {
map = NULL;
sdata = datas[0].data;
@ -215,7 +215,7 @@ static int on_source_ready(void *_data, int status)
SDL_RenderPresent(data->renderer);
if (map)
munmap(map, datas[0].maxsize + datas[0].mapoffset);
munmap(map, datas[0].maxsize);
}
if ((res = spa_node_process(data->source)) < 0)

View file

@ -185,11 +185,11 @@ static int on_source_ready(void *_data, int status)
sdata = datas[0].data;
if (datas[0].type == SPA_DATA_MemFd ||
datas[0].type == SPA_DATA_DmaBuf) {
map = mmap(NULL, datas[0].maxsize + datas[0].mapoffset, PROT_READ,
MAP_PRIVATE, datas[0].fd, 0);
map = mmap(NULL, datas[0].maxsize, PROT_READ,
MAP_PRIVATE, datas[0].fd, datas[0].mapoffset);
if (map == MAP_FAILED)
return -errno;
sdata = SPA_PTROFF(map, datas[0].mapoffset, uint8_t);
sdata = map;
} else if (datas[0].type == SPA_DATA_MemPtr) {
map = NULL;
sdata = datas[0].data;
@ -210,7 +210,7 @@ static int on_source_ready(void *_data, int status)
SDL_RenderPresent(data->renderer);
if (map)
munmap(map, datas[0].maxsize + datas[0].mapoffset);
munmap(map, datas[0].maxsize);
}
if ((res = spa_node_process(data->source)) < 0)

View file

@ -369,9 +369,9 @@ static int do_render(struct spa_loop *loop, bool async, uint32_t seq,
if (buf->datas[0].type == SPA_DATA_MemFd ||
buf->datas[0].type == SPA_DATA_DmaBuf) {
map = mmap(NULL, buf->datas[0].maxsize + buf->datas[0].mapoffset, PROT_READ,
MAP_PRIVATE, buf->datas[0].fd, 0);
sdata = SPA_PTROFF(map, buf->datas[0].mapoffset, uint8_t);
map = mmap(NULL, buf->datas[0].maxsize, PROT_READ,
MAP_PRIVATE, buf->datas[0].fd, buf->datas[0].mapoffset);
sdata = map;
} else if (buf->datas[0].type == SPA_DATA_MemPtr) {
map = NULL;
sdata = buf->datas[0].data;
@ -413,7 +413,7 @@ static int do_render(struct spa_loop *loop, bool async, uint32_t seq,
SDL_RenderPresent(d->renderer);
if (map)
munmap(map, buf->datas[0].maxsize + buf->datas[0].mapoffset);
munmap(map, buf->datas[0].maxsize);
return 0;
}

View file

@ -319,14 +319,13 @@ static int impl_port_use_buffers(void *object,
}
else if (datas[0].type == SPA_DATA_MemFd ||
datas[0].type == SPA_DATA_DmaBuf) {
b->ptr = mmap(NULL, datas[0].maxsize + datas[0].mapoffset, PROT_WRITE,
MAP_SHARED, datas[0].fd, 0);
b->ptr = mmap(NULL, datas[0].maxsize, PROT_WRITE,
MAP_SHARED, datas[0].fd, datas[0].mapoffset);
if (b->ptr == MAP_FAILED) {
pw_log_error("failed to buffer mem");
return -errno;
}
b->ptr = SPA_PTROFF(b->ptr, datas[0].mapoffset, void);
b->mapped = true;
}
else {

View file

@ -269,9 +269,9 @@ static int do_render(struct spa_loop *loop, bool async, uint32_t seq,
if (buf->datas[0].type == SPA_DATA_MemFd ||
buf->datas[0].type == SPA_DATA_DmaBuf) {
map = mmap(NULL, buf->datas[0].maxsize + buf->datas[0].mapoffset, PROT_READ,
MAP_PRIVATE, buf->datas[0].fd, 0);
sdata = SPA_PTROFF(map, buf->datas[0].mapoffset, uint8_t);
map = mmap(NULL, buf->datas[0].maxsize, PROT_READ,
MAP_PRIVATE, buf->datas[0].fd, buf->datas[0].mapoffset);
sdata = map;
} else if (buf->datas[0].type == SPA_DATA_MemPtr) {
map = NULL;
sdata = buf->datas[0].data;
@ -299,7 +299,7 @@ static int do_render(struct spa_loop *loop, bool async, uint32_t seq,
SDL_RenderPresent(d->renderer);
if (map)
munmap(map, buf->datas[0].maxsize + buf->datas[0].mapoffset);
munmap(map, buf->datas[0].maxsize);
return 0;
}