Пример #1
0
void usr_init()
{
	users = hashtab_create(str_hash, strcmp_hash, 101);
}
Пример #2
0
int main ()
{
	
	int i = 0;
	int res = 0;
	char *pres = NULL;
	hashtab_node * node = NULL;
	struct test_node *p = NULL;
    hashtab *h = NULL;
    #ifdef MEMORY_TEST
	setenv("MALLOC_TRACE","1.txt",1);
    mtrace();
    #endif

	h = hashtab_create(5,hashtab_hvalue,hashtab_keycmp,hashtab_node_free);
    assert(h!= NULL);
	while(1)
	{
		p = (struct test_node*)malloc(sizeof(struct test_node));
		assert(p != NULL);
		printf("\r\n 请输入key 和value,当可以等于\"quit\"时退出");
        scanf("%s",p->key);
		scanf("%s",p->data);

		if(strcmp(p->key,"quit") == 0)
		{
			free(p);
			break;
		}

        res = hashtab_insert(h,p->key,p->data);
		if (res != 0)
		{
			free(p);
			printf("\r\n key[%s],data[%s] insert failed %d",p->key,p->data,res);
		}
		else
		{
			printf("\r\n key[%s],data[%s] insert success %d",p->key,p->data,res);
		}
	}

	hashtab_dump(h);

	while(1)
	{
		p = (struct test_node*)malloc(sizeof(struct test_node));
		assert(p != NULL);
		printf("\r\n 请输入key 查询value的数值,当可以等于\"quit\"时退出");
        scanf("%s",p->key);

		if(strcmp(p->key,"quit") == 0)
		{
			free(p);
			break;
		}
        pres = hashtab_search(h,p->key);
		if (pres == NULL)
		{
			printf("\r\n key[%s] search data failed",p->key);
		}
		else
		{
			printf("\r\n key[%s],search data[%s] success",p->key,pres);
		}
		free(p);
	}
	hashtab_dump(h);
	while(1)
	{
		p = (struct test_node*)malloc(sizeof(struct test_node));
		assert(p != NULL);
		printf("\r\n 请输入key 删除节点的数值,当可以等于\"quit\"时退出");
        scanf("%s",p->key);

		if(strcmp(p->key,"quit") == 0)
		{
			free(p);
			break;
		}
        node = hashtab_delete(h,p->key);
		if (node == NULL)
		{
			printf("\r\n key[%s] delete node failed ",p->key);
		}
		else
		{
			printf("\r\n key[%s],delete data[%s] success",node->key,node->data);
		    h->hash_node_free(node);
		}
		free(p);
	    hashtab_dump(h);
	}

	hashtab_destory(h);
    #ifdef MEMORY_TEST
    muntrace();
    #endif
	return 0;

}
Пример #3
0
struct bpool * bpool_create_pool( void *environment, bpool_ops ops, unsigned num_blocks, unsigned num_partitions ){

  unsigned erred = BPOOL_FALSE;
  struct bpool *pool = NULL;
  pool = (struct bpool *) malloc(sizeof(struct bpool));
  
