SSL_SESSION *ssl_scache_shmht_retrieve(server_rec *s, UCHAR *id, int idlen)
{
    SSLModConfigRec *mc = myModConfig();
    void *vp;
    SSL_SESSION *sess = NULL;
    UCHAR *ucpData;
    int nData;
    time_t expiry;
    time_t now;
    int n;

    /* allow the regular expiring to occur */
    ssl_scache_shmht_expire(s);

    /* lookup key in table */
    ssl_mutex_on(s);
    if (table_retrieve(mc->tSessionCacheDataTable,
                       id, idlen, &vp, &n) != TABLE_ERROR_NONE) {
        ssl_mutex_off(s);
        return NULL;
    }

    /* copy over the information to the SCI */
    nData = n-sizeof(time_t);
    ucpData = (UCHAR *)malloc(nData);
    if (ucpData == NULL) {
        ssl_mutex_off(s);
        return NULL;
    }
    memcpy(&expiry, vp, sizeof(time_t));
    memcpy(ucpData, (char *)vp+sizeof(time_t), nData);
    ssl_mutex_off(s);

    /* make sure the stuff is still not expired */
    now = time(NULL);
    if (expiry <= now) {
        ssl_scache_shmht_remove(s, id, idlen);
        return NULL;
    }

    /* unstreamed SSL_SESSION */
    sess = d2i_SSL_SESSION(NULL, &ucpData, nData);

    return sess;
}
Exemple #2
0
/*
 * compare the keys in two tables.  returns 1 if equal else 0
 */
static	int	test_eq(table_t *tab1_p, table_t *tab2_p, const int verb_b)
{
  int	ret, eq = 1, key_size, data1_size, data2_size;
  void	*key_p, *data1_p, *data2_p;
  
  /* test the table entries */
  for (ret = table_first(tab1_p, (void **)&key_p, &key_size,
			 (void **)&data1_p, &data1_size);
       ret == TABLE_ERROR_NONE;
       ret = table_next(tab1_p, (void **)&key_p, &key_size,
			(void **)&data1_p, &data1_size)) {
    ret = table_retrieve(tab2_p, key_p, key_size,
			 (void **)&data2_p, &data2_size);
    if (ret != TABLE_ERROR_NONE) {
      (void)fprintf(stderr, "could not find key of %d bytes: %s\n",
		    key_size, table_strerror(ret));
      eq = 0;
    }
    else if (data1_size == data2_size
	     && memcmp(data1_p, data2_p, data1_size) == 0) {
      if (verb_b) {
	(void)printf("key of %d bytes, data of %d bytes\n",
		     key_size, data1_size);
	fflush(stdout);
      }
    }
    else {
      (void)fprintf(stderr,
		    "ERROR: key of %d bytes: data (size %d) != other "
		    "(size %d)\n",
		    key_size, data1_size, data2_size);
      eq = 0;
    }
  }
  
  if (ret != TABLE_ERROR_NOT_FOUND) {
    eq = 0;
  }
  return eq;
}
Exemple #3
0
/*
 * try ITERN random program iterations.
 */
static	void	stress(table_t *tab_p, const int iter_n, const int mmaping_b)
{
  void		*data, *key;
  int		which = 0, mode, weight_total;
  int		iter_c, pnt_c, free_c, ret, ksize, dsize;
  entry_t	*grid, *free_p, *grid_p, *last_p;
  int		linear_b = 0, linear_eof_b = 0;
  
  (void)printf("Performing stress tests with %d iterations:\n", iter_n);
  (void)fflush(stdout);
  
  grid = malloc(sizeof(entry_t) * MAX_ENTRIES);
  if (grid == NULL) {
    (void)printf("problems allocating space for %d entries.\n",
		 MAX_ENTRIES);
    exit(1);
  }
  
  /* initialize free list */
  free_p = grid;
  for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) {
    grid_p->en_free_b = 1;
    grid_p->en_key = NULL;
    grid_p->en_key_size = 0;
    grid_p->en_data = NULL;
    grid_p->en_data_size = 0;
    grid_p->en_next_p = grid_p + 1;
  }
  /* redo the last next pointer */
  (grid_p - 1)->en_next_p = NULL;
  free_c = MAX_ENTRIES;
  
#if 0
  /* load the list */
  if (mmaping_b) {
    for (ret = table_first(tab_p, (void **)&key_p, NULL, (void **)&data_p,
			   NULL);
	 ret == TABLE_ERROR_NONE;
	 ret = table_next(tab_p, (void **)&key_p, NULL, (void **)&data_p,
			  NULL)) {
    }
  }
