summaryrefslogtreecommitdiff
path: root/debian/patches/ihash-as-cache0004-libihash-fix-item-insertion.patch
blob: d278298cabc46117734c2988f015fb5e49ddeaef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
From 1e52fab24dd7dc549c6120ff4c2904be5f1f852b Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Sun, 22 Nov 2015 21:04:51 +0100
Subject: [PATCH hurd 4/4] libihash: fix item insertion

* libihash/ihash.c (find_index): Keep track and return the index where
we could insert the item.
(add_one): Use 'find_index'.
---
 libihash/ihash.c | 54 ++++++++++++++++++------------------------------------
 1 file changed, 18 insertions(+), 36 deletions(-)

diff --git a/libihash/ihash.c b/libihash/ihash.c
index 8e7383e..ae9f970 100644
--- a/libihash/ihash.c
+++ b/libihash/ihash.c
@@ -73,6 +73,8 @@ find_index (hurd_ihash_t ht, hurd_ihash_key_t key)
 {
   unsigned int idx;
   unsigned int up_idx;
+  unsigned int first_deleted = 0;
+  int first_deleted_set = 0;
   unsigned int mask = ht->size - 1;
 
   idx = hash (ht, key) & mask;
@@ -86,15 +88,21 @@ find_index (hurd_ihash_t ht, hurd_ihash_key_t key)
   do
     {
       up_idx = (up_idx + 1) & mask;
-      if (ht->items[up_idx].value == _HURD_IHASH_EMPTY
-	  || compare (ht, ht->items[up_idx].key, key))
+      if (ht->items[up_idx].value == _HURD_IHASH_EMPTY)
+        return first_deleted_set ? first_deleted : up_idx;
+      if (compare (ht, ht->items[up_idx].key, key))
 	return up_idx;
+      if (! first_deleted_set
+          && ht->items[up_idx].value == _HURD_IHASH_DELETED)
+        first_deleted = up_idx, first_deleted_set = 1;
     }
   while (up_idx != idx);
 
-  /* If we end up here, the item could not be found.  Return any
-     invalid index.  */
-  return idx;
+  /* If we end up here, the item could not be found.  Return the index
+     of the first deleted item, as this is the position where we can
+     insert an item with the given key once we established that it is
+     not in the table.  */
+  return first_deleted;
 }
 
 
@@ -231,48 +239,22 @@ static inline int
 add_one (hurd_ihash_t ht, hurd_ihash_key_t key, hurd_ihash_value_t value)
 {
   unsigned int idx;
-  unsigned int first_free;
-  unsigned int mask = ht->size - 1;
-
-  idx = hash (ht, key) & mask;
-  first_free = idx;
-
-  if (ht->items[idx].value != _HURD_IHASH_EMPTY
-      && ! compare (ht, ht->items[idx].key, key))
-    {
-      unsigned int up_idx = idx;
 
-      do
-	{
-        up_idx = (up_idx + 1) & mask;
-	  if (ht->items[up_idx].value == _HURD_IHASH_EMPTY
-	      || compare (ht, ht->items[up_idx].key, key))
-	    {
-	      idx = up_idx;
-	      break;
-	    }
-	}
-      while (up_idx != idx);
-    }
+  idx = find_index (ht, key);
 
   /* Remove the old entry for this key if necessary.  */
   if (index_valid (ht, idx, key))
     locp_remove (ht, &ht->items[idx].value);
 
-  /* If we have not found an empty slot, maybe the last one we
-     looked at was empty (or just got deleted).  */
-  if (!index_empty (ht, first_free))
-    first_free = idx;
- 
-  if (index_empty (ht, first_free))
+  if (index_empty (ht, idx))
     {
       ht->nr_items++;
-      ht->items[first_free].value = value;
-      ht->items[first_free].key = key;
+      ht->items[idx].value = value;
+      ht->items[idx].key = key;
 
       if (ht->locp_offset != HURD_IHASH_NO_LOCP)
 	*((hurd_ihash_locp_t *) (((char *) value) + ht->locp_offset))
-	  = &ht->items[first_free].value;
+	  = &ht->items[idx].value;
 
       return 1;
     }
-- 
2.1.4