presence: use unsigned int for presentity hash id
authorDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 21 Apr 2020 13:08:39 +0000 (15:08 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Tue, 21 Apr 2020 13:09:01 +0000 (15:09 +0200)
src/modules/presence/hash.c
src/modules/presence/hash.h
src/modules/presence/presentity.c

index 6767f8e..dc72a36 100644 (file)
@@ -701,7 +701,11 @@ done:
        return ret;
 }
 
-/* in-memory presentity records */
+/**
+ * ==============================
+ *  in-memory presentity records
+ * ==============================
+ */
 
 static ps_ptable_t *_ps_ptable = NULL;
 
@@ -724,7 +728,7 @@ ps_ptable_t *ps_ptable_get(void)
  */
 ps_presentity_t *ps_presentity_new(ps_presentity_t *pt, int mtype)
 {
-       int bsize = 0;
+       uint32_t bsize = 0;
        ps_presentity_t *ptn = NULL;
        char *p = NULL;
 
@@ -969,7 +973,7 @@ int ps_ptable_insert(ps_presentity_t *pt)
 {
        ps_presentity_t ptc;
        ps_presentity_t *ptn = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        /* copy struct to fill in missing fields */
        memcpy(&ptc, pt, sizeof(ps_presentity_t));
@@ -988,7 +992,7 @@ int ps_ptable_insert(ps_presentity_t *pt)
                return -1;
        }
 
-       idx = ptn->hashid % _ps_ptable->ssize;
+       idx = ptn->hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        if(_ps_ptable->slots[idx].plist == NULL) {
@@ -1010,7 +1014,7 @@ int ps_ptable_replace(ps_presentity_t *pt)
 {
        ps_presentity_t ptc;
        ps_presentity_t *ptn = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        /* copy struct to fill in missing fields */
        memcpy(&ptc, pt, sizeof(ps_presentity_t));
@@ -1024,7 +1028,7 @@ int ps_ptable_replace(ps_presentity_t *pt)
                ptc.ruid = pres_sruid.uid;
        }
 
-       idx = ptc.hashid % _ps_ptable->ssize;
+       idx = ptc.hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        ptn = _ps_ptable->slots[idx].plist;
@@ -1043,6 +1047,10 @@ int ps_ptable_replace(ps_presentity_t *pt)
                ptn = ptn->next;
        }
 
+       if(ptn!=NULL) {
+               ps_presentity_free(ptn, 0);
+       }
+
        ptn = ps_presentity_new(&ptc, 0);
        if(ptn==NULL) {
                lock_release(&_ps_ptable->slots[idx].lock);
@@ -1068,7 +1076,7 @@ int ps_ptable_update(ps_presentity_t *pt)
 {
        ps_presentity_t ptc;
        ps_presentity_t *ptn = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        /* copy struct to fill in missing fields */
        memcpy(&ptc, pt, sizeof(ps_presentity_t));
@@ -1082,7 +1090,7 @@ int ps_ptable_update(ps_presentity_t *pt)
                ptc.ruid = pres_sruid.uid;
        }
 
-       idx = ptc.hashid % _ps_ptable->ssize;
+       idx = ptc.hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        ptn = _ps_ptable->slots[idx].plist;
@@ -1132,13 +1140,13 @@ int ps_ptable_remove(ps_presentity_t *pt)
 {
        ps_presentity_t ptc;
        ps_presentity_t *ptn = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        /* copy struct to fill in missing fields */
        memcpy(&ptc, pt, sizeof(ps_presentity_t));
 
        ptc.hashid = core_case_hash(&pt->user, &pt->domain, 0);
-       idx = ptc.hashid % _ps_ptable->ssize;
+       idx = ptc.hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        ptn = _ps_ptable->slots[idx].plist;
@@ -1174,14 +1182,14 @@ ps_presentity_t *ps_ptable_get_list(str *user, str *domain)
        ps_presentity_t *ptl = NULL;
        ps_presentity_t *ptd = NULL;
        ps_presentity_t *pte = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        memset(&ptc, 0, sizeof(ps_presentity_t));
 
        ptc.user = *user;
        ptc.domain = *domain;
        ptc.hashid = core_case_hash(&ptc.user, &ptc.domain, 0);
-       idx = ptc.hashid % _ps_ptable->ssize;
+       idx = ptc.hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        ptn = _ps_ptable->slots[idx].plist;
@@ -1219,7 +1227,7 @@ ps_presentity_t *ps_ptable_get_item(str *user, str *domain, str *event, str *eta
        ps_presentity_t ptc;
        ps_presentity_t *ptn = NULL;
        ps_presentity_t *ptd = NULL;
-       int idx = 0;
+       uint32_t idx = 0;
 
        memset(&ptc, 0, sizeof(ps_presentity_t));
 
@@ -1228,7 +1236,7 @@ ps_presentity_t *ps_ptable_get_item(str *user, str *domain, str *event, str *eta
        ptc.event = *event;
        ptc.etag = *etag;
        ptc.hashid = core_case_hash(&ptc.user, &ptc.domain, 0);
-       idx = ptc.hashid % _ps_ptable->ssize;
+       idx = ptc.hashid & (_ps_ptable->ssize - 1);
 
        lock_get(&_ps_ptable->slots[idx].lock);
        ptn = _ps_ptable->slots[idx].plist;
index 4573242..4a0a46f 100644 (file)
@@ -30,6 +30,8 @@
 #ifndef PS_HASH_H
 #define PS_HASH_H
 
+#include <stdint.h>
+
 #include "../../core/lock_ops.h"
 
 struct presentity;
@@ -141,8 +143,8 @@ void destroy_phtable(void);
 int delete_db_subs(str* to_tag, str* from_tag, str* callid);
 
 typedef struct ps_presentity {
-       int bsize;
-       int hashid;
+       uint32_t bsize;
+       uint32_t hashid;
        str user;
        str domain;
        str ruid;
index 106b50c..2a42220 100644 (file)
@@ -1392,7 +1392,7 @@ static int ps_cache_update_presentity(sip_msg_t *msg, presentity_t *presentity,
                        }
                        LM_DBG("inserting presentity into hash table\n");
                        if(ps_ptable_insert(&ptc) < 0) {
-                               LM_ERR("inserting new record in database\n");
+                               LM_ERR("inserting new record in memory\n");
                                goto error;
                        }
                } else {