예제 #1
0
파일: time.c 프로젝트: jgriffiths/ccan
struct timerel time_divide(struct timerel t, unsigned long div)
{
	struct timerel res;
	uint64_t rem, ns;

	/* Dividing seconds is simple. */
	res.ts.tv_sec = TIMEREL_CHECK(t).ts.tv_sec / div;
	rem = t.ts.tv_sec % div;

	/* If we can't fit remainder * 1,000,000,000 in 64 bits? */
#if 0 /* ilog is great, but we use fp for multiply anyway. */
	bits = ilog64(rem);
	if (bits + 30 >= 64) {
		/* Reduce accuracy slightly */
		rem >>= (bits - (64 - 30));
		div >>= (bits - (64 - 30));
	}
예제 #2
0
int ilog64_nz(uint64_t _v)
{
  return ilog64(_v);
}
예제 #3
0
int main(int argc, char *argv[])
{
	unsigned int i, j, moves;
	struct tdb_context *tdb;
	unsigned char *buffer;
	tdb_off_t oldoff = 0, newoff;
	int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
			TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
			TDB_NOMMAP|TDB_CONVERT };
	struct tdb_data key = { (unsigned char *)"key", 3 };
	struct tdb_data data;

	buffer = malloc(MAX_SIZE);
	for (i = 0; i < MAX_SIZE; i++)
		buffer[i] = i;

	plan_tests(sizeof(flags) / sizeof(flags[0])
		   * ((3 + MAX_SIZE/SIZE_STEP * 4) * 2 + 6)
		   + 1);

	/* Using tdb_store. */
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		tdb = tdb_open("run-append.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(tdb);
		if (!tdb)
			continue;

		moves = 0;
		for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
			data.dptr = buffer;
			data.dsize = j;
			ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
			ok1(tdb_check(tdb, NULL, NULL) == 0);
			data = tdb_fetch(tdb, key);
			ok1(data.dsize == j);
			ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
			free(data.dptr);
			newoff = tdb_offset(tdb, key);
			if (newoff != oldoff)
				moves++;
			oldoff = newoff;
		}
		ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
		/* We should increase by 50% each time... */
		ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
		tdb_close(tdb);
	}

	/* Using tdb_append. */
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		size_t prev_len = 0;
		tdb = tdb_open("run-append.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(tdb);
		if (!tdb)
			continue;

		moves = 0;
		for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
			data.dptr = buffer + prev_len;
			data.dsize = j - prev_len;
			ok1(tdb_append(tdb, key, data) == 0);
			ok1(tdb_check(tdb, NULL, NULL) == 0);
			data = tdb_fetch(tdb, key);
			ok1(data.dsize == j);
			ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
			free(data.dptr);
			prev_len = data.dsize;
			newoff = tdb_offset(tdb, key);
			if (newoff != oldoff)
				moves++;
			oldoff = newoff;
		}
		ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
		/* We should increase by 50% each time... */
		ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
		tdb_close(tdb);
	}

	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		tdb = tdb_open("run-append.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(tdb);
		if (!tdb)
			continue;

		/* Huge initial store. */
		data.dptr = buffer;
		data.dsize = MAX_SIZE;
		ok1(tdb_append(tdb, key, data) == 0);
		ok1(tdb_check(tdb, NULL, NULL) == 0);
		data = tdb_fetch(tdb, key);
		ok1(data.dsize == MAX_SIZE);
		ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
		free(data.dptr);
		ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
		tdb_close(tdb);
	}

	ok1(tap_log_messages == 0);
	return exit_status();
}