Add 'git_revpool_object' and 'git_revpool_table' structures. All the objects which will will be eventually transversable from a revision pool (commits, trees, etc) now inherit from the 'git_revpool_object' structure which identifies them with their own OID. Furthermore, the 'git_revpool_table' and related functions have been added, which allow for constant time lookup (hash table) of the loaded revpool objects based on their OID. Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Andreas Ericsson <ae@op5.se>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
diff --git a/src/commit.c b/src/commit.c
index b196a80..226eca4 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -32,7 +32,7 @@
const git_oid *git_commit_id(git_commit *c)
{
- return &c->id;
+ return &c->object.id;
}
void git_commit__mark_uninteresting(git_commit *commit)
@@ -75,10 +75,7 @@ int git_commit_parse_existing(git_commit *commit)
if (commit->parsed)
return 0;
- if (commit->pool == NULL || commit->pool->db == NULL)
- return -1;
-
- if (git_odb_read(&commit_obj, commit->pool->db, &commit->id) < 0)
+ if (git_odb_read(&commit_obj, commit->object.pool->db, &commit->object.id) < 0)
return -1;
if (commit_obj.type != GIT_OBJ_COMMIT)
@@ -110,8 +107,9 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
memset(commit, 0x0, sizeof(git_commit));
- git_oid_cpy(&commit->id, id);
- commit->pool = pool;
+ // Initialize parent object
+ git_oid_cpy(&commit->object.id, id);
+ commit->object.pool = pool;
return commit;
}
@@ -182,7 +180,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
while (git_commit__parse_oid(&oid, &buffer, buffer_end, "parent ") == 0) {
git_commit *parent;
- if ((parent = git_commit_lookup(commit->pool, &oid)) == NULL)
+ if ((parent = git_commit_lookup(commit->object.pool, &oid)) == NULL)
return -1;
// Inherit uninteresting flag
diff --git a/src/commit.h b/src/commit.h
index 75e6d95..818d98c 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -2,6 +2,7 @@
#define INCLUDE_commit_h__
#include "git/commit.h"
+#include "revobject.h"
#include <time.h>
@@ -22,9 +23,9 @@ typedef struct git_commit_list git_commit_list;
typedef struct git_commit_node git_commit_node;
struct git_commit {
- git_oid id;
+ git_revpool_object object;
+
time_t commit_time;
- git_revpool *pool;
git_commit_list parents;
unsigned short in_degree;
diff --git a/src/revobject.c b/src/revobject.c
new file mode 100644
index 0000000..7c25137
--- /dev/null
+++ b/src/revobject.c
@@ -0,0 +1,167 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "common.h"
+#include "revobject.h"
+
+const float max_load_factor = 0.65;
+
+unsigned int git_revpool_table__hash(const git_oid *id)
+{
+ const unsigned int m = 0x5bd1e995;
+ const int r = 24;
+
+ unsigned int h = 0xA8A3D5 ^ (unsigned int)id;
+ int i;
+
+ for (i = 0; i < GIT_OID_RAWSZ / 4; ++i)
+ {
+ unsigned int k = ((unsigned int *)id->id)[i];
+
+ k *= m;
+ k ^= k >> r;
+ k *= m;
+ h *= m;
+ h ^= k;
+ }
+
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
+
+ return h;
+}
+
+git_revpool_table *git_revpool_table_create(unsigned int min_size)
+{
+ git_revpool_table *table;
+
+ table = git__malloc(sizeof(table));
+
+ if (table == NULL)
+ return NULL;
+
+ // round up size to closest power of 2
+ min_size--;
+ min_size |= min_size >> 1;
+ min_size |= min_size >> 2;
+ min_size |= min_size >> 4;
+ min_size |= min_size >> 8;
+ min_size |= min_size >> 16;
+
+ table->size_mask = min_size;
+ table->count = 0;
+ table->max_count = (min_size + 1) * max_load_factor;
+
+ table->nodes = git__malloc((min_size + 1) * sizeof(git_revpool_node *));
+
+ if (table->nodes == NULL)
+ {
+ free(table);
+ return NULL;
+ }
+
+ memset(table->nodes, 0x0, (min_size + 1) * sizeof(git_revpool_node *));
+
+ return table;
+}
+
+int git_revpool_table_insert(git_revpool_table *table, git_revpool_object *object)
+{
+ git_revpool_node *node;
+ unsigned int index, hash;
+
+ if (table->count + 1 > table->max_count)
+ git_revpool_table_resize(table);
+
+ node = git__malloc(sizeof(git_revpool_node));
+ if (node == NULL)
+ return -1;
+
+ hash = git_revpool_table__hash(&object->id);
+ index = (hash & table->size_mask);
+
+ node->object = object;
+ node->hash = hash;
+ node->next = table->nodes[index];
+
+ table->nodes[index] = node;
+ table->count++;
+
+ return 0;
+}
+
+git_revpool_object *git_revpool_table_lookup(git_revpool_table *table, const git_oid *id)
+{
+ git_revpool_node *node;
+ unsigned int index, hash;
+
+ hash = git_revpool_table__hash(id);
+ index = (hash & table->size_mask);
+ node = table->nodes[index];
+
+ while (node != NULL)
+ {
+ if (node->hash == hash && git_oid_cmp(id, &node->object->id) == 0)
+ return node->object;
+
+ node = node->next;
+ }
+
+ return NULL;
+}
+
+void git_revpool_table_resize(git_revpool_table *table)
+{
+ git_revpool_node **new_nodes;
+ unsigned int new_size, i;
+
+ new_size = (table->size_mask + 1) * 2;
+
+ new_nodes = git__malloc(new_size * sizeof(git_revpool_node *));
+ if (new_nodes == NULL)
+ return;
+
+ memset(new_nodes, 0x0, new_size * sizeof(git_revpool_node *));
+
+ for (i = 0; i <= table->size_mask; ++i)
+ {
+ git_revpool_node *n;
+ unsigned int index;
+
+ while ((n = table->nodes[i]) != NULL)
+ {
+ table->nodes[i] = n->next;
+ index = n->hash & (new_size - 1);
+ n->next = new_nodes[index];
+ new_nodes[index] = n;
+ }
+ }
+
+ free(table->nodes);
+ table->nodes = new_nodes;
+ table->size_mask = (new_size - 1);
+ table->max_count = new_size * max_load_factor;
+}
diff --git a/src/revobject.h b/src/revobject.h
new file mode 100644
index 0000000..8ea17a2
--- /dev/null
+++ b/src/revobject.h
@@ -0,0 +1,39 @@
+#ifndef INCLUDE_objecttable_h__
+#define INCLUDE_objecttable_h__
+
+#include "git/common.h"
+#include "git/oid.h"
+
+struct git_revpool_object
+{
+ git_oid id;
+ git_revpool *pool;
+};
+
+struct git_revpool_node
+{
+ struct git_revpool_object *object;
+ unsigned int hash;
+ struct git_revpool_node *next;
+};
+
+struct git_revpool_table
+{
+ unsigned int size_mask;
+ unsigned int count;
+ unsigned int max_count;
+ struct git_revpool_node **nodes;
+};
+
+
+typedef struct git_revpool_node git_revpool_node;
+typedef struct git_revpool_object git_revpool_object;
+typedef struct git_revpool_table git_revpool_table;
+
+git_revpool_table *git_revpool_table_create(unsigned int min_size);
+int git_revpool_table_insert(git_revpool_table *table, git_revpool_object *object);
+git_revpool_object *git_revpool_table_lookup(git_revpool_table *table, const git_oid *id);
+void git_revpool_table_resize(git_revpool_table *table);
+
+
+#endif
diff --git a/src/revwalk.c b/src/revwalk.c
index 94b6d09..cbf7144 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -48,7 +48,7 @@ void gitrp_free(git_revpool *walk)
void gitrp_push(git_revpool *pool, git_commit *commit)
{
- if (commit->pool != pool)
+ if (commit->object.pool != pool)
return;
if (commit->seen)