Title: [226504] trunk/Source/WebCore
Revision
226504
Author
[email protected]
Date
2018-01-08 01:34:36 -0800 (Mon, 08 Jan 2018)

Log Message

[GStreamer] use FastMalloc-based GstAllocator
https://bugs.webkit.org/show_bug.cgi?id=165793

Reviewed by Philippe Normand.

Add GstAllocatorFastMalloc, a GstAllocator implementation using fast malloc. It's only used when fast malloc is
enabled and can be disabled using an environment variable for debugging purposes.

* platform/GStreamer.cmake:
* platform/graphics/gstreamer/GStreamerUtilities.cpp:
(WebCore::initializeGStreamer):
* platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp: Added.
(gstMemoryFastMallocNew):
(gstAllocatorFastMallocAlloc):
(gstAllocatorFastMallocFree):
(gstAllocatorFastMallocMemMap):
(gstAllocatorFastMallocMemUnmap):
(gstAllocatorFastMallocMemCopy):
(gstAllocatorFastMallocMemShare):
(gstAllocatorFastMallocMemIsSpan):
(gst_allocator_fast_malloc_class_init):
(gst_allocator_fast_malloc_init):
* platform/graphics/gstreamer/GstAllocatorFastMalloc.h: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (226503 => 226504)


--- trunk/Source/WebCore/ChangeLog	2018-01-08 09:22:49 UTC (rev 226503)
+++ trunk/Source/WebCore/ChangeLog	2018-01-08 09:34:36 UTC (rev 226504)
@@ -1,3 +1,29 @@
+2018-01-08  Zan Dobersek  <[email protected]> and Carlos Garcia Campos  <[email protected]>
+
+        [GStreamer] use FastMalloc-based GstAllocator
+        https://bugs.webkit.org/show_bug.cgi?id=165793
+
+        Reviewed by Philippe Normand.
+
+        Add GstAllocatorFastMalloc, a GstAllocator implementation using fast malloc. It's only used when fast malloc is
+        enabled and can be disabled using an environment variable for debugging purposes.
+
+        * platform/GStreamer.cmake:
+        * platform/graphics/gstreamer/GStreamerUtilities.cpp:
+        (WebCore::initializeGStreamer):
+        * platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp: Added.
+        (gstMemoryFastMallocNew):
+        (gstAllocatorFastMallocAlloc):
+        (gstAllocatorFastMallocFree):
+        (gstAllocatorFastMallocMemMap):
+        (gstAllocatorFastMallocMemUnmap):
+        (gstAllocatorFastMallocMemCopy):
+        (gstAllocatorFastMallocMemShare):
+        (gstAllocatorFastMallocMemIsSpan):
+        (gst_allocator_fast_malloc_class_init):
+        (gst_allocator_fast_malloc_init):
+        * platform/graphics/gstreamer/GstAllocatorFastMalloc.h: Added.
+
 2018-01-07  Ms2ger  <[email protected]>
 
         Implement createImageBitmap(ImageBitmap)

Modified: trunk/Source/WebCore/platform/GStreamer.cmake (226503 => 226504)


--- trunk/Source/WebCore/platform/GStreamer.cmake	2018-01-08 09:22:49 UTC (rev 226503)
+++ trunk/Source/WebCore/platform/GStreamer.cmake	2018-01-08 09:34:36 UTC (rev 226504)
@@ -9,6 +9,7 @@
         platform/graphics/gstreamer/AudioTrackPrivateGStreamer.cpp
         platform/graphics/gstreamer/GRefPtrGStreamer.cpp
         platform/graphics/gstreamer/GStreamerUtilities.cpp
+        platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp
         platform/graphics/gstreamer/InbandTextTrackPrivateGStreamer.cpp
         platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
         platform/graphics/gstreamer/MediaPlayerPrivateGStreamerBase.cpp

Modified: trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerUtilities.cpp (226503 => 226504)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerUtilities.cpp	2018-01-08 09:22:49 UTC (rev 226503)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GStreamerUtilities.cpp	2018-01-08 09:34:36 UTC (rev 226504)
@@ -24,6 +24,7 @@
 #include "GStreamerUtilities.h"
 
 #include "GRefPtrGStreamer.h"
