// ------------------------------------------------------------ static uint32_t Qaullib_File_LL_Hashing (unsigned char *filehash) { uint32_t hash; hash = jenkins_hash((const uint8_t *)filehash, MAX_HASH_LEN); //printf("Qaullib_File_LL_Hashing mask: %u u: %u\n", HASHMASK, hash & HASHMASK); return hash & HASHMASK; }
unsigned long hash(char const *sym) { ub4 hashval; hashval = jenkins_hash((ub1 *)sym, (ub4)strlen(sym)); return (hashval); }
void slabs_alloc_test(void) { unsigned int total_chunk = 0; const char *key = "charliezhao"; size_t nkey = strlen(key) + 1; item *ptr = (item *)slabs_alloc(1024, slabs_clsid(1024), &total_chunk); strcpy(ITEM_key(ptr), key); ptr->nkey = nkey; strcpy(ITEM_data(ptr), "xuechaozhao"); uint32_t hv = jenkins_hash(key, strlen(key)); assoc_insert(ptr, hv); for(int i = 0; i <= 10922; ++i) { void *ptr = slabs_alloc(96, slabs_clsid(96), &total_chunk); if(ptr == NULL) { fprintf(stderr, "i: %7d slabs_alloc fail\n", i); break; } else { slabs_free(ptr, 96, slabs_clsid(96)); } } item *ptr2 = assoc_find(key, nkey, hv); fprintf(stdout, "key:%20s value:%20s\n", ITEM_key(ptr2), ITEM_data(ptr2)); }
int item_test() { int maxi = 0; //test set. for(int i = 0; i < 10; i++) { char key[1024]; memset(key, 0, 1024); sprintf(key, "charlie_%d", i); const size_t nkey = strlen(key) + 1; const int flags = 0; const time_t exptime = 0; const int nbytes = 1024; uint32_t cur_hv = jenkins_hash((void *)key, nkey); item *it = do_item_alloc((const char *)key, nkey, flags, exptime, nbytes, cur_hv); if(it == NULL) { fprintf(stderr, "\033[31malloc fail\033[0m"); maxi = i; break; } char val[1024]; sprintf(val, "%d", i); memcpy(ITEM_data(it), (void *)&val, strlen(val)+1); } //test get. for(int i = 0; i < 10; ++i) { char key[1024]; memset(key, 0, 1024); sprintf(key, "charlie_%d", i); const size_t nkey = strlen(key) + 1; uint32_t cur_hv = jenkins_hash((void *)key, nkey); item *it = assoc_find(key, nkey, cur_hv); if(it == NULL) { fprintf(stderr, "\033[31mget fail\033[0m"); return -1; } int val = 0; memcpy((void *)&val, ITEM_data(it), sizeof(val)); if(i&0x1) { fprintf(stdout, "del key:%s value:%d\n", ITEM_key(it), val); do_item_unlink(it, cur_hv); lru_traverse(NULL); } } return 0; }
/** * Hashing function. Creates a key based on an IP address. * @param address the address to hash * @return the hash(a value in the (0 to HASHMASK-1) range) */ uint32_t olsr_ip_hashing(const union olsr_ip_addr * address) { uint32_t hash; switch (olsr_cnf->ip_version) { case AF_INET: hash = jenkins_hash((const uint8_t *)&address->v4, sizeof(uint32_t)); break; case AF_INET6: hash = jenkins_hash((const uint8_t *)&address->v6, sizeof(struct in6_addr)); break; default: hash = 0; break; } return hash & HASHMASK; }
/* Function: esl_keyhash_Store() * Synopsis: Store a key and get a key index for it. * * Purpose: Store a string <key> of length <n> in the key index hash table <kh>. * Associate it with a unique key index, counting from * 0. It's this index that lets us map the hashed keys to * integer-indexed C arrays, clumsily emulating Perl's * hashes. Optionally returns the index through <opt_index>. * * <key>, <n> follow the standard idiom for strings and * unterminated buffers. * * Returns: <eslOK> on success; stores <key> in <kh>; <opt_index> is * returned, set to the next higher index value. * Returns <eslEDUP> if <key> was already stored in the table; * <opt_index> is set to the existing index for <key>. * * Throws: <eslEMEM> on allocation failure, and sets <opt_index> to -1. */ int esl_keyhash_Store(ESL_KEYHASH *kh, const char *key, esl_pos_t n, int *opt_index) { uint32_t val = jenkins_hash(key, n, kh->hashsize); int idx; int status; if (n == -1) n = strlen(key); /* Was this key already stored? */ for (idx = kh->hashtable[val]; idx != -1; idx = kh->nxt[idx]) if (esl_memstrcmp(key, n, kh->smem + kh->key_offset[idx])) { if (opt_index != NULL) *opt_index = idx; return eslEDUP; } /* Reallocate key ptr/index memory if needed */ if (kh->nkeys == kh->kalloc) { ESL_REALLOC(kh->key_offset, sizeof(int)*kh->kalloc*2); ESL_REALLOC(kh->nxt, sizeof(int)*kh->kalloc*2); kh->kalloc *= 2; } /* Reallocate key string memory if needed */ while (kh->sn + n + 1 > kh->salloc) { ESL_REALLOC(kh->smem, sizeof(char) * kh->salloc * 2); kh->salloc *= 2; } /* Copy the key, assign its index */ idx = kh->nkeys; kh->key_offset[idx] = kh->sn; kh->sn += n+1; esl_memstrcpy(key, n, kh->smem + kh->key_offset[idx]); kh->nkeys++; /* Insert new element at head of the approp linked list in hashtable */ kh->nxt[idx] = kh->hashtable[val]; kh->hashtable[val] = idx; /* Time to upsize? If we're 3x saturated, expand the hash table */ if (kh->nkeys > 3*kh->hashsize) if ((status = key_upsize(kh)) != eslOK) goto ERROR; if (opt_index != NULL) *opt_index = idx; return eslOK; ERROR: if (opt_index != NULL) *opt_index = -1; return status; }
int main() { int n,m,i,j,q,num,flag=0,falsecases=0; float probfalse; unsigned int x,y,z; scanf("%d",&m); n=14.43*m+1; for(i=0;i<m;i++) { scanf("%d",&numarr[i]); x=murmurhash3_32(numarr[i],n); y=fnv1(numarr[i],n); z=jenkins_hash(numarr[i],n); bitarr[x]=1; bitarr[y]=1; bitarr[z]=1; bitarr[hash4(x,y,z)]=1; bitarr[hash5(x,y,z)]=1; bitarr[hash6(x,y,z)]=1; bitarr[hash7(x,y,z)]=1; bitarr[hash8(x,y,z)]=1; bitarr[hash9(x,y,z)]=1; bitarr[hash10(x,y,z)]=1; } //printf("Enter no. of queries: "); scanf("%d",&q); for(i=0;i<q;i++) { //printf("Enter value to check: "); scanf("%d",&num); x=murmurhash3_32(num,n); y=fnv1(num,n); z=jenkins_hash(num,n); if(bitarr[x]==1 && bitarr[y]==1 && bitarr[z]==1 && bitarr[hash4(x,y,z)]==1 && bitarr[hash5(x,y,z)]==1 && bitarr[hash6(x,y,z)]==1 && bitarr[hash7(x,y,z)]==1 && bitarr[hash8(x,y,z)]==1 && bitarr[hash9(x,y,z)]==1 && bitarr[hash10(x,y,z)]==1) found++; else notfound++; //printf("Element not in set\n"); } printf("NUMBER OF FLASE OUTPUTS: %d\n",falsecases); probfalse=(falsecases*100)/q; printf("PROBABILITY OF FLASE OUTPUTS: %.2f %\n",probfalse); printf("No. actually there: %d\nNo. actually not there: %d\n",found,notfound); return 0; }
cmph_uint32 hash(hash_state_t *state, const char *key, cmph_uint32 keylen) { switch (state->hashfunc) { case CMPH_HASH_JENKINS: return jenkins_hash((jenkins_state_t *)state, key, keylen); default: assert(0); } assert(0); return 0; }
/* Function: esl_keyhash_Lookup() * Synopsis: Look up a key's array index. * * Purpose: Look up a <key> in the hash table <kh>. * If <key> is found, return <eslOK>, and optionally set <*opt_index> * to its array index (0..nkeys-1). * If <key> is not found, return <eslENOTFOUND>, and * optionally set <*opt_index> to -1. */ int esl_keyhash_Lookup(const ESL_KEYHASH *kh, const char *key, esl_pos_t n, int *opt_index) { uint32_t val = jenkins_hash(key, n, kh->hashsize); int idx; for (idx = kh->hashtable[val]; idx != -1; idx = kh->nxt[idx]) if (strcmp(key, kh->smem + kh->key_offset[idx]) == 0) { if (opt_index != NULL) *opt_index = idx; return eslOK; } if (opt_index != NULL) *opt_index = -1; return eslENOTFOUND; }
static inline size_t hash(size_t capacity, char *key) { size_t hash = jenkins_hash(key, strlen(key)); return hash % capacity; }
/* * osprd_ioctl(inode, filp, cmd, arg) * Called to perform an ioctl on the named file. */ int osprd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, char *passwd) { osprd_info_t *d = file2osprd(filp); // device info int r = 0; // return value: initially 0 // is file open for writing? int filp_writable = (filp->f_mode & FMODE_WRITE) != 0; // This line avoids compiler warnings; you may remove it. (void) filp_writable, (void) d; // Set 'r' to the ioctl's return value: 0 on success, negative on error //eprintk("cmd: %d\n", cmd); if (cmd == OSPRDIOCACQUIRE) { // EXERCISE: Lock the ramdisk. // // If *filp is open for writing (filp_writable), then attempt // to write-lock the ramdisk; otherwise attempt to read-lock // the ramdisk. // // This lock request must block using 'd->blockq' until: // 1) no other process holds a write lock; // 2) either the request is for a read lock, or no other process // holds a read lock; and // 3) lock requests should be serviced in order, so no process // that blocked earlier is still blocked waiting for the // lock. // // If a process acquires a lock, mark this fact by setting // 'filp->f_flags |= F_OSPRD_LOCKED'. You also need to // keep track of how many read and write locks are held: // change the 'osprd_info_t' structure to do this. // // Also wake up processes waiting on 'd->blockq' as needed. // // If the lock request would cause a deadlock, return -EDEADLK. // If the lock request blocks and is awoken by a signal, then // return -ERESTARTSYS. // Otherwise, if we can grant the lock request, return 0. // 'd->ticket_head' and 'd->ticket_tail' should help you // service lock requests in order. These implement a ticket // order: 'ticket_tail' is the next ticket, and 'ticket_head' // is the ticket currently being served. You should set a local // variable to 'd->ticket_head' and increment 'd->ticket_head'. // Then, block at least until 'd->ticket_tail == local_ticket'. // (Some of these operations are in a critical section and must // be protected by a spinlock; which ones?) // Your code here (instead of the next two lines). //eprintk("Attempting to acquire\n"); unsigned my_ticket; // check deadlock protection osp_spin_lock(&d->mutex); if (d->write_locking_pid == current->pid) { osp_spin_unlock(&d->mutex); return -EDEADLK; } my_ticket = d->ticket_head; d->ticket_head++; osp_spin_unlock(&d->mutex); //eprintk("pid = %d\n", current->pid); if (filp_writable) { //eprintk("write %d\n", d->write_locking_pid); // write lock if (wait_event_interruptible(d->blockq, d->ticket_tail == my_ticket && d->write_locking_pid == 0 && d->read_locking_pids.size == 0 )) { //eprintk("wait_event_interruptible: %d\n", current->pid); //osp_spin_lock(&d->mutex); // if blocked if(d->ticket_tail == my_ticket) { // this process is being served d->ticket_tail = return_valid_ticket( &d->invalid_tickets, d->ticket_tail); wake_up_all(&d->blockq); } else { // not being served // add spin lock for good measure osp_spin_lock(&d->mutex); linked_list_push(&d->invalid_tickets, my_ticket); osp_spin_unlock(&d->mutex); } //osp_spin_unlock(&d->mutex); return -ERESTARTSYS; } else { // acquire the lock //eprintk("acquire write lock\n"); osp_spin_lock(&d->mutex); filp->f_flags |= F_OSPRD_LOCKED; d->write_locking_pid = current->pid; d->ticket_tail = return_valid_ticket( &d->invalid_tickets, d->ticket_tail); osp_spin_unlock(&d->mutex); wake_up_all(&d->blockq); return 0; } } else { //read lock //eprintk("read\n"); if (wait_event_interruptible(d->blockq, d->ticket_tail == my_ticket && d->write_locking_pid == 0)) { //osp_spin_lock(&d->mutex); // if blocked if(d->ticket_tail == my_ticket) { // this process is being served d->ticket_tail = return_valid_ticket( &d->invalid_tickets, d->ticket_tail); wake_up_all(&d->blockq); } else { // not being served // add spin lock for good measure osp_spin_lock(&d->mutex); linked_list_push(&d->invalid_tickets, my_ticket); osp_spin_unlock(&d->mutex); } //osp_spin_unlock(&d->mutex); return -ERESTARTSYS; } else { // acquire the lock //eprintk("read lock\n"); osp_spin_lock(&d->mutex); filp->f_flags |= F_OSPRD_LOCKED; linked_list_push(&d->read_locking_pids, current->pid); d->ticket_tail = return_valid_ticket( &d->invalid_tickets, d->ticket_tail); osp_spin_unlock(&d->mutex); wake_up_all(&d->blockq); return 0; } } } else if (cmd == OSPRDIOCTRYACQUIRE) { // EXERCISE: ATTEMPT to lock the ramdisk. // // This is just like OSPRDIOCACQUIRE, except it should never // block. If OSPRDIOCACQUIRE would block or return deadlock, // OSPRDIOCTRYACQUIRE should return -EBUSY. // Otherwise, if we can grant the lock request, return 0. // Your code here (instead of the next two lines). //eprintk("Attempting to try acquire\n"); if (filp_writable) { osp_spin_lock(&d->mutex); if (d->write_locking_pid != 0 || d->read_locking_pids.size != 0) { osp_spin_unlock(&d->mutex); return -EBUSY; } filp->f_flags |= F_OSPRD_LOCKED; d->write_locking_pid = current->pid; osp_spin_unlock(&d->mutex); return 0; } else { osp_spin_lock(&d->mutex); if (d->write_locking_pid != 0) { osp_spin_unlock(&d->mutex); return -EBUSY; } filp->f_flags |= F_OSPRD_LOCKED; linked_list_push(&d->read_locking_pids, current->pid); osp_spin_unlock(&d->mutex); return 0; } } else if (cmd == OSPRDIOCRELEASE) { d->passwd_hash = 0; // EXERCISE: Unlock the ramdisk. // // If the file hasn't locked the ramdisk, return -EINVAL. // Otherwise, clear the lock from filp->f_flags, wake up // the wait queue, perform any additional accounting steps // you need, and return 0. // Your code here (instead of the next line). if (!(filp->f_flags & F_OSPRD_LOCKED)) { // ramdisk isn't locked return -EINVAL; } if (filp_writable) { // see if this process has write lock osp_spin_lock(&d->mutex); if (d->write_locking_pid != current->pid) { return -EINVAL; } //eprintk("release\n"); d->write_locking_pid = 0; if (d->read_locking_pids.size == 0) { //eprintk("unsetting flag\n"); filp->f_flags ^= F_OSPRD_LOCKED; } osp_spin_unlock(&d->mutex); wake_up_all(&d->blockq); return 0; } else { //eprintk("!!!!!reached\n"); if (d->read_locking_pids.size == 0) { return -EINVAL; } osp_spin_lock(&d->mutex); int removeStatus = linked_list_remove(&d->read_locking_pids, current->pid); //eprintk("%d\n", removeStatus); if (removeStatus) { if (d->read_locking_pids.size == 0 && d->write_locking_pid == 0) { filp->f_flags ^= F_OSPRD_LOCKED; } wake_up_all(&d->blockq); //return 0; } osp_spin_unlock(&d->mutex); return removeStatus ? 0 : -EINVAL; } } else if (cmd == OSPRDIOCPASSWD) { char *buf = (char*)kmalloc(20, GFP_ATOMIC); if (copy_from_user(buf, (const char __user*) passwd, 20)) { kfree(buf); return -EFAULT; } d->passwd_hash = jenkins_hash(buf); //eprintk("OSPRDIOCPASSWD: %d\n", d->passwd_hash); return 0; } else { r = -ENOTTY; /* unknown command */ //eprintk("not recognized\n"); } return r; }
uint32_t lhi_hash_jenkins2(const uint8_t *data, uint32_t len) { return jenkins_hash(data, len, 0); }
/* * Classify a packet to queue number using Jenkins hash function. * Return: queue number * the input of the hash are protocol no, perturbation, src IP, dst IP, * src port, dst port, */ static inline int fq_codel_classify_flow(struct mbuf *m, uint16_t fcount, struct fq_codel_si *si) { struct ip *ip; struct tcphdr *th; struct udphdr *uh; uint8_t tuple[41]; uint16_t hash=0; ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off); //#ifdef INET6 struct ip6_hdr *ip6; int isip6; isip6 = (ip->ip_v == 6); if(isip6) { ip6 = (struct ip6_hdr *)ip; *((uint8_t *) &tuple[0]) = ip6->ip6_nxt; *((uint32_t *) &tuple[1]) = si->perturbation; memcpy(&tuple[5], ip6->ip6_src.s6_addr, 16); memcpy(&tuple[21], ip6->ip6_dst.s6_addr, 16); switch (ip6->ip6_nxt) { case IPPROTO_TCP: th = (struct tcphdr *)(ip6 + 1); *((uint16_t *) &tuple[37]) = th->th_dport; *((uint16_t *) &tuple[39]) = th->th_sport; break; case IPPROTO_UDP: uh = (struct udphdr *)(ip6 + 1); *((uint16_t *) &tuple[37]) = uh->uh_dport; *((uint16_t *) &tuple[39]) = uh->uh_sport; break; default: memset(&tuple[37], 0, 4); } hash = jenkins_hash(tuple, 41, HASHINIT) % fcount; return hash; } //#endif /* IPv4 */ *((uint8_t *) &tuple[0]) = ip->ip_p; *((uint32_t *) &tuple[1]) = si->perturbation; *((uint32_t *) &tuple[5]) = ip->ip_src.s_addr; *((uint32_t *) &tuple[9]) = ip->ip_dst.s_addr; switch (ip->ip_p) { case IPPROTO_TCP: th = (struct tcphdr *)(ip + 1); *((uint16_t *) &tuple[13]) = th->th_dport; *((uint16_t *) &tuple[15]) = th->th_sport; break; case IPPROTO_UDP: uh = (struct udphdr *)(ip + 1); *((uint16_t *) &tuple[13]) = uh->uh_dport; *((uint16_t *) &tuple[15]) = uh->uh_sport; break; default: memset(&tuple[13], 0, 4); } hash = jenkins_hash(tuple, 17, HASHINIT) % fcount; return hash; }
hash_t memhash(const char *key, size_t len) { return jenkins_hash(key, len); }