From 52285ba801090d1fad15060d6b1a61460dcbdc36 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 7 Mar 2015 22:43:23 +0000 Subject: [PATCH] Allow storage of auxiliary userdata inside regexp structure. --- src/libutil/regexp.c | 36 ++++++++++++++++++++++++++++++++++++ src/libutil/regexp.h | 25 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 3b76d15e0..9151066f7 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -51,6 +51,7 @@ struct rspamd_regexp_s { pcre_extra *raw_extra; regexp_id_t id; ref_entry_t ref; + gpointer ud; gint flags; }; @@ -418,6 +419,22 @@ rspamd_regexp_unref (rspamd_regexp_t *re) REF_RELEASE (re); } +void +rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud) +{ + g_assert (re != NULL); + + re->ud = ud; +} + +gpointer +rspamd_regexp_get_ud (rspamd_regexp_t *re) +{ + g_assert (re != NULL); + + return re->ud; +} + static gboolean rspamd_regexp_equal (gconstpointer a, gconstpointer b) { @@ -499,6 +516,25 @@ rspamd_regexp_cache_create (struct rspamd_regexp_cache *cache, return res; } +void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache, + const gchar *pattern, + const gchar *flags, rspamd_regexp_t *re) +{ + g_assert (re != NULL); + g_assert (pattern != NULL); + + if (cache == NULL) { + cache = global_re_cache; + } + + g_assert (cache != NULL); + /* Generate custom id */ + rspamd_regexp_generate_id (pattern, flags, re->id); + + REF_RETAIN (re); + g_hash_table_insert (cache->tbl, re->id, re); +} + gboolean rspamd_regexp_cache_remove (struct rspamd_regexp_cache *cache, rspamd_regexp_t *re) diff --git a/src/libutil/regexp.h b/src/libutil/regexp.h index 41835af84..3c08de71a 100644 --- a/src/libutil/regexp.h +++ b/src/libutil/regexp.h @@ -75,6 +75,20 @@ rspamd_regexp_t* rspamd_regexp_ref (rspamd_regexp_t *re); */ void rspamd_regexp_unref (rspamd_regexp_t *re); +/** + * Set auxiliary userdata for the specified regexp + * @param re regexp object + * @param ud opaque pointer + */ +void rspamd_regexp_set_ud (rspamd_regexp_t *re, gpointer ud); + +/** + * Get userdata for a regexp object + * @param re regexp object + * @return opaque pointer + */ +gpointer rspamd_regexp_get_ud (rspamd_regexp_t *re); + /** * Get pattern for the specified regexp object * @param re @@ -99,6 +113,17 @@ rspamd_regexp_t* rspamd_regexp_cache_query (struct rspamd_regexp_cache* cache, const gchar *pattern, const gchar *flags); +/** + * Insert item to the cache using custom pattern and flags + * @param cache + * @param pattern + * @param flags + * @param re + */ +void rspamd_regexp_cache_insert (struct rspamd_regexp_cache* cache, + const gchar *pattern, + const gchar *flags, rspamd_regexp_t *re); + /** * Create or get cached regexp from the specified cache * @param cache regexp cache. if NULL, the superglobal cache is used (*not* thread-safe) -- 2.39.5