All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jukka Rissanen <jukka.rissanen@linux.intel.com>
To: ell@lists.01.org
Subject: [PATCH 1/9] hashmap: Add value free function
Date: Tue, 10 Feb 2015 16:42:16 +0200	[thread overview]
Message-ID: <1423579344-10933-2-git-send-email-jukka.rissanen@linux.intel.com> (raw)
In-Reply-To: <1423579344-10933-1-git-send-email-jukka.rissanen@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 2914 bytes --]

User is able to set a function to free the value of the hashmap
entry.
---
 ell/hashmap.c | 37 +++++++++++++++++++++++++++++++++++++
 ell/hashmap.h |  6 ++++++
 2 files changed, 43 insertions(+)

diff --git a/ell/hashmap.c b/ell/hashmap.c
index b60cc62..f707d64 100644
--- a/ell/hashmap.c
+++ b/ell/hashmap.c
@@ -54,6 +54,8 @@ struct l_hashmap {
 	l_hashmap_compare_func_t compare_func;
 	l_hashmap_key_new_func_t key_new_func;
 	l_hashmap_key_free_func_t key_free_func;
+	l_hashmap_value_free_func_t value_free_func;
+	void *user_data;
 	unsigned int entries;
 	struct entry buckets[NBUCKETS];
 };
@@ -73,6 +75,12 @@ static inline void free_key(const struct l_hashmap *hashmap, void *key)
 		hashmap->key_free_func(key);
 }
 
+static inline void free_value(const struct l_hashmap *hashmap, void *value)
+{
+	if (hashmap->value_free_func)
+		hashmap->value_free_func(hashmap, value, hashmap->user_data);
+}
+
 static inline unsigned int hash_superfast(const uint8_t *key, unsigned int len)
 {
 	/*
@@ -307,6 +315,35 @@ LIB_EXPORT bool l_hashmap_set_key_free_function(struct l_hashmap *hashmap,
 }
 
 /**
+ * l_hashmap_set_value_free_function:
+ * @hashmap: hash table object
+ * @func: Value destructor function
+ *
+ * Sets the value destructor function to be used by this object.
+ * This function can be NULL, in which case no destructor is called.
+ *
+ * This function can only be called when the @hashmap is empty.
+ *
+ * Returns: #true when the value free function could be updated successfully,
+ * and #false otherwise.
+ **/
+LIB_EXPORT bool l_hashmap_set_value_free_function(struct l_hashmap *hashmap,
+					l_hashmap_value_free_func_t func,
+					void *user_data)
+{
+	if (unlikely(!hashmap))
+		return false;
+
+	if (hashmap->entries != 0)
+		return false;
+
+	hashmap->value_free_func = func;
+	hashmap->user_data = user_data;
+
+	return true;
+}
+
+/**
  * l_hashmap_destroy:
  * @hashmap: hash table object
  * @destroy: destroy function
diff --git a/ell/hashmap.h b/ell/hashmap.h
index 9f4f5fa..a904109 100644
--- a/ell/hashmap.h
+++ b/ell/hashmap.h
@@ -39,6 +39,9 @@ typedef void (*l_hashmap_key_free_func_t) (void *p);
 
 struct l_hashmap;
 
+typedef void (*l_hashmap_value_free_func_t) (const struct l_hashmap *hashmap,
+					void *p, void *user_data);
+
 unsigned int l_str_hash(const void *p);
 
 struct l_hashmap *l_hashmap_new(void);
@@ -52,6 +55,9 @@ bool l_hashmap_set_key_copy_function(struct l_hashmap *hashmap,
 						l_hashmap_key_new_func_t func);
 bool l_hashmap_set_key_free_function(struct l_hashmap *hashmap,
 					l_hashmap_key_free_func_t func);
+bool l_hashmap_set_value_free_function(struct l_hashmap *hashmap,
+				l_hashmap_value_free_func_t func,
+				void *user_data);
 
 void l_hashmap_destroy(struct l_hashmap *hashmap,
 			l_hashmap_destroy_func_t destroy);
-- 
1.8.3.1


  reply	other threads:[~2015-02-10 14:42 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-10 14:42 [PATCH 0/9] hashmap fixes Jukka Rissanen
2015-02-10 14:42 ` Jukka Rissanen [this message]
2015-02-10 14:42 ` [PATCH 2/9] hashmap: Call user supplied value free function in destroy Jukka Rissanen
2015-02-10 19:07   ` Denis Kenzior
2015-02-10 14:42 ` [PATCH 3/9] hashmap: Call user supplied value free function in insert Jukka Rissanen
2015-02-10 19:18   ` Denis Kenzior
2015-02-11  9:27     ` Patrik Flykt
2015-02-11 11:04       ` Tomasz Bursztyka
2015-02-11 13:50       ` Denis Kenzior
2015-02-10 14:42 ` [PATCH 4/9] unit: hashmap: Add value free hash entry test Jukka Rissanen
2015-02-10 14:42 ` [PATCH 5/9] unit: hashmap: Add replace " Jukka Rissanen
2015-02-10 14:42 ` [PATCH 6/9] hashmap: Add re-entrancy support to foreach function Jukka Rissanen
2015-02-10 19:47   ` Denis Kenzior
2015-02-11  9:21     ` Patrik Flykt
2015-02-11 14:06       ` Denis Kenzior
2015-02-12  7:23         ` Jukka Rissanen
2015-02-12 18:02           ` Denis Kenzior
2015-02-12  7:25         ` Jukka Rissanen
2015-02-12 17:55           ` Denis Kenzior
2015-02-13 15:38             ` Luiz Augusto von Dentz
2015-02-13 17:04               ` Denis Kenzior
2015-02-13 17:36               ` Marcel Holtmann
2015-02-16  9:44                 ` Luiz Augusto von Dentz
2015-02-16 16:18                   ` Marcel Holtmann
2015-02-16 18:27                     ` Luiz Augusto von Dentz
2015-02-16 19:03                       ` Marcel Holtmann
2015-02-17  9:48                 ` Tomasz Bursztyka
2015-02-17 16:41                   ` Denis Kenzior
2015-02-18  8:23                     ` Tomasz Bursztyka
2015-02-10 14:42 ` [PATCH 7/9] unit: hashmap: Re-entrancy tests added Jukka Rissanen
2015-02-10 14:42 ` [PATCH 8/9] hashmap: Add support to finding an element from hash Jukka Rissanen
2015-02-12  8:35   ` Jukka Rissanen
2015-02-13  0:19     ` Denis Kenzior
2015-02-10 14:42 ` [PATCH 9/9] unit: hashmap: Add unit test for l_hashmap_find Jukka Rissanen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1423579344-10933-2-git-send-email-jukka.rissanen@linux.intel.com \
    --to=jukka.rissanen@linux.intel.com \
    --cc=ell@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.