Exemplo n.º 1
0
static void test_hash() {
    int size = 1003;
    struct _hash * hash = hash_create(
                        int_hash,
                        int_cmp,
                        sizeof(int),
                        sizeof(int),
                        size);
    int obj;
    int key=0;

    assert (hash_get(hash, &key, &obj) == NULL);

    for (int i=0; i < 1500; i++) {
        hash_add(hash, &i, &i);
    }

    for (int i=0; i < 1500; i++) {
        hash_get(hash, &i, &obj);
        assert ( obj == i );
    }
    
    key=1500;
    assert ( hash_get(hash, &key, &obj) == NULL );

    // check for-each macro
    struct _bitem * bitem;
    int i=0;
    _for_each_hashitem(hash, bitem) {
        i++;
    }
    assert ( i == 1500 );

    hash_remove(hash);

    // Another hash table
    struct _hash * hash2 = hash_create(
                        int_hash,
                        int_cmp,
                        sizeof(int),
                        sizeof(int),
                        10);

    int a = 0, b = 1;

    key=1;

    hash_add(hash2, &key, &a);
    hash_add(hash2, &key, &b);
    assert ( hash_get(hash2, &key, &obj) != NULL );
    assert ( obj == key );
    hash_remove(hash2);
}
Exemplo n.º 2
0
/* Tests remove for stack allocated entries */
void confirm_remove(hash_table* ht,
                    char* exp_rk, int64_t* exp_rv, bool exp,
                    char* location, int* errors) {
  char* removed_key;
  int64_t* removed_value;
  bool result = hash_remove(ht, exp_rk, (void**) &removed_key,
                            (void**) &removed_value);

  if (exp != result) {
    if (result) {
      printf("Remove error in %s: expeceted table to not remove '%s', "
             "but it did\n", location, exp_rk);
    } else {
      printf("Remove error in %s: expeceted table to remove '%s', "
             "but it did not\n", location, exp_rk);
    }
    (*errors)++;
  }

  if (result || exp) {
    if (((removed_key == NULL || exp_rk == NULL) && removed_key != exp_rk) ||
        (removed_key != NULL && exp_rk != NULL &&
         strcmp(removed_key, exp_rk) != 0)) {
      printf("Incorrect key removed in %s: expeceted '%s', got '%s'\n",
             location, exp_rk, removed_key);
      (*errors)++;
    }
    if (*removed_value != *removed_value) {
      printf("Incorrect value removed %s: expeceted %"
             PRIi64 ", got %" PRIi64 "\n",
             location, *exp_rv, *removed_value);
      (*errors)++;
    }
  }
}
Exemplo n.º 3
0
/// Set a value in a dict. Objects are recursively expanded into their
/// vimscript equivalents. Passing 'nil' as value deletes the key.
///
/// @param dict The vimscript dict
/// @param key The key
/// @param value The new value
/// @param[out] err Details of an error that may have occurred
/// @return the old value, if any
Object dict_set_value(dict_T *dict, String key, Object value, Error *err)
{
  Object rv = OBJECT_INIT;

  if (dict->dv_lock) {
    set_api_error("Dictionary is locked", err);
    return rv;
  }

  if (key.size == 0) {
    set_api_error("Empty dictionary keys aren't allowed", err);
    return rv;
  }

  if (key.size > INT_MAX) {
    set_api_error("Key length is too high", err);
    return rv;
  }

  dictitem_T *di = dict_find(dict, (uint8_t *)key.data, (int)key.size);

  if (value.type == kObjectTypeNil) {
    // Delete the key
    if (di == NULL) {
      // Doesn't exist, fail
      set_api_error("Key doesn't exist", err);
    } else {
      // Return the old value
      rv = vim_to_object(&di->di_tv);
      // Delete the entry
      hashitem_T *hi = hash_find(&dict->dv_hashtab, di->di_key);
      hash_remove(&dict->dv_hashtab, hi);
      dictitem_free(di);
    }
  } else {
    // Update the key
    typval_T tv;

    // Convert the object to a vimscript type in the temporary variable
    if (!object_to_vim(value, &tv, err)) {
      return rv;
    }

    if (di == NULL) {
      // Need to create an entry
      di = dictitem_alloc((uint8_t *) key.data);
      dict_add(dict, di);
    } else {
      // Return the old value
      clear_tv(&di->di_tv);
    }

    // Update the value
    copy_tv(&tv, &di->di_tv);
    // Clear the temporary variable
    clear_tv(&tv);
  }

  return rv;
}
Exemplo n.º 4
0
int
IpConnTrack_conn_delete(IpConnTrack *self, IpConn *conn) {
    hash_remove(&self->m_conn_hash, conn);
    IpConn_clear(conn);
    free(conn);
    return 0;
}
Exemplo n.º 5
0
static void test_load_str() {
    char token[64];

    struct _hash * hash = hash_create(
                        fnv32,
                        str_cmp,
                        sizeof(token),
                        sizeof(int),
                        10000);
    int i, obj;
 
    for (i=0; i < 100000; i++) {
        sprintf(token, "%d", i);
        hash_add(hash, token, &i);
    }
    
    for (i=0; i < 100000; i++) {
        sprintf(token, "%d", i);

        assert ( hash_get(hash, token, &obj) != NULL );
        assert ( obj == i );

        sprintf(token, "%d", ~i);
        assert ( hash_get(hash, token, &obj) == NULL );
    }

    hash_remove(hash);
}
Exemplo n.º 6
0
Arquivo: dict.c Projeto: HarmtH/vim
/*
 * Free a Dictionary, including all non-container items it contains.
 * Ignores the reference count.
 */
    static void
