diff --git a/gst-libs/gst/allocators/gstfdmemory.c b/gst-libs/gst/allocators/gstfdmemory.c index 9efcf89d3d..398914c852 100644 --- a/gst-libs/gst/allocators/gstfdmemory.c +++ b/gst-libs/gst/allocators/gstfdmemory.c @@ -97,6 +97,12 @@ gst_fd_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags) if ((mem->mmapping_flags & prot) == prot) { ret = mem->data; mem->mmap_count++; + } else if ((mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED) + && mem->mmap_count == 0 + && mprotect (mem->data, gmem->maxsize, prot) == 0) { + ret = mem->data; + mem->mmapping_flags = prot; + mem->mmap_count++; } goto out; @@ -154,8 +160,12 @@ gst_fd_mem_unmap (GstMemory * gmem) if (gmem->parent) return gst_fd_mem_unmap (gmem->parent); - if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED) + if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED) { + g_mutex_lock (&mem->lock); + mem->mmap_count--; + g_mutex_unlock (&mem->lock); return; + } g_mutex_lock (&mem->lock); if (mem->data && !(--mem->mmap_count)) { diff --git a/tests/check/libs/allocators.c b/tests/check/libs/allocators.c index c96edf1613..36c4a3878f 100644 --- a/tests/check/libs/allocators.c +++ b/tests/check/libs/allocators.c @@ -25,8 +25,13 @@ #include #include +#include #include +#include #include +#include +#include +#include #define FILE_SIZE 4096 @@ -60,6 +65,38 @@ GST_START_TEST (test_dmabuf) GST_END_TEST; +GST_START_TEST (test_fdmem) +{ + GstAllocator *alloc; + GstMemory *mem; + GstMapInfo info; + int fd; + const char *data = "0123456789"; + + fd = open ("test.txt", O_RDWR | O_CREAT); + g_assert (write (fd, data, 10) == 10); + + alloc = gst_fd_allocator_new (); + g_assert (alloc); + mem = gst_fd_allocator_alloc (alloc, fd, 10, GST_FD_MEMORY_FLAG_KEEP_MAPPED); + + g_assert (gst_memory_map (mem, &info, GST_MAP_READ)); + g_assert (info.data[5] == '5'); + gst_memory_unmap (mem, &info); + g_assert (gst_memory_map (mem, &info, GST_MAP_WRITE)); + info.data[5] = 'X'; + gst_memory_unmap (mem, &info); + g_assert (gst_memory_map (mem, &info, GST_MAP_READ)); + g_assert (info.data[5] == 'X'); + gst_memory_unmap (mem, &info); + + gst_memory_unref (mem); + g_assert (remove ("test.txt") == 0); + gst_object_unref (alloc); +} + +GST_END_TEST; + static Suite * allocators_suite (void) { @@ -68,6 +105,7 @@ allocators_suite (void) suite_add_tcase (s, tc_chain); tcase_add_test (tc_chain, test_dmabuf); + tcase_add_test (tc_chain, test_fdmem); return s; }