ht_cell_t* ht_cell_pkg_copy(ht_t *ht, str *name, ht_cell_t *old) { unsigned int idx; unsigned int hid; ht_cell_t *it, *cell; if(ht==NULL || ht->entries==NULL) return NULL; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); /* head test and return */ if(ht->entries[idx].first==NULL) return NULL; lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) it = it->next; while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* found */ if(ht->htexpire>0 && it->expire!=0 && it->expire<time(NULL)) { /* entry has expired, delete it and return NULL */ if(it->prev==NULL) ht->entries[idx].first = it->next; else it->prev->next = it->next; if(it->next) it->next->prev = it->prev; ht->entries[idx].esize--; lock_release(&ht->entries[idx].lock); ht_cell_free(it); return NULL; } if(old!=NULL) { if(old->msize>=it->msize) { memcpy(old, it, it->msize); lock_release(&ht->entries[idx].lock); return old; } } cell = (ht_cell_t*)pkg_malloc(it->msize); if(cell!=NULL) memcpy(cell, it, it->msize); lock_release(&ht->entries[idx].lock); return cell; } it = it->next; } lock_release(&ht->entries[idx].lock); return NULL; }
static int ki_ht_slot_xlock(sip_msg_t *msg, str *htname, str *skey, int lmode) { ht_t *ht; unsigned int hid; unsigned int idx; ht = ht_get_table(htname); if(ht==NULL) { LM_ERR("cannot get hash table by name [%.*s] (%d)\n", htname->len, htname->s, lmode); return -1; } hid = ht_compute_hash(skey); idx = ht_get_entry(hid, ht->htsize); if(lmode==0) { LM_DBG("locking slot %.*s[%u] for key %.*s\n", htname->len, htname->s, idx, skey->len, skey->s); ht_slot_lock(ht, idx); } else { LM_DBG("unlocking slot %.*s[%u] for key %.*s\n", htname->len, htname->s, idx, skey->len, skey->s); ht_slot_unlock(ht, idx); } return 1; }
static t_cli *get_client(t_cli *c, t_msg *msg) { int id; if (msg == NULL || msg->params == NULL || msg->params->first == NULL || msg->params->first->data == NULL || (id = atoi((char *)msg->params->first->data)) == 0) return (NULL); return ((t_cli *)ht_get_entry(c->servptr->clients, &id, sizeof(c->sock.fd))); }
static int get_object(t_hash *inventory, char *obj_name) { t_item *it; int len; if ((it = ht_get_entry(inventory, obj_name, strlen(obj_name))) == NULL) len = snprintf(buff + i, 64 - i, "0 "); else len = snprintf(buff + i, 64 - i, "%lu ", it->amount); return (len); }
const char *color(const char *clr, const char *message) { char *clr_code = ""; if (ht_get_entry(colors, clr, &clr_code) != 0) { fprintf(stderr, "Color Error: color %s doesn't exist\n", clr); return message; } char *clr_message; asprintf(&clr_message, "%s%s" COLOR_RESET, clr_code, message); return clr_message; }
int ht_set_cell_expire(ht_t *ht, str *name, int type, int_str *val) { unsigned int idx; unsigned int hid; ht_cell_t *it; time_t now; if(ht==NULL || ht->entries==NULL) return -1; /* str value - ignore */ if(type&AVP_VAL_STR) return 0; /* not auto-expire htable */ if(ht->htexpire==0) return 0; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); now = 0; if(val->n>0) now = time(NULL) + val->n; LM_DBG("set auto-expire to %u (%d)\n", (unsigned int)now, val->n); lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) it = it->next; while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* update value */ it->expire = now; lock_release(&ht->entries[idx].lock); return 0; } it = it->next; } lock_release(&ht->entries[idx].lock); return 0; }
struct function * module_get_or_create_function(struct module *m, struct symbol *sym) { struct function *fun; if (ht_get_entry(m->funtable, sym->name, &fun) != 0) { fun = fun_new(sym); ht_add_entry(m->funtable, sym->name, fun); list_append(m->funlist, fun); if (!strcmp(sym->name, "main")) { check_main_prototype(sym); } } return fun; }
void module_print(struct module *m, FILE * out) { int si; /* puts("declare i8* @GC_malloc(i64 %s)"); */ /* puts("declare void @map(i8*)"); */ /* puts("declare void @reduce(i8*)"); */ /* fputs("\n", out); */ si = list_size(m->protolist); // debug("protolist size: %d\n", si); for (int i = 1; i <= si; ++i) { struct prototype *pt = list_get(m->protolist, i); struct function *f = NULL; ht_get_entry(m->funtable, pt->name, &f); if (!f->body_set) fputs(pt->code, out); /* else debug("code is set: %s\n", pt->name); */ } fputs("\n;literals\n", out); struct list *literals = string_get_literals_list(); si = list_size(literals); for (int i = 1; i <= si; ++i) { struct literal *lit = list_get(literals, i); fprintf(out, "%s = private unnamed_addr constant [%zu x i8] c\"%s\\00\"", lit->reg, lit->length, lit->value); } fputs("\n", out); si = list_size(m->globlist); for (int i = 1; i <= si; ++i) fputs(list_get(m->globlist, i), out); fputs("\n", out); si = list_size(m->funlist); for (int i = 1; i <= si; ++i) { struct function *fun = list_get(m->funlist, i); fun_cg(fun); fputs(fun->code, out); } }
int ht_del_cell(ht_t *ht, str *name) { unsigned int idx; unsigned int hid; ht_cell_t *it; if(ht==NULL || ht->entries==NULL) return -1; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); /* head test and return */ if(ht->entries[idx].first==NULL) return 0; lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) it = it->next; while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* found */ if(it->prev==NULL) ht->entries[idx].first = it->next; else it->prev->next = it->next; if(it->next) it->next->prev = it->prev; ht->entries[idx].esize--; lock_release(&ht->entries[idx].lock); ht_cell_free(it); return 0; } it = it->next; } lock_release(&ht->entries[idx].lock); return 0; }
int give_to_client(t_cli *c, enum e_rsrc type, int quantity) { t_item *item; char const *str; if (!c || !c->inventory || quantity <= 0 || !(str = get_rsrc_string_from_enum(type))) return (0); if (!(item = ht_get_entry(c->inventory, str, strlen(str)))) { if (!(item = new_t_item(type, quantity))) return (TRACE("new_t_item() failed"), 0); if (!ht_add_entry(c->inventory, item, str, strlen(str))) return (TRACE("ht_add_entry() failed"), free(item), 0); } else item->amount += quantity; return (quantity); }
int ht_get_cell_expire(ht_t *ht, str *name, unsigned int *val) { unsigned int idx; unsigned int hid; ht_cell_t *it; time_t now; if(ht==NULL || ht->entries==NULL) return -1; *val = 0; /* not auto-expire htable */ if(ht->htexpire==0) return 0; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); now = time(NULL); lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) it = it->next; while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* update value */ *val = (unsigned int)(it->expire - now); lock_release(&ht->entries[idx].lock); return 0; } it = it->next; } lock_release(&ht->entries[idx].lock); return 0; }
/** * unlock the slot for a given key in a hash table */ static int w_ht_slot_unlock(struct sip_msg* msg, char* key, char* foo) { ht_pv_t *hpv; str skey; pv_spec_t *sp; unsigned int hid; unsigned int idx; sp = (pv_spec_t*)key; hpv = (ht_pv_t*)sp->pvp.pvn.u.dname; if(hpv->ht==NULL) { hpv->ht = ht_get_table(&hpv->htname); if(hpv->ht==NULL) { LM_ERR("cannot get $sht root\n"); return -11; } } if(pv_printf_s(msg, hpv->pve, &skey)!=0) { LM_ERR("cannot get $sht key\n"); return -1; } hid = ht_compute_hash(&skey); idx = ht_get_entry(hid, hpv->ht->htsize); LM_DBG("unlocking slot %.*s[%u] for key %.*s\n", hpv->htname.len, hpv->htname.s, idx, skey.len, skey.s); ht_slot_unlock(hpv->ht, idx); return 1; }
int take_from_client(t_cli *c, enum e_rsrc type, int quantity) { t_item *item; char const *str; int ret; if (!c || !c->inventory || quantity <= 0 || !(str = get_rsrc_string_from_enum(type)) || !(item = ht_get_entry(c->inventory, str, strlen(str))) || (item->amount <= 0)) return (0); if (item->amount < ((uint)quantity)) { ret = item->amount; item->amount = 0; return (ret); } else item->amount -= quantity; return (quantity); }
ht_cell_t* ht_cell_value_add(ht_t *ht, str *name, int val, int mode, ht_cell_t *old) { unsigned int idx; unsigned int hid; ht_cell_t *it, *prev, *cell; time_t now; int_str isval; if(ht==NULL || ht->entries==NULL) return NULL; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); now = 0; if(ht->htexpire>0) now = time(NULL); prev = NULL; if(mode) lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) { prev = it; it = it->next; } while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* found */ if(now>0 && it->expire!=0 && it->expire<now) { /* entry has expired */ if(ht->flags==PV_VAL_INT) { /* initval is integer, use it to create a fresh entry */ it->flags &= ~AVP_VAL_STR; it->value.n = ht->initval.n; /* increment will be done below */ } else { /* delete expired entry */ if(it->prev==NULL) ht->entries[idx].first = it->next; else it->prev->next = it->next; if(it->next) it->next->prev = it->prev; ht->entries[idx].esize--; lock_release(&ht->entries[idx].lock); ht_cell_free(it); return NULL; } } /* update value */ if(it->flags&AVP_VAL_STR) { /* string value cannot be incremented */ if(mode) lock_release(&ht->entries[idx].lock); return NULL; } else { it->value.n += val; it->expire = now + ht->htexpire; if(old!=NULL) { if(old->msize>=it->msize) { memcpy(old, it, it->msize); lock_release(&ht->entries[idx].lock); return old; } } cell = (ht_cell_t*)pkg_malloc(it->msize); if(cell!=NULL) memcpy(cell, it, it->msize); if(mode) lock_release(&ht->entries[idx].lock); return cell; } } prev = it; it = it->next; } /* add val if htable has an integer init value */ if(ht->flags!=PV_VAL_INT) return NULL; isval.n = ht->initval.n + val; it = ht_cell_new(name, 0, &isval, hid); if(it == NULL) { LM_ERR("cannot create new cell.\n"); if(mode) lock_release(&ht->entries[idx].lock); return NULL; } it->expire = now + ht->htexpire; if(prev==NULL) { if(ht->entries[idx].first!=NULL) { it->next = ht->entries[idx].first; ht->entries[idx].first->prev = it; } ht->entries[idx].first = it; } else { it->next = prev->next; it->prev = prev; if(prev->next) prev->next->prev = it; prev->next = it; } ht->entries[idx].esize++; if(old!=NULL) { if(old->msize>=it->msize) { memcpy(old, it, it->msize); lock_release(&ht->entries[idx].lock); return old; } } cell = (ht_cell_t*)pkg_malloc(it->msize); if(cell!=NULL) memcpy(cell, it, it->msize); if(mode) lock_release(&ht->entries[idx].lock); return cell; }
int ht_set_cell(ht_t *ht, str *name, int type, int_str *val, int mode) { unsigned int idx; unsigned int hid; ht_cell_t *it, *prev, *cell; time_t now; if(ht==NULL || ht->entries==NULL) return -1; hid = ht_compute_hash(name); idx = ht_get_entry(hid, ht->htsize); now = 0; if(ht->htexpire>0) now = time(NULL); prev = NULL; if(mode) lock_get(&ht->entries[idx].lock); it = ht->entries[idx].first; while(it!=NULL && it->cellid < hid) { prev = it; it = it->next; } while(it!=NULL && it->cellid == hid) { if(name->len==it->name.len && strncmp(name->s, it->name.s, name->len)==0) { /* update value */ if(it->flags&AVP_VAL_STR) { if(type&AVP_VAL_STR) { if(it->value.s.len >= val->s.len) { /* copy */ it->value.s.len = val->s.len; memcpy(it->value.s.s, val->s.s, val->s.len); it->value.s.s[it->value.s.len] = '\0'; if(ht->updateexpire) it->expire = now + ht->htexpire; } else { /* new */ cell = ht_cell_new(name, type, val, hid); if(cell == NULL) { LM_ERR("cannot create new cell\n"); if(mode) lock_release(&ht->entries[idx].lock); return -1; } cell->next = it->next; cell->prev = it->prev; cell->expire = now + ht->htexpire; if(it->prev) it->prev->next = cell; else ht->entries[idx].first = cell; if(it->next) it->next->prev = cell; ht_cell_free(it); } } else { it->flags &= ~AVP_VAL_STR; it->value.n = val->n; if(ht->updateexpire) it->expire = now + ht->htexpire; } if(mode) lock_release(&ht->entries[idx].lock); return 0; } else { if(type&AVP_VAL_STR) { /* new */ cell = ht_cell_new(name, type, val, hid); if(cell == NULL) { LM_ERR("cannot create new cell.\n"); if(mode) lock_release(&ht->entries[idx].lock); return -1; } cell->expire = now + ht->htexpire; cell->next = it->next; cell->prev = it->prev; if(it->prev) it->prev->next = cell; else ht->entries[idx].first = cell; if(it->next) it->next->prev = cell; ht_cell_free(it); } else { it->value.n = val->n; if(ht->updateexpire) it->expire = now + ht->htexpire; } if(mode) lock_release(&ht->entries[idx].lock); return 0; } } prev = it; it = it->next; } /* add */ cell = ht_cell_new(name, type, val, hid); if(cell == NULL) { LM_ERR("cannot create new cell.\n"); if(mode) lock_release(&ht->entries[idx].lock); return -1; } cell->expire = now + ht->htexpire; if(prev==NULL) { if(ht->entries[idx].first!=NULL) { cell->next = ht->entries[idx].first; ht->entries[idx].first->prev = cell; } ht->entries[idx].first = cell; } else { cell->next = prev->next; cell->prev = prev; if(prev->next) prev->next->prev = cell; prev->next = cell; } ht->entries[idx].esize++; if(mode) lock_release(&ht->entries[idx].lock); return 0; }