Ejemplo n.º 1
0
static void
_nc_table_insert_type(table_t *tin, int type, char *key, const char *ckey, void *datum)
{
	table_private_t *t;
	table_node_t *n;
	uint32_t b;

	if (tin == NULL) return;
	if ((key == NULL) && (ckey == NULL)) return;
	if (datum == NULL) return;

	t = (table_private_t *)tin;
	if (t->type == KEY_UNKNOWN) t->type = type;
	else os_assumes(t->type == type);

	n = (table_node_t *)malloc(sizeof(table_node_t));

	if (key != NULL)
	{
		b = hash_key(t->bucket_count, key);
		n->key.string = key;
	}
	else
	{
		b = hash_key(t->bucket_count, ckey);
		n->key.const_string = ckey;
	}

	n->datum = datum;
	n->next = t->bucket[b];
	t->bucket[b] = n;
}
Ejemplo n.º 2
0
int Hash_Add(Hash_Table *M(hash_table), char *entry)
{ Table *table = T(hash_table);
  int key, chain, len;

  key   = hash_key(entry) % table->size;
  chain = table->vector[key];
  while (chain >= 0)
    { if (strcmp(table->strings + table->cells[chain].text,entry) == 0)
        return (-1);
      chain = table->cells[chain].next;
    }

  if (table->count+1 > table->size*CELL_RATIO)
    { table = double_hash_table(table);
      key   = hash_key(entry) % table->size;
    }

  chain = table->count;
  table->cells[chain].next = table->vector[key];
  table->vector[key] = chain;

  len = strlen(entry) + 1;
  if (table->strtop + len > table->strmax)
    { int size = table->size;
      int smax = (table->strtop + len) * (CELL_RATIO * table->size / table->count) * 1.1 + 1000;
      allocate_table_strings(table,smax,"Hash_Add");
      table->strmax = smax;
    }
  strcpy(table->strings + table->strtop, entry);
  table->cells[chain].text = table->strtop;
  table->strtop += len;
  return (table->count++);
}
int rpc_memcached_forward (void **IP, void **Data) {
  const char *key = *Data;
  int key_len = (long)*(Data + 1);
  int z = 0;
  if (key_len >= 4 && *key == '#' && *(key + 1) == '#')  {
    z = 2;
    while (z < key_len && key[z] != '#') {
      z ++;
    }
    if (z < key_len - 1 && key[z] == '#' && key[z + 1] == '#') {
      z += 2;
    } else {
      z = 0;
    }
    if (z >= key_len) {
      z = 0;
    }
  }
  long long hash = hash_key (key + z, key_len - z);
  if (CC->step > 0) {
    hash /= CC->step;
  }
  struct rpc_cluster_bucket *B = &CC->buckets[hash % CC->tot_buckets];
  int i = 0;
  char key_buffer[key_len + 2];
  while (B->methods->get_state (B) < 0) {
    if (!i) {
      memcpy (key_buffer+2, key + z, key_len - z);
      key_buffer[1] = '0';
      key_buffer[0] = '0';
    }
    if (++i > MAX_RETRIES) {
      *(Data + 2) = 0;
      return 0;
    }
    key_buffer[1]++;
    if (i < 10) {
      hash += hash_key (key_buffer+1, key_len + 1 - z);
    } else {
      if (key_buffer[1] == ':') {
        key_buffer[1] = '0';
        key_buffer[0]++;
      }
      hash += hash_key (key_buffer, key_len + 2 - z);
    }
    B = &CC->buckets[hash % CC->tot_buckets];
  }
  if (B) {
    *(Data + 2) = B;
    return 0;
  } else {
    *(Data + 2) = 0;
    return -1;
  }
}
void Hash::insert_key(char * key)
{
	p_hash_element p, q; 

	if(this->find_key(key)) 
		return;
	
	p = (hash_element *) malloc(sizeof(hash_element));
	p->key = (char *)malloc(strlen(key));
	memcpy(p->key, key, sizeof(p->key));
	p->next = NULL;
  
	int hash = hash_key(key);
  
	if(m_hash_list[hash] == NULL)
		m_hash_list[hash] = p;
	else
	{   
		q = m_hash_list[hash];
		while(q->next != NULL){
			q = q->next;
		}
		q->next = p;
  }
}
Ejemplo n.º 5
0
void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
                       unsigned int *keylen, u8 *out)
{
	unsigned int i;
	struct scatterlist tmp;
	char *opad = tfm->crt_digest.dit_hmac_block;
	
	if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
		hash_key(tfm, key, *keylen);
		*keylen = crypto_tfm_alg_digestsize(tfm);
	}

	crypto_digest_final(tfm, out);

	memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
	memcpy(opad, key, *keylen);
		
	for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
		opad[i] ^= 0x5c;

	tmp.page = virt_to_page(opad);
	tmp.offset = offset_in_page(opad);
	tmp.length = crypto_tfm_alg_blocksize(tfm);

	crypto_digest_init(tfm);
	crypto_digest_update(tfm, &tmp, 1);
	
	tmp.page = virt_to_page(out);
	tmp.offset = offset_in_page(out);
	tmp.length = crypto_tfm_alg_digestsize(tfm);
	
	crypto_digest_update(tfm, &tmp, 1);
	crypto_digest_final(tfm, out);
}
Ejemplo n.º 6
0
void hash_table_insert(struct hash_table* ht, hash_key_t k, hash_val_t v)
{
    float load_factor = (float)ht->size / ht->capacity;
    if (load_factor >= HASH_TABLE_UPSIZE_LOAD_FACTOR) {
        size_t new_cap = ht->capacity * 2;
        hash_table_resize(ht, new_cap);
    }

    uint32_t hash = hash_key(ht, k);
    for (size_t i = 0; ; ++i) {
        size_t index         = index_from_hash((hash + i), ht->capacity);
        uint32_t cur_hash    = ht->hashes[index];
        struct hash_entry* e = ht->table + index;

        /* Found empty or deleted slot, insert */
        if (cur_hash == 0 || is_deleted(cur_hash)) {
            e->key = k;
            e->val = v;
            ht->hashes[index] = hash;
            ++ht->size;
            return;
        }

        /* Found occupied slot with same hash and key, replace value */
        if (hash == cur_hash && ht->eql_fn(e->key, k)) {
            e->val = v;
            return;
        }
    }
}
Ejemplo n.º 7
0
static int
exp_key( sql_exp *e )
{
	if (e->name)
		return hash_key(e->name);
	return 0;
}
int rpc_persistent_forward (void **IP, void **Data) {
  const char *key = *Data;
  int key_len = (long)*(Data + 1);
  int z = 0;
  if (key_len >= 4 && *key == '#' && *(key + 1) == '#')  {
    z = 2;
    while (z < key_len && key[z] != '#') {
      z ++;
    }
    if (z < key_len - 1 && key[z] == '#' && key[z + 1] == '#') {
      z += 2;
    } else {
      z = 0;
    }
    if (z >= key_len) {
      z = 0;
    }
  }
  long long hash = hash_key (key + z, key_len - z);
  if (CC->step > 0) {
    hash /= CC->step;
  }
  *(Data + 2) = &CC->buckets[hash % CC->tot_buckets];
  return 0;
}
void Hash::delete_key(char * key)
{
	p_hash_element p, q; 
	int hash = hash_key(key);
	p = m_hash_list[hash];
	if(p != NULL){
		if(strcmp(p->key, key)==0)
		{
			m_hash_list[hash] = m_hash_list[hash]->next;//第一个节点 
			free(p);
		}
		else
		{
			while(p->next != NULL){
				if(strcmp(p->key, key)==0)
				{
					q = p->next;
					p->next = q->next;
					free(q);
				}
				else
				{
					p = p->next;
				}  
			}
		} 
	}
}
Ejemplo n.º 10
0
/*
  DESCRIPTION
    inserts a new element to a hash. it will have a _copy_ of
    data, not a pointer to it.

  RETURN
    0 - inserted
    1 - didn't (unique key conflict)
   -1 - out of memory

  NOTE
    see linsert() for pin usage notes
*/
int lf_hash_insert(LF_HASH *hash, LF_PINS *pins, const void *data)
{
  int csize, bucket, hashnr;
  LF_SLIST *node, * volatile *el;

  lf_rwlock_by_pins(pins);
  node= (LF_SLIST *)_lf_alloc_new(pins);
  if (unlikely(!node))
    return -1;
  memcpy(node+1, data, hash->element_size);
  node->key= hash_key(hash, (uchar *)(node+1), &node->keylen);
  hashnr= calc_hash(hash, node->key, node->keylen);
  bucket= hashnr % hash->size;
  el= _lf_dynarray_lvalue(&hash->array, bucket);
  if (unlikely(!el))
    return -1;
  if (*el == NULL && unlikely(initialize_bucket(hash, el, bucket, pins)))
    return -1;
  node->hashnr= my_reverse_bits(hashnr) | 1; /* normal node */
  if (linsert(el, hash->charset, node, pins, hash->flags))
  {
    _lf_alloc_free(pins, node);
    lf_rwunlock_by_pins(pins);
    return 1;
  }
  csize= hash->size;
  if ((my_atomic_add32(&hash->count, 1)+1.0) / csize > MAX_LOAD)
    my_atomic_cas32(&hash->size, &csize, csize*2);
  lf_rwunlock_by_pins(pins);
  return 0;
}
Ejemplo n.º 11
0
Archivo: hash.c Proyecto: Tumas/labs
void*
spellcast_hash_iterator_next(hash_iterator *h_iter)
{
    int bucket, i;
    listElement *element;

    while (h_iter->index != h_iter->htable->buckets) {
        bucket = hash_key(h_iter->htable, h_iter->index);

        while (dlist_size(&h_iter->htable->table[bucket]) != h_iter->inner_index) {
            element = dlist_head(&h_iter->htable->table[bucket]);

            for (i = 0; i < h_iter->inner_index; i++) {
                element = dlist_next(element);
            }

            h_iter->inner_index++;
            return dlist_data(element);
        }

        h_iter->inner_index = 0;
        h_iter->index++;
    }

    h_iter->index = 0;
    return NULL;
}
Ejemplo n.º 12
0
/* caller must hold lock */
static bool
hashtable_check_for_resize(hashtable_t *table)
{
    size_t capacity = (size_t) HASHTABLE_SIZE(table->table_bits);
    if (table->config.resizable &&
        /* avoid fp ops.  should check for overflow. */
        table->entries * 100 > table->config.resize_threshold * capacity) {
        hash_entry_t **new_table;
        size_t new_sz;
        uint i, old_bits;
        /* double the size */
        old_bits = table->table_bits;
        table->table_bits++;
        new_sz = (size_t) HASHTABLE_SIZE(table->table_bits) * sizeof(hash_entry_t*);
        new_table = (hash_entry_t **) hash_alloc(new_sz);
        memset(new_table, 0, new_sz);
        /* rehash the old table into the new */
        for (i = 0; i < HASHTABLE_SIZE(old_bits); i++) {
            hash_entry_t *e = table->table[i];
            while (e != NULL) {
                hash_entry_t *nexte = e->next;
                uint hindex = hash_key(table, e->key);
                e->next = new_table[hindex];
                new_table[hindex] = e;
                e = nexte;
            }
        }
        hash_free(table->table, capacity * sizeof(hash_entry_t*));
        table->table = new_table;
        return true;
    }
    return false;
}
Ejemplo n.º 13
0
void *
_nc_table_find_get_key(table_t *tin, const char *key, const char **shared_key)
{
	table_private_t *t;
	table_node_t *n;
	uint32_t b;

	if (tin == NULL) return NULL;
	if (key == NULL) return NULL;

	if (shared_key != NULL) *shared_key = NULL;

	t = (table_private_t *)tin;
	b = hash_key(t->bucket_count, key);

	for (n = t->bucket[b]; n != NULL; n = n->next)
	{
		if ((n->key.string != NULL) && (!strcmp(key, n->key.string)))
		{
			if (shared_key != NULL) *shared_key = n->key.const_string;
			return n->datum;
		}
	}

	return NULL;
}
Ejemplo n.º 14
0
// -----------------------------------------------------------------------------
bool HashTable::Insert(int key) noexcept
{
    bool result = false;

    if (_capacity == 0) {
        return result;
    }

    size_t index = hash_key(key);
    while (true) {
        auto & slot = _storage_array[index];
        if ((!slot.occupied) || (slot.occupied && slot.deleted)) {
            // Find a slot to save the value.
            slot.value = key;
            slot.occupied = true;
            slot.deleted = false;
            // Decrease the capacity
            --_capacity;
            // Set the insertion result.
            result = true;
            break;
        } else if (slot.occupied && !slot.deleted && slot.value != key) {
            // We haven't found the key yet and it seems the positions right
            // to it may have the key. Continue the search.
            index = adjust_index(index);
        } else {
            // Already inserted.
            result = true;
            break;
        }
    }

    return result;
}
Ejemplo n.º 15
0
// -----------------------------------------------------------------------------
size_t HashTable::find(int key) const noexcept
{
    // The index of the key in the internal array.
    size_t ret_index = (size_t)(-1);
    size_t index = hash_key(key);
    size_t start_index = index;
    while (true) {
        const auto & slot = _storage_array[index];
        if (!slot.occupied) {
            // If the mapped slot has never been occupied, then the key doesn't
            // exist in the hash table.
            break;
        } else if (slot.deleted || (!slot.deleted && slot.value != key)) {
            // If the current element has been deleted, the element next to it
            // might be one that was stored there because of earlier collision,
            // so the search should continue.
            // If the current element has not been deleted but the value is
            // not key, just continue the search.
            index = adjust_index(index);
            if (index == start_index) {
                // If we've searched all the elements but still haven't found
                // the key, that means it doesn't exist in the table.
                break;
            }
        } else {
            // The slot has not been deleted and its value is the key, then
            // this is the element we want.
            ret_index = index;
            break;
        }
    }

    return ret_index;
}
Ejemplo n.º 16
0
int nn_hash_insert (hash *self, void *key, hash_item *item)
{
    hash_item *it;
    uint32_t i;

    nn_assert (item->next == NN_HASH_NOTINHASH);
    i = hash_key(self, key) % self->slots;

    for (it = self->array[i]; it != NULL; it = it->next) 
    {
        if(hash_compare_keys(self, key, it->key))
            return -1;                 //该key 已经存在
    }

    item->key = key;
    item->next = self->array[i];
    self->array[i] = item;

    ++self->items;

    /*  If the hash is getting full, double the amount of slots and
        re-hash all the items. */
    if (nn_slow (self->items > self->slots * 2 && self->slots < 0x80000000))
        nn_hash_rehash(self);
    return 0;
}
Ejemplo n.º 17
0
int nn_hash_erase (hash *self, hash_item *item)
{
    hash_item *it;
    hash_item *prev;
    uint32_t slot;

    nn_assert (item->next != NN_HASH_NOTINHASH);
    slot = hash_key(self, item->key) % self->slots;

    prev = NULL;
    for (it = self->array[slot]; it != NULL; it = it->next) 
    {
        if(it == item)
            break;
        prev = it;
    }
    if(it != item)
        return -1;

    if(prev == NULL)
        self->array[slot] = item->next;
    else
        prev->next = it->next;

    --self->items;
    item->next = NN_HASH_NOTINHASH;

    return 0;    
}
Ejemplo n.º 18
0
bool
hashtable_remove(hashtable_t *table, void *key)
{
    bool res = false;
    hash_entry_t *e, *prev_e;
    uint hindex = hash_key(table, key);
    if (table->synch)
        dr_mutex_lock(table->lock);
    for (e = table->table[hindex], prev_e = NULL; e != NULL; prev_e = e, e = e->next) {
        if (keys_equal(table, e->key, key)) {
            if (prev_e == NULL)
                table->table[hindex] = e->next;
            else
                prev_e->next = e->next;
            if (table->str_dup)
                hash_free(e->key, strlen((const char *)e->key) + 1);
            if (table->free_payload_func != NULL)
                (table->free_payload_func)(e->payload);
            hash_free(e, sizeof(*e));
            res = true;
            table->entries--;
            break;
        }
    }
    if (table->synch)
        dr_mutex_unlock(table->lock);
    return res;
}
Ejemplo n.º 19
0
bool
hashtable_add(hashtable_t *table, void *key, void *payload)
{
    uint hindex = hash_key(table, key);
    hash_entry_t *e;
    /* if payload is null can't tell from lookup miss */
    ASSERT(payload != NULL, "hashtable_add internal error");
    if (table->synch)
        dr_mutex_lock(table->lock);
    for (e = table->table[hindex]; e != NULL; e = e->next) {
        if (keys_equal(table, e->key, key)) {
            /* we have a use where payload != existing entry so we don't assert on that */
            if (table->synch)
                dr_mutex_unlock(table->lock);
            return false;
        }
    }
    e = (hash_entry_t *) hash_alloc(sizeof(*e));
    if (table->str_dup) {
        const char *s = (const char *) key;
        e->key = hash_alloc(strlen(s)+1);
        strncpy((char *)e->key, s, strlen(s)+1);
    } else
        e->key = key;
    e->payload = payload;
    e->next = table->table[hindex];
    table->table[hindex] = e;
    table->entries++;
    hashtable_check_for_resize(table);
    if (table->synch)
        dr_mutex_unlock(table->lock);
    return true;
}
Ejemplo n.º 20
0
void
_nc_table_delete(table_t *tin, const char *key)
{
	table_private_t *t;
	table_node_t *n, *p;
	uint32_t b;

	if (tin == NULL) return;
	if (key == NULL) return;

	t = (table_private_t *)tin;
	os_assumes((t->type == KEY_STR_MINE) || (t->type == KEY_STR_SHARED));

	b = hash_key(t->bucket_count, key);

	p = NULL;
	for (n = t->bucket[b]; n != NULL; n = n->next)
	{
		if ((n->key.string != NULL) && (!strcmp(key, n->key.string)))
		{
			if (p == NULL) t->bucket[b] = n->next;
			else p->next = n->next;
			if (t->type == KEY_STR_MINE) free(n->key.string);
			free(n);
			return;
		}
		p = n;
	}
}
Ejemplo n.º 21
0
static Table *double_hash_table(Table *table)
{ int size, smax;

  size = next_prime(2*table->size);
  smax = 2.1 * table->strtop + 1000;
 
  allocate_table_vector(table,sizeof(int)*size,"Hash_Add");
  allocate_table_cells(table,sizeof(Hash_Entry)*((int) (size*CELL_RATIO)),"Hash_Add");
  allocate_table_strings(table,smax,"Hash_Add");

  { int        *vector = table->vector;
    Hash_Entry *cells  = table->cells;
    int         c;

    table->size   = size;
    table->strmax = smax;
    for (c = 0; c < size; c++)
      vector[c] = -1;
    for (c = 0; c < table->count; c++)
      { int key = hash_key(table->strings + cells[c].text) % size;
        cells[c].next = vector[key];
        vector[key] = c;
      }
  }

  return (table);
}
Ejemplo n.º 22
0
int dot_fits (const char *const key, int key_len) {
  char *dot_pos = memchr (key, '.', key_len);
  if (dot_pos) {
    key_len = dot_pos - key;
  }
  unsigned id = hash_key (key, key_len);
  return id % copy_mod == copy_rem;
}
Ejemplo n.º 23
0
/**********************************************************************************************************************
	CProcessBase::Handle_Message -- central message handling dispatcher

		key -- message sender
		message -- the process message to handle
					
**********************************************************************************************************************/
void CProcessBase::Handle_Message( EProcessID::Enum process_id, const shared_ptr< const IProcessMessage > &message )
{
	const IProcessMessage *msg_base = message.get();

	Loki::TypeInfo hash_key( typeid( *msg_base ) );
	auto iter = MessageHandlers.find( hash_key );
	FATAL_ASSERT( iter != MessageHandlers.end() );

	iter->second->Handle_Message( process_id, message );
}
Ejemplo n.º 24
0
static Uint32 get_colorhash(struct color_hash *hash, const char *key, int cpp)
{
	struct hash_entry *entry = hash->table[hash_key(key, cpp, hash->size)];
	while(entry) {
		if(memcmp(key, entry->key, cpp) == 0)
			return entry->color;
		entry = entry->next;
	}
	return 0;		/* garbage in - garbage out */
}
Ejemplo n.º 25
0
// -----------------------------------------------------------------------------
bool HashLinkedList::Search(int key) const noexcept
{
    size_t index = hash_key(key);

    const auto & lst = _storage[index];

    auto itor = std::find(lst.begin(), lst.end(), key);

    return (itor != lst.end());
}
Ejemplo n.º 26
0
void _tnl_UpdateFixedFunctionProgram( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct state_key *key;
   GLuint hash;
   const struct gl_vertex_program *prev = ctx->VertexProgram._Current;

   if (!ctx->VertexProgram._Current ||
       ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) {
      struct gl_vertex_program *newProg;

      /* Grab all the relevent state and put it in a single structure:
       */
      key = make_state_key(ctx);
      hash = hash_key(key);

      /* Look for an already-prepared program for this state:
       */
      newProg = search_cache( tnl->vp_cache, hash, key, sizeof(*key));
   
      /* OK, we'll have to build a new one:
       */
      if (!newProg) {

	 if (0)
	    _mesa_printf("Build new TNL program\n");
	 
	 newProg = (struct gl_vertex_program *)
	    ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0); 

	 create_new_program( key, newProg, ctx->Const.VertexProgram.MaxTemps );

	 if (ctx->Driver.ProgramStringNotify)
	    ctx->Driver.ProgramStringNotify( ctx, GL_VERTEX_PROGRAM_ARB, 
                                             &newProg->Base );

         /* Our ownership of newProg is transferred to the cache */
	 cache_item(ctx, tnl->vp_cache, hash, key, newProg);
      }
      else {
	 FREE(key);
      }

      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, newProg);
      _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, newProg);
   }

   /* Tell the driver about the change.  Could define a new target for
    * this?
    */
   if (ctx->VertexProgram._Current != prev && ctx->Driver.BindProgram) {
      ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
                            (struct gl_program *) ctx->VertexProgram._Current);
   }
}
Ejemplo n.º 27
0
static int add_colorhash(struct color_hash *hash,
                         char *key, int cpp, Uint32 color)
{
	int index = hash_key(key, cpp, hash->size);
	struct hash_entry *e = hash->next_free++;
	e->color = color;
	e->key = key;
	e->next = hash->table[index];
	hash->table[index] = e;
	return 1;
}
Ejemplo n.º 28
0
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
{
    struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
                                    - sizeof(ev_io));

    LOGE("UDP connection timeout");

    char *key = hash_key(remote_ctx->addr_header,
                         remote_ctx->addr_header_len, &remote_ctx->src_addr);
    cache_remove(remote_ctx->server_ctx->conn_cache, key);
}
Ejemplo n.º 29
0
char *ht_get(struct hash_cell **hash_t, char* key)
{
	struct hash_cell *temp = hash_t[hash_key(key)];
	do {
		if (strcmp(temp->key, key) == 0)
			return temp->val;
		else 
			temp = temp->next;	
	}while(temp != NULL);
	return NULL;
}
Ejemplo n.º 30
0
static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
{
    remote_ctx_t *remote_ctx = (remote_ctx_t *)(((void *)watcher)
                                                - sizeof(ev_io));

    if (verbose) {
        LOGI("[udp] connection timeout");
    }

    char *key = hash_key(remote_ctx->af, &remote_ctx->src_addr);
    cache_remove(remote_ctx->server_ctx->conn_cache, key, HASH_KEY_LEN);
}