2013-12-14 03:06:40 +01:00
|
|
|
#include "git-compat-util.h"
|
2013-11-14 20:17:54 +01:00
|
|
|
#include "hashmap.h"
|
2018-02-14 19:07:19 +01:00
|
|
|
#include "strbuf.h"
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
struct test_entry
|
|
|
|
{
|
|
|
|
struct hashmap_entry ent;
|
|
|
|
/* key and value as two \0-terminated strings */
|
|
|
|
char key[FLEX_ARRAY];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *get_value(const struct test_entry *e)
|
|
|
|
{
|
|
|
|
return e->key + strlen(e->key) + 1;
|
|
|
|
}
|
|
|
|
|
2017-07-01 02:28:38 +02:00
|
|
|
static int test_entry_cmp(const void *cmp_data,
|
|
|
|
const void *entry,
|
|
|
|
const void *entry_or_key,
|
|
|
|
const void *keydata)
|
2013-11-14 20:17:54 +01:00
|
|
|
{
|
2017-07-01 02:28:38 +02:00
|
|
|
const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
|
|
|
|
const struct test_entry *e1 = entry;
|
|
|
|
const struct test_entry *e2 = entry_or_key;
|
|
|
|
const char *key = keydata;
|
|
|
|
|
|
|
|
if (ignore_case)
|
|
|
|
return strcasecmp(e1->key, key ? key : e2->key);
|
|
|
|
else
|
|
|
|
return strcmp(e1->key, key ? key : e2->key);
|
2013-11-14 20:17:54 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 19:08:57 +01:00
|
|
|
static struct test_entry *alloc_test_entry(unsigned int hash,
|
|
|
|
char *key, char *value)
|
2013-11-14 20:17:54 +01:00
|
|
|
{
|
2018-02-14 19:08:20 +01:00
|
|
|
size_t klen = strlen(key);
|
|
|
|
size_t vlen = strlen(value);
|
2018-02-14 19:06:34 +01:00
|
|
|
struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
|
2013-11-14 20:17:54 +01:00
|
|
|
hashmap_entry_init(entry, hash);
|
|
|
|
memcpy(entry->key, key, klen + 1);
|
|
|
|
memcpy(entry->key + klen + 1, value, vlen + 1);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HASH_METHOD_FNV 0
|
|
|
|
#define HASH_METHOD_I 1
|
|
|
|
#define HASH_METHOD_IDIV10 2
|
|
|
|
#define HASH_METHOD_0 3
|
|
|
|
#define HASH_METHOD_X2 4
|
|
|
|
#define TEST_SPARSE 8
|
|
|
|
#define TEST_ADD 16
|
|
|
|
#define TEST_SIZE 100000
|
|
|
|
|
|
|
|
static unsigned int hash(unsigned int method, unsigned int i, const char *key)
|
|
|
|
{
|
2014-12-09 10:48:36 +01:00
|
|
|
unsigned int hash = 0;
|
2013-11-14 20:17:54 +01:00
|
|
|
switch (method & 3)
|
|
|
|
{
|
|
|
|
case HASH_METHOD_FNV:
|
|
|
|
hash = strhash(key);
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_I:
|
|
|
|
hash = i;
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_IDIV10:
|
|
|
|
hash = i / 10;
|
|
|
|
break;
|
|
|
|
case HASH_METHOD_0:
|
|
|
|
hash = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method & HASH_METHOD_X2)
|
|
|
|
hash = 2 * hash;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test performance of hashmap.[ch]
|
|
|
|
* Usage: time echo "perfhashmap method rounds" | test-hashmap
|
|
|
|
*/
|
|
|
|
static void perf_hashmap(unsigned int method, unsigned int rounds)
|
|
|
|
{
|
|
|
|
struct hashmap map;
|
|
|
|
char buf[16];
|
|
|
|
struct test_entry **entries;
|
|
|
|
unsigned int *hashes;
|
|
|
|
unsigned int i, j;
|
|
|
|
|
2018-02-14 19:05:46 +01:00
|
|
|
ALLOC_ARRAY(entries, TEST_SIZE);
|
|
|
|
ALLOC_ARRAY(hashes, TEST_SIZE);
|
2013-11-14 20:17:54 +01:00
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
2018-02-14 19:06:57 +01:00
|
|
|
xsnprintf(buf, sizeof(buf), "%i", i);
|
2018-02-14 19:08:20 +01:00
|
|
|
entries[i] = alloc_test_entry(0, buf, "");
|
2013-11-14 20:17:54 +01:00
|
|
|
hashes[i] = hash(method, i, entries[i]->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method & TEST_ADD) {
|
|
|
|
/* test adding to the map */
|
|
|
|
for (j = 0; j < rounds; j++) {
|
2017-07-01 02:28:38 +02:00
|
|
|
hashmap_init(&map, test_entry_cmp, NULL, 0);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* add entries */
|
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
|
|
|
hashmap_entry_init(entries[i], hashes[i]);
|
|
|
|
hashmap_add(&map, entries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap_free(&map, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* test map lookups */
|
2017-07-01 02:28:38 +02:00
|
|
|
hashmap_init(&map, test_entry_cmp, NULL, 0);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* fill the map (sparsely if specified) */
|
|
|
|
j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
|
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
hashmap_entry_init(entries[i], hashes[i]);
|
|
|
|
hashmap_add(&map, entries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < rounds; j++) {
|
|
|
|
for (i = 0; i < TEST_SIZE; i++) {
|
2014-07-03 00:22:11 +02:00
|
|
|
hashmap_get_from_hash(&map, hashes[i],
|
|
|
|
entries[i]->key);
|
2013-11-14 20:17:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap_free(&map, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DELIM " \t\r\n"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read stdin line by line and print result of commands to stdout:
|
|
|
|
*
|
|
|
|
* hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
|
|
|
|
* put key value -> NULL / old value
|
|
|
|
* get key -> NULL / value
|
|
|
|
* remove key -> NULL / old value
|
|
|
|
* iterate -> key1 value1\nkey2 value2\n...
|
|
|
|
* size -> tablesize numentries
|
|
|
|
*
|
|
|
|
* perfhashmap method rounds -> test hashmap.[ch] performance
|
|
|
|
*/
|
add an extra level of indirection to main()
There are certain startup tasks that we expect every git
process to do. In some cases this is just to improve the
quality of the program (e.g., setting up gettext()). In
others it is a requirement for using certain functions in
libgit.a (e.g., system_path() expects that you have called
git_extract_argv0_path()).
Most commands are builtins and are covered by the git.c
version of main(). However, there are still a few external
commands that use their own main(). Each of these has to
remember to include the correct startup sequence, and we are
not always consistent.
Rather than just fix the inconsistencies, let's make this
harder to get wrong by providing a common main() that can
run this standard startup.
We basically have two options to do this:
- the compat/mingw.h file already does something like this by
adding a #define that replaces the definition of main with a
wrapper that calls mingw_startup().
The upside is that the code in each program doesn't need
to be changed at all; it's rewritten on the fly by the
preprocessor.
The downside is that it may make debugging of the startup
sequence a bit more confusing, as the preprocessor is
quietly inserting new code.
- the builtin functions are all of the form cmd_foo(),
and git.c's main() calls them.
This is much more explicit, which may make things more
obvious to somebody reading the code. It's also more
flexible (because of course we have to figure out _which_
cmd_foo() to call).
The downside is that each of the builtins must define
cmd_foo(), instead of just main().
This patch chooses the latter option, preferring the more
explicit approach, even though it is more invasive. We
introduce a new file common-main.c, with the "real" main. It
expects to call cmd_main() from whatever other objects it is
linked against.
We link common-main.o against anything that links against
libgit.a, since we know that such programs will need to do
this setup. Note that common-main.o can't actually go inside
libgit.a, as the linker would not pick up its main()
function automatically (it has no callers).
The rest of the patch is just adjusting all of the various
external programs (mostly in t/helper) to use cmd_main().
I've provided a global declaration for cmd_main(), which
means that all of the programs also need to match its
signature. In particular, many functions need to switch to
"const char **" instead of "char **" for argv. This effect
ripples out to a few other variables and functions, as well.
This makes the patch even more invasive, but the end result
is much better. We should be treating argv strings as const
anyway, and now all programs conform to the same signature
(which also matches the way builtins are defined).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2016-07-01 07:58:58 +02:00
|
|
|
int cmd_main(int argc, const char **argv)
|
2013-11-14 20:17:54 +01:00
|
|
|
{
|
2018-02-14 19:07:19 +01:00
|
|
|
struct strbuf line = STRBUF_INIT;
|
2013-11-14 20:17:54 +01:00
|
|
|
struct hashmap map;
|
|
|
|
int icase;
|
|
|
|
|
|
|
|
/* init hash map */
|
|
|
|
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
|
2017-07-01 02:28:38 +02:00
|
|
|
hashmap_init(&map, test_entry_cmp, &icase, 0);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* process commands from stdin */
|
2018-02-14 19:07:19 +01:00
|
|
|
while (strbuf_getline(&line, stdin) != EOF) {
|
2013-11-14 20:17:54 +01:00
|
|
|
char *cmd, *p1 = NULL, *p2 = NULL;
|
2018-02-14 19:08:57 +01:00
|
|
|
unsigned int hash = 0;
|
2013-11-14 20:17:54 +01:00
|
|
|
struct test_entry *entry;
|
|
|
|
|
|
|
|
/* break line into command and up to two parameters */
|
2018-02-14 19:07:19 +01:00
|
|
|
cmd = strtok(line.buf, DELIM);
|
2013-11-14 20:17:54 +01:00
|
|
|
/* ignore empty lines */
|
|
|
|
if (!cmd || *cmd == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p1 = strtok(NULL, DELIM);
|
|
|
|
if (p1) {
|
|
|
|
hash = icase ? strihash(p1) : strhash(p1);
|
|
|
|
p2 = strtok(NULL, DELIM);
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
if (!strcmp("hash", cmd) && p1) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* print results of different hash functions */
|
2018-02-14 19:08:20 +01:00
|
|
|
printf("%u %u %u %u\n",
|
|
|
|
strhash(p1), memhash(p1, strlen(p1)),
|
|
|
|
strihash(p1), memihash(p1, strlen(p1)));
|
2013-11-14 20:17:54 +01:00
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("add", cmd) && p1 && p2) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* create entry with key = p1, value = p2 */
|
2018-02-14 19:08:20 +01:00
|
|
|
entry = alloc_test_entry(hash, p1, p2);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* add to hashmap */
|
|
|
|
hashmap_add(&map, entry);
|
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("put", cmd) && p1 && p2) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* create entry with key = p1, value = p2 */
|
2018-02-14 19:08:20 +01:00
|
|
|
entry = alloc_test_entry(hash, p1, p2);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* add / replace entry */
|
|
|
|
entry = hashmap_put(&map, entry);
|
|
|
|
|
|
|
|
/* print and free replaced entry, if any */
|
|
|
|
puts(entry ? get_value(entry) : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("get", cmd) && p1) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* lookup entry in hashmap */
|
2014-07-03 00:22:11 +02:00
|
|
|
entry = hashmap_get_from_hash(&map, hash, p1);
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* print result */
|
|
|
|
if (!entry)
|
|
|
|
puts("NULL");
|
|
|
|
while (entry) {
|
|
|
|
puts(get_value(entry));
|
|
|
|
entry = hashmap_get_next(&map, entry);
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("remove", cmd) && p1) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
/* setup static key */
|
|
|
|
struct hashmap_entry key;
|
|
|
|
hashmap_entry_init(&key, hash);
|
|
|
|
|
|
|
|
/* remove entry from hashmap */
|
|
|
|
entry = hashmap_remove(&map, &key, p1);
|
|
|
|
|
|
|
|
/* print result and free entry*/
|
|
|
|
puts(entry ? get_value(entry) : "NULL");
|
|
|
|
free(entry);
|
|
|
|
|
|
|
|
} else if (!strcmp("iterate", cmd)) {
|
|
|
|
|
|
|
|
struct hashmap_iter iter;
|
|
|
|
hashmap_iter_init(&map, &iter);
|
|
|
|
while ((entry = hashmap_iter_next(&iter)))
|
|
|
|
printf("%s %s\n", entry->key, get_value(entry));
|
|
|
|
|
|
|
|
} else if (!strcmp("size", cmd)) {
|
|
|
|
|
|
|
|
/* print table sizes */
|
2017-09-06 17:43:48 +02:00
|
|
|
printf("%u %u\n", map.tablesize,
|
|
|
|
hashmap_get_size(&map));
|
2013-11-14 20:17:54 +01:00
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("intern", cmd) && p1) {
|
2014-07-03 00:22:54 +02:00
|
|
|
|
|
|
|
/* test that strintern works */
|
|
|
|
const char *i1 = strintern(p1);
|
|
|
|
const char *i2 = strintern(p1);
|
|
|
|
if (strcmp(i1, p1))
|
|
|
|
printf("strintern(%s) returns %s\n", p1, i1);
|
|
|
|
else if (i1 == p1)
|
|
|
|
printf("strintern(%s) returns input pointer\n", p1);
|
|
|
|
else if (i1 != i2)
|
|
|
|
printf("strintern(%s) != strintern(%s)", i1, i2);
|
|
|
|
else
|
|
|
|
printf("%s\n", i1);
|
|
|
|
|
2018-02-14 19:08:20 +01:00
|
|
|
} else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
|
2013-11-14 20:17:54 +01:00
|
|
|
|
|
|
|
perf_hashmap(atoi(p1), atoi(p2));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
printf("Unknown command %s\n", cmd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 19:07:19 +01:00
|
|
|
strbuf_release(&line);
|
2013-11-14 20:17:54 +01:00
|
|
|
hashmap_free(&map, 1);
|
|
|
|
return 0;
|
|
|
|
}
|