#endif
  
  /* total the weights */
  weight_total = 0;
  for (mode = 0; mode < MODE_MAX; mode++) {
    weight_total += mode_weights[mode];
  }
  
  for (iter_c = 0; iter_c < iter_n;) {
    int		weight;
    
    /* decide what to do */
    weight = RANDOM_VALUE(weight_total) + 1;
    for (mode = 0; mode < MODE_MAX; mode++) {
      weight -= mode_weights[mode];
      if (weight <= 0) {
	break;
      }
    }
    
    /* out of bounds */
    if (mode >= MODE_MAX) {
      continue;
    }
    
    switch (mode) {
      
    case MODE_CLEAR:
      if (mmaping_b || large_b) {
	continue;
      }
      
      call_c++;
      table_clear(tab_p);
      
      /* re-init free list */
      free_p = grid;
      for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) {
	if (! grid_p->en_free_b) {
	  if (grid_p->en_key != NULL) {
	    free(grid_p->en_key);
	  }
	  if (grid_p->en_data != NULL) {
	    free(grid_p->en_data);
	  }
	}
	grid_p->en_free_b = 1;
	grid_p->en_next_p = grid_p + 1;
      }
      /* redo the last next pointer */
      (grid_p - 1)->en_next_p = NULL;
      free_c = MAX_ENTRIES;
      linear_b = 0;
      linear_eof_b = 0;
      iter_c++;
      if (verbose_b) {
	(void)printf("table cleared.\n");
	fflush(stdout);
      }
      break;
      
    case MODE_INSERT:
      if (mmaping_b) {
	continue;
      }
      if (free_c > 0) {
	which = RANDOM_VALUE(free_c);
	last_p = NULL;
	grid_p = free_p;
	for (pnt_c = 0; pnt_c < which && grid_p != NULL; pnt_c++) {
	  last_p = grid_p;
	  grid_p = grid_p->en_next_p;
	}
	if (grid_p == NULL) {
	  (void)printf("reached end of free list prematurely\n");
	  exit(1);
	}
	
	do {
	  key = random_block(&ksize);
	} while (key == NULL);
	data = random_block(&dsize);
	
	call_c++;
	ret = table_insert(tab_p, key, ksize, data, dsize, NULL, 0);
	if (ret == TABLE_ERROR_NONE) {
	  if (verbose_b) {
	    (void)printf("stored in pos %d: %d, %d bytes of key, data\n",
			 grid_p - grid, ksize, dsize);
	    fflush(stdout);
	  }
	  
	  grid_p->en_free_b = 0;
	  grid_p->en_key = key;
	  grid_p->en_key_size = ksize;
	  grid_p->en_data = data;
	  grid_p->en_data_size = dsize;
	  
	  /* shift free list */
	  if (last_p == NULL) {
	    free_p = grid_p->en_next_p;
	  }
	  else {
	    last_p->en_next_p = grid_p->en_next_p;
	  }
	  grid_p->en_next_p = NULL;
	  free_c--;
	  iter_c++;
	}
	else {
	  for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) {
	    if (grid_p->en_free_b) {
	      continue;
	    }
	    if (grid_p->en_key_size == ksize
		&& memcmp(grid_p->en_key, key, ksize) == 0) {
	      break;
	    }
	  }
	  
	  /* if we did not store it then error */
	  if (grid_p >= grid + MAX_ENTRIES) {
	    (void)fprintf(stderr, "ERROR storing #%d: %s\n",
			  which, table_strerror(ret));
	  }
	  if (key != NULL) {
	    free(key);
	  }
	  if (data != NULL) {
	    free(data);
	  }
	}
      }
      break;
      
    case MODE_OVERWRITE:
      if (mmaping_b) {
	continue;
      }
      if (free_c < MAX_ENTRIES) {
	which = RANDOM_VALUE(MAX_ENTRIES);
	
	if (grid[which].en_free_b) {
	  continue;
	}
	
	data = random_block(&dsize);
	
	call_c++;
	ret = table_insert(tab_p, grid[which].en_key, grid[which].en_key_size,
			   data, dsize, NULL, 1);
	if (ret == TABLE_ERROR_NONE) {
	  if (verbose_b) {
	    (void)printf("overwrite pos %d with data of %d bytes\n",
			 which, dsize);
	    fflush(stdout);
	  }
	  grid[which].en_free_b = 0;
	  if (grid[which].en_data != NULL) {
	    free(grid[which].en_data);
	  }
	  grid[which].en_data = data;
	  grid[which].en_data_size = dsize;
	  grid[which].en_next_p = NULL;
	  free_c--;
	  iter_c++;
	}
	else {
	  (void)fprintf(stderr, "ERROR overwriting #%d: %s\n",
			which, table_strerror(ret));
	  free(data);
	}
      }
      break;
      
    case MODE_RETRIEVE:
      if (free_c < MAX_ENTRIES) {
	which = RANDOM_VALUE(MAX_ENTRIES);
	
	if (grid[which].en_free_b) {
	  continue;
	}
	
	call_c++;
	ret = table_retrieve(tab_p, grid[which].en_key, grid[which].en_key_size,
			     (void **)&data, &dsize);
	if (ret == TABLE_ERROR_NONE) {
	  if (grid[which].en_data_size == dsize
	      && memcmp(grid[which].en_data, data, dsize) == 0) {
	    if (verbose_b) {
	      (void)printf("retrieved key #%d, got data of %d bytes\n",
			   which, dsize);
	      fflush(stdout);
	    }
	  }
	  else {
	    (void)fprintf(stderr,
			  "ERROR: retrieve key #%d: data (%d bytes) didn't "
			  "match table (%d bytes)\n",
			  which, grid[which].en_data_size, dsize);
	  }
	  iter_c++;
	}
	else {
	  (void)fprintf(stderr, "error retrieving key #%d: %s\n",
			which, table_strerror(ret));
	}
      }
      break;
      
    case MODE_DELETE:
      if (mmaping_b) {
	continue;
      }
      if (free_c >= MAX_ENTRIES) {
	continue;
      }
      
      which = RANDOM_VALUE(MAX_ENTRIES);
      
      if (grid[which].en_free_b) {
	continue;
      }
      
      call_c++;
      ret = table_delete(tab_p, grid[which].en_key, grid[which].en_key_size,
			 (void **)&data, &dsize);
      if (ret == TABLE_ERROR_NONE) {
	if (grid[which].en_data_size == dsize
	    && memcmp(grid[which].en_data, data, dsize) == 0) {
	  if (verbose_b) {
	    (void)printf("deleted key #%d, got data of %d bytes\n",
			 which, dsize);
	    fflush(stdout);
	  }
	}
	else {
	  (void)fprintf(stderr,
			"ERROR deleting key #%d: data didn't match table\n",
			which);
	}
	grid[which].en_free_b = 1;
	if (grid[which].en_key != NULL) {
	  free(grid[which].en_key);
	}
	if (grid[which].en_data != NULL) {
	  free(grid[which].en_data);
	}
	grid[which].en_next_p = free_p;
	free_p = grid + which;
	free_c++;
	if (free_c == MAX_ENTRIES) {
	  linear_b = 0;
	  linear_eof_b = 0;
	}
	iter_c++;
	if (data != NULL) {
	  free(data);
	}
      }
      else {
	(void)fprintf(stderr, "ERROR deleting key %d: %s\n",
		      which, table_strerror(ret));
      }
      break;
      
    case MODE_DELETE_FIRST:
      /*
       * We have a problem here.  This is the only action routine
       * which modifies the table and is not key based.  We don't have
       * a way of looking up the key in our local data structure.
       */
      break;
      
    case MODE_FIRST:
      call_c++;
      ret = table_first(tab_p, (void **)&key, &ksize, (void **)&data, &dsize);
      if (ret == TABLE_ERROR_NONE) {
	linear_b = 1;
	linear_eof_b = 0;
	if (verbose_b) {
	  (void)printf("first entry has key, data of %d, %d bytes\n",
		       ksize, dsize);
	  fflush(stdout);
	}
	iter_c++;
      }
      else if (free_c == MAX_ENTRIES) {
	if (verbose_b) {
	  (void)printf("no first in table\n");
	  fflush(stdout);
	}
      }
      else {
	(void)fprintf(stderr, "ERROR: first in table: %s\n",
		      table_strerror(ret));
      }
      break;
      
    case MODE_NEXT:
      call_c++;
      ret = table_next(tab_p, (void **)&key, &ksize, (void **)&data, &dsize);
      if (ret == TABLE_ERROR_NONE) {
	if (verbose_b) {
	  (void)printf("next entry has key, data of %d, %d\n",
		       ksize, dsize);
	  fflush(stdout);
	}
	iter_c++;
      }
      else if (ret == TABLE_ERROR_LINEAR && (! linear_b)) {
	if (verbose_b) {
	  (void)printf("no first command run yet\n");
	  fflush(stdout);
	}
      }
      else if (ret == TABLE_ERROR_NOT_FOUND) {
	if (verbose_b) {
	  (void)printf("reached EOF with next in table: %s\n",
		       table_strerror(ret));
	  fflush(stdout);
	}
	linear_b = 0;
	linear_eof_b = 1;
      }
      else {
	(void)fprintf(stderr, "ERROR: table_next reports: %s\n",
		      table_strerror(ret));
	linear_b = 0;
	linear_eof_b = 0;
      }
      break;
      
    case MODE_THIS:
      call_c++;
      ret = table_this(tab_p, (void **)&key, &ksize, (void **)&data, &dsize);
      if (ret == TABLE_ERROR_NONE) {
	if (verbose_b) {
	  (void)printf("this entry has key,data of %d, %d bytes\n",
		       ksize, dsize);
	  fflush(stdout);
	}
	iter_c++;
      }
      else if (ret == TABLE_ERROR_LINEAR && (! linear_b)) {
	if (verbose_b) {
	  (void)printf("no first command run yet\n");
	  fflush(stdout);
	}
      }
      else if (ret == TABLE_ERROR_NOT_FOUND || linear_eof_b) {
	if (verbose_b) {
	  (void)printf("table linear already reached EOF\n");
	  fflush(stdout);
	}
      }
      else {
	(void)fprintf(stderr, "ERROR: this table: %s\n", table_strerror(ret));
	linear_b = 0;
	linear_eof_b = 0;
      }
      break;
      
    case MODE_INFO:
      {
	int	buckets, entries;
	
	call_c++;
	ret = table_info(tab_p, &buckets, &entries);
	if (ret == TABLE_ERROR_NONE) {
	  if (verbose_b) {
	    (void)printf("table has %d buckets, %d entries\n",
			 buckets, entries);
	    fflush(stdout);
	  }
	  iter_c++;
	}
	else {
	  (void)fprintf(stderr, "ERROR: table info: %s\n",
			table_strerror(ret));
	}
      }
    break;
    
    case MODE_ADJUST:
      {
	int	buckets, entries;
	
	if (mmaping_b || auto_adjust_b || large_b) {
	  continue;
	}
	
	call_c++;
	ret = table_info(tab_p, &buckets, &entries);
	if (ret == TABLE_ERROR_NONE) {
	  if (entries == 0) {
	    if (verbose_b) {
	      (void)printf("cannot adjusted table, %d entries\n", entries);
	      fflush(stdout);
	    }
	  }
	  else if (buckets == entries) {
	    if (verbose_b) {
	      (void)printf("no need to adjust table, %d buckets and entries\n",
			   buckets);
	      fflush(stdout);
	    }
	  }
	  else {
	    ret = table_adjust(tab_p, entries);
	    if (ret == TABLE_ERROR_NONE) {
	      (void)printf("adjusted table from %d to %d buckets\n",
			   buckets, entries);
	      iter_c++;
	    }
	    else {
	      (void)printf("ERROR: table adjust to %d buckets: %s\n",
			   entries, table_strerror(ret));
	    }
	  }
	}
	else {
	  (void)fprintf(stderr, "ERROR: table info: %s\n",
			table_strerror(ret));
	}
      }
      break;
      
    default:
      (void)printf("unknown mode %d\n", which);
      break;
    }
  }
  
  /* run through the grid and free the entries */
  for (grid_p = grid; grid_p < grid + MAX_ENTRIES; grid_p++) {
    if (! grid_p->en_free_b) {
      if (grid_p->en_key != NULL) {
	free(grid_p->en_key);
      }
      if (grid_p->en_data != NULL) {
	free(grid_p->en_data);
      }
    }
  }
  
  /* free used pointers */
  free(grid);
}
/* driver to test sequence of inserts and deletes.
*/
void equilibriumDriver(void)
{
    int i, code;
    int key_range, num_keys;
    int size;
    int ran_index;
    int suc_search, suc_trials, unsuc_search, unsuc_trials;
    int keys_added, keys_removed;
    int *ip;
    table_t *test_table;
    hashkey_t key;
    data_t dp;
    clock_t start, end;

    /* print parameters for this test run */
    printf("\n----- Equilibrium test driver -----\n");
    printf("  Trials: %d\n", Trials);

    test_table = table_construct(TableSize, ProbeDec);
    num_keys = (int) (TableSize * LoadFactor);

    /* build a table as starting point */
    build_table(test_table, num_keys);
    size = num_keys;

    key_range = MAXID - MINID + 1;
    /* in equilibrium make inserts and removes with equal probability */
    suc_search = suc_trials = unsuc_search = unsuc_trials = 0;
    keys_added = keys_removed = 0;
    start = clock();
    for (i = 0; i < Trials; i++) {
        if (drand48() < 0.5 && table_full(test_table) == FALSE) {
            // insert only if table not full
            // for separate chaining table is never full
            key = (hashkey_t) (drand48() * key_range) + MINID;
            ip = (int *) malloc(sizeof(int));
            *ip = key;
            /* insert returns 0 if key not found, 1 if older key found */
            if (Verbose) printf("Trial %d, Insert Key %u", i, key);
            code = table_insert(test_table, key, ip);
            if (code == 0) {
                /* key was not in table so added */
                unsuc_search += table_stats(test_table);
                unsuc_trials++;
                keys_added++;
                if (Verbose) printf(" added\n");
            } else if (code == 1) {
                suc_search += table_stats(test_table);
                suc_trials++;
                if (Verbose) printf(" replaced (rare!)\n");
            } else {
                printf("!!!Trial %d failed to insert key (%u) with code (%d)\n", i, key, code);
                exit(10);
            }
        } else if (table_entries(test_table) > TableSize/4) {
            // delete only if table is at least 25% full
            // why 25%?  Would 10% be better?  Lower than 10% will
            // be computationally expensive
            do {
                ran_index = (int) (drand48() * TableSize);
                key = table_peek(test_table, ran_index,0);
            } while (key == 0);
            if (Verbose) printf("Trial %d, Delete Key %u", i, key);
            if (key < MINID || MAXID < key) {
                printf("\n\n  table peek failed: invalid key (%u) during trial (%d)\n", key, i);
                exit(12);
            }
            dp = table_delete(test_table, key);
            if (dp != NULL) {
                if (Verbose) printf(" removed\n");
                suc_search += table_stats(test_table);
                suc_trials++;
                keys_removed++;
                assert(*(int *)dp == key);
                free(dp);
            } else {
                printf("!!! failed to find key (%u) in table, trial (%d)!\n", key, i);
                printf("this is a catastrophic error!!!\n");
                exit(11);
            }
        }
    }
    end = clock();

    if (Verbose) {
        printf("Table after equilibrium trials\n");
        table_debug_print(test_table);
    }

    size += keys_added - keys_removed;
    printf("  Keys added (%d), removed (%d) new size should be (%d) and is (%d)\n",
           keys_added, keys_removed, size, table_entries(test_table));
    assert(size == table_entries(test_table));
    printf("  After exercise, time=%g \n",
           1000*((double)(end-start))/CLOCKS_PER_SEC);
    printf("  successful searches during exercise=%g, trials=%d\n",
           (double) suc_search/suc_trials, suc_trials);
    printf("  unsuccessful searches during exercise=%g, trials=%d\n",
           (double) unsuc_search/unsuc_trials, unsuc_trials);


    /* test access times for new table */

    /* separate chaining handled differently
     * should improve design of table_peek function so it
     * returns 0 if count is invalid when using open addressing.
     * In current design it is ignored
     */
    suc_search = suc_trials = unsuc_search = unsuc_trials = 0;
    start = clock();
    /* check each position in table for key */
    if (ProbeDec == CHAIN) {
        for (i = 0; i < TableSize; i++) {
            int count = 0;
            key = table_peek(test_table, i, count);
            while (key != 0) {
                assert(MINID <= key && key <= MAXID);
                dp = table_retrieve(test_table, key);
                if (dp == NULL) {
                    printf("Failed key (%u) should be at (%d)\n", key, i);
                    exit(15);
                } else {
                    suc_search += table_stats(test_table);
                    suc_trials++;
                    assert(*(int *)dp == key);
                }
                key = table_peek(test_table, i, ++count);
            }
        }
    } else {
        for (i = 0; i < TableSize; i++) {
            key = table_peek(test_table, i, 0);
            if (key != 0) {
                assert(MINID <= key && key <= MAXID);
                dp = table_retrieve(test_table, key);
                if (dp == NULL) {
                    printf("Failed to find key (%u) but it is in location (%d)\n",
                           key, i);
                    exit(16);
                } else {
                    suc_search += table_stats(test_table);
                    suc_trials++;
                    assert(*(int *)dp == key);
                }
            }
        }
    }
    for (i = 0; i < Trials; i++) {
        /* random key with uniform distribution */
        key = (hashkey_t) (drand48() * key_range) + MINID;
        dp = table_retrieve(test_table, key);
        if (dp == NULL) {
            unsuc_search += table_stats(test_table);
            unsuc_trials++;
        } else {
            // this should be very rare
            assert(*(int *)dp == key);
        }
    }
    end = clock();
    size = table_entries(test_table);
    printf("  After retrieve experiment, time=%g\n",
           1000*((double)(end-start))/CLOCKS_PER_SEC);
    printf("  New load factor = %g\n", (double) size/TableSize);
    printf("  Percent empty locations marked deleted = %g\n",
           (double) 100.0 * table_deletekeys(test_table)
           / (TableSize - table_entries(test_table)));

    printf("   Measured avg probes for successful search=%g, trials=%d\n",
           (double) suc_search/suc_trials, suc_trials);

    if (ProbeDec == CHAIN && LoadFactor > 0.5  && LoadFactor < 1.5) {
        printf("     ** This measure is biased.  See comments\n\n");
        /* The design of the equilibirum driver depends on the uniform
         * selection of keys to insert and remove.  For linear, double, and
         * quadratic probing selecting a key to remove is done with a uniform
         * distribution among all possible keys.  However, for separate
         * chaining, the algorithm simply picks a table location with a uniform
         * distribution, but this is not the same as picking a key with a
         * uniform distribution.  So, there is a bias that a key in a table
         * location with fewer other keys is more likely to be selected.  This
         * causes the average number of probes for a successful search to
         * increase as the equilibrium driver runs for a long time.  To remove
         * the bias, a solution is needed to pick a key with a uniform
         * distribution when chaining is used.  It is not clear how to select a
         * key with low computational cost.
         */
    }
    printf("   Measured avg probes for unsuccessful search=%g, trials=%d\n",
           (double) unsuc_search/unsuc_trials, unsuc_trials);
    printf("    Do deletions increase avg number of probes?\n");
    performanceFormulas((double) size/TableSize);

    /* rehash and retest table */
    printf("  Rehash table\n");
    test_table = table_rehash(test_table, TableSize);
    /* number entries in table should not change */
    assert(size == table_entries(test_table));
    /* rehashing must clear all entries marked for deletion */
    assert(0 == table_deletekeys(test_table));

    /* test access times for rehashed table */

    suc_search = suc_trials = unsuc_search = unsuc_trials = 0;
    start = clock();
    /* check each position in table for key */
    if (ProbeDec == CHAIN) {
        for (i = 0; i < TableSize; i++) {
            int count = 0;
            key = table_peek(test_table, i, count);
            while (key != 0) {
                assert(MINID <= key && key <= MAXID);
                dp = table_retrieve(test_table, key);
                if (dp == NULL) {
                    printf("Failed key (%u) should be at (%d)\n", key, i);
                    exit(25);
                } else {
                    suc_search += table_stats(test_table);
                    suc_trials++;
                    assert(*(int *)dp == key);
                }
                key = table_peek(test_table, i, ++count);
            }
        }
    } else {
        for (i = 0; i < TableSize; i++) {
            key = table_peek(test_table, i, 0);
            if (key != 0) {
                assert(MINID <= key && key <= MAXID);
                dp = table_retrieve(test_table, key);
                if (dp == NULL) {
                    printf("Failed to find key (%u) after rehash but it is in location (%d)\n",
                           key, i);
                    exit(26);
                } else {
                    suc_search += table_stats(test_table);
                    suc_trials++;
                    assert(*(int *)dp == key);
                }
            }
        }
    }
    for (i = 0; i < Trials; i++) {
        /* random key with uniform distribution */
        key = (hashkey_t) (drand48() * key_range) + MINID;
        dp = table_retrieve(test_table, key);
        if (dp == NULL) {
            unsuc_search += table_stats(test_table);
            unsuc_trials++;
        } else {
            // this should be very rare
            assert(*(int *)dp == key);
        }
    }
    end = clock();
    size = table_entries(test_table);
    printf("  After rehash, time=%g\n",
           1000*((double)(end-start))/CLOCKS_PER_SEC);
    printf("   Measured avg probes for successful search=%g, trials=%d\n",
           (double) suc_search/suc_trials, suc_trials);

    printf("   Measured avg probes for unsuccessful search=%g, trials=%d\n",
           (double) unsuc_search/unsuc_trials, unsuc_trials);

    /* remove and free all items from table */
    table_destruct(test_table);

    printf("----- End of equilibrium test -----\n\n");
}
/* driver to build and test tables. Note this driver
 * does not delete keys from the table.
 */
