diff mbox series

[v2,10/18] tools/xenstore: add hashtable_replace() function

Message ID 20230710065947.4201-11-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series tools/xenstore: drop TDB | expand

Commit Message

Juergen Gross July 10, 2023, 6:59 a.m. UTC
For an effective way to replace a hashtable entry add a new function
hashtable_replace().

While at it let hashtable_add() fail if an entry with the specified
key does already exist.

This is in preparation to replace TDB with a more simple data storage.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/xenstore/hashtable.c | 20 ++++++++++++++++++++
 tools/xenstore/hashtable.h | 16 ++++++++++++++++
 2 files changed, 36 insertions(+)

Comments

Julien Grall July 18, 2023, 8:50 p.m. UTC | #1
Hi Juergen,

On 10/07/2023 07:59, Juergen Gross wrote:
> For an effective way to replace a hashtable entry add a new function
> hashtable_replace().
> 
> While at it let hashtable_add() fail if an entry with the specified
> key does already exist.

This seems to be a left-over from the previous version.

> 
> This is in preparation to replace TDB with a more simple data storage.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>   tools/xenstore/hashtable.c | 20 ++++++++++++++++++++
>   tools/xenstore/hashtable.h | 16 ++++++++++++++++
>   2 files changed, 36 insertions(+)
> 
> diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c
> index 29c247f918..0c26a09567 100644
> --- a/tools/xenstore/hashtable.c
> +++ b/tools/xenstore/hashtable.c
> @@ -160,6 +160,7 @@ static struct entry *hashtable_search_entry(const struct hashtable *h,
>   
>       return NULL;
>   }
> +

I think this belong to the previous patch.

>   int hashtable_add(struct hashtable *h, const void *k, void *v)
>   {
>       unsigned int index;
> @@ -204,6 +205,25 @@ void *hashtable_search(const struct hashtable *h, const void *k)
>       return e ? e->v : NULL;
>   }
>   
> +int hashtable_replace(struct hashtable *h, const void *k, void *v)
> +{
> +    struct entry *e;
> +
> +    e = hashtable_search_entry(h, k);
> +    if (!e)
> +        return ENOENT;
> +
> +    if (h->flags & HASHTABLE_FREE_VALUE)
> +    {
> +        talloc_free(e->v);
> +        talloc_steal(e, v);
> +    }
> +
> +    e->v = v;
> +
> +    return 0;
> +}
> +
>   void
>   hashtable_remove(struct hashtable *h, const void *k)
>   {
> diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h
> index 792f6cda7b..214aea1b3d 100644
> --- a/tools/xenstore/hashtable.h
> +++ b/tools/xenstore/hashtable.h
> @@ -51,6 +51,22 @@ create_hashtable(const void *ctx, const char *name,
>   int
>   hashtable_add(struct hashtable *h, const void *k, void *v);
>   
> +/*****************************************************************************
> + * hashtable_replace
> +
> + * @name        hashtable_nsert
> + * @param   h   the hashtable to insert into
> + * @param   k   the key - hashtable claims ownership and will free on removal
> + * @param   v   the value - does not claim ownership
> + * @return      zero for successful insertion
> + *
> + * This function does check for an entry being present before replacing it
> + * with a new value.
> + */
> +
> +int
> +hashtable_replace(struct hashtable *h, const void *k, void *v);
> +
>   /*****************************************************************************
>    * hashtable_search
>      

Chees,
Juergen Gross July 19, 2023, 5:20 a.m. UTC | #2
On 18.07.23 22:50, Julien Grall wrote:
> Hi Juergen,
> 
> On 10/07/2023 07:59, Juergen Gross wrote:
>> For an effective way to replace a hashtable entry add a new function
>> hashtable_replace().
>>
>> While at it let hashtable_add() fail if an entry with the specified
>> key does already exist.
> 
> This seems to be a left-over from the previous version.

Yes, will remove it.

> 
>>
>> This is in preparation to replace TDB with a more simple data storage.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>>   tools/xenstore/hashtable.c | 20 ++++++++++++++++++++
>>   tools/xenstore/hashtable.h | 16 ++++++++++++++++
>>   2 files changed, 36 insertions(+)
>>
>> diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c
>> index 29c247f918..0c26a09567 100644
>> --- a/tools/xenstore/hashtable.c
>> +++ b/tools/xenstore/hashtable.c
>> @@ -160,6 +160,7 @@ static struct entry *hashtable_search_entry(const struct 
>> hashtable *h,
>>       return NULL;
>>   }
>> +
> 
> I think this belong to the previous patch.

Indeed it does,


Juergen
diff mbox series

Patch

diff --git a/tools/xenstore/hashtable.c b/tools/xenstore/hashtable.c
index 29c247f918..0c26a09567 100644
--- a/tools/xenstore/hashtable.c
+++ b/tools/xenstore/hashtable.c
@@ -160,6 +160,7 @@  static struct entry *hashtable_search_entry(const struct hashtable *h,
 
     return NULL;
 }
+
 int hashtable_add(struct hashtable *h, const void *k, void *v)
 {
     unsigned int index;
@@ -204,6 +205,25 @@  void *hashtable_search(const struct hashtable *h, const void *k)
     return e ? e->v : NULL;
 }
 
+int hashtable_replace(struct hashtable *h, const void *k, void *v)
+{
+    struct entry *e;
+
+    e = hashtable_search_entry(h, k);
+    if (!e)
+        return ENOENT;
+
+    if (h->flags & HASHTABLE_FREE_VALUE)
+    {
+        talloc_free(e->v);
+        talloc_steal(e, v);
+    }
+
+    e->v = v;
+
+    return 0;
+}
+
 void
 hashtable_remove(struct hashtable *h, const void *k)
 {
diff --git a/tools/xenstore/hashtable.h b/tools/xenstore/hashtable.h
index 792f6cda7b..214aea1b3d 100644
--- a/tools/xenstore/hashtable.h
+++ b/tools/xenstore/hashtable.h
@@ -51,6 +51,22 @@  create_hashtable(const void *ctx, const char *name,
 int
 hashtable_add(struct hashtable *h, const void *k, void *v);
 
+/*****************************************************************************
+ * hashtable_replace
+
+ * @name        hashtable_nsert
+ * @param   h   the hashtable to insert into
+ * @param   k   the key - hashtable claims ownership and will free on removal
+ * @param   v   the value - does not claim ownership
+ * @return      zero for successful insertion
+ *
+ * This function does check for an entry being present before replacing it
+ * with a new value.
+ */
+
+int
+hashtable_replace(struct hashtable *h, const void *k, void *v);
+
 /*****************************************************************************
  * hashtable_search