[vlc-commits] Use copy/free paradigm rather than hold/release for message items

Rémi Denis-Courmont git at videolan.org
Wed May 11 18:54:34 CEST 2011


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed May 11 17:51:30 2011 +0300| [9e3c1a6d39b509e3672a170b54939f7825f555df] | committer: Rémi Denis-Courmont

Use copy/free paradigm rather than hold/release for message items

Message items are more often than not not rereferenced, and hardly ever
rereferenced more than once. Demand-copying will be faster in most
common circumstances (especially built-in console or logger).

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9e3c1a6d39b509e3672a170b54939f7825f555df
---

 include/vlc_messages.h               |   22 +++++++++++++++-------
 modules/gui/ncurses.c                |    6 +++---
 modules/gui/qt4/dialogs/messages.cpp |    8 ++++----
 src/control/log.c                    |    9 ++++-----
 src/misc/messages.c                  |   17 +----------------
 5 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/include/vlc_messages.h b/include/vlc_messages.h
index 7a954d2..7221414 100644
--- a/include/vlc_messages.h
+++ b/include/vlc_messages.h
@@ -53,8 +53,6 @@ typedef struct
     char *  psz_module;
     char *  psz_msg;                            /**< the message itself */
     char *  psz_header;                         /**< Additional header */
-
-    gc_object_t vlc_gc_data;
 } msg_item_t;
 
 /* Message types */
@@ -67,15 +65,25 @@ typedef struct
 /** debug messages */
 #define VLC_MSG_DBG   3
 
-static inline msg_item_t *msg_Hold (msg_item_t *msg)
+VLC_MALLOC VLC_USED
+static inline msg_item_t *msg_Copy (const msg_item_t *msg)
 {
-    vlc_hold (&msg->vlc_gc_data);
-    return msg;
+    msg_item_t *copy = (msg_item_t *)xmalloc (sizeof (*copy));
+    copy->i_type = msg->i_type;
+    copy->i_object_id = msg->i_object_id;
+    copy->psz_object_type = msg->psz_object_type;
+    copy->psz_module = strdup (msg->psz_module);
+    copy->psz_msg = strdup (msg->psz_msg);
+    copy->psz_header = strdup (msg->psz_header);
+    return copy;
 }
 
-static inline void msg_Release (msg_item_t *msg)
+static inline void msg_Free (msg_item_t *msg)
 {
-    vlc_release (&msg->vlc_gc_data);
+    free (msg->psz_module);
+    free (msg->psz_msg);
+    free (msg->psz_header);
+    free (msg);
 }
 
 /**
diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c
index 172a68d..da70182 100644
--- a/modules/gui/ncurses.c
+++ b/modules/gui/ncurses.c
@@ -1782,8 +1782,8 @@ static void MsgCallback(msg_cb_data_t *data, msg_item_t *msg, unsigned i)
     vlc_mutex_lock(&p_sys->msg_lock);
 
     if (p_sys->msgs[p_sys->i_msgs])
-        msg_Release(p_sys->msgs[p_sys->i_msgs]);
-    p_sys->msgs[p_sys->i_msgs++] = msg_Hold(msg);
+        msg_Free(p_sys->msgs[p_sys->i_msgs]);
+    p_sys->msgs[p_sys->i_msgs++] = msg_Copy(msg);
 
     if (p_sys->i_msgs == (sizeof p_sys->msgs / sizeof *p_sys->msgs))
         p_sys->i_msgs = 0;
@@ -1915,7 +1915,7 @@ static void Close(vlc_object_t *p_this)
     vlc_mutex_destroy(&p_sys->msg_lock);
     for(unsigned i = 0; i < sizeof p_sys->msgs / sizeof *p_sys->msgs; i++)
         if (p_sys->msgs[i])
-            msg_Release(p_sys->msgs[i]);
+            msg_Free(p_sys->msgs[i]);
 
     free(p_sys);
 }
diff --git a/modules/gui/qt4/dialogs/messages.cpp b/modules/gui/qt4/dialogs/messages.cpp
index f807d42..3315104 100644
--- a/modules/gui/qt4/dialogs/messages.cpp
+++ b/modules/gui/qt4/dialogs/messages.cpp
@@ -48,13 +48,13 @@ class MsgEvent : public QEvent
 {
 public:
     MsgEvent( msg_item_t *msg )
-        : QEvent( (QEvent::Type)MsgEvent_Type ), msg(msg)
+        : QEvent( (QEvent::Type)MsgEvent_Type )
     {
-        msg_Hold( msg );
+        this->msg = msg_Copy( msg );
     }
-    virtual ~MsgEvent()
+    virtual ~MsgEvent( void )
     {
-        msg_Release( msg );
+        msg_Free( msg );
     }
 
     msg_item_t *msg;
diff --git a/src/control/log.c b/src/control/log.c
index 5442870..c9b5ce6 100644
--- a/src/control/log.c
+++ b/src/control/log.c
@@ -53,12 +53,11 @@ static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
     if (p_item->i_type > d->verbosity)
         return;
 
+    msg_item_t *msg = msg_Copy (p_item);
+
     vlc_spin_lock (&d->lock);
     if (d->count < VLC_MSG_QSIZE)
-    {
-        d->items[d->count++] = p_item;
-        msg_Hold (p_item);
-    }
+        d->items[d->count++] = msg;
     vlc_spin_unlock (&d->lock);
     (void)i_drop;
 }
@@ -156,7 +155,7 @@ void libvlc_log_clear( libvlc_log_t *p_log )
     vlc_spin_unlock (&p_log->data.lock);
 
     for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
-         msg_Release (tab[i]);
+         msg_Free (tab[i]);
 }
 
 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
diff --git a/src/misc/messages.c b/src/misc/messages.c
index 02ab2b3..8ef3857 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -248,19 +248,6 @@ void msg_Generic( vlc_object_t *p_this, int i_type, const char *psz_module,
     va_end( args );
 }
 
-/**
- * Destroys a message.
- */
-static void msg_Free (gc_object_t *gc)
-{
-    msg_item_t *msg = vlc_priv (gc, msg_item_t);
-
-    free (msg->psz_module);
-    free (msg->psz_msg);
-    free (msg->psz_header);
-    free (msg);
-}
-
 #undef msg_GenericVa
 /**
  * Add a message to a queue
@@ -383,11 +370,9 @@ void msg_GenericVa (vlc_object_t *p_this, int i_type,
     if (p_item == NULL)
         return; /* Uho! */
 
-    vlc_gc_init (p_item, msg_Free);
     p_item->psz_module = p_item->psz_msg = p_item->psz_header = NULL;
 
 
-
     i_header_size = 0;
     p_obj = p_this;
     while( p_obj != NULL )
@@ -458,7 +443,7 @@ void msg_GenericVa (vlc_object_t *p_this, int i_type,
         sub->func (sub->opaque, p_item, 0);
     }
     vlc_rwlock_unlock (&bank->lock);
-    msg_Release (p_item);
+    msg_Free (p_item);
 }
 
 /*****************************************************************************



More information about the vlc-commits mailing list