void RetrieveDriver()
{
    int i;
    int key_range, num_keys;
    int suc_search, suc_trials, unsuc_search, unsuc_trials;
    table_t *test_table;
    hashkey_t key;
    data_t dp;

    /* print parameters for this test run */
    printf("\n----- Retrieve driver -----\n");
    printf("  Trials: %d\n", Trials);

    num_keys = (int) (TableSize * LoadFactor);
    test_table = table_construct(TableSize, ProbeDec);

    build_table(test_table, num_keys);

    key_range = MAXID - MINID + 1;

    if (Trials > 0) {
        /* access table to measure probes for an unsuccessful search */
        suc_search = suc_trials = unsuc_search = unsuc_trials = 0;
        for (i = 0; i < Trials; i++) {
            /* random key with uniform distribution */
            key = (hashkey_t) (drand48() * key_range) + MINID;
            if (Verbose)
                printf("%d: looking for %d\n", i, key);
            dp = table_retrieve(test_table, key);
            if (dp == NULL) {
                unsuc_search += table_stats(test_table);
                unsuc_trials++;
                if (Verbose)
                    printf("\t not found with %d probes\n",
                           table_stats(test_table));
            } else {
                // this should be very rare
                suc_search += table_stats(test_table);
                suc_trials++;
                if (Verbose)
                    printf("\t\t FOUND with %d probes (this is rare!)\n",
                           table_stats(test_table));
                assert(*(int *)dp == key);
            }
        }
        assert(num_keys == table_entries(test_table));
        if (suc_trials > 0)
            printf("    Avg probes for successful search = %g measured with %d trials\n",
                   (double) suc_search/suc_trials, suc_trials);
        if (unsuc_trials > 0)
            printf("    Avg probes for unsuccessful search = %g measured with %d trials\n",
                   (double) unsuc_search/unsuc_trials, unsuc_trials);
    }

    /* print expected values from analysis with compare to experimental
     * measurements */
    performanceFormulas(LoadFactor);

    /* remove and free all items from table */
    table_destruct(test_table);
    printf("----- End of access driver -----\n\n");
}
/* driver to test small tables.  This is a series of
 * simple tests and is not exhaustive.
 *
 * input: test_M is the table size for this test run
 */
