Changeset 04d66804 in mainline
- Timestamp:
- 2012-11-20T17:35:55Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 669f3d32
- Parents:
- 0adfc9d
- Location:
- kernel
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/adt/cht.h
r0adfc9d r04d66804 142 142 143 143 extern void cht_insert(cht_t *h, cht_link_t *item); 144 extern bool cht_insert_unique(cht_t *h, cht_link_t *item );144 extern bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item); 145 145 extern size_t cht_remove_key(cht_t *h, void *key); 146 146 extern bool cht_remove_item(cht_t *h, cht_link_t *item); -
kernel/generic/src/adt/cht.c
r0adfc9d r04d66804 414 414 static cht_link_t *find_resizing(cht_t *h, void *key, size_t hash, 415 415 marked_ptr_t old_head, size_t old_idx); 416 static bool insert_impl(cht_t *h, cht_link_t *item, bool unique);416 static bool insert_impl(cht_t *h, cht_link_t *item, cht_link_t **dup_item); 417 417 static bool insert_at(cht_link_t *item, const wnd_t *wnd, walk_mode_t walk_mode, 418 418 bool *resizing); 419 static bool has_duplicate s(cht_t *h, const cht_link_t *item, size_t hash,420 c onst wnd_t *cwnd);419 static bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 420 cht_link_t *cur, cht_link_t **dup_item); 421 421 static cht_link_t *find_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 422 422 cht_link_t *start); … … 886 886 void cht_insert(cht_t *h, cht_link_t *item) 887 887 { 888 insert_impl(h, item, false);888 insert_impl(h, item, NULL); 889 889 } 890 890 … … 908 908 * @code 909 909 * item = malloc(..); 910 * if (!cht_insert_unique(h, item)) { 911 * // Whoops, someone beat us to it - an equal item had already been inserted. 910 * if (!cht_insert_unique(h, item, &dup_item)) { 911 * // Whoops, someone beat us to it - an equal item 'dup_item' 912 * // had already been inserted. 912 913 * free(item); 913 914 * } else { … … 918 919 * 919 920 */ 920 bool cht_insert_unique(cht_t *h, cht_link_t *item) 921 { 922 return insert_impl(h, item, true); 923 } 924 925 /** Inserts the item into the table and checks for duplicates if unique is true.*/ 926 static bool insert_impl(cht_t *h, cht_link_t *item, bool unique) 921 bool cht_insert_unique(cht_t *h, cht_link_t *item, cht_link_t **dup_item) 922 { 923 ASSERT(rcu_read_locked()); 924 ASSERT(dup_item); 925 return insert_impl(h, item, dup_item); 926 } 927 928 /** Inserts the item into the table and checks for duplicates if dup_item. */ 929 static bool insert_impl(cht_t *h, cht_link_t *item, cht_link_t **dup_item) 927 930 { 928 931 rcu_read_lock(); … … 959 962 } 960 963 961 if ( unique && has_duplicates(h, item, hash, &wnd)) {964 if (dup_item && has_duplicate(h, item, hash, wnd.cur, dup_item)) { 962 965 rcu_read_unlock(); 963 966 return false; … … 1032 1035 } 1033 1036 1034 /** Returns true the chain starting at wnd hashan item equal to \a item.1037 /** Returns true if the chain starting at cur has an item equal to \a item. 1035 1038 * 1036 1039 * @param h CHT to operate on. 1037 1040 * @param item Item whose duplicates the function looks for. 1038 1041 * @param hash Hash of \a item. 1039 * @param[in] wnd wnd.cur is the first node with a hash greater to or equal1040 * to item's hash.1042 * @param[in] cur The first node with a hash greater to or equal to item's hash. 1043 * @param[out] dup_item The first duplicate item encountered. 1041 1044 * @return True if a non-deleted item equal to \a item exists in the table. 1042 1045 */ 1043 static inline bool has_duplicate s(cht_t *h, const cht_link_t *item, size_t hash,1044 c onst wnd_t *wnd)1045 { 1046 ASSERT( wnd->cur);1047 ASSERT( wnd->cur == &sentinel || hash <= node_hash(h, wnd->cur)1048 || node_hash(h, wnd->cur) == h->invalid_hash);1049 1050 /* hash < node_hash(h, wnd->cur) */1051 if (hash != node_hash(h, wnd->cur) && h->invalid_hash != node_hash(h, wnd->cur))1046 static inline bool has_duplicate(cht_t *h, const cht_link_t *item, size_t hash, 1047 cht_link_t *cur, cht_link_t **dup_item) 1048 { 1049 ASSERT(cur); 1050 ASSERT(cur == &sentinel || hash <= node_hash(h, cur) 1051 || node_hash(h, cur) == h->invalid_hash); 1052 1053 /* hash < node_hash(h, cur) */ 1054 if (hash != node_hash(h, cur) && h->invalid_hash != node_hash(h, cur)) 1052 1055 return false; 1053 1056 … … 1058 1061 */ 1059 1062 read_barrier(); 1060 return NULL != find_duplicate(h, item, hash, wnd->cur); 1063 1064 *dup_item = find_duplicate(h, item, hash, cur); 1065 return NULL != *dup_item; 1061 1066 } 1062 1067 -
kernel/test/cht/cht1.c
r0adfc9d r04d66804 124 124 set_val(v[4], 2, key[4]); 125 125 set_val(v[5], 3, key[5]); 126 127 cht_link_t *dup; 126 128 127 if (!cht_insert_unique(h, &v[0]->link ))129 if (!cht_insert_unique(h, &v[0]->link, &dup)) 128 130 return "Duplicates in empty"; 129 131 130 if (cht_insert_unique(h, &v[1]->link ))132 if (cht_insert_unique(h, &v[1]->link, &dup)) 131 133 return "Inserted a duplicate"; 132 133 if (!cht_insert_unique(h, &v[3]->link)) 134 135 if (dup != &v[0]->link) 136 return "Returned wrong duplicate"; 137 138 if (!cht_insert_unique(h, &v[3]->link, &dup)) 134 139 return "Refused non-equal item but with a hash in table."; 135 140 … … 138 143 139 144 bool ok = true; 140 ok = ok && cht_insert_unique(h, &v[4]->link );141 ok = ok && cht_insert_unique(h, &v[5]->link );145 ok = ok && cht_insert_unique(h, &v[4]->link, &dup); 146 ok = ok && cht_insert_unique(h, &v[5]->link, &dup); 142 147 143 148 if (!ok) … … 398 403 399 404 if (item_op) { 400 if (!cht_insert_unique(work->h, &work->elem[elem_idx].link)) { 405 rcu_read_lock(); 406 cht_link_t *dup; 407 if (!cht_insert_unique(work->h, &work->elem[elem_idx].link, 408 &dup)) { 401 409 TPRINTF("Err: already inserted\n"); 402 410 work->failed = true; 403 411 } 412 rcu_read_unlock(); 404 413 } else { 405 414 cht_insert(work->h, &work->elem[elem_idx].link);
Note:
See TracChangeset
for help on using the changeset viewer.