Пример #1
0
static void test_hashmap_remove(void) {
        _cleanup_hashmap_free_ Hashmap *m = NULL;
        char *r;

        log_info("%s", __func__);

        r = hashmap_remove(NULL, "key 1");
        assert_se(r == NULL);

        m = hashmap_new(&string_hash_ops);
        assert_se(m);

        r = hashmap_remove(m, "no such key");
        assert_se(r == NULL);

        hashmap_put(m, "key 1", (void*) "val 1");
        hashmap_put(m, "key 2", (void*) "val 2");

        r = hashmap_remove(m, "key 1");
        assert_se(streq(r, "val 1"));

        r = hashmap_get(m, "key 2");
        assert_se(streq(r, "val 2"));
        assert_se(!hashmap_get(m, "key 1"));
}
Пример #2
0
/*
 * shm_disconnect - Disconnects and cleans up the device
 * [shm_t * dev] - Pointer to the device we're going to destroy
 */
static void shm_disconnect(shm_t * dev) {
	if(dev->dev_type == SHM_DEV_SERVER) {
		hashmap_remove(shm_network, dev->shm_key);
		hashmap_remove(shm_client_list, dev->unique_id);
		servers_online--;
	}
}
Пример #3
0
Link *link_free(Link *l) {

        if (!l)
                return NULL;

        if (l->manager) {
                hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));
                hashmap_remove(l->manager->links_by_name, l->ifname);
        }

        free(l->ifname);
        return mfree(l);
 }
