END_TEST START_TEST(test_map_put_get) { bloom_hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); out = (void*)(intptr_t)i; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } for (int i=0; i<100;i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_get(map, (char*)buf, &out) == 0); fail_unless(out == (void*)(intptr_t)i); } res = hashmap_destroy(map); fail_unless(res == 0); }
/** * Parse a CSS file. Add css rules to the tree if they are mentioned as * properties in the markup file. * @param data the css data to parse * @param len its length * @param props the properties from all the markup files * @param css store the css rules in here * @return 1 if it succeeded, else 0 */ int css_parse( const char *data, int len, hashset *props, hashmap *css ) { int offset = 0; do { css_rule *rule = get_css_rule( data, &offset ); if ( rule != NULL ) { char *class_name = css_rule_get_class(rule); // only put into the css properties seen in the markup if ( hashset_contains(props,class_name) ) { //fprintf(stderr,"adding class %s\n",class_name); if ( hashmap_contains(css,class_name) ) { css_rule *r = hashmap_get(css,class_name); css_rule_dispose( r ); } hashmap_put( css, class_name, rule ); } else { //fprintf(stderr,"disposing of class %s\n",class_name); css_rule_dispose( rule ); //fprintf(stderr,"ended dispose\n"); } } // point beyond closing brace offset++; } while ( offset < len ); //fprintf( stderr, "contents of css rule hashmap:\n" ); //hashmap_print( css, (print_value)css_rule_print ); return 1; }
static int on_llmnr_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) { _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; DnsTransaction *t = NULL; Manager *m = userdata; DnsScope *scope; int r; r = manager_recv(m, fd, DNS_PROTOCOL_LLMNR, &p); if (r <= 0) return r; scope = manager_find_scope(m, p); if (!scope) { log_warning("Got LLMNR UDP packet on unknown scope. Ignoring."); return 0; } if (dns_packet_validate_reply(p) > 0) { log_debug("Got LLMNR reply packet for id %u", DNS_PACKET_ID(p)); dns_scope_check_conflicts(scope, p); t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); if (t) dns_transaction_process_reply(t, p); } else if (dns_packet_validate_query(p) > 0) { log_debug("Got LLMNR query packet for id %u", DNS_PACKET_ID(p)); dns_scope_process_query(scope, NULL, p); } else log_debug("Invalid LLMNR UDP packet."); return 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); }
END_TEST START_TEST(test_map_put_clear_get) { hashmap *map; int res = hashmap_init(0, &map); fail_unless(res == 0); char buf[100]; void *out; for (int i=0; i<100; i++) { snprintf((char*)&buf, 100, "test%d", i); out = 0 & i; fail_unless(hashmap_put(map, (char*)buf, out) == 1); } fail_unless(hashmap_size(map) == 100); fail_unless(hashmap_clear(map) == 0); fail_unless(hashmap_size(map) == 0); for (int i=0; i<100; i++) { snprintf((char*)&buf, 100, "test%d", i); fail_unless(hashmap_get(map, (char*)buf, &out) == -1); } res = hashmap_destroy(map); fail_unless(res == 0); }
static int device_set_sysfs(Device *d, const char *sysfs) { Device *first; char *copy; int r; assert(d); if (streq_ptr(d->sysfs, sysfs)) return 0; r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &string_hash_ops); if (r < 0) return r; copy = strdup(sysfs); if (!copy) return -ENOMEM; device_unset_sysfs(d); first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); LIST_PREPEND(same_sysfs, first, d); r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); if (r < 0) { LIST_REMOVE(same_sysfs, first, d); free(copy); return r; } d->sysfs = copy; return 0; }
static crypto_device *get_crypto_device(const char *uuid) { int r; crypto_device *d; assert(uuid); d = hashmap_get(arg_disks, uuid); if (!d) { d = new0(struct crypto_device, 1); if (!d) return NULL; d->create = false; d->keyfile = d->options = d->name = NULL; d->uuid = strdup(uuid); if (!d->uuid) { free(d); return NULL; } r = hashmap_put(arg_disks, d->uuid, d); if (r < 0) { free(d->uuid); free(d); return NULL; } }
static void JNICALL callbackSingleStep(jvmtiEnv * const jvmti_env, JNIEnv * const jni_env, const jthread thread, const jmethodID method, const jlocation location) { (void) jvmti_env; (void) jni_env; (void) thread; (void) location; (void) method; #ifdef DETAILED_RESULTS enter_critical_section(jvmti); if (last_jmethod != method) { last_jmethod = method; if (hashmap_get(map, (map_key_t) method, (void *) &cur_method_stat) == MAP_MISSING) { cur_method_stat = NEW(method_stat_t); cur_method_stat->id = method; (void)hashmap_put(map, (map_key_t) method, cur_method_stat); } } cur_method_stat->counter++; num_instructions_proccessed++; exit_critical_section(jvmti); #else __sync_fetch_and_add(&num_instructions_proccessed, 1); #endif }
/** * Work out which dest file to send a named range to * @param s the stripper object * @param range_name the range name * @return the appropriate dest_file object */ dest_file *userdata_get_markup_dest( userdata *u, char *range_name ) { if ( hashmap_contains(u->dest_map,range_name) ) { return (dest_file*)hashmap_get( u->dest_map, range_name ); } else { int i=1; while ( u->markup_dest[i] != NULL ) { layer *l = dest_file_layer(u->markup_dest[i]); if ( l!=NULL&& layer_has_milestone(l,range_name) ) { // remember for future calls hashmap_put( u->dest_map,range_name, u->markup_dest[i] ); return u->markup_dest[i]; } i++; } hashmap_put( userdata_dest_map(u), range_name, u->markup_dest[0] ); return u->markup_dest[0]; } }
void irc_privmsg(struct bot *bot, struct message *msg) { char *strptr; char *arg = strdup(msg->arg); char *host = strdup(msg->host); char *nick = strtok_r(host, "!", &strptr); nick++; strptr = NULL; char *chan = strtok_r(arg, " ", &strptr); char *message = strtok_r(NULL, "", &strptr); message++; printf("[irc\tinfo] [%s] <%s> %s\n", chan, nick, message); if (EQ(nick, "svkampen") && EQ(message, "'quit")) { bot->running = 0; } if (startswith(message, "'")) { char *cmd = strtok_r(message+1, " ", &strptr); char *args = strtok_r(NULL, "", &strptr); msg_handler callback = (msg_handler)hashmap_get(cmd, bot->commands); if (callback) { callback(bot, nick, chan, args); } } free(arg); free(host); }
// Grabs a string from a buffered queue and counts it void *reduce(void* rArgs){ struct reducerArgs *args = (struct reducerArgs*) rArgs; int mappersFinished = 0; int error; int count; map_t map = *(args->map); struct buffered_queue *conn = (args->conns)[args->reducerId]; // Corresponding buffered queue while (1){ // continually attempt to read until signal received all mappers done char *str = (char*)buffered_queue_pop(conn); // Word to reduce from buffered queue if (str == NULL) { // Received mappers finished signal break; } else { printf("Reducer %d is counting \"%s\"\n", args->reducerId, str); error = hashmap_get(map, str, &count); if (error == MAP_OK){ hashmap_put(map, str, count + 1); printf("Reducer %d counted \"%s\" %d times\n", args->reducerId, str, count + 1); } else { hashmap_put(map, str, 1); printf("Reducer %d counted \"%s\" 1 time\n",args->reducerId, str); } } } hashmap_iterate(map, print_result); return NULL; }
LXSuccess LXMapGetInteger(LXMapPtr r, const char *key, LXInteger *outValue) { if ( !r || !key || !outValue) return NO; map_t map = (map_t)r; LXMapValue *mapValue = NULL; if (MAP_OK != hashmap_get(map, key, (any_t)&mapValue) || !mapValue) { return NO; } else { LXPropertyType propType = LXMapValueTypeGetType(mapValue->type); switch (propType) { case kLXFloatProperty: *outValue = lround(*((double *)mapValue->data)); return YES; case kLXIntegerProperty: if (LXMapValueTypeGetFlags(mapValue->type) & kLXMapValueIsInt64) { *outValue = (LXInteger)(*((int64_t *)mapValue->data)); } else { *outValue = (LXInteger)(mapValue->data); } return YES; default: return NO; } } }
/* * Basically keep a cache of X->Y so that we can repeatedly replace * the same anonymized string with another. The actual generation * is farmed out to the generate function. */ static const void *anonymize_mem(struct hashmap *map, void *(*generate)(const void *, size_t *), const void *orig, size_t *len) { struct anonymized_entry key, *ret; if (!map->cmpfn) hashmap_init(map, anonymized_entry_cmp, 0); hashmap_entry_init(&key, memhash(orig, *len)); key.orig = orig; key.orig_len = *len; ret = hashmap_get(map, &key, NULL); if (!ret) { ret = xmalloc(sizeof(*ret)); hashmap_entry_init(&ret->hash, key.hash.hash); ret->orig = xstrdup(orig); ret->orig_len = *len; ret->anon = generate(orig, len); ret->anon_len = *len; hashmap_put(map, ret); } *len = ret->anon_len; return ret->anon; }
int address_new_static(Network *network, unsigned section, Address **ret) { _cleanup_address_free_ Address *address = NULL; if (section) { uint64_t key = section; address = hashmap_get(network->addresses_by_section, &key); if (address) { *ret = address; address = NULL; return 0; } } address = new0(Address, 1); if (!address) return -ENOMEM; address_init(address); address->network = network; LIST_PREPEND(static_addresses, network->static_addresses, address); if (section) { address->section = section; hashmap_put(network->addresses_by_section, &address->section, address); } *ret = address; address = NULL; return 0; }
int route_new_static(Network *network, unsigned section, Route **ret) { _cleanup_route_free_ Route *route = NULL; int r; if (section) { route = hashmap_get(network->routes_by_section, UINT_TO_PTR(section)); if (route) { *ret = route; route = NULL; return 0; } } r = route_new(&route); if (r < 0) return r; route->protocol = RTPROT_STATIC; route->network = network; LIST_PREPEND(routes, network->static_routes, route); if (section) { route->section = section; hashmap_put(network->routes_by_section, UINT_TO_PTR(route->section), route); } *ret = route; route = NULL; return 0; }
static int list_dependencies_one(sd_bus *bus, const char *name, unsigned int level, char ***units, unsigned int branches) { _cleanup_strv_free_ char **deps = NULL; char **c; int r = 0; usec_t service_longest = 0; int to_print = 0; struct unit_times *times; struct boot_times *boot; if (strv_extend(units, name)) return log_oom(); r = list_dependencies_get_dependencies(bus, name, &deps); if (r < 0) return r; qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare); r = acquire_boot_times(bus, &boot); if (r < 0) return r; STRV_FOREACH(c, deps) { times = hashmap_get(unit_times_hashmap, *c); if (times && times->activated && times->activated <= boot->finish_time && (times->activated >= service_longest || service_longest == 0)) { service_longest = times->activated; break; } }
node *LVARglobaldef( node *arg_node, info *arg_info) { char *var_name; DBUG_ENTER("LVARglobaldef"); var_name = VAR_NAME(GLOBALDEF_ID( arg_node)); /* set declaration of this global variable */ VAR_DECL(GLOBALDEF_ID( arg_node) ) = arg_node; /* check for duplicated variable declaration */ if(hashmap_contains(arg_info->global, var_name)) { CTIerror(":%d: error: '%s' has already been defined in this context", NODE_LINE(arg_node), var_name); CTIerror(":%d: error: location of earlier definition", NODE_LINE((node *)hashmap_get(arg_info->global, var_name))); DBUG_RETURN(arg_node); } /* add global variabele declaration to hashmap */ hashmap_add(arg_info->global, var_name, arg_node); DBUG_RETURN( arg_node); }
static int resolve_lazy(int argc, char **argv) { int i; size_t destrootdirlen = strlen(destrootdir); int ret = 0; char *item; for (i = 0; i < argc; i++) { const char *src = argv[i]; char *p = argv[i]; char *existing; log_debug("resolve_deps('%s')", src); if (strstr(src, destrootdir)) { p = &argv[i][destrootdirlen]; } existing = hashmap_get(items, p); if (existing) { if (strcmp(existing, p) == 0) continue; } item = strdup(p); hashmap_put(items, item, item); ret += resolve_deps(src); } return ret; }
int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { _cleanup_free_ char *e = NULL; Manager *m = userdata; Image *image = NULL; const char *p; int r; assert(bus); assert(path); assert(interface); assert(found); p = startswith(path, "/org/freedesktop/machine1/image/"); if (!p) return 0; e = bus_label_unescape(p); if (!e) return -ENOMEM; image = hashmap_get(m->image_cache, e); if (image) { *found = image; return 1; } r = hashmap_ensure_allocated(&m->image_cache, &string_hash_ops); if (r < 0) return r; if (!m->image_cache_defer_event) { r = sd_event_add_defer(m->event, &m->image_cache_defer_event, image_flush_cache, m); if (r < 0) return r; r = sd_event_source_set_priority(m->image_cache_defer_event, SD_EVENT_PRIORITY_IDLE); if (r < 0) return r; } r = sd_event_source_set_enabled(m->image_cache_defer_event, SD_EVENT_ONESHOT); if (r < 0) return r; r = image_find(e, &image); if (r <= 0) return r; image->userdata = m; r = hashmap_put(m->image_cache, image->name, image); if (r < 0) { image_unref(image); return r; } *found = image; return 1; }
static int client_acquire(Context *context, uint64_t id, Client **_c) { char *watch = NULL; Client *c; int r; assert(context); assert(_c); c = hashmap_get(context->clients, &id); if (c) { *_c = c; return 0; } if (hashmap_size(context->clients) >= CLIENTS_MAX) return -ENOBUFS; r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func); if (r < 0) return r; c = new0(Client, 1); if (!c) return -ENOMEM; c->id = id; r = hashmap_put(context->clients, &c->id, c); if (r < 0) goto fail; c->context = context; if (asprintf(&watch, "type='signal'," "sender='org.freedesktop.DBus'," "path='/org/freedesktop/DBus'," "interface='org.freedesktop.DBus'," "member='NameOwnerChanged'," "arg0=':1.%llu'", (unsigned long long) id) < 0) { r = -ENOMEM; goto fail; } r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c); if (r < 0) { free(watch); goto fail; } c->watch = watch; *_c = c; return 0; fail: client_free(c); return r; }
static struct dir_entry *find_dir_entry(struct index_state *istate, const char *name, unsigned int namelen) { struct dir_entry key; hashmap_entry_init(&key, memihash(name, namelen)); key.namelen = namelen; return hashmap_get(&istate->dir_hash, &key, name); }
struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd) { struct subprocess_entry key; hashmap_entry_init(&key, strhash(cmd)); key.cmd = cmd; return hashmap_get(hashmap, &key, NULL); }
// Helper method for setting cache entry tcache_entry_value* tcache_get_entry(tcache_entry_key *key) { tcache_entry_value *val = NULL; unsigned int tmp_size; hashmap_get(&cache->entries, (void*)key, sizeof(tcache_entry_key), (void**)&val, &tmp_size); return val; }
int main() { HashMap* hashmap = hashmap_create(); hashmap_add_node(hashmap, "trykey", "tryval", NULL, NULL); hashmap_add_node(hashmap, "trykey2", "tryval2", NULL, NULL); HashMapNode* node = hashmap_get(hashmap, "trykey"); assert(!strcmp("trykey", node->key)); assert(!strcmp("tryval", node->value)); node = hashmap_get(hashmap, "trykey2"); assert(!strcmp("trykey2", node->key)); assert(!strcmp("tryval2", node->value)); assert(hashmap->count == 2); hashmap_destroy(hashmap); assert(hashmap->count == 0); return 0; }
double variable_resolver(const char* name) { double *b; if ((b = hashmap_get(hm, name)) != NULL) return *b; fprintf(stderr, "! unknown variable `%s`\n", name); return 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--; }
idev_device *idev_find_keyboard(idev_session *s, const char *name) { char *kname; assert_return(s, NULL); assert_return(name, NULL); kname = strjoina("keyboard/", name); return hashmap_get(s->device_map, kname); }
/* create a new FDB entry or get an existing one. */ int fdb_entry_new_static( Network *network, unsigned section, FdbEntry **ret) { _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; struct ether_addr *mac_addr = NULL; assert(network); assert(ret); /* search entry in hashmap first. */ if (section) { fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section)); if (fdb_entry) { *ret = fdb_entry; fdb_entry = NULL; return 0; } } if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX) return -E2BIG; /* allocate space for MAC address. */ mac_addr = new0(struct ether_addr, 1); if (!mac_addr) return -ENOMEM; /* allocate space for and FDB entry. */ fdb_entry = new0(FdbEntry, 1); if (!fdb_entry) { /* free previously allocated space for mac_addr. */ free(mac_addr); return -ENOMEM; } /* init FDB structure. */ fdb_entry->network = network; fdb_entry->mac_addr = mac_addr; LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry); network->n_static_fdb_entries++; if (section) { fdb_entry->section = section; hashmap_put(network->fdb_entries_by_section, UINT_TO_PTR(fdb_entry->section), fdb_entry); } /* return allocated FDB structure. */ *ret = fdb_entry; fdb_entry = NULL; return 0; }
char *thing_get(char *key) { int res; char *sval = NULL; res = hashmap_get(thing, key, (void**)&sval); if(res==MAP_OK) return(sval); else return("NULL"); }
/* * Test that we can get and set key/value pairs. */ char * test_get_set() { int rc = hashmap_set(map, &test1, &expect1); mu_assert(rc == 1, "hashmap_test: failed to set &test1."); bstring result = hashmap_get(map, &test1); mu_assert(result == &expect1, "Wrong value for test1."); rc = hashmap_set(map, &test2, &expect2); mu_assert(rc == 1, "hashmap_test: failed to set test2."); result = hashmap_get(map, &test2); mu_assert(result == &expect2, "hashmap_test: wrong value for test2."); rc = hashmap_set(map, &test3, &expect3); mu_assert(rc == 1, "hashmap_test: failed to set test3."); result = hashmap_get(map, &test3); mu_assert(result == &expect3, "hashmap_test: wrong value for test3."); return NULL; }