Ejemplo n.º 1
0
void test_ht_search(void **state)
{
    Hashtable *ht;
    int i;

    ht = ht_create(0, uint16_hash,
            uint16_compare, uint16_copy, uint16_free,
            uint16_compare, uint16_copy, uint16_free);

    for (i=0; i<LIMIT; i++)
    {
        uint16_t *key;
        uint16_t *data;

        data = malloc(sizeof(uint16_t));
        *data = i;
        key = malloc(sizeof(uint16_t));
        *key = i;
        ht_insert(ht, key, data);
    }

    for (i=0; i<LIMIT; i++)
    {
        uint16_t *data;

        data = ht_search(ht, &i);
        assert_true(data != NULL);
        assert_int_equal(*data, i);
    }
    i = LIMIT + 1;
    assert_true(ht_search(ht, &i) == NULL);

    ht_free(ht);
}
Ejemplo n.º 2
0
int main(void)
{
	dict_h lh ;
	dict_iter iter ;
	int i ;
	int *ip ;
	struct ht_args args ;

	args.ht_bucket_entries = 2 ;
	args.ht_table_entries = 2 ;
	args.ht_objvalue = getval ;
	args.ht_keyvalue = getval ;

	lh = ht_create( int_comp, int_comp, 0, &args ) ;

	for ( i = 0 ; i < N ; i++ )
	{
		nums[ i ] = 10-i ;
		if ( ht_insert( lh, &nums[ i ] ) != DICT_OK )
		{
			printf( "Failed at %d\n", i ) ;
			exit( 1 ) ;
		}
	}

	printf( "Search/delete test\n" ) ;
	i = 7 ;
	ip = INTP( ht_search( lh, &i ) ) ;
	if ( ip == NULL )
		printf( "Search failed\n" ) ;
	else
		if ( ht_delete( lh, ip ) != DICT_OK )
		{
			printf( "Delete failed\n" ) ;
			exit( 0 ) ;
		}

	for ( i = 0 ; i < N ; i++ )
		if (( ip = INTP( ht_search( lh, &nums[ i ] ) ) ))
			printf( "%d found\n", nums[ i ] ) ;
		else
			printf( "%d not found\n", nums[ i ] ) ;

	iter = ht_iterate( lh , DICT_FROM_START ) ;
	while (( ip = INTP( ht_nextobj( lh, iter ) ) ))
		printf( "Object = %d\n", *ip ) ;

	for ( ip = INTP(ht_minimum( lh )) ; ip ; ip = INTP(ht_successor( lh, ip )) )
		printf( "Object = %d\n", *ip ) ;

	for ( ip=INTP(ht_maximum( lh )) ; ip ; ip=INTP(ht_predecessor( lh, ip )) )
		printf( "Object = %d\n", *ip ) ;

	exit( 0 ) ;
}
static bddp hit_z2b_rec(zddp f, my_hash *h)
{
  if(f == zdd_top()) return bdd_bot();
  if(f == zdd_bot()) return bdd_top();

  bddp r;
  if(ht_search((uintptr_t)f, (uintptr_t*)&r, h)) return r;

  INC_RECDEPTH(recdepth);  
  bddp r0 = hit_z2b_rec(zdd_lo(f), h);
  bddp r1 = hit_z2b_rec(zdd_hi(f), h);
  DEC_RECDEPTH(recdepth);

  bddp t = bdd_node(zdd_itemval(f), r1, bdd_top());
  ENSURE_TRUE_MSG(t != BDD_NULL, "BDD operation failed");
  r = bdd_and(r0, t);
  ENSURE_TRUE_MSG(r != BDD_NULL, "BDD operation failed");

  ht_insert((uintptr_t)f, (uintptr_t)r, h);

#ifdef SIZE_LOG
  const uintmax_t insize  = zdd_size(f);
  const uintmax_t outsize = bdd_size(r);
  fprintf(sizelog, "%ju\t%ju\n", insize, outsize);
  if(maxsize < outsize) maxsize = outsize;
#endif /*SIZE_LOG*/

  return r;
}
/**
 * Insert <key, val> into the global hash table
 *
 * return int:
 * 		0 - success
 * 		1 - failed to insert
 */