Пример #4
0
void button_free(Button *b) {
        assert(b);

        hashmap_remove(b->manager->buttons, b->name);

        if (b->fd >= 0) {
                hashmap_remove(b->manager->button_fds, INT_TO_PTR(b->fd + 1));
                assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0);
                close_nointr_nofail(b->fd);
        }

        free(b->name);
        free(b->seat);
        free(b);
}
void TestHashmaplinked_IterateAndRemoveDoesntBreakIteration(
    CuTest * tc
)
{
    hashmap_t *hm;
    hashmap_t *hm2;
    hashmap_iterator_t iter;
    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 11);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 11);

    hashmap_put(hm, (void *) 50, (void *) 92);
    hashmap_put(hm, (void *) 49, (void *) 91);
    hashmap_put(hm, (void *) 48, (void *) 90);
    hashmap_put(hm, (void *) 47, (void *) 89);
    hashmap_put(hm, (void *) 46, (void *) 88);
    hashmap_put(hm, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 50, (void *) 92);
    hashmap_put(hm2, (void *) 49, (void *) 91);
    hashmap_put(hm2, (void *) 48, (void *) 90);
    hashmap_put(hm2, (void *) 47, (void *) 89);
    hashmap_put(hm2, (void *) 46, (void *) 88);
    hashmap_put(hm2, (void *) 45, (void *) 87);
    /*  the following 3 collide: */
    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
        hashmap_remove(hm,key);
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
    hashmap_freeall(hm2);
}
Пример #6
0
CurlGlue *curl_glue_unref(CurlGlue *g) {
        sd_event_source *io;

        if (!g)
                return NULL;

        if (g->curl)
                curl_multi_cleanup(g->curl);

        while ((io = hashmap_steal_first(g->ios))) {
                int fd;

                fd = sd_event_source_get_io_fd(io);
                assert(fd >= 0);

                hashmap_remove(g->translate_fds, FD_TO_PTR(fd));

                safe_close(fd);
                sd_event_source_unref(io);
        }

        hashmap_free(g->ios);

        sd_event_source_unref(g->timer);
        sd_event_unref(g->event);
        free(g);

        return NULL;
}
Пример #7
0
void route_free(Route *route) {
        if (!route)
                return;

        if (route->network) {
                LIST_REMOVE(routes, route->network->static_routes, route);

                assert(route->network->n_static_routes > 0);
                route->network->n_static_routes--;

                if (route->section)
                        hashmap_remove(route->network->routes_by_section, route->section);
        }

        network_config_section_free(route->section);

        if (route->link) {
                set_remove(route->link->routes, route);
                set_remove(route->link->routes_foreign, route);
        }

        sd_event_source_unref(route->expire);

        free(route);
}
void routing_policy_rule_free(RoutingPolicyRule *rule) {

        if (!rule)
                return;

        if (rule->network) {
                LIST_REMOVE(rules, rule->network->rules, rule);
                assert(rule->network->n_rules > 0);
                rule->network->n_rules--;

                if (rule->section) {
                        hashmap_remove(rule->network->rules_by_section, rule->section);
                        network_config_section_free(rule->section);
                }

        }

        if (rule->manager) {
                set_remove(rule->manager->rules, rule);
                set_remove(rule->manager->rules_foreign, rule);
        }

        free(rule->iif);
        free(rule->oif);
        free(rule);
}
Пример #9
0
void db_destroy_table_groups(ContentTableGroups *groups)
{
    int error;
    ContentTable *table;
    const App *s_app = appe_get_app_instance();
    Logger *logger = s_app->logger;

    DEBUG("db_destroy_tables: BEGIN\n");
    int i;
    for (i = 0; i < DB_MAX_TABLES; i++) {
        error = hashmap_get(groups->hashmap_table_groups, groups->table_names[i], (void**)(&table));
        if (table != NULL) {
            DEBUG("db_destroy_tables: table_name=%s\n", table->table_name);
            db_destroy_table(table);

            error = hashmap_remove(groups->hashmap_table_groups, groups->table_names[i]);
            if (error != MAP_OK) {
                logger->log_e(logger, "DB: Groups Free, Table Removed Error.");
            }
        }

        free(groups->table_names[i]);
        groups->table_names[i] = NULL;
    }

    groups->table_count = 0;

    DEBUG("db_destroy_tables: OK\n");

    hashmap_free(groups->hashmap_table_groups);

    free(groups);
    groups = NULL;
}
void TestHashmaplinked_IterateHandlesCollisions(
    CuTest * tc
)
{
    hashmap_t *hm, *hm2;

    hashmap_iterator_t iter;

    void *key;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hm2 = hashmap_new(__uint_hash, __uint_compare, 4);

    hashmap_put(hm, (void *) 1, (void *) 92);
    hashmap_put(hm, (void *) 5, (void *) 91);
    hashmap_put(hm, (void *) 9, (void *) 90);

    hashmap_put(hm2, (void *) 1, (void *) 92);
    hashmap_put(hm2, (void *) 5, (void *) 91);
    hashmap_put(hm2, (void *) 9, (void *) 90);

    hashmap_iterator(hm, &iter);

    /*  remove every key we iterate on */
    while ((key = hashmap_iterator_next(hm, &iter)))
    {
        CuAssertTrue(tc, NULL != hashmap_remove(hm2, key));
    }

    /*  check if the hashmap is empty */
    CuAssertTrue(tc, 0 == hashmap_count(hm2));
    hashmap_freeall(hm);
}
Пример #11
0
Link *link_free(Link *l) {
        if (!l)
                return NULL;

        /* Send goodbye messages. */
        dns_scope_announce(l->mdns_ipv4_scope, true);
        dns_scope_announce(l->mdns_ipv6_scope, true);

        link_flush_settings(l);

        while (l->addresses)
                (void) link_address_free(l->addresses);

        if (l->manager)
                hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex));

        dns_scope_free(l->unicast_scope);
        dns_scope_free(l->llmnr_ipv4_scope);
        dns_scope_free(l->llmnr_ipv6_scope);
        dns_scope_free(l->mdns_ipv4_scope);
        dns_scope_free(l->mdns_ipv6_scope);

        free(l->state_file);

        return mfree(l);
}
Пример #12
0
int
load_words(struct test *t)
{
	int n;
	char *word;
	int count = 1;

	while ((n = get_word(t, &word)) > 0) {
errno = 0;
		if (hashmap_put(t->words, word, (void *)count) == -1) {
			char *key = word;
			int data;

			if (errno != EEXIST) {
				AMSG("");
				return -1;
			}
			if (hashmap_remove(t->words, (void **)&key, (void **)&data) == -1) {
				AMSG("");
				return -1;
			}
			data++;
			if (hashmap_put(t->words, word, (void *)data) == -1) {
				AMSG("");
				return -1;
			}
		}
	}
	if (n == -1) {
		MMSG("");
		return -1;
	}

	return 0;
}
int link_update_rtnl(Link *l, sd_rtnl_message *m) {
        const char *ifname;
        int r;

        assert(l);
        assert(l->manager);
        assert(m);

        r = sd_rtnl_message_link_get_flags(m, &l->flags);
        if (r < 0)
                return r;

        r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
        if (r < 0)
                return r;

        if (!streq(l->ifname, ifname)) {
                char *new_ifname;

                new_ifname = strdup(ifname);
                if (!new_ifname)
                        return -ENOMEM;

                hashmap_remove(l->manager->links_by_name, l->ifname);
                free(l->ifname);
                l->ifname = new_ifname;

                r = hashmap_put(l->manager->links_by_name, l->ifname, l);
                if (r < 0)
                        return r;
        }

        return 0;
}
Пример #14
0
void cgroup_bonding_free(CGroupBonding *b, bool trim) {
        assert(b);

        if (b->unit) {
                CGroupBonding *f;

                LIST_REMOVE(CGroupBonding, by_unit, b->unit->cgroup_bondings, b);

                if (streq(b->controller, SYSTEMD_CGROUP_CONTROLLER)) {
                        assert_se(f = hashmap_get(b->unit->manager->cgroup_bondings, b->path));
                        LIST_REMOVE(CGroupBonding, by_path, f, b);

                        if (f)
                                hashmap_replace(b->unit->manager->cgroup_bondings, b->path, f);
                        else
                                hashmap_remove(b->unit->manager->cgroup_bondings, b->path);
                }
        }

        if (b->realized && b->ours && trim)
                cg_trim(b->controller, b->path, false);

        free(b->controller);
        free(b->path);
        free(b);
}
Пример #15
0
/*
 * Extract the headers to remove.  These headers were listed in the Connection
 * and Proxy-Connection headers.
 */