dict_free_contents(dict_T *d)
{
    int		todo;
    hashitem_T	*hi;
    dictitem_T	*di;

    /* Lock the hashtab, we don't want it to resize while freeing items. */
    hash_lock(&d->dv_hashtab);
    todo = (int)d->dv_hashtab.ht_used;
    for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
    {
	if (!HASHITEM_EMPTY(hi))
	{
	    /* Remove the item before deleting it, just in case there is
	     * something recursive causing trouble. */
	    di = HI2DI(hi);
	    hash_remove(&d->dv_hashtab, hi);
	    clear_tv(&di->di_tv);
	    vim_free(di);
	    --todo;
	}
    }
    hash_clear(&d->dv_hashtab);
}
Exemplo n.º 7
0
int
replay_remove (munge_cred_t c)
{
/*  Removes the credential [c] from the replay hash.
 */
    m_msg_t           m = c->msg;
    union replay_key  rkey_st;
    replay_t          rkey = &rkey_st;
    replay_t          r;

    if (!replay_hash) {
        if (conf->got_benchmark)
            return (0);
        errno = EPERM;
        return (-1);
    }
    /*  Compute the cred's "hash key".
     */
    rkey->data.t_expired = (time_t) (m->time0 + m->ttl);
    assert (c->mac_len >= sizeof (rkey->data.mac));
    memcpy (rkey->data.mac, c->mac, sizeof (rkey->data.mac));

    if ((r = hash_remove (replay_hash, rkey))) {
        replay_free (r);
    }
    return (r ? 0 : -1);
}
Exemplo n.º 8
0
void
egl_state_delete_cached_renderbuffer (egl_state_t *egl_state,
                                      GLuint renderbuffer_id)
{
    if (renderbuffer_id != 0)
        hash_remove (egl_state_get_texture_cache (egl_state), renderbuffer_id);
}
Exemplo n.º 9
0
int32_t text_renderer_font_new(text_renderer_p renderer, const char* font_path, size_t font_size) {
	int32_t handle = renderer->fonts->length;
	text_renderer_font_p font = hash_put_ptr(renderer->fonts, handle);
	
	FT_Error error = FT_New_Face(renderer->freetype, font_path, 0, &font->face);
	if (error) {
		printf("FT_New_Face error\n");
		goto failed_new_face;
	}
	
	error = FT_Set_Pixel_Sizes(font->face, 0, font_size);
	if (error) {
		printf("FT_Set_Pixel_Size error\n");
		goto failed_set_pixel_size;
	}
	
	font->cell_refs = hash_of(text_renderer_cell_ref_t);
	
	return handle;
	
	failed_set_pixel_size:
		FT_Done_Face(font->face);
	failed_new_face:
		hash_remove(renderer->fonts, handle);
	return -1;
}
Exemplo n.º 10
0
static void remove_half(int start)
{
	int i;

	for (i = start; i < N; i += 2)
		hash_remove(&hsh, &words[i].node);
}
Exemplo n.º 11
0
Arquivo: if_lua.c Projeto: LeonB/vim
    static int