int ht_insert(const char *key, const char *val)
{
	e.key = (char *)key;
	e.data = (void *)val;

	ep = hsearch(e, ENTER);
	if (NULL == ep) {
		fprintf(stderr, "entry failed in hash table \n");
		exit(EXIT_FAILURE);
	}

	/* DFZ: debug info */
	log_msg("\n =====DFZ debug: ep->key = %s, ep->data = %s \n", ep->key, ep->data);

	/*
	 * DFZ: test ht_search
	 */
	ep = ht_search("/tmpfile");
	if ((ENTRY*)0 != ep)
		log_msg("\n =====DFZ debug: ep->key = %s, ep->data = %s (after 'ht_search')\n", ep->key, ep->data);
	else
		log_msg("\n =====DFZ debug: not found '/tmpfile' \n ");

	return 0;
}
Ejemplo n.º 5
0
void ht_set(SdbHash *ht, ut32 hash, void *data) {
	SdbHashEntry *e = ht_search (ht, hash);
	if (e) {
		if (ht->list->free)
			ht->list->free (e->data);
		e->data = data;
		e->iter->data = data;
	} else ht_insert (ht, hash, data, NULL);
}
Ejemplo n.º 6
0
/* search and remove an element */
int ht_rm(struct hashtable *t, void *key)
{
	int ret;
	unsigned int index;

	if ((ret = ht_search(t, key, &index)) != HT_FOUND)
		return ret;
	return ht_free(t, index);
}
Ejemplo n.º 7
0
int
sbz_ahc_d(unsigned char *input, unsigned char *output, unsigned *size)
{
  struct ht htree;
  int ch, k;
  unsigned code;
  bl_state_t bls;
  unsigned char *start;
  unsigned char codelengths[SBZ_NSYM];
  
  ht_generate_codelengths(codelengths);  
  ht_build(&htree, SBZ_NSYM - 1, codelengths);

  start = output;
  *size = 0;
  bl_new_state(&bls, input, &input[SBZ_BLOCK_SIZE]);
  
  for(;;) {
    code = bl_read_bits(&bls, htree.min_bits);
    
    for(k = htree.min_bits;; k++) {
      ch = ht_search(&htree, code, k);
      if(ch >= 0 || k == htree.max_bits) {
	break;
      }
      code = (code << 1) | bl_read_bits(&bls, 1);
    }
    
    if(ch < 0) {
      ht_dispose(&htree);
      return -1;
    }
    if(ch == SBZ_EOF) {
      break;
    }
    
    *output++ = ch;
  }
  
  *size = output - start;
  
  ht_dispose(&htree);
  
  return bl_get_offset(&bls) + 1;
}
Ejemplo n.º 8
0
/* Add an element, discarding the old if the key already exists */
int ht_replace(struct hashtable *t, void *key, void *data)
{
	int ret;
	unsigned int index;

	/* Try to add the element */
	ret = ht_add(t, key, data);
	if (ret == HT_OK || ret != HT_BUSY)
		return ret;
	/* It already exists, get the index */
	ret = ht_search(t, key, &index);
	assert(ret == HT_FOUND);
	/* Remove the old */
	ret = ht_free(t, index);
	assert(ret == HT_OK);
	/* And add the new */
	return ht_add(t, key, data);
}
static zddp minhit_nonsup_rec(zddp f, my_hash *h)
{
  if(f == zdd_bot()) return zdd_top();
  if(f == zdd_top()) return zdd_bot();

  zddp r; 
  if(ht_search((uintptr_t)f, (uintptr_t*)&r, h)) return r;

  zddp t  = zdd_union(zdd_lo(f), zdd_hi(f));
  ENSURE_TRUE_MSG(t != BDD_NULL, "ZDD operation failed");

  INC_RECDEPTH(recdepth);  
  zddp r0 = minhit_nonsup_rec(t, h);
  zddp tt = minhit_nonsup_rec(zdd_lo(f), h);
  DEC_RECDEPTH(recdepth);

  zddp r1 = nonsup(tt, r0);
  ENSURE_TRUE_MSG(r1 != BDD_NULL, "ZDD extra operation failed");
  r = zdd_node(zdd_itemval(f), r0, r1);
  ENSURE_TRUE_MSG(r  != BDD_NULL, "ZDD operation failed");

  ht_insert((uintptr_t)f, (uintptr_t)r, h);
  return r;
}
Ejemplo n.º 10
0
void *ht_lookup(SdbHash *ht, ut32 hash) {
	SdbHashEntry *entry = ht_search (ht, hash);
	return entry? entry->data : NULL;
}
Ejemplo n.º 11
0
Archivo: ht.c Proyecto: thejh/radare2
void *ht_lookup(SdbHash *ht, ut32 hash) {
	SdbHashEntry *entry;
	if (!ht->list->head) return NULL;
	entry = ht_search (ht, hash);
	return entry? entry->data : NULL;
}
Ejemplo n.º 12
0
//main simulator for the PUMP analysis
void simulate(  gzFile* inputFile
		, gzFile* allocFile 
		, FILE *result_file
		, gzFile dfile
		, FILE *mvec_file
		, FILE *l2misses_file
		, unsigned int size_l1
		, unsigned int size_l2
		)
{ 
	int cycle = 0;

//	int key_size = 
//	int val_size = 

	//initialize the pump structures...
	init_pumps(
			  size_l1
			, size_l2
	);
	
	char instring[1024];
	char* ptr;
	char* sstr;

	char astring[1024];
	char* asstr;

	unsigned int pc, opgroup;
	unsigned int if1 = 0, if2 = 0, if3 = 0;
	unsigned int r1, r2, r3;
	unsigned int op1, op2, op3, mem_addr;
	
	unsigned int alloc_ptr, alloc_size, alloc_tag=0;
	unsigned int dealloc_ptr, dealloc_size;

	unsigned int m[6];
	unsigned int r[2];

	struct PUMP_Output pump_out;


	unsigned int memcare, care, rescare;
	unsigned int tmpval;


	unsigned int inc = 1e3;
	unsigned int checkpoint = inc;

	//Processing trace...
  	while ((ptr=gzgets(*inputFile, instring, 1000))!= NULL) {
		//printf("%d - %s", cycle, instring);

		cycle++;

		instring[strlen(instring)] = '\0';	
		sstr = strtok(instring, " \n");

		pc = strToLInt(sstr);

		sstr = strtok(NULL, " ");

		if(opcodeToInt(sstr, &opgroup, &care, &rescare) == 0)
		{
			printf("%s\n", sstr);
			exit(0);
		}

		if((strcmp(sstr, "call_pal") != 0) && (strcmp(sstr,"wh64") != 0))
		{
			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r1 = atoi(sstr+1);
				if(*sstr == 'r')
					if1 = 0;
				else
					if1 = 1;
			}
			else {
				r1 = 0;
				if1 = 0;
			}

			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r2 = atoi(sstr+1);
				if(*sstr == 'r')
					if2 = 0;
				else
					if2 = 1;
			}
			else {
				r2 = 0;
				if2 = 0;
			}

			sstr = strtok(NULL, " ");
			if(*sstr != '-') {
				r3 = atoi(sstr+1);
				if(*sstr == 'r')
					if3 = 0;
				else
					if3 = 1;
			}
			else {
				r3 = 0;
				if3 = 0;
			}

			if((opgroup==arith2s1d) && (*sstr == '-'))
				opgroup = arith1s1d;


			op1=r1;
			op2=r2;
			op3=r3;

			int t;

			if((opgroup==arith2s1d) || (opgroup==arith1s1d) || (opgroup==move))
			{
				op3 = r1;
				op1 = r2;
				op2 = r3;

				t = if3;
				if3 = if1;
				if1 = if2;
				if2 = t;
			}
			else if(opgroup==other)
			{
				op1 = r2;

				if1 = if2;
			}
			else if((opgroup==bload) || (opgroup==wload))
			{
				op3 = r1;
				op1 = r2;

				if3 = if1;
				if1 = if2;
			}
			else if((opgroup==bstore) || (opgroup==wstore))
			{ 
				op1 = r1;
				op2 = r2;
			}
			else if((opgroup==ret) || (opgroup==icall) || (opgroup==dcall) || (opgroup==ijump))
			{
				op1 = r2;
				if1 = if2;
			}

			sstr = strtok(NULL, " ");
			if(*sstr == 'm')
			{
				sstr++;
				sstr[strlen(sstr)-1] = '\0';
				mem_addr = strToLInt(sstr);
			}
			else 
				mem_addr = 0;

			if(pc == pc_malloc_start)
			{
				//new allocation
				if(skip_alloc==0)
				{
					gzgets(*allocFile, astring, 128);
					//printf("%s", astring);
					astring[strlen(astring)-1] = '\0';
					asstr = strtok(astring, " ");
					asstr++;
					alloc_size = (unsigned int) strToLInt(asstr);
					gzgets(*allocFile, astring, 128);
					astring[strlen(astring)-1] = '\0';
					asstr = strtok(astring, " ");
					asstr++;
					alloc_ptr = (unsigned int) strToLInt(asstr);
					alloc_tag = create_new_tag_for_allocation();//create_new_segment(alloc_size, alloc_ptr);
					create_new_segment(alloc_size, alloc_ptr);
					//printf("alloc_size: %d\n", alloc_size);

					num_allocs++;
					while (alloc_size>0)
					{
						ht_insert(mem, alloc_ptr, alloc_tag);
						alloc_ptr+=4;
						alloc_size--;
					}
				}
				else
				{
					gzgets(*allocFile, astring, 128);
					gzgets(*allocFile, astring, 128);

					skip_alloc--;
				}	
			}
			else if(pc == pc_malloc_end)
			{
				if(num_allocs >= 1)
				{
					irf.tags[0] = alloc_tag;
					//printf("alloc_tag: %d (%d)\n", alloc_tag, irf.tags[0]);
				}
			} 
			else if(pc == pc_free_start)
			{
				//new deallocation
				gzgets(*allocFile, astring, 1000);
				astring[strlen(astring)-1] = '\0';
				asstr = strtok(astring, " ");
				asstr++;
				dealloc_ptr = (unsigned int) strToLInt(asstr);
				dealloc_size = free_mem_segment(&dealloc_ptr);

				if(dealloc_size == 0)
				{
//					printf("%x double free...exiting()\n", dealloc_ptr);
//					exit(0);
				}
				while(dealloc_size > 0)
				{
					ht_insert(mem, dealloc_ptr, BOTTOM);
					dealloc_ptr+=4;
					dealloc_size--;
				}
			}


			m[M_PC] = BOTTOM;//pctag;

//			if (ht_search(mem, pc, &tmpval) == -1)
//			{
//				//ht_insert(mem, pc, INSN_TAG);
//				//tmpval = INSN_TAG;
//				printf("%08x not found\n", pc);
//				exit(0);
//			}
			m[M_CI] = BOTTOM;//tmpval;

			m[M_OP] = opgroup;	

			if(if1 == 0)
				if(op1<32)
					m[M_OP1] = irf.tags[op1];
				else 
					m[M_OP1] = 0;
			else
				if(op1<32)
					m[M_OP1] = frf.tags[op1];
				else
					m[M_OP1] = 0;

			if(if2 == 0)
				if(op2 < 32)
					m[M_OP2] = irf.tags[op2];
				else
					m[M_OP2] = 0;
			else
				if(op2<32)
					m[M_OP2] = frf.tags[op2];
				else
					m[M_OP2] = 0;

			memcare = care & 0x1;
			if(memcare == 1)
			{
				if (ht_search(mem, mem_addr, &tmpval) == -1)
				{
					ht_insert(mem, mem_addr, BOTTOM);
					tmpval = BOTTOM;
				}
				m[M_MR] = tmpval;
			}
			else {
				m[M_MR] = BOTTOM;
			}


			unsigned int pump_input[6] = {m[M_PC], m[M_CI], m[M_OP], m[M_OP1], m[M_OP2], m[M_MR]};	//these are long tags
			//unsigned int i;
			mask_dc(pump_input, care);
			//printf("\t"); 
			//for(i=0;i<6;i++)
			//{
			//	printf("%d ", pump_input[i]);
			//}
			//printf("\n");

			digested_rule(pump_input, dfile);

			pump_out = pump_lookup(pump_input, care);	//pump_out is in long tags

			if(pump_out.found == 1)
			{
				if((rescare&1) == 1)
				{
					ht_insert(mem, mem_addr, pump_out.r[R_RES]);

				}
				if(((rescare&2)>>1) == 1)
				{
					if(if3 == 0)
					{
						irf.tags[op3] = pump_out.r[R_RES];
					}
					else
					{
						frf.tags[op3] = pump_out.r[R_RES];
					}
				}
				if(((rescare&4)>>2) == 1)
				{
					pctag = pump_out.r[R_PC];
				}
			}
			else {
				fprintf(l2misses_file, "%d\n", cycle);

				mvec_out(m, mvec_file);


				lm_memsafety(	  m
						, r
						);

				unsigned int pump_m[6] = {m[M_PC], m[M_CI], m[M_OP], m[M_OP1], m[M_OP2], m[M_MR]};
				unsigned int pump_r[2] = {r[R_PC], r[R_RES]};

				install_new_mr(pump_m, pump_r, care);

				//re-execute the instruction, this time with a hit!
				if((rescare&1) == 1)
				{
					ht_insert(mem, mem_addr, r[R_RES]);
				}
				if(((rescare&2)>>1) == 1)
				{
					if(if3 == 0)
						irf.tags[op3] = r[R_RES];
					else
					{
						frf.tags[op3] = r[R_RES];
					}
				}
				if(((rescare&4)>>2) == 1)
				{
					pctag = r[R_PC];
				}
			}
		}