static int
remove_connection_headers(hashmap_t hashofheaders)
{
	static char* headers[] = {
		"connection",
		"proxy-connection"
	};

	char *data;
	char* ptr;
	ssize_t len;
	int i;

	for (i = 0; i != (sizeof(headers) / sizeof(char *)); ++i) {
		/* Look for the connection header.  If it's not found, return. */
		len = hashmap_entry_by_key(hashofheaders, headers[i], (void **)&data);
		if (len <= 0)
			return 0;

		/*
		 * Go through the data line and replace any special characters
		 * with a NULL.
		 */
		ptr = data;
		while ((ptr = strpbrk(ptr, "()<>@,;:\\\"/[]?={} \t")))
			*ptr++ = '\0';

		/*
		 * All the tokens are separated by NULLs.  Now go through the
		 * token and remove them from the hashofheaders.
		 */
		ptr = data;
		while (ptr < data + len) {
			hashmap_remove(hashofheaders, ptr);

			/* Advance ptr to the next token */
			ptr += strlen(ptr) + 1;
			while (*ptr == '\0')
				ptr++;
		}

		/* Now remove the connection header it self. */
		hashmap_remove(hashofheaders, headers[i]);
	}

	return 0;
}
Пример #16
0
void dcache_remove(struct _fs_instance_t *instance, struct _dentry_t* dentry, const char * name) {
	if (!instance->fs->unique_inode) return;
	struct key_t key;
	key.instance = instance;
	key.dentry = dentry;
	key.name = name;
	hashmap_remove(map, (struct hashmap_key_t*)&key);
}
Пример #17
0
/*
 * Removes a directory listing from the cache.
 */
static void fscache_remove(struct fsentry *fse)
{
	if (fse->list)
		fse = fse->list;

	for (; fse; fse = fse->next)
		hashmap_remove(&map, fse, NULL);
}
Пример #18
0
/**
 * Remove the peer.
 * Disconnect the peer
 *
 * @todo add disconnection functionality
 *
 * @return 1 on sucess; otherwise 0
 */