  if(pool) {

    /* TODO: Initialize variables in pool */

    /* Simple error checking -- gotta have my callbacks  */
    G_ASSERT(ops.fill,     "ERROR: fill() needs to be defined!\n",    HARD_ASSERT);
    G_ASSERT(ops.flush,    "ERROR: flush() needs to be defined!\n",   HARD_ASSERT);
    G_ASSERT(ops.demote,   "ERROR: demote() needs to be defined!\n",  HARD_ASSERT);
    G_ASSERT(ops.hash,     "ERROR: hash() needs to be defined!\n",    HARD_ASSERT);
    G_ASSERT(ops.compare,  "ERROR: compare() needs to be defined!\n", HARD_ASSERT);
    G_ASSERT(ops.mread,  "ERROR: memread() needs to be defined!\n", HARD_ASSERT);
    G_ASSERT(ops.mset,   "ERROR: memset() needs to be defined!\n",  HARD_ASSERT);
    G_ASSERT(ops.mwrite, "ERROR: memwrite() needs to be defined!\n",HARD_ASSERT);
    G_ASSERT(ops.malloc, "ERROR: memalloc() needs to be defined!\n",HARD_ASSERT);
    G_ASSERT(ops.mfree,  "ERROR: memfree() needs to be defined!\n", HARD_ASSERT);

    /* must be less than MAX_PARTITIONS */
    G_ASSERT( num_partitions < BPOOL_MAX_PARTITIONS, 
	      "ERROR: num_partitions must be less than BPOOL_MAX_PARTITIONS!\n",
	      HARD_ASSERT);

    /* initialize parameters */
    pool->ops = ops;
    pool->environment = environment;
    pool->num_blocks = num_blocks;
    pool->num_partitions = num_partitions;

    /* create internals */
    pool->table = (struct hashtab *) hashtab_create(ops.hash, ops.compare, pool->num_blocks );
    if(!pool->table) {
      fprintf(stderr, 
	      "ERROR: bpool_create_pool(): Can't malloc pool->table!\nAsked for %u\n", 
	      pool->num_blocks);
      erred = BPOOL_TRUE;
      goto init_cleanup;
    }


    fprintf(stdout, "NOTE: sizeof(bpool_node) is %lu\n", sizeof(struct bpool_node) );
    pool->clock = (struct bpool_node *) malloc(pool->num_blocks * sizeof(struct bpool_node));
    if(!pool->clock) {
      fprintf(stderr, 
	      "ERROR: bpool_create_pool(): Can't malloc pool->clock!\n"
	      );
      erred = BPOOL_TRUE;
      goto init_cleanup;
    }

    pool->clock_hand = calloc( pool->num_partitions, sizeof(unsigned) );
    if( !pool->clock_hand ) {
      fprintf(stderr,
	      "ERROR: Could not malloc space for clock_hand!!\n"
	      );
      erred = BPOOL_TRUE;
      goto init_cleanup;
    }
    
    pool->part_sizes = calloc( pool->num_partitions, sizeof(unsigned) );
    if( !pool->part_sizes ) {
      fprintf(stderr,
	      "ERROR: Could not malloc space for quotas!!\n"
	      );
      erred = BPOOL_TRUE;
      goto init_cleanup;
    }

    pool->part_quotas = calloc( pool->num_partitions, sizeof(unsigned) );
    if( !pool->part_quotas ) {
      fprintf(stderr,
	      "ERROR: Could not malloc space for quotas!!\n"
	      );
      erred = BPOOL_TRUE;
      goto init_cleanup;
    }

    /* create the buffers */
    unsigned i = 0;
    for(i=0; i < pool->num_blocks; i++) {
      pool->clock[i].buf = (char *) pool->ops.malloc(pool->environment, BPOOL_BUF);
      pool->clock[i].id  = (char *) pool->ops.malloc(pool->environment, BPOOL_KEY);
      pool->ops.mset(pool->environment, BPOOL_BUF, pool->clock[i].buf);
      pool->ops.mset(pool->environment, BPOOL_KEY, pool->clock[i].id);
    } /* end for i loop */

    /* good all memory allocated with no problems */
    /* setup locks */
#ifdef BPOOL_ENABLE_LOCKS
    pthread_mutexattr_t mattr;
    pthread_mutexattr_init(&mattr);
    pthread_mutexattr_settype(&mattr,  PTHREAD_MUTEX_ERRORCHECK);
    assert(0 == pthread_mutex_init(&pool->big_lock, &mattr));
#endif

    for(i=0; i < pool->num_blocks; i++) {
#ifdef BPOOL_ENABLE_LOCKS
      assert(0 == pthread_mutex_init(&(pool->clock[i].page_lock), &mattr));
#endif
      pool->clock[i].valid = 0;
      pool->clock[i].refbit = 0;
      pool->clock[i].part_id = 0;
    }

    for(i=0; i < pool->num_partitions; i++ ) {
      pool->clock_hand[i] = 0;
      pool->part_sizes[i] = 0;
      pool->part_quotas[i] = 0;
      pool->global_size = 0;
    }

    /* initialize internal variables */
    pool->ops.print = ops.print;
    pool->ops.fill = ops.fill;
    pool->ops.flush = ops.flush;
    pool->ops.demote = ops.demote;
    pool->ops.hash = ops.hash;
    pool->ops.compare = ops.compare;
    pool->ops.mset = ops.mset;
    pool->ops.mread = ops.mread;
    pool->ops.mwrite = ops.mwrite;
    pool->ops.malloc = ops.malloc;
    pool->ops.mfree = ops.mfree;
    pool->ops.stat = ops.stat;

    /* initialize stats */    
#ifdef BPOOL_ENABLE_LOCKS
    pthread_mutex_init( &pool->stats_lock, &mattr );
#endif
    {
      unsigned x=0, y=0;
      for(x=0; x < BPOOL_MAX_PARTITIONS; x++) {
	for(y=0; y < BPOOL_STAT_MAX; y++) {
	  pool->stats.part_num_gets[x][y] = 0;
	  pool->stats.part_num_hits[x][y] = 0;
	  pool->stats.part_num_misses[x][y] = 0;
	  pool->stats.part_num_fills[x][y] = 0;
	  pool->stats.part_num_flushes[x][y] = 0;
	  pool->stats.part_num_evicts[x][y] = 0;
	  pool->stats.part_num_victims[x][y] = 0;

	  pool->stats.part_lat_gets[x][y] = 0;
	  pool->stats.part_lat_hits[x][y] = 0;
	  pool->stats.part_lat_misses[x][y] = 0;
	  pool->stats.part_lat_fills[x][y] = 0;
	  pool->stats.part_lat_flushes[x][y] = 0;
	  pool->stats.part_lat_evicts[x][y] = 0;
	  pool->stats.part_lat_victims[x][y] = 0;

	  pool->stats.global_num_gets[y] = 0;
	  pool->stats.global_num_hits[y] = 0;
	  pool->stats.global_num_misses[y] = 0;
	  pool->stats.global_num_fills[y] = 0;
	  pool->stats.global_num_flushes[y] = 0;
	  pool->stats.global_num_evicts[y] = 0;
	  pool->stats.global_num_victims[y] = 0;
	  
	  pool->stats.global_lat_gets[y] = 0;
	  pool->stats.global_lat_hits[y] = 0;
	  pool->stats.global_lat_misses[y] = 0;
	  pool->stats.global_lat_fills[y] = 0;
	  pool->stats.global_lat_flushes[y] = 0;
	  pool->stats.global_lat_evicts[y] = 0;
	  pool->stats.global_lat_victims[y] = 0;
	  
	}
      }



    }

  }

 init_cleanup:
  /* TODO: Better Cleanup */
  if(erred == BPOOL_TRUE) { exit(-1); }

  return pool;
}