Пример #1
0
int main(int argc, char *argv[])
{
	gzFile fp;
	kstream_t *ks;
	khash_t(s) *hash;
	mask32_t *q = 0;
	kstring_t *str;
	int i, dret, c, last = 0;

	while ((c = getopt(argc, argv, "")) >= 0) {
	}
	if (argc <= optind + 1) {
		fprintf(stderr, "Usage: uniq-dist <in.mask.fa> <in-sorted.list>\n");
		return 1;
	}

	str = (kstring_t*)calloc(1, sizeof(kstring_t));
	fprintf(stderr, "[uniq-dist] loading mask...\n");
	hash = load_mask(argv[optind]);
	fp = gzopen(argv[optind+1], "r");
	ks = ks_init(fp);
	fprintf(stderr, "[uniq-dist] calculating unique distance...\n");
	while (ks_getuntil(ks, 0, str, &dret) >= 0) {
		khint_t k;
		mask32_t *p;
		int pos;
		k = kh_get(s, hash, str->s);
		p = (k != kh_end(hash))? &kh_val(hash, k) : 0;
		ks_getuntil(ks, 0, str, &dret);
		pos = atoi(str->s) - 1;
		if (p && pos >= 0 && pos < p->ori_len) {
			if (p != q) q = p; // change of reference
			else {
				if (last >= pos) {
					fprintf(stderr, "[uniq-dist] out of order: %s:%d <= %d\n", kh_key(hash, k), pos+1, last+1);
				} else {
					for (i = last, c = 0; i < pos; ++i)
						if (p->mask[i/32] & 1u<<i%32) ++c;
					if (last > 0) printf("%s\t%d\t%d\t%d\n", kh_key(hash, k), last, pos, c);
				}
			}
			last = pos;
		}
		if (dret != '\n')
			while ((c = ks_getc(ks)) != -1 && c != '\n');
	}
	ks_destroy(ks);
	gzclose(fp);
	free(str->s); free(str);
	// hash table is not freed...
	return 0;
}
Пример #2
0
/** Free the memory allocated internally to the keys in the hash data structure (as the hash table can't free this) 
	@param[in] container: The memoization container to containing the context data
	@param[in] contextID: The context to clear the memoization internal data for (e.g. the hash table)
*/
void clearMemoizationContextInternalData(MEMOIZATION_DATA_CONTAINER* container, unsigned int contextID){
	if (contextID < container->numContexts){
		int i;
		khash_t(MEMO_CACHE)* context = container->contexts[contextID];
		for (i=kh_begin(context); i < kh_end(context); i++){
			if (kh_exist(context, i)) {
				clearMemoizationSignatureInternalData(kh_key(context, i));
				free(kh_key(context, i));
				free(kh_value(context, i));
			}
		}
	}
}
Пример #3
0
int main(int argc, char *argv[])
{
	int i, l, n = 1000000, ret, c = 0;
	khash_t(str) *h;
	khint_t k;
	h = kh_init(str);
	if (argc > 1) n = atoi(argv[1]);
	for (i = 1; i <= n; ++i) {
		char buf[16];
		int2str(i, 16, buf);
		k = kh_put(str, h, strdup(buf), &ret);
		kh_val(h, k) = i;
	}
	for (i = 1; i <= n; ++i) {
		char buf[16];
		int2str(i, 10, buf);
		k = kh_get(str, h, buf);
		if (k != kh_end(h)) ++c;
	}
	for (k = kh_begin(h); k != kh_end(h); ++k) // explicitly freeing memory takes 10-20% CPU time.
		if (kh_exist(h, k)) free((char*)kh_key(h, k));
	printf("%d\n", c);
	kh_destroy(str, h);
	return 0;
}
Пример #4
0
//
// Lua-C Interface
//
static int reg_plugin_func(lua_State *l)
{
    const char* name = lua_tostring(l, 1);
    const char* sig = lua_tostring(l, 2);


    // Find the matching id for the given state
    for (khiter_t k = kh_begin(states); k != kh_end(states); ++k)
    {
        if (kh_exist(states, k))
        {
            int key = kh_key(states, k);
            struct instance *val = kh_val(states, k);

            if (val->state == l)
            {
                if (plugin_interface.register_func(plugin_interface.host, key, name, sig))
                {
                    // Registered properly
                    return 0;
                }
                else
                {
                    // Register failed in host
                    lua_pushstring(l, "host could not register function");
                    return 1;
                }
            }
        }
    }

    // The given lua state isn't in our states hashtable
    lua_pushstring(l, "could not find plugin to match lua state");
    return 1;
}
Пример #5
0
// Debug function
static void bed_print(void *reg_hash) {
    reghash_t *h = (reghash_t *)reg_hash;
    bed_reglist_t *p;
    khint_t k;
    int i;
    const char *reg;
    uint32_t beg, end;

    if (!h) {
        printf("Hash table is empty!\n");
        return;
    }
    for (k = kh_begin(h); k < kh_end(h); k++) {
        if (kh_exist(h,k)) {
            reg = kh_key(h,k);
            printf("Region: '%s'\n", reg);
            if ((p = &kh_val(h,k)) != NULL && p->n > 0) {
                printf("Filter: %d\n", p->filter);
                for (i=0; i<p->n; i++) {
                    beg = (uint32_t)(p->a[i]>>32);
                    end = (uint32_t)(p->a[i]);

                    printf("\tinterval[%d]: %d-%d\n",i,beg,end);
                }
            } else {
                printf("Region '%s' has no intervals!\n", reg);
            }
        }
Пример #6
0
Файл: hash.c Проект: denji/mruby
static mrb_value
mrb_hash_dup(mrb_state *mrb, mrb_value hash)
{
  struct RHash* ret;
  khash_t(ht) *h, *ret_h;
  khiter_t k, ret_k;

  h = RHASH_TBL(hash);
  ret = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
  ret->ht = kh_init(ht, mrb);

  if (kh_size(h) > 0) {
    ret_h = ret->ht;

    for (k = kh_begin(h); k != kh_end(h); k++) {
      if (kh_exist(h,k)) {
        int ai = mrb_gc_arena_save(mrb);
        ret_k = kh_put(ht, mrb, ret_h, KEY(kh_key(h,k)));
        mrb_gc_arena_restore(mrb, ai);
        kh_val(ret_h, ret_k) = kh_val(h,k);
      }
    }
  }

  return mrb_obj_value(ret);
}
Пример #7
0
Файл: hash.c Проект: denji/mruby
static mrb_value
inspect_hash(mrb_state *mrb, mrb_value hash, int recur)
{
  mrb_value str, str2;
  khash_t(ht) *h = RHASH_TBL(hash);
  khiter_t k;

  if (recur) return mrb_str_new_lit(mrb, "{...}");

  str = mrb_str_new_lit(mrb, "{");
  if (h && kh_size(h) > 0) {
    for (k = kh_begin(h); k != kh_end(h); k++) {
      int ai;

      if (!kh_exist(h,k)) continue;

      ai = mrb_gc_arena_save(mrb);

      if (RSTRING_LEN(str) > 1) mrb_str_cat_lit(mrb, str, ", ");

      str2 = mrb_inspect(mrb, kh_key(h,k));
      mrb_str_append(mrb, str, str2);
      mrb_str_cat_lit(mrb, str, "=>");
      str2 = mrb_inspect(mrb, kh_value(h,k));
      mrb_str_append(mrb, str, str2);

      mrb_gc_arena_restore(mrb, ai);
    }
  }
  mrb_str_cat_lit(mrb, str, "}");

  return str;
}
Пример #8
0
MRB_API void
mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
{
  khash_t(ht) *h;
  khiter_t k;
  int r;

  mrb_hash_modify(mrb, hash);
  h = RHASH_TBL(hash);

  if (!h) h = RHASH_TBL(hash) = kh_init(ht, mrb);
  k = kh_put2(ht, mrb, h, key, &r);
  kh_value(h, k).v = val;

  if (r != 0) {
    /* expand */
    int ai = mrb_gc_arena_save(mrb);
    key = kh_key(h, k) = KEY(key);
    mrb_gc_arena_restore(mrb, ai);
    kh_value(h, k).n = kh_size(h)-1;
  }

  mrb_field_write_barrier_value(mrb, (struct RBasic*)RHASH(hash), key);
  mrb_field_write_barrier_value(mrb, (struct RBasic*)RHASH(hash), val);
  return;
}
Пример #9
0
static strm_string
str_intern(const char *p, strm_int len, int foreign)
{
  khiter_t k;
  struct sym_key key;
  int ret;
  strm_string str;

  if (len <= 6) {
    return str_new(p, len, foreign);
  }
  if (!sym_table) {
    sym_table = kh_init(sym);
  }
  key.ptr = p;
  key.len = len;
  k = kh_put(sym, sym_table, key, &ret);

  if (ret == 0) {               /* found */
    return kh_value(sym_table, k);
  }
  str = str_new(p, len, foreign);
  kh_key(sym_table, k).ptr = strm_str_ptr(str);
  kh_value(sym_table, k) = str;

  return str;
}
Пример #10
0
/* checks an individual file for hard links and updates its cicrular linked
 * list, also updates the sizes of the parent dirs */
static void hlink_check(struct dir *d) {
  struct dir *t, *pt, *par;
  int i;

  /* add to links table */
  khiter_t k = kh_put(hl, links, d, &i);

  /* found in the table? update hlnk */
  if(!i) {
    t = d->hlnk = kh_key(links, k);
    if(t->hlnk != NULL)
      for(t=t->hlnk; t->hlnk!=d->hlnk; t=t->hlnk)
        ;
    t->hlnk = d;
  }

  /* now update the sizes of the parent directories,
   * This works by only counting this file in the parent directories where this
   * file hasn't been counted yet, which can be determined from the hlnk list.
   * XXX: This may not be the most efficient algorithm to do this */
  for(i=1,par=d->parent; i&&par; par=par->parent) {
    if(d->hlnk)
      for(t=d->hlnk; i&&t!=d; t=t->hlnk)
        for(pt=t->parent; i&&pt; pt=pt->parent)
          if(pt==par)
            i=0;
    if(i) {
      par->size += d->size;
      par->asize += d->asize;
    }
  }
}
Пример #11
0
Файл: hash.c Проект: denji/mruby
static mrb_value
mrb_hash_replace(mrb_state *mrb, mrb_value hash)
{
  mrb_value hash2, ifnone;
  khash_t(ht) *h2;
  khiter_t k;

  mrb_get_args(mrb, "o", &hash2);
  hash2 = to_hash(mrb, hash2);
  if (mrb_obj_equal(mrb, hash, hash2)) return hash;
  mrb_hash_clear(mrb, hash);

  h2 = RHASH_TBL(hash2);
  if (h2) {
    for (k = kh_begin(h2); k != kh_end(h2); k++) {
      if (kh_exist(h2, k))
        mrb_hash_set(mrb, hash, kh_key(h2, k), kh_value(h2, k));
    }
  }

  if (MRB_RHASH_PROCDEFAULT_P(hash2)) {
    RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT;
    ifnone = RHASH_PROCDEFAULT(hash2);
  }
  else {
    ifnone = RHASH_IFNONE(hash2);
  }
  mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone);

  return hash;
}
Пример #12
0
static strm_string*
str_intern(const char *p, size_t len)
{
  khiter_t k;
  struct sym_key key;
  int ret;
  strm_string *str;

  if (!sym_table) {
    sym_table = kh_init(sym);
  }
  key.ptr = p;
  key.len = len;
  k = kh_put(sym, sym_table, key, &ret);

  if (ret == 0) {               /* found */
    return kh_value(sym_table, k);
  }
  str = str_new(p, len);
  str->flags |= STRM_STR_INTERNED;
  kh_key(sym_table, k).ptr = str->ptr;
  kh_value(sym_table, k) = str;

  return str;
}
Пример #13
0
/**
 * Gets sequence names and lengths
 */
void bcf_hdr_get_seqs_and_lens(const bcf_hdr_t *h, const char**& seqs, int32_t*& lens, int *n)
{
    vdict_t *d = (vdict_t*)h->dict[BCF_DT_CTG];
    int tid, m = kh_size(d);
    seqs = (const char**) calloc(m,sizeof(const char*));
    lens = (int32_t*) calloc(m,sizeof(int32_t));
    khint_t k;
    for (k=kh_begin(d); k<kh_end(d); k++)
    {
        if ( !kh_exist(d,k) ) continue;
        tid = kh_val(d,k).id;
        assert( tid<m );
        seqs[tid] = kh_key(d,k);

        lens[tid] = 0;
        bcf_hrec_t *hrec = kh_val(d, k).hrec[0];
        for (int i=0; i<hrec->nkeys; ++i)
        {
            if (!strcmp(hrec->keys[i],"length"))
            {
                lens[tid] = atoi(hrec->vals[i]);
            }
        }
        assert(lens[tid]);
    }
    // sanity check: there should be no gaps
    for (tid=0; tid<m; tid++)
        assert(seqs[tid]);
    *n = m;
}
Пример #14
0
// This is called when the headers are received so we can look for a message waiting for
// this person, or leave them connected until one comes, or time them out after 50s maybe?
void receivedHeaders(clientStatus *thisClient) {
	printf ("Connected by >%s<\r\n", thisClient->clientId);

	// Check to see if there's a message queued for this person
	// if so, send it and drop the connection
	khiter_t q = kh_get(queue, queue, (char*)thisClient->clientId);
	if (q != kh_end(queue)) {
		char *queuedMessage;
		kl_shift(messages, kh_value(queue,q), &queuedMessage);
		// Now send the message to the person and close
		snprintf(httpResponse, HTTP_RESPONSE_SIZE, HTTP_TEMPLATE, (int)strlen(queuedMessage), queuedMessage); // Compose the response message
		free(queuedMessage);
		write(thisClient->io.fd, httpResponse, strlen(httpResponse)); // Send it
		closeConnectionSkipHash((ev_io*)thisClient);
		// If that was the last one, free the list and remove it from the hash
		if (!kh_value(queue, q)->head->next) {
			kl_destroy(messages, kh_value(queue, q)); // Free the list
			free((void*)kh_key(queue, q)); // Free the key (the client id)
			kh_del(queue, queue, q); // Remove this client id from the hash
		}
	} else {
		// If there's no message, then add their client id to the hash for later
		int ret;
		khiter_t k = kh_put(clientStatuses, clientStatuses, thisClient->clientId, &ret);
		kh_value(clientStatuses, k) = thisClient;
	}

}
Пример #15
0
Файл: hash.c Проект: denji/mruby
static mrb_value
mrb_hash_shift(mrb_state *mrb, mrb_value hash)
{
  khash_t(ht) *h = RHASH_TBL(hash);
  khiter_t k;
  mrb_value delKey, delVal;

  mrb_hash_modify(mrb, hash);
  if (h) {
    if (kh_size(h) > 0) {
      for (k = kh_begin(h); k != kh_end(h); k++) {
        if (!kh_exist(h,k)) continue;

        delKey = kh_key(h,k);
        mrb_gc_protect(mrb, delKey);
        delVal = mrb_hash_delete_key(mrb, hash, delKey);
        mrb_gc_protect(mrb, delVal);

        return mrb_assoc_new(mrb, delKey, delVal);
      }
    }
  }

  if (MRB_RHASH_PROCDEFAULT_P(hash)) {
    return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, mrb_nil_value());
  }
  else {
    return RHASH_IFNONE(hash);
  }
}
Пример #16
0
/**
 * Copies contigs found in bcf header to another bcf header.
 */
