[vlc-commits] freetype: add lru cache

Francois Cartegnie git at videolan.org
Mon Aug 17 23:43:13 CEST 2020


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Thu Jul 30 23:46:19 2020 +0200| [876360dfeb541d724ffad837f47eda21f0ea102b] | committer: Francois Cartegnie

freetype: add lru cache

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

 modules/text_renderer/freetype/lru.c | 123 +++++++++++++++++++++++++++++++++++
 modules/text_renderer/freetype/lru.h |  39 +++++++++++
 2 files changed, 162 insertions(+)

diff --git a/modules/text_renderer/freetype/lru.c b/modules/text_renderer/freetype/lru.c
new file mode 100644
index 0000000000..316a584755
--- /dev/null
+++ b/modules/text_renderer/freetype/lru.c
@@ -0,0 +1,123 @@
+/*****************************************************************************
+ * lru.c : Last recently used cache implementation
+ *****************************************************************************
+ * Copyright (C) 2020 - VideoLabs, VLC authors and VideoLAN
+ *
+ * This program 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.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation, Inc.,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_list.h>
+#include "lru.h"
+
+struct vlc_lru_entry
+{
+    char *psz_key;
+    void *value;
+    struct vlc_list node;
+};
+
+struct vlc_lru
+{
+    void (*releaseValue)(void *);
+    unsigned max;
+    vlc_dictionary_t dict;
+    struct vlc_list list;
+    struct vlc_lru_entry *last;
+};
+
+static void vlc_lru_releaseentry( void *value, void *priv )
+{
+    struct vlc_lru_entry *entry = value;
+    struct vlc_lru *lru = priv;
+    free(entry->psz_key);
+    lru->releaseValue(entry->value);
+    free(entry);
+}
+
+vlc_lru * vlc_lru_New( unsigned max, void(*releaseValue)(void *) )
+{
+    vlc_lru *lru = malloc(sizeof(*lru));
+    if( lru )
+    {
+        lru->max = max;
+        vlc_dictionary_init( &lru->dict, max );
+        vlc_list_init( &lru->list );
+        lru->releaseValue = releaseValue;
+        lru->last = NULL;
+    }
+    return lru;
+}
+
+void vlc_lru_Release( vlc_lru *lru )
+{
+    vlc_dictionary_clear( &lru->dict, vlc_lru_releaseentry, lru );
+    free( lru );
+}
+
+void * vlc_lru_Get( vlc_lru *lru, const char *psz_key )
+{
+    struct vlc_lru_entry *entry = vlc_dictionary_value_for_key( &lru->dict, psz_key );
+    if( entry )
+    {
+        if( !vlc_list_is_first( &entry->node, &lru->list ) )
+        {
+            if( vlc_list_is_last( &entry->node, &lru->list ) )
+            {
+                lru->last = vlc_list_entry(entry->node.prev, struct vlc_lru_entry, node);
+            }
+            vlc_list_remove( &entry->node );
+            vlc_list_add_after( &entry->node, &lru->list );
+        }
+        return entry->value;
+    }
+    return NULL;
+}
+
+void vlc_lru_Insert( vlc_lru *lru, const char *psz_key, void *value )
+{
+    struct vlc_lru_entry *entry = calloc(1, sizeof(*entry));
+    if(!entry)
+    {
+        lru->releaseValue(value);
+        return;
+    }
+    entry->psz_key = strdup( psz_key );
+    if(!entry->psz_key)
+    {
+        lru->releaseValue(value);
+        free(entry);
+        return;
+    }
+    entry->value = value;
+    vlc_list_init( &entry->node );
+
+    if( vlc_list_is_empty( &lru->list ) )
+        lru->last = entry;
+    vlc_dictionary_insert( &lru->dict, psz_key, entry );
+    vlc_list_add_after( &entry->node, &lru->list );
+
+    if( (unsigned)vlc_dictionary_keys_count(&lru->dict) >= lru->max )
+    {
+        struct vlc_lru_entry *toremove = lru->last;
+        lru->last = vlc_list_entry(toremove->node.prev, struct vlc_lru_entry, node);
+        vlc_list_remove(&toremove->node);
+        vlc_dictionary_remove_value_for_key(&lru->dict, toremove->psz_key, NULL, NULL);
+        vlc_lru_releaseentry(toremove, lru);
+    }
+}
diff --git a/modules/text_renderer/freetype/lru.h b/modules/text_renderer/freetype/lru.h
new file mode 100644
index 0000000000..3dcb72b66c
--- /dev/null
+++ b/modules/text_renderer/freetype/lru.h
@@ -0,0 +1,39 @@
+/*****************************************************************************
+ * lru.h : Last recently used cache implementation
+ *****************************************************************************
+ * Copyright (C) 2020 - VideoLabs, VLC authors and VideoLAN
+ *
+ * This program 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.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 program; if not, write to the Free Software Foundation, Inc.,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#ifndef LRU_H
+#define LRU_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct vlc_lru vlc_lru;
+
+vlc_lru * vlc_lru_New( unsigned max, void(*releaseValue)(void *) );
+void vlc_lru_Release( vlc_lru *lru );
+
+void * vlc_lru_Get( vlc_lru *lru, const char *psz_key );
+void   vlc_lru_Insert( vlc_lru *lru, const char *psz_key, void *value );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif



More information about the vlc-commits mailing list