[canopy-httpd] r1120 committed - modify cnp_htab_insert() so that it checks for an existing entry with ...

0 views
Skip to first unread message

codesite...@google.com

unread,
Dec 14, 2010, 11:35:14 PM12/14/10
to canopy-s...@googlegroups.com
Revision: 1120
Author: phrakt
Date: Tue Dec 14 20:34:09 2010
Log: modify cnp_htab_insert() so that it checks for an existing entry with
the
same key before inserting a new entry in the hash table

http://code.google.com/p/canopy-httpd/source/detail?r=1120

Modified:
/trunk/src/lib/canopy/htab.c

=======================================
--- /trunk/src/lib/canopy/htab.c Thu Apr 22 22:58:05 2010
+++ /trunk/src/lib/canopy/htab.c Tue Dec 14 20:34:09 2010
@@ -223,43 +223,47 @@
unsigned int hash;
struct cnp_hash_entry *ent;

- if ((ent = cnp_malloc(sizeof(*ent))) == NULL) {
- cnp_log_errno(cnp_logchan,
- "failed to allocate hash table entry");
+ if (cnp_rwlock_wrlock(&htab->ht_rwlock) == -1) {
+ cnp_log_err(cnp_logchan,
+ "failed to obtain write lock on hash table");
return (-1);
}

- ent->he_data = data;
- ent->he_klen = klen;
- if (htab->ht_flags & CNP_HTAB_COPYKEY) {
- if ((ent->he_key = cnp_malloc(klen)) == NULL) {
+ if ((ent = cnp_htab_getent(htab, key, klen)) == NULL) {
+ /* no previous entry with the same key, allocate a new entry */
+
+ if ((ent = cnp_malloc(sizeof(*ent))) == NULL) {
cnp_log_errno(cnp_logchan,
"failed to allocate hash table entry");
- cnp_free(ent);
+ cnp_rwlock_unlock(&htab->ht_rwlock);
return (-1);
}

- memcpy(ent->he_key, key, ent->he_klen);
- }
- else {
- ent->he_key = (void *)key;
- }
-
-
- if (cnp_rwlock_wrlock(&htab->ht_rwlock) == -1) {
- cnp_log_err(cnp_logchan,
- "failed to obtain write lock on hash table");
- if (htab->ht_flags & CNP_HTAB_COPYKEY)
- cnp_free(ent->he_key);
- cnp_free(ent);
- return (-1);
+ if (htab->ht_flags & CNP_HTAB_COPYKEY) {
+ if ((ent->he_key = cnp_malloc(klen)) == NULL) {
+ cnp_log_errno(cnp_logchan,
+ "failed to copy hash key");
+ cnp_free(ent);
+ cnp_rwlock_unlock(&htab->ht_rwlock);
+ return (-1);
+ }
+
+ memcpy(ent->he_key, key, klen);
+ }
+ else {
+ ent->he_key = (void *)key;
+ }
+
+ ent->he_klen = klen;
+
+ /* insert the new entry */
+ hash = (*htab->ht_keyhash)(key, klen) % htab->ht_bktcnt;
+ TAILQ_INSERT_TAIL(&htab->ht_buckets[hash].hb_list, ent, he_link);
+ htab->ht_buckets[hash].hb_count++;
+ htab->ht_count++;
}

- hash = (*htab->ht_keyhash)(key, klen) % htab->ht_bktcnt;
-
- TAILQ_INSERT_TAIL(&htab->ht_buckets[hash].hb_list, ent, he_link);
- htab->ht_buckets[hash].hb_count++;
- htab->ht_count++;
+ ent->he_data = data;

(void)cnp_rwlock_unlock(&htab->ht_rwlock);

@@ -411,13 +415,12 @@
* cnp_htab_keycmp_def()
*/
static int
-cnp_htab_keycmp_def(const void *k1, size_t k1len, const void *k2,
- size_t k2len)
-{
- if (k1len > k2len)
+cnp_htab_keycmp_def(const void *k1, size_t l1, const void *k2, size_t l2)
+{
+ if (l1 > l2)
return (1);
- else if (k2len > k1len)
+ else if (l2 > l1)
return (-1);
- else
- return memcmp(k1, k2, MIN(k1len, k2len));
-}
+
+ return memcmp(k1, k2, l1);
+}

Reply all
Reply to author
Forward
0 new messages