void bcf_hdr_transfer_contigs(const bcf_hdr_t *hsrc, bcf_hdr_t *hdest)
{
    vdict_t *d = (vdict_t*)hsrc->dict[BCF_DT_CTG];
    int tid, m = kh_size(d);
    const char **names = (const char**) calloc(m,sizeof(const char*));
    int len[m];
    khint_t k;
    
    
    for (k=kh_begin(d); k<kh_end(d); k++)
    {
        if ( !kh_exist(d,k) ) continue;
        tid = kh_val(d,k).id;
        len[tid] = bcf_hrec_find_key(kh_val(d, k).hrec[0],"length"); 
        int j;
        if ( sscanf(kh_val(d, k).hrec[0]->vals[len[tid]],"%d",&j) ) 
            len[tid] = j;     
        names[tid] = kh_key(d,k);
    }
    
    kstring_t s = {0,0,0};
    for (tid=0; tid<m; tid++)
    {
        s.l = 0;
        ksprintf(&s, "##contig=<ID=%s,length=%d>", names[tid], len[tid]);
        bcf_hdr_append(hdest, s.s);
    }
    if (s.m) free(s.s);
}
Пример #17
0
MRB_API mrb_value
mrb_hash_keys(mrb_state *mrb, mrb_value hash)
{
  khash_t(ht) *h = RHASH_TBL(hash);
  khiter_t k;
  mrb_int end;
  mrb_value ary;
  mrb_value *p;

  if (!h || kh_size(h) == 0) return mrb_ary_new(mrb);
  ary = mrb_ary_new_capa(mrb, kh_size(h));
  end = kh_size(h)-1;
  mrb_ary_set(mrb, ary, end, mrb_nil_value());
  p = RARRAY_PTR(ary);
  for (k = kh_begin(h); k != kh_end(h); k++) {
    if (kh_exist(h, k)) {
      mrb_value kv = kh_key(h, k);
      mrb_hash_value hv = kh_value(h, k);

      if (hv.n <= end) {
        p[hv.n] = kv;
      }
      else {
        p[end] = kv;
      }
    }
  }
  return ary;
}
Пример #18
0
bool convert_mrb_to_js_hash(mrb_state *mrb, mrb_value value, NPP npp, NPVariant *result)
{
    NPObject *window;
    NPN_GetValue(npp, NPNVWindowNPObject, &window);

    NPVariant hash;
    NPN_Invoke(npp, window, NPN_GetStringIdentifier("Object"), NULL, 0, &hash);
    NPN_ReleaseObject(window);

    khash_t(ht) *h = RHASH_TBL(value);
    if (!h){
        *result = hash;
        return true;
    }

    NPObject *hash_obj = NPVARIANT_TO_OBJECT(hash);
    for (khiter_t k=kh_begin(h); k!=kh_end(h); k++){
        if (kh_exist(h, k)) {
            mrb_value key = mrb_funcall(mrb, kh_key(h, k), "to_s", 0);
            mrb_value val = kh_value(h, k);

            NPVariant var;
            if (!convert_mrb_to_js(mrb, val, npp, &var)){
                NPN_ReleaseVariantValue(&hash);
                return false;
            }

            std::string name(RSTRING_PTR(key), RSTRING_LEN(key));
            NPN_SetProperty(npp, hash_obj, NPN_GetStringIdentifier(name.c_str()), &var);
        }
    }
    *result = hash;

    return true;
}
Пример #19
0
/* destroy the memory allocated for bgpstream filter */
void bgpstream_filter_mgr_destroy(bgpstream_filter_mgr_t *bs_filter_mgr)
{
  bgpstream_debug("\tBSF_MGR:: destroy start");
  if (bs_filter_mgr == NULL) {
    return; // nothing to destroy
  }
  // destroying filters
  bgpstream_interval_filter_t *tif;
  khiter_t k;
  // projects
  if (bs_filter_mgr->projects != NULL) {
    bgpstream_str_set_destroy(bs_filter_mgr->projects);
  }
  // collectors
  if (bs_filter_mgr->collectors != NULL) {
    bgpstream_str_set_destroy(bs_filter_mgr->collectors);
  }
  // bgp_types
  if (bs_filter_mgr->bgp_types != NULL) {
    bgpstream_str_set_destroy(bs_filter_mgr->bgp_types);
  }
  // peer asns
  if (bs_filter_mgr->peer_asns != NULL) {
    bgpstream_id_set_destroy(bs_filter_mgr->peer_asns);
  }
  // aspath expressions
  if (bs_filter_mgr->aspath_exprs != NULL) {
    bgpstream_str_set_destroy(bs_filter_mgr->aspath_exprs);
  }
  // prefixes
  if (bs_filter_mgr->prefixes != NULL) {
    bgpstream_patricia_tree_destroy(bs_filter_mgr->prefixes);
  }
  // communities
  if (bs_filter_mgr->communities != NULL) {
    kh_destroy(bgpstream_community_filter, bs_filter_mgr->communities);
  }
  // time_intervals
  tif = NULL;
  while (bs_filter_mgr->time_intervals != NULL) {
    tif = bs_filter_mgr->time_intervals;
    bs_filter_mgr->time_intervals = bs_filter_mgr->time_intervals->next;
    free(tif);
  }
  // rib/update frequency
  if (bs_filter_mgr->last_processed_ts != NULL) {
    for (k = kh_begin(bs_filter_mgr->last_processed_ts);
         k != kh_end(bs_filter_mgr->last_processed_ts); ++k) {
      if (kh_exist(bs_filter_mgr->last_processed_ts, k)) {
        free(kh_key(bs_filter_mgr->last_processed_ts, k));
      }
    }
    kh_destroy(collector_ts, bs_filter_mgr->last_processed_ts);
  }
  // free the mgr structure
  free(bs_filter_mgr);
  bs_filter_mgr = NULL;
  bgpstream_debug("\tBSF_MGR:: destroy end");
}
Пример #20
0
static void print_hashstats(famstats_t *stats, FILE *fp)
{
    std::vector<fm_t> fms(kh_size(stats->fm));
    unsigned i;
    khiter_t ki;
    fprintf(fp, "#Family size\tNumber of families\n");
    for(i = 0, ki = kh_begin(stats->fm); ki != kh_end(stats->fm); ++ki)
        if(kh_exist(stats->fm, ki))
            fms[i++] = {kh_val(stats->fm, ki), kh_key(stats->fm, ki)};
    std::sort(fms.begin(), fms.end(), [](const fm_t a, const fm_t b){
        return a.fm < b.fm;
    });
    for(i = 0; i < stats->fm->n_occupied; ++i)
        fprintf(fp, "%" PRIu64 "\t%" PRIu64 "\n", fms[i].fm, fms[i].n);

    fms.resize(stats->rc->n_occupied);
    for(i = 0, ki = kh_begin(stats->rc); ki != kh_end(stats->rc); ++ki)
        if(kh_exist(stats->rc, ki))
            fms[i++] = {kh_val(stats->rc, ki), kh_key(stats->rc, ki)};
    std::sort(fms.begin(), fms.end(), [](const fm_t a, const fm_t b){
        return a.fm < b.fm;
    });
    if(fms[0].fm != (uint64_t)-1) {
        fprintf(fp, "#RV'd in family\tNumber of families\n");
        for(i = 0; i < stats->rc->n_occupied; ++i)
            fprintf(fp, "%" PRIu64 "\t%" PRIu64 "\n", fms[i].fm, fms[i].n);
    }
    // Handle stats->np
    fms.resize(stats->np->n_occupied);
    size_t n_rsq_fams(0);
    for(i = 0, ki = kh_begin(stats->np); ki != kh_end(stats->np); ++ki) {
        if(kh_exist(stats->np, ki)) {
            fms[i++] = {kh_val(stats->np, ki), kh_key(stats->np, ki)};
            n_rsq_fams += kh_val(stats->np, ki);
        }
    }
    std::sort(fms.begin(), fms.end(), [](const fm_t a, const fm_t b){
        return a.fm < b.fm;
    });
    fprintf(fp, "#Number of families that were rescued: %lu\n", n_rsq_fams);
    fputs("#Number of pre-rescue reads in rescued\tNumber of families\n", fp);
    for(i = 0; i < stats->np->n_occupied; ++i)
        fprintf(fp, "%" PRIu64 "\t%" PRIu64 "\n", fms[i].fm, fms[i].n);
}
Пример #21
0
void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
{
	khiter_t idx = kh_put(str, map, key, rval);

	if ((*rval) >= 0) {
		if ((*rval) == 0)
			kh_key(map, idx) = key;
		kh_val(map, idx) = value;
	}
}
Пример #22
0
static void trans_tbl_destroy(trans_tbl_t *tbl) {
    free(tbl->tid_trans);
    khiter_t iter;
    for (iter = kh_begin(tbl->rg_trans); iter != kh_end(tbl->rg_trans); ++iter) {
        if (kh_exist(tbl->rg_trans, iter)) {
            free(kh_value(tbl->rg_trans, iter));
            free(kh_key(tbl->rg_trans, iter));
        }
    }
    for (iter = kh_begin(tbl->pg_trans); iter != kh_end(tbl->pg_trans); ++iter) {
        if (kh_exist(tbl->pg_trans, iter)) {
            free(kh_value(tbl->pg_trans, iter));
            free(kh_key(tbl->pg_trans, iter));
        }
    }

    kh_destroy(c2c,tbl->rg_trans);
    kh_destroy(c2c,tbl->pg_trans);
}
Пример #23
0
void
mrb_free_symtbl(mrb_state *mrb)
{
  khash_t(n2s) *h = mrb->name2sym;
  khiter_t k;

  for (k = kh_begin(h); k != kh_end(h); k++)
    if (kh_exist(h, k)) mrb_free(mrb, (char*)kh_key(h, k).name);
  kh_destroy(n2s,mrb->name2sym);
}
Пример #24
0
h2o_filecache_ref_t *h2o_filecache_open_file(h2o_filecache_t *cache, const char *path, int oflag)
{
    khiter_t iter = kh_get(opencache_set, cache->hash, path);
    h2o_filecache_ref_t *ref;
    int dummy;

    /* lookup cache, and return the one if found */
    if (iter != kh_end(cache->hash)) {
        ref = H2O_STRUCT_FROM_MEMBER(h2o_filecache_ref_t, _path, kh_key(cache->hash, iter));
        ++ref->_refcnt;
        goto Exit;
    }

    /* create a new cache entry */
    ref = h2o_mem_alloc(offsetof(h2o_filecache_ref_t, _path) + strlen(path) + 1);
    ref->_refcnt = 1;
    ref->_lru = (h2o_linklist_t){NULL};
    strcpy(ref->_path, path);

    /* if cache is used, then... */
    if (cache->capacity != 0) {
        /* purge one entry from LRU if cache is full */
        if (kh_size(cache->hash) == cache->capacity) {
            h2o_filecache_ref_t *purge_ref = H2O_STRUCT_FROM_MEMBER(h2o_filecache_ref_t, _lru, cache->lru.prev);
            khiter_t purge_iter = kh_get(opencache_set, cache->hash, purge_ref->_path);
            assert(purge_iter != kh_end(cache->hash));
            release_from_cache(cache, purge_iter);
        }
        /* assign the new entry */
        ++ref->_refcnt;
        kh_put(opencache_set, cache->hash, ref->_path, &dummy);
        h2o_linklist_insert(cache->lru.next, &ref->_lru);
    }

    /* open the file, or memoize the error */
    if ((ref->fd = open(path, oflag)) != -1 && fstat(ref->fd, &ref->st) == 0) {
        ref->_last_modified.str[0] = '\0';
        ref->_etag.len = 0;
    } else {
        ref->open_err = errno;
        if (ref->fd != -1) {
            close(ref->fd);
            ref->fd = -1;
        }
    }

Exit:
    /* if the cache entry retains an error, return it instead of the reference */
    if (ref->fd == -1) {
        errno = ref->open_err;
        h2o_filecache_close_file(ref);
        ref = NULL;
    }
    return ref;
}
Пример #25
0
void HashMap_destroy(HashMap* map) {

    for (khint_t k = kh_begin(map->h); k != kh_end(map->h); k++) {
        if (kh_exist(map->h, k)) {
            free((char*) kh_key(map->h, k));
        }
    }

    kh_destroy(str, map->h);
    free(map);
}
Пример #26
0
wp_error* wp_entry_write_to_segment(wp_entry* entry, wp_segment* seg, docid_t doc_id) {
  for(khiter_t i = kh_begin(entry->entries); i < kh_end(entry->entries); i++) {
    if(kh_exist(entry->entries, i)) {
      fielded_term ft = kh_key(entry->entries, i);
      RARRAY(pos_t) positions = kh_val(entry->entries, i);
      RELAY_ERROR(wp_segment_add_posting(seg, ft.field, ft.term, doc_id, RARRAY_NELEM(positions), RARRAY_ALL(positions)));
    }
  }

  return NO_ERROR;
}
Пример #27
0
void stk_reg_destroy(reghash_t *h)
{
	khint_t k;
	for (k = 0; k < kh_end(h); ++k) {
		if (kh_exist(h, k)) {
			free(kh_val(h, k).a);
			free((char*)kh_key(h, k));
		}
	}
	kh_destroy(reg, h);
}
Пример #28
0
void dna_hash_free(struct dna_hash_t *dna)
{
    khiter_t key;
    for (key = kh_begin(dna->hash); key != kh_end(dna->hash); ++key) {
        if (kh_exist(dna->hash, key))
            Free(kh_key(dna->hash, key));
    }
    kh_destroy(ref, dna->hash);
    Free(dna->offset);
    Free(dna);
}
Пример #29
0
static inline void release_from_cache(h2o_filecache_t *cache, khiter_t iter)
{
    const char *path = kh_key(cache->hash, iter);
    h2o_filecache_ref_t *ref = H2O_STRUCT_FROM_MEMBER(h2o_filecache_ref_t, _path, path);

    /* detach from list */
    kh_del(opencache_set, cache->hash, iter);
    h2o_linklist_unlink(&ref->_lru);

    /* and close */
    h2o_filecache_close_file(ref);
}
Пример #30
0
static void erase_ref(h2o_cache_t *cache, khiter_t iter, int reuse)
{
    h2o_cache_ref_t *ref = kh_key(cache->table, iter);

    if (!reuse)
        kh_del(cache, cache->table, iter);
    h2o_linklist_unlink(&ref->_lru_link);
    h2o_linklist_unlink(&ref->_age_link);
    cache->size -= ref->value.len;

    h2o_cache_release(cache, ref);
}