Ejemplo n.º 13
0
int main(int argc, char *argv[])
{
	int i, op1, op2, found, answer;
	int attempts;
	keys_t *key;
	int *val;
	float av_repeats;
	hashtab_ptr ht;

	ht = ht_init(MAX_LF, hash, cmp);
	srand(time(NULL));

	if (argc == 1) {
		goto run;
	}
	for (i = 1; i < argc; i++) {
		/* TODO: handle arguments and flags */	
		printf("%s", argv[i]);
		set_max_operand(12);
	}

run:

	for (i = 0; i < num_qs; i++) {
		av_repeats = (0.0f + i) / num_qs;
		av_repeats *= expect_repeats(); 
		key = malloc(sizeof(keys_t));
		if (!key) {
			printf("Memory error\n");
			return 1;
		}
		val = malloc(sizeof(int));
		if (!val) {
			printf("Memory error\n");
			return 2;
		}
get_ops:
		op1 = get_operand();
		op2 = get_operand();
		key->op1 = op1;
		key->op2 = op2;
		*val = 1;
		if ((found = ht_search(ht, (void *)key, (void **)(&val)))) {
			if ((float)*val > (av_repeats * 3)) {
				goto get_ops;
			}
			printf("%d\n", *val);
		}
ht_insert_update(ht, (void *)key, (void *)val, freeval);
		if (found) {
			free(val);
		}
		printf("%d * %d = ", op1, op2);
		attempts = 0;
scanner:
		attempts++;
		scanf("%d", &answer);
		if (answer == op1 * op2) {
			printf("Correct!! \n");
		} else if (attempts >= 3) {
			printf("The correct answer is %d", (op1 * op2));
		} else {
			printf("Sorry, try again!\n");
			goto scanner;
		}
	}

	ht_free(ht, freekey, freeval);

	return 0;
}