int bt_peermanager_remove_peer(void *pm, bt_peer_t* peer)
{
    bt_peermanager_t *me = pm;

//    bt_leeching_choker_add_peer(me->lchoke, peer);
    hashmap_remove(me->peers,peer);
    return 1;
}
Пример #19
0
static int enumerate_dir(Hashmap *top, Hashmap *bottom, const char *path) {
        _cleanup_closedir_ DIR *d;

        assert(top);
        assert(bottom);
        assert(path);

        d = opendir(path);
        if (!d) {
                if (errno == ENOENT)
                        return 0;

                log_error("Failed to enumerate %s: %m", path);
                return -errno;
        }

        for (;;) {
                struct dirent *de;
                union dirent_storage buf;
                int k;
                char *p;

                k = readdir_r(d, &buf.de, &de);
                if (k != 0)
                        return -k;

                if (!de)
                        break;

                if (!dirent_is_file(de))
                        continue;

                p = strjoin(path, "/", de->d_name, NULL);
                if (!p)
                        return -ENOMEM;

                path_kill_slashes(p);

                k = hashmap_put(top, path_get_file_name(p), p);
                if (k >= 0) {
                        p = strdup(p);
                        if (!p)
                                return -ENOMEM;
                } else if (k != -EEXIST) {
                        free(p);
                        return k;
                }

                free(hashmap_remove(bottom, path_get_file_name(p)));
                k = hashmap_put(bottom, path_get_file_name(p), p);
                if (k < 0) {
                        free(p);
                        return k;
                }
        }

        return 0;
}
Пример #20
0
void closeConnection(Channel ch)
{
	int connectionFD = 0;
	connectionFD = (int) hashmap_get(sockets_hmap , ch->port);
	close(connectionFD);
	hashmap_remove(sockets_hmap, ch->port);
	openedSockets--;

}
Пример #21
0
int main(int argc, char* argv) {
    int index;
    int error;
    map_t mymap;
    char key_string[KEY_MAX_LENGTH];
    data_struct_t* value;
    
    mymap = hashmap_new();

    /* First, populate the hash map with ascending values */
    for (index=0; index<KEY_COUNT; index+=1) {
        /* Store the key string along side the numerical value so we can free it later */
        value = malloc(sizeof(data_struct_t));
        snprintf(value->key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);
        value->number = index;

        error = hashmap_put(mymap, value->key_string, value);
        assert(error==MAP_OK);
    }

    /* Now, check all of the expected values are there */
    for (index=0; index<KEY_COUNT; index+=1) {
        snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);

        error = hashmap_get(mymap, key_string, (void**)(&value));
        
        /* Make sure the value was both found and the correct number */
        assert(error==MAP_OK);
        assert(value->number==index);
    }
    
    /* Make sure that a value that wasn't in the map can't be found */
    snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, KEY_COUNT);

    error = hashmap_get(mymap, key_string, (void**)(&value));
        
    /* Make sure the value was not found */
    assert(error==MAP_MISSING);

    /* Free all of the values we allocated and remove them from the map */
    for (index=0; index<KEY_COUNT; index+=1) {
        snprintf(key_string, KEY_MAX_LENGTH, "%s%d", KEY_PREFIX, index);

        error = hashmap_get(mymap, key_string, (void**)(&value));
        assert(error==MAP_OK);

        error = hashmap_remove(mymap, key_string);
        assert(error==MAP_OK);

        free(value);        
    }
    
    /* Now, destroy the map */
    hashmap_free(mymap);

    return 1;
}
Пример #22
0
void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
{
	if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
		return;
	ce->ce_flags &= ~CE_HASHED;
	hashmap_remove(&istate->name_hash, ce, ce);

	if (ignore_case)
		remove_dir_entry(istate, ce);
}
void TestHashmaplinked_RemoveReturnsNullIfMissingAndTraversesChain(
    CuTest * tc
)
{
    hashmap_t *hm;
    unsigned long val;

    hm = hashmap_new(__uint_hash, __uint_compare, 4);
    hashmap_put(hm, (void *) 1, (void *) 92);

    val = (unsigned long) hashmap_remove(hm, (void *) 5);
    CuAssertTrue(tc, 0 == val);

    val = (unsigned long) hashmap_remove(hm, (void *) 1);
    CuAssertTrue(tc, 0 != val);
    CuAssertTrue(tc, val == 92);

    hashmap_freeall(hm);
}
Пример #24
0
void device_free(Device *d) {
        assert(d);

        device_detach(d);

        hashmap_remove(d->manager->devices, d->sysfs);

        free(d->sysfs);
        free(d);
}
Пример #25
0
static DynamicUser* dynamic_user_free(DynamicUser *d) {
        if (!d)
                return NULL;

        if (d->manager)
                (void) hashmap_remove(d->manager->dynamic_users, d->name);

        safe_close_pair(d->storage_socket);
        return mfree(d);
}
Пример #26
0
void *oidmap_remove(struct oidmap *map, const struct object_id *key)
{
	struct hashmap_entry entry;

	if (!map->map.cmpfn)
		oidmap_init(map, 0);

	hashmap_entry_init(&entry, hash(key));
	return hashmap_remove(&map->map, &entry, key);
}
Пример #27
0
void Arena::HookOnPlayerDeath(PlayerPointer plr)
{
	ASSERT(plr != NULL);

	if (hashmap_get(m_playersAlive, plr->GetLowGUID(), NULL) == MAP_OK) {
		m_playersCount[plr->GetTeam()]--;
		UpdatePlayerCounts();
		hashmap_remove(m_playersAlive, plr->GetLowGUID());
	}
}
Пример #28
0
void netlink_slot_disconnect(sd_netlink_slot *slot, bool unref) {
        sd_netlink *nl;

        assert(slot);

        nl = slot->netlink;
        if (!nl)
                return;

        switch (slot->type) {

        case NETLINK_REPLY_CALLBACK:
                (void) hashmap_remove(nl->reply_callbacks, &slot->reply_callback.serial);

                if (slot->reply_callback.timeout != 0)
                        prioq_remove(nl->reply_callbacks_prioq, &slot->reply_callback, &slot->reply_callback.prioq_idx);

                break;
        case NETLINK_MATCH_CALLBACK:
                LIST_REMOVE(match_callbacks, nl->match_callbacks, &slot->match_callback);

                switch (slot->match_callback.type) {
                case RTM_NEWLINK:
                case RTM_DELLINK:
                        (void) socket_broadcast_group_unref(nl, RTNLGRP_LINK);

                        break;
                case RTM_NEWADDR:
                case RTM_DELADDR:
                        (void) socket_broadcast_group_unref(nl, RTNLGRP_IPV4_IFADDR);
                        (void) socket_broadcast_group_unref(nl, RTNLGRP_IPV6_IFADDR);

                        break;
                case RTM_NEWROUTE:
                case RTM_DELROUTE:
                        (void) socket_broadcast_group_unref(nl, RTNLGRP_IPV4_ROUTE);
                        (void) socket_broadcast_group_unref(nl, RTNLGRP_IPV6_ROUTE);

                        break;
                }

                break;
        default:
                assert_not_reached("Wut? Unknown slot type?");
        }

        slot->type = _NETLINK_SLOT_INVALID;
        slot->netlink = NULL;
        LIST_REMOVE(slots, nl->slots, slot);

        if (!slot->floating)
                sd_netlink_unref(nl);
        else if (unref)
                sd_netlink_slot_unref(slot);
}
Пример #29
0
static void __peer_release(peer_t* p)
{
    hashmap_iterator_t iter;
    piece_t* pce;

    for (hashmap_iterator(p->have_pieces, &iter);
        (pce = hashmap_iterator_next(p->have_pieces, &iter));)
    {
        hashmap_remove(p->have_pieces, p);
    }
}
Пример #30
0
void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry)
{
	if (!entry)
		return;

	entry->process.clean_on_exit = 0;
	kill(entry->process.pid, SIGTERM);
	finish_command(&entry->process);

	hashmap_remove(hashmap, entry, NULL);
}