g_return_if_fail (GST_IS_BUFFER (buffer));
g_return_if_fail (info != NULL);
- /* we need to check for NULL, it is possible that we tried to map a buffer
- * without memory and we should be able to unmap that fine */
- if (G_LIKELY (info->memory)) {
- gst_memory_unmap (info->memory, info);
- gst_memory_unref (info->memory);
- }
+ _gst_buffer_map_info_clear ((GstBufferMapInfo *) info);
}
/**
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstBufferPool, gst_object_unref)
+/**
+ * GstBufferMapInfo: (skip):
+ *
+ * Alias for #GstMapInfo to be used with g_auto():
+ * ```c
+ * void my_func(GstBuffer *buf)
+ * {
+ * g_auto(GstBufferMapInfo) map = GST_MAP_INFO_INIT;
+ * if (!gst_buffer_map(buf, &map, GST_MAP_READWRITE))
+ * return;
+ * ...
+ * // No need to call gst_buffer_unmap()
+ * }
+ * ```
+ *
+ * Since: 1.22
+ */
+typedef GstMapInfo GstBufferMapInfo;
+
+static inline void _gst_buffer_map_info_clear(GstBufferMapInfo *info)
+{
+ /* we need to check for NULL, it is possible that we tried to map a buffer
+ * without memory and we should be able to unmap that fine */
+ if (G_LIKELY (info->memory)) {
+ gst_memory_unmap (info->memory, info);
+ gst_memory_unref (info->memory);
+ }
+}
+
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GstBufferMapInfo, _gst_buffer_map_info_clear)
+
G_END_DECLS
#endif /* __GST_BUFFER_H__ */
GST_END_TEST;
+GST_START_TEST (test_auto_unmap)
+{
+#ifdef g_auto
+ g_autoptr (GstBuffer) buf = NULL;
+ g_autoptr (GstMemory) mem = NULL;
+
+ buf = gst_buffer_new_memdup (ro_memory, sizeof (ro_memory));
+
+ {
+ g_auto (GstBufferMapInfo) map = GST_MAP_INFO_INIT;
+ fail_unless (gst_buffer_map (buf, &map, GST_MAP_READ));
+ mem = gst_memory_ref (map.memory);
+ /* mem should be reffed by buffer, map and us */
+ fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (mem), 3);
+ }
+
+ /* mem should have been unreffed by g_auto() */
+ fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (mem), 2);
+#endif
+}
+
+GST_END_TEST;
+
static Suite *
gst_buffer_suite (void)
{
tcase_add_test (tc_chain, test_writable_memory);
tcase_add_test (tc_chain, test_wrapped_bytes);
tcase_add_test (tc_chain, test_new_memdup);
+ tcase_add_test (tc_chain, test_auto_unmap);
return s;
}