]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Allow to steal variables from the mempool
authorVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 6 Dec 2023 14:43:41 +0000 (14:43 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Wed, 6 Dec 2023 14:43:41 +0000 (14:43 +0000)
src/libutil/mem_pool.c
src/libutil/mem_pool.h

index 39bc79f255c9ecfa6687a4814524c1c24e4a295a..119ade33d845cd5e73e99466000a00d0a44f8500 100644 (file)
@@ -1226,6 +1226,31 @@ rspamd_mempool_get_variable(rspamd_mempool_t *pool, const gchar *name)
        return NULL;
 }
 
+gpointer
+rspamd_mempool_steal_variable(rspamd_mempool_t *pool, const gchar *name)
+{
+       if (pool->priv->variables == NULL) {
+               return NULL;
+       }
+
+       khiter_t it;
+       gint hv = rspamd_cryptobox_fast_hash(name, strlen(name),
+                                                                                RSPAMD_MEMPOOL_VARS_HASH_SEED);
+
+       it = kh_get(rspamd_mempool_vars_hash, pool->priv->variables, hv);
+
+       if (it != kh_end(pool->priv->variables)) {
+               struct rspamd_mempool_variable *pvar;
+
+               pvar = &kh_val(pool->priv->variables, it);
+               kh_del(rspamd_mempool_vars_hash, pool->priv->variables, it);
+
+               return pvar->data;
+       }
+
+       return NULL;
+}
+
 void rspamd_mempool_remove_variable(rspamd_mempool_t *pool, const gchar *name)
 {
        if (pool->priv->variables != NULL) {
index aea5f4ae8260a9e0dafa2e7b2c47c2932792ed65..de0fea14b7d609bedb2d5074187bb3c670d0f82e 100644 (file)
@@ -389,6 +389,14 @@ void rspamd_mempool_set_variable(rspamd_mempool_t *pool,
  */
 gpointer rspamd_mempool_get_variable(rspamd_mempool_t *pool,
                                                                         const gchar *name);
+/**
+ * Steal memory pool variable
+ * @param pool
+ * @param name
+ * @return
+ */
+gpointer rspamd_mempool_steal_variable(rspamd_mempool_t *pool,
+                                                                          const gchar *name);
 
 /**
  * Removes variable from memory pool