luaV_dict_newindex (lua_State *L)
{
    dict_T *d = luaV_unbox(L, luaV_Dict, 1);
    char_u *key = (char_u *) luaL_checkstring(L, 2);
    dictitem_T *di;
    if (d->dv_lock)
	luaL_error(L, "dict is locked");
    di = dict_find(d, key, -1);
    if (di == NULL) /* non-existing key? */
    {
	if (lua_isnil(L, 3)) return 0;
	di = dictitem_alloc(key);
	if (di == NULL) return 0;
	if (dict_add(d, di) == FAIL)
	{
		vim_free(di);
		return 0;
	}
    }
    else
	clear_tv(&di->di_tv);
    if (lua_isnil(L, 3)) /* remove? */
    {
	hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
	hash_remove(&d->dv_hashtab, hi);
	dictitem_free(di);
    }
    else {
	typval_T v;
	luaV_totypval(L, 3, &v);
	copy_tv(&v, &di->di_tv);
    }
    return 0;
}
Exemplo n.º 12
0
/*
 * Removes an entry from pre_cache or cache.  Removes from the hash table.
 * Frees off the cache block if necc.
 */
static void del(struct mq_policy *mq, struct entry *e)
{
	queue_remove(&e->list);
	hash_remove(e);
	if (e->in_cache)
		free_cblock(mq, e->cblock);
}
Exemplo n.º 13
0
struct pagemap_map*
pagemap_unmap (struct pagemap* pm, void* vaddr)
{
    struct hash_elem* e;
    struct pagemap_map* mp;
    struct pagemap_map m = 
    {
        HASH_ELEM_INITIALIZER,
        vaddr,
        PAGELOC_MEMORY,
        {0}
    };

    ASSERT (pm != NULL);

    /* Find the record with this virtual address, then delete it if it
       exists.  We return the actual record, since it can be used later
       for eviction (plus we don't have authority to 'free' it since
       we did not allocate it). */
    lock_acquire (&pm->lock);
    e = hash_find (&pm->pm, &m.elem, NULL);
    if (e == NULL)
    {
        lock_release (&pm->lock);
        return NULL;
    }
    hash_remove (&m.elem);
    lock_release (&pm->lock);

    mp = HASH_ENTRY (e, struct pagemap_map, elem);

    return mp;
};
bool quad_hash_table::remove(int x){
	int index = hash_remove(x, 0);
	if (index == -1)
		return false;
	else
		main_list[index].removeItem();
}
Exemplo n.º 15
0
static void test_hash_delete_1() {
    char token[64];

    struct _hash * hash = hash_create(
                        fnv32,
                        str_cmp,
                        sizeof(token),
                        sizeof(int),
                        100);
    int i;

    for (i=0; i < 100; i++) {
        sprintf(token, "%d", i);
        hash_add(hash, token, &i);
    }

    assert ( hash->n_items == 100 );

    for (i=0; i < 100; i++) {
        sprintf(token, "%d", i);
        hash_delete(hash, token, &i);
    }

    assert ( hash->n_items == 0 );

    for (i=0; i < 100; i++) {
        sprintf(token, "%d", i);
        assert ( hash_get(hash, token, &i) == NULL);
    }
   
    hash_remove(hash);
}
Exemplo n.º 16
0
int udp_bind(void *prot_data, i4sockaddr *addr)
{
    udp_endpoint *e = prot_data;
    int err;

    mutex_lock(&e->lock);

    if(e->port == 0) {
        // BUG TODO XXX search to make sure this port isn't used already
        if(addr->port != e->port) {
            // remove it from the hashtable
            mutex_lock(&endpoints_lock);
            hash_remove(endpoints, e);
            e->port = addr->port;
            hash_insert(endpoints, e);
            mutex_unlock(&endpoints_lock);
        }
        err = NO_ERROR;
    } else {
        err = ERR_NET_SOCKET_ALREADY_BOUND;
    }

    mutex_unlock(&e->lock);

    return err;
}
Exemplo n.º 17
0
void
egl_state_delete_cached_framebuffer (egl_state_t *egl_state,
                                     GLuint framebuffer_id)
{
    if (framebuffer_id != 0)
        hash_remove (egl_state_get_texture_cache (egl_state), framebuffer_id);
}
Exemplo n.º 18
0
void
egl_state_delete_cached_texture (egl_state_t *egl_state,
                                  GLuint texture_id)
{
    if (texture_id != 0)
        hash_remove (egl_state_get_texture_cache (egl_state), texture_id);
}
Exemplo n.º 19
0
int
_host_timeout_queue_task(const void *key)
{
    free(hash_remove(hosttable, key, sizeof(HostKey)));

    return 0;
}
Exemplo n.º 20
0
/*
 * Update instance with new peer address
 */