+#include "GstAllocatorFastMalloc.h"
 #include "IntSize.h"
 
 #include <gst/audio/audio-info.h>
@@ -154,6 +155,12 @@
     bool gstInitialized = gst_init_check(nullptr, nullptr, &error.outPtr());
     ASSERT_WITH_MESSAGE(gstInitialized, "GStreamer initialization failed: %s", error ? error->message : "unknown error occurred");
 
+    if (isFastMallocEnabled()) {
+        const char* disableFastMalloc = getenv("WEBKIT_GST_DISABLE_FAST_MALLOC");
+        if (!disableFastMalloc || !strcmp(disableFastMalloc, "0"))
+            gst_allocator_set_default(GST_ALLOCATOR(g_object_new(gst_allocator_fast_malloc_get_type(), nullptr)));
+    }
+
 #if ENABLE(VIDEO_TRACK) && USE(GSTREAMER_MPEGTS)
     if (gstInitialized)
         gst_mpegts_initialize();

Added: trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp (0 => 226504)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp	                        (rev 0)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.cpp	2018-01-08 09:34:36 UTC (rev 226504)
@@ -0,0 +1,155 @@
+/*
+ *  Copyright (C) 2018 Igalia S.L
+ *  Copyright (C) 2018 Metrological Group B.V.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "config.h"
+#include "GstAllocatorFastMalloc.h"
+
+#include <gst/gst.h>
+#include <wtf/FastMalloc.h>
+
+typedef struct {
+    GstMemory base;
+
+    uint8_t* data;
+} GstMemoryFastMalloc;
+
+typedef struct {
+    GstAllocator parent;
+} GstAllocatorFastMalloc;
+
+typedef struct {
+    GstAllocatorClass parent;
+} GstAllocatorFastMallocClass;
+
+G_DEFINE_TYPE(GstAllocatorFastMalloc, gst_allocator_fast_malloc, GST_TYPE_ALLOCATOR)
+
+static GstMemoryFastMalloc* gstMemoryFastMallocNew(GstAllocator* allocator, gsize size, gsize alignment, gsize offset, gsize padding, GstMemoryFlags flags)
+{
+    // alignment should be a (power-of-two - 1).
+    alignment |= gst_memory_alignment;
+    ASSERT(!((alignment + 1) & alignment));
+
+    gsize headerSize = (sizeof(GstMemoryFastMalloc) + alignment) & ~alignment;
+    gsize allocationSize = offset + size + padding;
+    auto* memory = static_cast<GstMemoryFastMalloc*>(tryFastAlignedMalloc(alignment + 1, headerSize + allocationSize));
+    if (!memory)
+        return nullptr;
+
+    memory->data = "" + headerSize;
+
+    if (offset && (flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
+        std::memset(memory->data, 0, offset);
+    if (padding && (flags & GST_MEMORY_FLAG_ZERO_PADDED))
+        std::memset(memory->data + offset + size, 0, padding);
+
+    gst_memory_init(GST_MEMORY_CAST(memory), flags, allocator, nullptr, allocationSize, alignment, offset, size);
+
+    return memory;
+}
+
+static GstMemory* gstAllocatorFastMallocAlloc(GstAllocator* allocator, gsize size, GstAllocationParams* params)
+{
+    ASSERT(G_TYPE_CHECK_INSTANCE_TYPE(allocator, gst_allocator_fast_malloc_get_type()));
+
+    return GST_MEMORY_CAST(gstMemoryFastMallocNew(allocator, size, params->align, params->prefix, params->padding, params->flags));
+}
+
+static void gstAllocatorFastMallocFree(GstAllocator* allocator, GstMemory* memory)
+{
+#if !ASSERT_DISABLED
+    ASSERT(G_TYPE_CHECK_INSTANCE_TYPE(allocator, gst_allocator_fast_malloc_get_type()));
+#else
+    UNUSED_PARAM(allocator);
+#endif
+
+    fastAlignedFree(memory);
+}
+
+static gpointer gstAllocatorFastMallocMemMap(GstMemoryFastMalloc* memory, gsize, GstMapFlags)
+{
+    return memory->data;
+}
+
+static gboolean gstAllocatorFastMallocMemUnmap(GstMemoryFastMalloc*)
+{
+    return TRUE;
+}
+
+static GstMemoryFastMalloc* gstAllocatorFastMallocMemCopy(GstMemoryFastMalloc* memory, gssize offset, gsize size)
+{
+    if (size == static_cast<gsize>(-1))
+        size = memory->base.size > static_cast<gsize>(offset) ? memory->base.size - offset : 0;
+
+    auto* copy = gstMemoryFastMallocNew(memory->base.allocator, size, memory->base.align, 0, 0, static_cast<GstMemoryFlags>(0));
+    if (!copy)
+        return nullptr;
+
+    std::memcpy(copy->data, memory->data + memory->base.offset + offset, size);
+    return copy;
+}
+
+static GstMemoryFastMalloc* gstAllocatorFastMallocMemShare(GstMemoryFastMalloc* memory, gssize offset, gsize size)
+{
+    GstMemoryFastMalloc* sharedMemory;
+    if (!tryFastMalloc(sizeof(GstMemoryFastMalloc)).getValue(sharedMemory))
+        return nullptr;
+
+    sharedMemory->data = ""
+
+    if (size == static_cast<gsize>(-1))
+        size = memory->base.size - offset;
+
+    auto* parent = memory->base.parent ? memory->base.parent : GST_MEMORY_CAST(memory);
+    gst_memory_init(GST_MEMORY_CAST(sharedMemory),
+        static_cast<GstMemoryFlags>(GST_MINI_OBJECT_FLAGS(parent) | GST_MINI_OBJECT_FLAG_LOCK_READONLY),
+        memory->base.allocator, parent, memory->base.maxsize, memory->base.align,
+        memory->base.offset + offset, size);
+
+    return sharedMemory;
+}
+
+static gboolean gstAllocatorFastMallocMemIsSpan(GstMemoryFastMalloc* memory, GstMemoryFastMalloc* other, gsize* offset)
+{
+    if (offset) {
+        auto* parent = reinterpret_cast<GstMemoryFastMalloc*>(memory->base.parent);
+        ASSERT(parent);
+        *offset = memory->base.offset - parent->base.offset;
+    }
+
+    return memory->data + memory->base.offset + memory->base.size == other->data + other->base.offset;
+}
+
+static void gst_allocator_fast_malloc_class_init(GstAllocatorFastMallocClass* klass)
+{
+    auto* gstAllocatorClass = GST_ALLOCATOR_CLASS(klass);
+    gstAllocatorClass->alloc = gstAllocatorFastMallocAlloc;
+    gstAllocatorClass->free = gstAllocatorFastMallocFree;
+}
+
+static void gst_allocator_fast_malloc_init(GstAllocatorFastMalloc* allocator)
+{
+    auto* baseAllocator = GST_ALLOCATOR_CAST(allocator);
+
+    baseAllocator->mem_type = "FastMalloc";
+    baseAllocator->mem_map = reinterpret_cast<GstMemoryMapFunction>(gstAllocatorFastMallocMemMap);
+    baseAllocator->mem_unmap = reinterpret_cast<GstMemoryUnmapFunction>(gstAllocatorFastMallocMemUnmap);
+    baseAllocator->mem_copy = reinterpret_cast<GstMemoryCopyFunction>(gstAllocatorFastMallocMemCopy);
+    baseAllocator->mem_share = reinterpret_cast<GstMemoryShareFunction>(gstAllocatorFastMallocMemShare);
+    baseAllocator->mem_is_span = reinterpret_cast<GstMemoryIsSpanFunction>(gstAllocatorFastMallocMemIsSpan);
+}

Added: trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.h (0 => 226504)


--- trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.h	                        (rev 0)
+++ trunk/Source/WebCore/platform/graphics/gstreamer/GstAllocatorFastMalloc.h	2018-01-08 09:34:36 UTC (rev 226504)
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2018 Igalia S.L
+ *  Copyright (C) 2018 Metrological Group B.V.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+GType gst_allocator_fast_malloc_get_type();
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to