commit 18c4390: [Minor] Allow to steal variables from the mempool

Vsevolod Stakhov vsevolod at rspamd.com
Mon Jul 29 17:49:47 UTC 2024


Author: Vsevolod Stakhov
Date: 2023-12-06 14:43:41 +0000
URL: https://github.com/rspamd/rspamd/commit/18c4390ea090d7259255b0742eb1ddf4152de663

[Minor] Allow to steal variables from the mempool

---
 src/libutil/mem_pool.c | 25 +++++++++++++++++++++++++
 src/libutil/mem_pool.h |  8 ++++++++
 2 files changed, 33 insertions(+)

diff --git a/src/libutil/mem_pool.c b/src/libutil/mem_pool.c
index 39bc79f25..119ade33d 100644
--- a/src/libutil/mem_pool.c
+++ b/src/libutil/mem_pool.c
@@ -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) {
diff --git a/src/libutil/mem_pool.h b/src/libutil/mem_pool.h
index aea5f4ae8..de0fea14b 100644
--- a/src/libutil/mem_pool.h
+++ b/src/libutil/mem_pool.h
@@ -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


More information about the Commits mailing list