void
update_floated(struct multi_context *m, struct multi_instance *mi,
	       struct mroute_addr real, uint32_t hv)
{
  struct mroute_addr real_old;

  real_old = mi->real;
  generate_prefix (mi);

  /* remove before modifying mi->real, since it also modifies key in hash */
  hash_remove(m->hash, &real_old);
  hash_remove(m->iter, &real_old);

  /* update address */
  memcpy(&mi->real, &real, sizeof(real));

  mi->context.c2.from = m->top.c2.from;
  mi->context.c2.to_link_addr = &mi->context.c2.from;

  /* switch to new log prefix */
  generate_prefix (mi);
  /* inherit buffers */
  mi->context.c2.buffers = m->top.c2.buffers;

  /* inherit parent link_socket and link_socket_info */
  mi->context.c2.link_socket = m->top.c2.link_socket;
  mi->context.c2.link_socket_info->lsa->actual = m->top.c2.from;

  /* fix remote_addr in tls structure */
  tls_update_remote_addr (mi->context.c2.tls_multi, &mi->context.c2.from);
  mi->did_open_context = true;

  hash_add(m->hash, &mi->real, mi, false);
  hash_add(m->iter, &mi->real, mi, false);

  mi->did_real_hash = true;
#ifdef MANAGEMENT_DEF_AUTH
  hash_remove (m->cid_hash, &mi->context.c2.mda_context.cid);
  hash_add (m->cid_hash, &mi->context.c2.mda_context.cid, mi, false);
#endif

#ifdef MANAGEMENT_DEF_AUTH
  mi->did_cid_hash = true;
#endif
}
Exemplo n.º 21
0
void
hash_remove_all (hash_table_type *table, string key)
{
  string *val;
  while ((val = hash_lookup(table, key))) {
    hash_remove(table, key, *val);
    free (val);
  }
}
Exemplo n.º 22
0
static void hna_local_del(struct hna_local_entry *hna_local_entry,
			  char *message)
{
	bat_dbg(DBG_ROUTES, "Deleting local hna entry (%pM): %s\n",
		hna_local_entry->addr, message);

	hash_remove(hna_local_hash, hna_local_entry->addr);
	_hna_local_del(hna_local_entry);
}
Exemplo n.º 23
0
static int free_file(Stream_t *Stream)
{
	DeclareThis(File_t);
	Fs_t *Fs = This->Fs;
	fsPreallocateClusters(Fs, -This->preallocatedClusters);       
	FREE(&This->direntry.Dir);
	freeDirCache(Stream);
	return hash_remove(filehash, (void *) Stream, This->hint);
}
Exemplo n.º 24
0
void
Session::RemoveNode(dev_t device, ino_t id)
{
	struct node *node = _FindNode(device, id);
	if (node != NULL && --node->ref_count <= 0) {
		hash_remove(fNodeHash, node);
		fNodeCount--;
	}
}
Exemplo n.º 25
0
/*!	Writes the specified \a block back to disk. It will always only write back
	the oldest change of the block if it is part of more than one transaction.
	It will automatically send out TRANSACTION_WRITTEN notices, as well as
	delete transactions when they are no longer used, and \a deleteTransaction
	is \c true.
*/
static fssh_status_t
write_cached_block(block_cache* cache, cached_block* block,
	bool deleteTransaction)
{
	cache_transaction* previous = block->previous_transaction;
	int32_t blockSize = cache->block_size;

	void* data = previous && block->original_data
		? block->original_data : block->current_data;
		// we first need to write back changes from previous transactions

	TRACE(("write_cached_block(block %Ld)\n", block->block_number));

	fssh_ssize_t written = fssh_write_pos(cache->fd, block->block_number * blockSize,
		data, blockSize);

	if (written < blockSize) {
		FATAL(("could not write back block %" FSSH_B_PRIdOFF " (%s)\n",
			block->block_number, fssh_strerror(fssh_get_errno())));
		return FSSH_B_IO_ERROR;
	}

	if (data == block->current_data)
		block->is_dirty = false;

	if (previous != NULL) {
		previous->blocks.Remove(block);
		block->previous_transaction = NULL;

		if (block->original_data != NULL && block->transaction == NULL) {
			// This block is not part of a transaction, so it does not need
			// its original pointer anymore.
			cache->Free(block->original_data);
			block->original_data = NULL;
		}

		// Has the previous transation been finished with that write?
		if (--previous->num_blocks == 0) {
			TRACE(("cache transaction %ld finished!\n", previous->id));

			notify_transaction_listeners(cache, previous, FSSH_TRANSACTION_WRITTEN);

			if (deleteTransaction) {
				hash_remove(cache->transaction_hash, previous);
				delete_transaction(cache, previous);
			}
		}
	}
	if (block->transaction == NULL && block->ref_count == 0) {
		// the block is no longer used
		block->unused = true;
		cache->unused_blocks.Add(block);
	}

	return FSSH_B_OK;
}
Exemplo n.º 26
0
Arquivo: masset.c Projeto: bigml/emoon
void masset_finish()
{
    HASH *mh = hash_lookup(g_datah, ASSET_KEY);
    hash_destroy(&mh);
    hash_remove(g_datah, ASSET_KEY);

    uListDestroyFunc(&asset_drivers, masset_driver_free);

    asset_drivers = NULL;
}
Exemplo n.º 27
0
void
thread_free(L4_ThreadId_t thread)
{
    int r;
    struct thread * dead;

    dead = hash_lookup(l4tid_to_thread, thread.raw);

    if (dead != NULL)
    {
        /* Removed both mappings */
        hash_remove(l4tid_to_thread, dead->id.raw);
        hash_remove(l4tid_to_thread, dead->handle.raw);

        /* Add thread back to free pool */
        r = rfl_free(thread_list, L4_ThreadNo(dead->id));
        assert(r == RFL_SUCCESS);
    }
}
Exemplo n.º 28
0
void mem_free( void * ptr ) {

    if( ptr == NULL ) {
        printf( "tried to free a null pointer!\n" );
        return;
    }

    hash_remove( ptr );

}
Exemplo n.º 29
0
void _hna_global_del_orig(struct hna_global_entry *hna_global_entry,
			  char *message)
{
	bat_dbg(DBG_ROUTES, "Deleting global hna entry %pM (via %pM): %s\n",
		hna_global_entry->addr, hna_global_entry->orig_node->orig,
		message);

	hash_remove(hna_global_hash, hna_global_entry->addr);
	kfree(hna_global_entry);
}
int quad_hash_table::hash_remove(int x, int i) {
	if (main_list[(x+(i*i)) % size].isUsed()) {
		if(main_list[(x+(i*i)) % size].getItem() == x){
			return (x+(i*i)) % size;
		} else {
			return hash_remove(x, i+1);	
		}
	} else {
		return -1;
	}
}