void RehashDriver(int test_M)
{
    int i, *ip, code;
    table_t *H;

    printf("\n----- Rehash driver -----\n");
    if (ProbeDec == CHAIN) {
        printf("This design of the rehash driver does not work with separate chaining\n");
        return;
    }
    hashkey_t startkey = MINID + (test_M - MINID%test_M);
    assert(startkey%test_M == 0);
    assert(test_M > 5);  // tests designed for size at least 6

    H = table_construct(test_M, ProbeDec);
    // fill table sequentially
    for (i = 0; i < test_M-1; i++) {
        ip = (int *) malloc(sizeof(int));
        *ip = 10*i;
        assert(table_full(H) == 0);
        code = table_insert(H, startkey+i, ip);
        ip = NULL;
        assert(code == 0);
        assert(table_entries(H) == i+1);
        assert(table_stats(H) == 1);
        assert(table_peek(H,i,0) == startkey+i);
    }
    if (Verbose) {
        printf("\nfull table, last entry empty\n");
        table_debug_print(H);
    }
    // tests on empty position
    assert(table_peek(H,i,0) == 0);
    assert(NULL == table_retrieve(H, startkey+i));
    assert(table_stats(H) == 1);
    assert(table_full(H) == 1);
    assert(-1 == table_insert(H, MAXID, NULL));
    // retrieve and replace each entry
    for (i = 0; i < test_M-1; i++) {
        ip = table_retrieve(H, startkey+i);
        assert(*(int *)ip == 10*i);
        ip = NULL;
        assert(table_stats(H) == 1);
        ip = table_retrieve(H, startkey+i+test_M);
        assert(ip == NULL);
        assert(2 <= table_stats(H) && table_stats(H) <= test_M);
        if (ProbeDec == LINEAR)
            assert(table_stats(H) == i+2);
        ip = (int *) malloc(sizeof(int));
        *ip = 99*i;
        assert(1 == table_insert(H, startkey+i, ip));
        ip = NULL;
        ip = table_retrieve(H, startkey+i);
        assert(*(int *)ip == 99*i);
        ip = NULL;
    }
    assert(table_entries(H) == test_M-1);
    assert(table_full(H) == 1);
    // delete tests
    assert(table_deletekeys(H) == 0);
    ip = table_delete(H, startkey+1);
    assert(*(int *)ip == 99);
    free(ip);
    ip = NULL;
    if (Verbose) {
        printf("\nsecond entry deleted, last entry empty\n");
        table_debug_print(H);
    }
    assert(table_entries(H) == test_M-2);
    assert(table_full(H) == 0);
    assert(table_peek(H,1,0) == 0);
    assert(table_deletekeys(H) == 1);
    ip = table_retrieve(H, startkey+1);  // check key is not there
    assert(ip == NULL);
    assert(table_stats(H) >= 2);
    // attempt to delete keys not in table
    assert(NULL == table_delete(H, startkey+1));
    assert(NULL == table_delete(H, startkey+test_M-1));
    // insert key in its place
    ip = (int *) malloc(sizeof(int));
    *ip = 123;
    assert(0 == table_insert(H, startkey+1+test_M, ip));
    ip = NULL;
    assert(table_peek(H,1,0) == startkey+1+test_M);
    ip = table_retrieve(H, startkey+1+test_M);
    assert(*(int *)ip == 123);
    ip = NULL;
    assert(table_entries(H) == test_M-1);
    assert(table_full(H) == 1);
    assert(table_deletekeys(H) == 0);
    for (i = 2; i < test_M-1; i++) {     // clear out all but two keys
        ip = table_delete(H, startkey+i);
        assert(*(int *)ip == 99*i);
        free(ip);
        ip = NULL;
    }
    assert(table_entries(H) == 2);
    ip = (int *) malloc(sizeof(int));    // fill last empty
    *ip = 456;
    assert(0 == table_insert(H, startkey+test_M-1, ip));
    ip = NULL;
    assert(table_entries(H) == 3);
    // unsuccessful search when no empty keys
    assert(NULL == table_retrieve(H, startkey+test_M));

    // two keys the collide in position 0
    ip = (int *) malloc(sizeof(int));
    *ip = 77;
    assert(0 == table_insert(H, startkey+test_M, ip));
    ip = (int *) malloc(sizeof(int));
    *ip = 88;
    assert(0 == table_insert(H, startkey+10*test_M, ip));
    ip = NULL;
    assert(table_entries(H) == 5);
    ip = table_delete(H, startkey);  // delete position 0
    assert(*(int *)ip == 0);
    free(ip);
    ip = NULL;
    assert(table_entries(H) == 4);
    ip = (int *) malloc(sizeof(int));  // replace
    *ip = 87;
    assert(1 == table_insert(H, startkey+10*test_M, ip));
    ip = NULL;
    assert(table_entries(H) == 4);
    ip = (int *) malloc(sizeof(int));   // put back position 0
    *ip = 76;
    assert(0 == table_insert(H, startkey+20*test_M, ip));
    ip = NULL;
    assert(table_entries(H) == 5);
    assert(table_peek(H,0,0) == startkey+20*test_M);
    assert(table_deletekeys(H) == test_M-5);
    // verify 5 items in table
    ip = table_retrieve(H, startkey+1+test_M);
    assert(*(int *)ip == 123);
    ip = table_retrieve(H, startkey+test_M);
    assert(*(int *)ip == 77);
    ip = table_retrieve(H, startkey+10*test_M);
    assert(*(int *)ip == 87);
    ip = table_retrieve(H, startkey+20*test_M);
    assert(*(int *)ip == 76);
    ip = table_retrieve(H, startkey+test_M-1);
    assert(*(int *)ip == 456);
    ip = NULL;
    // rehash
    H = table_rehash(H, test_M);
    assert(table_entries(H) == 5);
    assert(table_deletekeys(H) == 0);
    if (Verbose) {
        printf("\ntable after rehash with 5 items\n");
        table_debug_print(H);
    }
    // verify 5 items in table
    ip = table_retrieve(H, startkey+1+test_M);
    assert(*(int *)ip == 123);
    ip = table_retrieve(H, startkey+test_M);
    assert(*(int *)ip == 77);
    ip = table_retrieve(H, startkey+10*test_M);
    assert(*(int *)ip == 87);
    ip = table_retrieve(H, startkey+20*test_M);
    assert(*(int *)ip == 76);
    ip = table_retrieve(H, startkey+test_M-1);
    assert(*(int *)ip == 456);
    ip = NULL;

    // rehash and increase table size
    // If linear double the size
    // If double, need new prime
    int new_M = 2*test_M;
    if (ProbeDec == DOUBLE)
        new_M = find_first_prime(new_M);

    H = table_rehash(H, new_M);
    assert(table_entries(H) == 5);
    assert(table_deletekeys(H) == 0);
    if (Verbose) {
        printf("\nafter increase table to %d with 5 items\n", new_M);
        table_debug_print(H);
    }
    // verify 5 keys and information not lost during rehash
    ip = table_retrieve(H, startkey+1+test_M);
    assert(*(int *)ip == 123);
    ip = table_retrieve(H, startkey+test_M);
    assert(*(int *)ip == 77);
    ip = table_retrieve(H, startkey+10*test_M);
    assert(*(int *)ip == 87);
    ip = table_retrieve(H, startkey+20*test_M);
    assert(*(int *)ip == 76);
    ip = table_retrieve(H, startkey+test_M-1);
    assert(*(int *)ip == 456);
    ip = NULL;

    // fill the new larger table
    assert(table_full(H) == 0);
    int new_items = new_M - 1 - 5;
    int base_addr = 2*startkey + 20*test_M*test_M;
    if (base_addr+new_items*test_M > MAXID) {
        printf("re-run -b driver with smaller table size\n");
        exit(1);
    }
    for (i = 0; i < new_items; i++) {
        ip = (int *) malloc(sizeof(int));
        *ip = 10*i;
        code = table_insert(H, base_addr+i*test_M, ip);
        ip = NULL;
        assert(code == 0);
        assert(table_entries(H) == i+1+5);
    }
    assert(table_full(H) == 1);
    assert(table_entries(H) == new_M-1);
    if (Verbose) {
        printf("\nafter larger table filled\n");
        table_debug_print(H);
    }
    // verify new items are found
    for (i = 0; i < new_items; i++) {
        ip = table_retrieve(H, base_addr+i*test_M);
        assert(*(int *)ip == 10*i);
        ip = NULL;
    }

    // clean up table
    table_destruct(H);
    printf("----- Passed rehash driver -----\n\n");
}