int main(int argc, char **argv) { char *s1 = "asdfghjkl"; char *s2 = "iud8fmndd"; unsigned int v1 = jhash(s1, strlen(s1), 0); unsigned int v2 = jhash(s2, strlen(s2), 0); printf("%X, %X\n", v1, v2); return 0; }
/* * rfs_rule_hash */ unsigned int rfs_rule_hash(uint32_t type, uint8_t *data) { switch (type) { case RFS_RULE_TYPE_MAC_RULE: return jhash(data, ETH_ALEN, 0) & RFS_RULE_HASH_MASK; case RFS_RULE_TYPE_IP4_RULE: return jhash(data, 4, 0) & RFS_RULE_HASH_MASK; case RFS_RULE_TYPE_IP6_RULE: return jhash(data, sizeof(struct in6_addr), 0) & RFS_RULE_HASH_MASK; default: return 0; } }
/* * try to find the mac related vxlan client *if b_rcu_protect is True,then this routine will accquire RCU lock,and traverse the entire list in RCU context,otherwise not. *and we set b_rcu_protect not NULL,as long as we make it clear that,this function is called with rcu_read_lock() invoked before somewhere * the most important point is if we set b_age_check to TRUE,then we must be make sure that the spin-lock is accquired somewhere else before, */ struct vxlan_client * vxlan_server_find_client(struct net*net,uint8_t mac[],int b_rcu_protect,int b_age_check) { uint64_t diff_time; struct vxlan_client *cl=NULL,*cl_tmp=NULL; struct vxlan_server_net* vsn=net_generic(net,vxlan_server_id); struct hlist_head *hhead; int hash_idx=jhash(mac,6,0x12345678); hash_idx&=CLIENT_HASH_MASK; hhead=&vsn->client_hhead[hash_idx]; if(b_rcu_protect){ hlist_for_each_entry_rcu(cl_tmp,hhead,hnode){ if(compare_ether_addr(cl_tmp->mac,mac)==0){ cl=cl_tmp; cl->jiffie_cnt=jiffies_64; break; } if(!b_age_check) continue; /*here we check age expiry timer */ if(cl_tmp->is_local_port) continue;//local port must be skipped and remain here for a long time diff_time=jiffies_64-cl_tmp->jiffie_cnt; if(diff_time>DEFAULT_CLIENT_AGE_TIME){ hlist_del_rcu(&cl_tmp->hnode); call_rcu(&cl_tmp->rcu,vxlan_server_client_free); } } }else{
/* * p: in_addr (key) * */ static unsigned int origin_key_make(void *p) { struct in_addr *router_id = (struct in_addr *)p; unsigned int key = jhash(router_id, sizeof(struct in_addr), 314159); return key; }
static struct lttng_ust_dl_node *find_or_create_dl_node(struct bin_info_data *bin_data) { struct cds_hlist_head *head; struct lttng_ust_dl_node *e; unsigned int hash; bool found = false; hash = jhash(&bin_data->base_addr_ptr, sizeof(bin_data->base_addr_ptr), 0); head = &dl_state_table[hash & (UST_DL_STATE_TABLE_SIZE - 1)]; cds_hlist_for_each_entry_2(e, head, node) { if (compare_bin_data(&e->bin_data, bin_data) != 0) continue; found = true; break; } if (!found) { /* Create */ e = alloc_dl_node(bin_data); if (!e) return NULL; cds_hlist_add_head(&e->node, head); } return e; }
static int nfp_add_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len) { struct nfp_flower_priv *priv = app->priv; struct nfp_mask_id_table *mask_entry; unsigned long hash_key; u8 mask_id; if (nfp_mask_alloc(app, &mask_id)) return -ENOENT; mask_entry = kmalloc(sizeof(*mask_entry), GFP_KERNEL); if (!mask_entry) { nfp_release_mask_id(app, mask_id); return -ENOMEM; } INIT_HLIST_NODE(&mask_entry->link); mask_entry->mask_id = mask_id; hash_key = jhash(mask_data, mask_len, priv->mask_id_seed); mask_entry->hash_key = hash_key; mask_entry->ref_cnt = 1; hash_add(priv->mask_table, &mask_entry->link, hash_key); return mask_id; }
//p: name_prefix (key) static unsigned int prefix_key_make(void *p) { struct name_prefix *np = (struct name_prefix *)p; unsigned int key = jhash(np->name, np->length, 314159); return key; }
static int get_emmc0_cid(void) { struct device *emmc_disk; /* * Automation people are needing proper serial number for ADB * lets derivate from the serial number of the emmc card. */ emmc_disk = class_find_device(&block_class, NULL, NULL, mmcblk0_match); if (emmc_disk) { struct gendisk *disk = dev_to_disk(emmc_disk); struct mmc_card *card = mmc_dev_to_card(disk->driverfs_dev); if (card) { cid_legacy.manfid = card->cid.manfid; cid_legacy.prod_name[0] = card->cid.prod_name[0]; cid_legacy.prod_name[1] = card->cid.prod_name[1]; cid_legacy.prod_name[2] = card->cid.prod_name[2]; cid_legacy.prod_name[3] = card->cid.prod_name[3]; cid_legacy.prod_name[4] = card->cid.prod_name[4]; cid_legacy.prod_name[5] = card->cid.prod_name[5]; cid_legacy.prod_name[6] = card->cid.prod_name[6]; cid_legacy.prod_name[7] = card->cid.prod_name[7]; cid_legacy.serial = card->cid.serial; cid_legacy.oemid = card->cid.oemid; cid_legacy.year = card->cid.year; cid_legacy.hwrev = card->cid.hwrev; cid_legacy.fwrev = card->cid.fwrev; cid_legacy.month = card->cid.month; snprintf(emmc0_id, sizeof(emmc0_id), "Medfield%08X", jhash(&cid_legacy, sizeof(cid_legacy), 0)); return 1; } } return 0; }
umtsRab* VidscaleSHMIntf::createSHMHashEntry(char* key, ShmKeyType keyType, umtsRab* pSHMRab, umtsRab* pOldRabRec) { FirstStrideEntry *firstStrideEnt = NULL; SHM_Hash_Table *pHashTable = NULL; unsigned int keyLength = 0; LogDebug("createSHMHashEntry Entry\n"); FirstStrideBlock *pFirstStrideBlock = NULL; //zeroKeyPadding(key, keyType); uint8_t memMgrQueuIdx = (pSHMRab->imsi % MAX_CONTROLLING_ENTITIES); uint32_t firstStrideIndex = 0; switch(keyType) { case SHM_KEY_TUN: { LogDebug("createSHMHashEntry TUN Key\n"); FirstStrideBlock *pFirstTunStrideBlock = (FirstStrideBlock *)offToPtr(sm_firstTunnelStrideOffset); firstStrideEnt = (FirstStrideEntry *)&(pFirstTunStrideBlock->stride[0]); uint32_t fsKey = ipHash(&(((struct RabRecordKey*)key)->dstIP)); //hash key to find entry in the first stride fsKey = fsKey & 0x0000FFFF; //take the 2 LSBytes of the hashed IP address firstStrideIndex = jhash(&fsKey, sizeof(fsKey), 10); firstStrideIndex = firstStrideIndex & (NUM_TUN_SECOND_STRIDES - 1); pFirstStrideBlock = (FirstStrideBlock *)offToPtr(sm_firstTunnelStrideOffset); pHashTable = (SHM_Hash_Table *)(offToPtr((firstStrideEnt[firstStrideIndex].offForNextStride))); keyLength = sizeof(RabRecordKey); } break; default: { LogError("Invalid key type\n"); return NULL; } } int oldSessValueOffset = INALID_OFFSET ; umtsRab* pRabRec = NULL; if(FALSE == Hash_CreateEntry(pHashTable, key, keyLength, ptrToOff( pSHMRab), memMgrQueuIdx, &m_shmUpdateLock, &oldSessValueOffset)) { __sync_fetch_and_add(&(pFirstStrideBlock->numCreateFail) , 1); pOldRabRec = (umtsRab*)offToPtrHash(pHashTable, oldSessValueOffset); return NULL; } if(oldSessValueOffset != INALID_OFFSET) { pOldRabRec = (umtsRab*)offToPtrHash(pHashTable, oldSessValueOffset); } __sync_fetch_and_add(&(pFirstStrideBlock->numCreateSuccess) , 1); return pSHMRab; }
static struct process_val_t* find_current_process(void) { u32 hash; struct process_key_t process_key; process_key.tgid = get_current()->tgid; hash = jhash(&process_key, sizeof(process_key), 0); return find_process(&process_key, hash); }
uint32_t conntrack_hash(struct ptype *a, struct ptype *b, void *parent) { // Create a reversible hash for a and b if (!a) return POM_ERR; // Use the parent pointer as an init value uint32_t parent_initval = (uint32_t) ((uint64_t)parent & 0xFFFFFFFF); size_t size_a = ptype_get_value_size(a); if (!b) { // Only fwd direction // Try to use the best hash function if (size_a == sizeof(uint32_t)) { // exactly one word return jhash_1word(*((uint32_t*)a->value), parent_initval); } else if (size_a == 2 * sizeof(uint32_t)) { // exactly two words return jhash_2words(*((uint32_t*)a->value), *((uint32_t*)(a->value + sizeof(uint32_t))), parent_initval); } else if (size_a == 3 * sizeof(uint32_t)) { // exactly 3 words return jhash_3words(*((uint32_t*)a->value), *((uint32_t*)(a->value + sizeof(uint32_t))), *((uint32_t*)(a->value + (2 * sizeof(uint32_t)))), parent_initval); } // Fallback on all size function return jhash((char*)a->value, size_a, parent_initval); } size_t size_b = ptype_get_value_size(b); // Try to use the best hash function if (size_a == sizeof(uint16_t) && size_b == sizeof(uint16_t)) { // Multiply the two 16bit values uint32_t value_a = *((uint16_t*)a->value); uint32_t value_b = *((uint16_t*)b->value); return jhash_1word(value_a * value_b, parent_initval); } else if (size_a == sizeof(uint32_t) && size_b == sizeof(uint32_t)) { // XOR the two 32bit values before hashing return jhash_1word(*((uint32_t*)a->value) ^ *((uint32_t*)b->value), parent_initval); } uint32_t hash_a = jhash((char*)a->value, size_a, parent_initval); uint32_t hash_b = jhash((char*)b->value, size_b, parent_initval); return hash_a ^ hash_b; }
bool VidscaleSHMIntf::deleteSHMHashEntry(char* key, ShmKeyType keyType, uint64_t imsi) { FirstStrideEntry *firstStrideEnt = NULL; SHM_Hash_Table *pHashTable = NULL; unsigned int keyLength = 0; LogDebug("deleteSHMHashEntry Entry\n"); FirstStrideBlock *pFirstStrideBlock = NULL; //zeroKeyPadding(key, keyType); uint8_t memMgrQueuIdx = (imsi % MAX_CONTROLLING_ENTITIES); switch(keyType) { case SHM_KEY_TUN: { FirstStrideBlock *pFirstTunStrideBlock = (FirstStrideBlock *)offToPtr(sm_firstTunnelStrideOffset); firstStrideEnt = (FirstStrideEntry *)&(pFirstTunStrideBlock->stride[0]); uint32_t fsKey = 0; //hash key to find entry in the first stride fsKey = ipHash(&(((struct RabRecordKey*)key)->dstIP)); //hash key to find entry in the first stride fsKey = fsKey & 0x0000FFFF; //take the 2 LSBytes of the hashed IP address uint32_t firstStrideIndex = jhash(&fsKey, sizeof(fsKey), 10); firstStrideIndex = firstStrideIndex & (NUM_TUN_SECOND_STRIDES - 1); if(firstStrideEnt[firstStrideIndex].offForNextStride == 0) { LogDebug("Tun Hash table and entry not there\n"); __sync_fetch_and_add(&(pFirstTunStrideBlock->numDeleteFail) , 1); return FALSE; } pHashTable = (SHM_Hash_Table *)(offToPtr( (firstStrideEnt[firstStrideIndex].offForNextStride))); keyLength = sizeof(RabRecordKey); pFirstStrideBlock = (FirstStrideBlock *)offToPtr(sm_firstTunnelStrideOffset); } break; default: { LogError("Invalid key type\n"); return FALSE; } } umtsRab* pSHMSess = NULL; pSHMSess = Hash_DeleteEntry(pHashTable, key, keyLength, memMgrQueuIdx, &m_shmUpdateLock); if(NULL == pSHMSess) { __sync_fetch_and_add(&(pFirstStrideBlock->numDeleteFail) , 1); return FALSE; } else { __sync_fetch_and_add(&(pFirstStrideBlock->numDeleteSuccess) , 1); return TRUE; } }
/* Compute source port for outgoing packet * first choice to use L4 flow hash since it will spread * better and maybe available from hardware * secondary choice is to use jhash on the Ethernet header */ __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb) { unsigned int range = (port_max - port_min) + 1; u32 hash; hash = skb_get_hash(skb); if (!hash) hash = jhash(skb->data, 2 * ETH_ALEN, (__force u32) skb->protocol); return htons((((u64) hash * range) >> 32) + port_min); }
static void syscall_exit_probe( void *__data, struct pt_regs *regs, long ret) { u32 hash; struct process_val_t *process_val; struct thread_key_t thread_key; struct thread_val_t *thread_val; struct task_struct *task = get_current(); uint64_t latency_threshold = 0; uint64_t sys_entry_ts = 0; uint64_t sys_exit_ts = trace_clock_read64(); int sys_id; // Check whether the process is registered to receive signals. rcu_read_lock(); process_val = find_current_process(); if (process_val == NULL) { rcu_read_unlock(); return; } latency_threshold = process_val->latency_threshold; // Get the timestamp of the syscall entry. thread_key.pid = task->pid; hash = jhash(&thread_key, sizeof(thread_key), 0); thread_val = find_thread(&thread_key, hash); if (thread_val == NULL) { rcu_read_unlock(); return; } sys_entry_ts = thread_val->sys_entry_ts; sys_id = thread_val->sys_id; rcu_read_unlock(); // Check whether the system call was longer than the threshold. if (sys_exit_ts - sys_entry_ts < latency_threshold) { return; } // Send the signal. send_sig_info(SIGPROF, SEND_SIG_NOINFO, task); // Log event. trace_syscall_latency(sys_entry_ts, sys_exit_ts - sys_entry_ts, sys_id); }
/* * Get marker if the marker is present in the marker hash table. * Must be called with markers_mutex held. * Returns NULL if not present. */ static struct marker_entry *get_marker(const char *name) { struct hlist_head *head; struct hlist_node *node; struct marker_entry *e; u32 hash = jhash(name, strlen(name), 0); head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)]; hlist_for_each_entry(e, node, head, hlist) { if (!strcmp(name, e->name)) return e; } return NULL; }
static inline struct tracing_map_elt * __tracing_map_insert(struct tracing_map *map, void *key, bool lookup_only) { u32 idx, key_hash, test_key; struct tracing_map_entry *entry; key_hash = jhash(key, map->key_size, 0); if (key_hash == 0) key_hash = 1; idx = key_hash >> (32 - (map->map_bits + 1)); while (1) { idx &= (map->map_size - 1); entry = TRACING_MAP_ENTRY(map->map, idx); test_key = entry->key; if (test_key && test_key == key_hash && entry->val && keys_match(key, entry->val->key, map->key_size)) { if (!lookup_only) atomic64_inc(&map->hits); return entry->val; } if (!test_key) { if (lookup_only) break; if (!cmpxchg(&entry->key, 0, key_hash)) { struct tracing_map_elt *elt; elt = get_free_elt(map); if (!elt) { atomic64_inc(&map->drops); entry->key = 0; break; } memcpy(elt->key, key, map->key_size); entry->val = elt; atomic64_inc(&map->hits); return entry->val; } } idx++; } return NULL; }
static struct nfp_mask_id_table * nfp_search_mask_table(struct nfp_app *app, char *mask_data, u32 mask_len) { struct nfp_flower_priv *priv = app->priv; struct nfp_mask_id_table *mask_entry; unsigned long hash_key; hash_key = jhash(mask_data, mask_len, priv->mask_id_seed); hash_for_each_possible(priv->mask_table, mask_entry, link, hash_key) if (mask_entry->hash_key == hash_key) return mask_entry; return NULL; }
/* * Module ioctl interface. */ long lttngprofile_module_ioctl( struct file *file, unsigned int cmd, unsigned long arg) { u32 hash; struct process_key_t key; struct process_val_t *val; struct lttngprofile_module_msg msg; struct task_struct *task = get_current(); int ret = 0; void __user *umsg = (void *) arg; if (cmd != LTTNGPROFILE_MODULE_IOCTL) return -ENOIOCTLCMD; if (copy_from_user(&msg, umsg, sizeof(struct lttngprofile_module_msg))) return -EFAULT; key.tgid = task->tgid; hash = jhash(&key, sizeof(key), 0); switch(msg.cmd) { case LTTNGPROFILE_MODULE_REGISTER: /* check if already registered */ rcu_read_lock(); val = find_process(&key, hash); if (val) { rcu_read_unlock(); break; } rcu_read_unlock(); /* do registration */ val = kzalloc(sizeof(struct process_val_t), GFP_KERNEL); val->tgid = key.tgid; val->latency_threshold = msg.latency_threshold; hash_add_rcu(process_map, &val->hlist, hash); printk("lttngprofile_module_ioctl register %d\n", task->tgid); break; case LTTNGPROFILE_MODULE_UNREGISTER: process_unregister(task->tgid); break; default: ret = -ENOTSUPP; break; } return ret; }
uint32_t ptype_get_hash(struct ptype *pt) { size_t size = pt->type->info->value_size(pt); // Try to use the best hash function if (size == sizeof(uint32_t)) { // exactly one word return jhash_1word(*((uint32_t*)pt->value), INITVAL); } else if (size == 2 * sizeof(uint32_t)) { // exactly two words return jhash_2words(*((uint32_t*)pt->value), *((uint32_t*)(pt->value + sizeof(uint32_t))), INITVAL); } else if (size == 3 * sizeof(uint32_t)) { // exactly 3 words return jhash_3words(*((uint32_t*)pt->value), *((uint32_t*)(pt->value + sizeof(uint32_t))), *((uint32_t*)(pt->value + (2 * sizeof(uint32_t)))), INITVAL); } // Fallback on all size function return jhash((char*)pt->value, size, INITVAL); }
/* * Utility functions. */ static void process_unregister(pid_t tgid) { u32 hash; struct process_key_t key; struct process_val_t *val; key.tgid = tgid; hash = jhash(&key, sizeof(key), 0); rcu_read_lock(); val = find_process(&key, hash); if (val) { hash_del_rcu(&val->hlist); call_rcu(&val->rcu, free_process_val_rcu); printk("lttngprofile unregister process %d\n", tgid); } rcu_read_unlock(); }
int db_tag_add_uniq(list_t *tags) { #ifdef UNIQ_TAGS_LIST sqlite3_stmt *stmt = NULL; uint32_t hash = 0; size_t len = 0; int ret = 0; char *p = NULL; ASSERT(tags != NULL, MSG_M_NULLPTR); len = strlen(SQL_T_UNIQ_ADD); if (sqlite3_prepare_v2(db_conn, SQL_T_UNIQ_ADD, len, &stmt, NULL) != SQLITE_OK) { msg(msg_warn, COMMON_ERR_FMTN, MSG_D_FAILPREPARE, sqlite3_errmsg(db_conn)); return 1; } while (list_items_walk(tags, &p) > 0) { len = strlen(p); hash = jhash(p, len); sqlite3_bind_int(stmt, 1, hash); sqlite3_bind_text(stmt, 2, p, len, SQLITE_STATIC); if ((ret = sqlite3_step(stmt)) != SQLITE_DONE) { msg(msg_warn, COMMON_ERR_FMTN, MSG_D_FAILEXEC, sqlite3_errmsg(db_conn)); break; } sqlite3_clear_bindings(stmt); sqlite3_reset(stmt); } sqlite3_finalize(stmt); return (ret == SQLITE_DONE) ? 0 : 1; #else return 0; #endif }
/* * Probes. */ static void syscall_entry_probe( void *__data, struct pt_regs *regs, long id) { u32 hash; struct process_val_t *process_val; struct thread_key_t thread_key; struct thread_val_t *thread_val; struct task_struct *task = get_current(); uint64_t sys_entry_ts = trace_clock_read64(); // Check whether the process is registered to receive signals. rcu_read_lock(); process_val = find_current_process(); if (process_val == NULL) { rcu_read_unlock(); return; } // Keep track of the timestamp of this syscall entry. thread_key.pid = task->pid; hash = jhash(&thread_key, sizeof(thread_key), 0); thread_val = find_thread(&thread_key, hash); if (thread_val != NULL) { thread_val->sys_entry_ts = sys_entry_ts; thread_val->sys_id = id; rcu_read_unlock(); return; } rcu_read_unlock(); thread_val = kzalloc(sizeof(struct thread_val_t), GFP_KERNEL); thread_val->pid = task->pid; thread_val->sys_entry_ts = sys_entry_ts; thread_val->sys_id = id; hash_add_rcu(thread_map, &thread_val->hlist, hash); }
static int objhash_add_one(struct my_obj *obj) { u32 hash_idx; if (obj == NULL) { pr_err("%s(): Failed, NULL object\n", __func__); return 0; } objhash_cnt++; INIT_HLIST_NODE(&obj->node); obj->page = virt_to_head_page(obj); /* Hash on the page address of the object */ hash_idx = jhash(&obj->page, 8, 13); //pr_info("DEBUG: hash_idx=0x%x [%u] page=0x%p\n", // hash_idx, hash_idx % HASHSZ, obj->page); hash_idx = hash_idx % HASHSZ; hlist_add_head(&obj->node, &objhash[hash_idx]); return 1; }
/** * Calculates a value from 0 to max from a hash of the arguments. * * @key * @len: length * @initval * @max * @return: a 32 bits index */ static u_int32_t pknock_hash(const void *key, u_int32_t len, u_int32_t initval, u_int32_t max) { return jhash(key, len, initval) % max; }
static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed) { const struct nft_hash_elem *he = data; return jhash(nft_set_ext_key(&he->ext), len, seed); }
static inline u32 nft_hash_key(const void *data, u32 len, u32 seed) { const struct nft_hash_cmp_arg *arg = data; return jhash(arg->key, len, seed); }
static struct hlist_head *hash_bucket(const struct net *net, const char *name) { unsigned int hash = jhash(name, strlen(name), (unsigned long) net); return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)]; }
static u32 ip_vs_sip_hashkey_raw(const struct ip_vs_conn_param *p, u32 initval, bool inverse) { return jhash(p->pe_data, p->pe_data_len, initval); }
/* *--------------------------------------------------------- * * Hash_CreateEntry -- * * Searches a hash table for an entry corresponding to * key. If no entry is found, then one is created. * * Results: * The return value is a pointer to the entry. If *newPtr * isn't NULL, then *newPtr is filled in with TRUE if a * new entry was created, and FALSE if an entry already existed * with the given key. * * Side Effects: * Memory may be allocated, and the hash buckets may be modified. *--------------------------------------------------------- */ bool VidscaleSHMIntf:: Hash_CreateEntry(SHM_Hash_Table *t, const char *key,unsigned int length, int valueOffset, uint8_t partitionIndex, pthread_mutex_t *shmUpdateLock, int *oldSessValueOffset) { unsigned int h; LogDebug("Hash_CreateEntry\n"); if(!shmUpdateLock) { LogError("SHM UPDATE LOCk IS NULL\n"); return FALSE; } h = jhash(key, length, 10); h = h & (HASH_TABLE_SIZE - 1); /*First Look if particular key exist*/ LogDebug("Hash_CreateEntry hash index %u \n", h); SHM_Hash_Entry *pNewHashObj = NULL; if(((FreeMemMgr<SHM_Hash_Entry>*)t->phashEntryFreeMemMgrObj)) { pNewHashObj = ((FreeMemMgr<SHM_Hash_Entry> *)t->phashEntryFreeMemMgrObj)->getFreeMem(partitionIndex); } else { LogError("Invalid pointer for NewHashObject Memory Manager : HashTable Pointer %p \n", t); return FALSE; } if(NULL == pNewHashObj) { LogError("Not enough memory for hash entry \n"); return FALSE; } pNewHashObj->offsetValue = valueOffset; pNewHashObj->keySize = length; memcpy(&(pNewHashObj->hashKey), key, length); pNewHashObj->offsetNext = 0; int loopcount = 0; { AutoLock lock(*shmUpdateLock); if(0 == t->offsetHashEntry[h]) //first entry of the bucket { (perfarray[0])++; t->offsetHashEntry[h] = ptrToOffHash(t, pNewHashObj); return TRUE; } bool isLastElemRemoved = false; SHM_Hash_Entry *pCurrHashObj = NULL; SHM_Hash_Entry *pTmpLastHashObj = NULL; int nextHashEntryOffset = t->offsetHashEntry[h]; int currentHashEntryOffset = nextHashEntryOffset; for (;(0 != nextHashEntryOffset);) { loopcount++; //For benchmarking has debug support. __sync_fetch_and_add(&(perfarray[loopcount]) , 1); pCurrHashObj = (SHM_Hash_Entry *)(offToPtrHash(t, nextHashEntryOffset)); if(!pCurrHashObj) { LogInfo("Hash_CreateEntry hash entry not found offset hashbucket %u tableptr %p\n", h, t ); break; } if (memcmp(&(pCurrHashObj->hashKey), key, length) == 0) { int16_t *node; node = (int16_t*)&valueOffset; if(currentHashEntryOffset == nextHashEntryOffset) //remove the head element { LogInfo("Hash_CreateEntry hash entry found. Head element offSet %Zu hashbucket %u tableptr %p\n", ptrToOffHash(t, pCurrHashObj), h, t); t->offsetHashEntry[h] = pCurrHashObj->offsetNext; } else //remove non head element { LogInfo("Hash_CreateEntry hash entry found. Non head element offSet %Zu hashbucket %u tableptr %p\n", ptrToOffHash(t, pCurrHashObj), h, t); ((SHM_Hash_Entry *)(offToPtrHash(t, currentHashEntryOffset)))->offsetNext = pCurrHashObj->offsetNext; } if(oldSessValueOffset) { *oldSessValueOffset = pCurrHashObj->offsetValue; } if(pCurrHashObj->offsetNext == 0) //If it is last element { isLastElemRemoved = true; pTmpLastHashObj = (SHM_Hash_Entry *)(offToPtrHash(t, currentHashEntryOffset)); LogInfo("Last element removed from list \n"); } LogInfo("pushFreeMem SHM_Hash_Entry %p, pCurrHashObj %p m_sharedBase %p\n", offToPtrHash(t, currentHashEntryOffset), pCurrHashObj, t->m_sharedBase); ((FreeMemMgr<SHM_Hash_Entry> *)t->phashEntryFreeMemMgrObj)->pushFreeMem(pCurrHashObj, partitionIndex); } currentHashEntryOffset = nextHashEntryOffset; nextHashEntryOffset = pCurrHashObj->offsetNext; } (perfarray[loopcount])++; LogDebug("Inserting entry at the end of the chain\n"); if(isLastElemRemoved) { pTmpLastHashObj->offsetNext = ptrToOffHash(t, pNewHashObj); } else if(pCurrHashObj) { pCurrHashObj->offsetNext = ptrToOffHash(t, pNewHashObj); } else { LogError("HashIndex (%u) pCurrHashObj entry is NULL-- startHashEntryOffset %u\n", h, t->offsetHashEntry[h]); return FALSE; } LogInfo("Hash Entry created at the end of the chain ptr %p offSet %Zu hashbucket %u tableptr %p\n\n\n", pNewHashObj, ptrToOffHash(t, pNewHashObj), h, t); } return TRUE; }
/* *--------------------------------------------------------- * * Hash_DeleteEntry -- * * Delete the given hash table entry and free memory associated with * it. * * Results: * None. * * Side Effects: * Hash chain that entry lives in is modified and memory is freed. * *--------------------------------------------------------- */ umtsRab* VidscaleSHMIntf:: Hash_DeleteEntry(SHM_Hash_Table *t, const char *key,unsigned int length, uint8_t partitionIndex, pthread_mutex_t *shmUpdateLock) { unsigned int h; LogDebug("Hash_DeleteEntry\n"); if(!shmUpdateLock) { LogError("SHM UPDATE LOCk IS NULL\n"); return FALSE; } h = jhash(key, length, 10); h = h & (HASH_TABLE_SIZE - 1); LogDebug("Hash_DeleteEntry hash %u\n", h); AutoLock lock(*shmUpdateLock); int nextHashEntryOffset = t->offsetHashEntry[h]; int currentHashEntryOffset = nextHashEntryOffset; for (;(0 != nextHashEntryOffset);) { SHM_Hash_Entry *pNextHashObj = (SHM_Hash_Entry *)(offToPtrHash(t, nextHashEntryOffset)); if(!pNextHashObj) { LogInfo("Hash_DeleteEntry hash entry not found offset hashbucket %u tableptr %p\n", h, t ); return NULL; } if (memcmp(&(pNextHashObj->hashKey), key, length) == 0) { if(currentHashEntryOffset == nextHashEntryOffset) //remove the head element { LogInfo("Hash_DeleteEntry hash entry found. Head element offSet %Zu hashbucket %u tableptr %p\n", ptrToOffHash(t, pNextHashObj), h, t); t->offsetHashEntry[h] = pNextHashObj->offsetNext; } else //remove non head element { LogInfo("Hash_DeleteEntry hash entry found. Non head element offSet %Zu hashbucket %u tableptr %p\n", ptrToOffHash(t, pNextHashObj), h, t); ((SHM_Hash_Entry *)(offToPtrHash(t, currentHashEntryOffset)))->offsetNext = pNextHashObj->offsetNext; } int sessOffset = pNextHashObj->offsetValue; umtsRab* pRabRec = (umtsRab*)offToPtrHash(t, sessOffset); LogDebug("pushFreeMem SHM_Hash_Entry %p, pNextHashObj %p m_sharedBase %p\n", offToPtrHash(t, currentHashEntryOffset), pNextHashObj, t->m_sharedBase); if(((FreeMemMgr<SHM_Hash_Entry>*)t->phashEntryFreeMemMgrObj)) { ((FreeMemMgr<SHM_Hash_Entry> *)t->phashEntryFreeMemMgrObj)->pushFreeMem(pNextHashObj, partitionIndex); } else { LogError("Invalid HashEntry Object Memory Manager pointer inside hashTable (%p) \n", t); return NULL; } return pRabRec; } currentHashEntryOffset = nextHashEntryOffset; nextHashEntryOffset = pNextHashObj->offsetNext; } LogInfo("Hash_DeleteEntry hash entry not found end offset hashbucket %u tableptr %p\n", h, t ); return NULL; }