static GstMemory * gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params) { GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator); char filename[1024]; static int init = 0; int fd; GstMemory *mem; GstMapInfo info; /* TODO: make use of the allocation params, if necessary */ /* allocate shm pool */ snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (), "wayland-shm", init++, "XXXXXX"); fd = g_mkstemp (filename); if (fd < 0) { GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename, strerror (errno)); return NULL; } if (ftruncate (fd, size) < 0) { GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno)); close (fd); return NULL; } mem = gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_KEEP_MAPPED); if (G_UNLIKELY (!mem)) { GST_ERROR_OBJECT (self, "GstFdMemory allocation failed"); close (fd); return NULL; } /* we need to map the memory in order to unlink the file without losing it */ if (!gst_memory_map (mem, &info, GST_MAP_READWRITE)) { GST_ERROR_OBJECT (self, "GstFdMemory map failed"); close (fd); return NULL; } /* unmap will not really munmap(), we just * need it to release the miniobject lock */ gst_memory_unmap (mem, &info); unlink (filename); return mem; }
static GstMemory * gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params) { GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator); char filename[1024]; static int init = 0; int fd; gpointer data; GstWlShmMemory *mem; /* TODO: make use of the allocation params, if necessary */ /* allocate shm pool */ snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (), "wayland-shm", init++, "XXXXXX"); fd = g_mkstemp (filename); if (fd < 0) { GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename, strerror (errno)); return NULL; } if (ftruncate (fd, size) < 0) { GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno)); close (fd); return NULL; } data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { GST_ERROR_OBJECT (self, "mmap failed: %s", strerror (errno)); close (fd); return NULL; } unlink (filename); mem = g_slice_new0 (GstWlShmMemory); gst_memory_init ((GstMemory *) mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL, size, 0, 0, size); mem->data = data; mem->fd = fd; return (GstMemory *) mem; }