|
5 | 5 | #include "commit.h" |
6 | 6 | #include "tag.h" |
7 | 7 |
|
8 | | -struct object **objs; |
9 | | -static int nr_objs; |
10 | | -int obj_allocs; |
| 8 | +static struct object **obj_hash; |
| 9 | +static int nr_objs, obj_hash_size; |
| 10 | + |
| 11 | +unsigned int get_max_object_index(void) |
| 12 | +{ |
| 13 | + return obj_hash_size; |
| 14 | +} |
| 15 | + |
| 16 | +struct object *get_indexed_object(unsigned int idx) |
| 17 | +{ |
| 18 | + return obj_hash[idx]; |
| 19 | +} |
11 | 20 |
|
12 | 21 | const char *type_names[] = { |
13 | 22 | "none", "blob", "tree", "commit", "bad" |
14 | 23 | }; |
15 | 24 |
|
| 25 | +static unsigned int hash_obj(struct object *obj, unsigned int n) |
| 26 | +{ |
| 27 | + unsigned int hash = *(unsigned int *)obj->sha1; |
| 28 | + return hash % n; |
| 29 | +} |
| 30 | + |
| 31 | +static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size) |
| 32 | +{ |
| 33 | + int j = hash_obj(obj, size); |
| 34 | + |
| 35 | + while (hash[j]) { |
| 36 | + j++; |
| 37 | + if (j >= size) |
| 38 | + j = 0; |
| 39 | + } |
| 40 | + hash[j] = obj; |
| 41 | +} |
| 42 | + |
16 | 43 | static int hashtable_index(const unsigned char *sha1) |
17 | 44 | { |
18 | 45 | unsigned int i; |
19 | 46 | memcpy(&i, sha1, sizeof(unsigned int)); |
20 | | - return (int)(i % obj_allocs); |
| 47 | + return (int)(i % obj_hash_size); |
21 | 48 | } |
22 | 49 |
|
23 | | -static int find_object(const unsigned char *sha1) |
| 50 | +struct object *lookup_object(const unsigned char *sha1) |
24 | 51 | { |
25 | 52 | int i; |
| 53 | + struct object *obj; |
26 | 54 |
|
27 | | - if (!objs) |
28 | | - return -1; |
| 55 | + if (!obj_hash) |
| 56 | + return NULL; |
29 | 57 |
|
30 | 58 | i = hashtable_index(sha1); |
31 | | - while (objs[i]) { |
32 | | - if (memcmp(sha1, objs[i]->sha1, 20) == 0) |
33 | | - return i; |
| 59 | + while ((obj = obj_hash[i]) != NULL) { |
| 60 | + if (!memcmp(sha1, obj->sha1, 20)) |
| 61 | + break; |
34 | 62 | i++; |
35 | | - if (i == obj_allocs) |
| 63 | + if (i == obj_hash_size) |
36 | 64 | i = 0; |
37 | 65 | } |
38 | | - return -1 - i; |
| 66 | + return obj; |
39 | 67 | } |
40 | 68 |
|
41 | | -struct object *lookup_object(const unsigned char *sha1) |
| 69 | +static void grow_object_hash(void) |
42 | 70 | { |
43 | | - int pos = find_object(sha1); |
44 | | - if (pos >= 0) |
45 | | - return objs[pos]; |
46 | | - return NULL; |
| 71 | + int i; |
| 72 | + int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size; |
| 73 | + struct object **new_hash; |
| 74 | + |
| 75 | + new_hash = calloc(new_hash_size, sizeof(struct object *)); |
| 76 | + for (i = 0; i < obj_hash_size; i++) { |
| 77 | + struct object *obj = obj_hash[i]; |
| 78 | + if (!obj) |
| 79 | + continue; |
| 80 | + insert_obj_hash(obj, new_hash, new_hash_size); |
| 81 | + } |
| 82 | + free(obj_hash); |
| 83 | + obj_hash = new_hash; |
| 84 | + obj_hash_size = new_hash_size; |
47 | 85 | } |
48 | 86 |
|
49 | 87 | void created_object(const unsigned char *sha1, struct object *obj) |
50 | 88 | { |
51 | | - int pos; |
52 | | - |
53 | 89 | obj->parsed = 0; |
54 | | - memcpy(obj->sha1, sha1, 20); |
55 | | - obj->type = TYPE_NONE; |
56 | 90 | obj->used = 0; |
| 91 | + obj->type = TYPE_NONE; |
| 92 | + obj->flags = 0; |
| 93 | + memcpy(obj->sha1, sha1, 20); |
57 | 94 |
|
58 | | - if (obj_allocs - 1 <= nr_objs * 2) { |
59 | | - int i, count = obj_allocs; |
60 | | - obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs); |
61 | | - objs = xrealloc(objs, obj_allocs * sizeof(struct object *)); |
62 | | - memset(objs + count, 0, (obj_allocs - count) |
63 | | - * sizeof(struct object *)); |
64 | | - for (i = 0; i < obj_allocs; i++) |
65 | | - if (objs[i]) { |
66 | | - int j = find_object(objs[i]->sha1); |
67 | | - if (j != i) { |
68 | | - j = -1 - j; |
69 | | - objs[j] = objs[i]; |
70 | | - objs[i] = NULL; |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - pos = find_object(sha1); |
76 | | - if (pos >= 0) |
77 | | - die("Inserting %s twice\n", sha1_to_hex(sha1)); |
78 | | - pos = -pos-1; |
| 95 | + if (obj_hash_size - 1 <= nr_objs * 2) |
| 96 | + grow_object_hash(); |
79 | 97 |
|
80 | | - objs[pos] = obj; |
| 98 | + insert_obj_hash(obj, obj_hash, obj_hash_size); |
81 | 99 | nr_objs++; |
82 | 100 | } |
83 | 101 |
|
@@ -222,7 +240,7 @@ void clear_object_marks(unsigned mark) |
222 | 240 | { |
223 | 241 | int i; |
224 | 242 |
|
225 | | - for (i = 0; i < obj_allocs; i++) |
226 | | - if (objs[i]) |
227 | | - objs[i]->flags &= ~mark; |
| 243 | + for (i = 0; i < obj_hash_size; i++) |
| 244 | + if (obj_hash[i]) |
| 245 | + obj_hash[i]->flags &= ~mark; |
228 | 246 | } |
0 commit comments