Code

I like the idea of the new ':/<oneline prefix>' notation, and gave it
[git.git] / object-refs.c
index a7d49c60d7176fbc90635e07dc2ce658125edafc..98ea10005a851c3879c6e65929e3e34068dc9e8b 100644 (file)
@@ -12,38 +12,36 @@ static unsigned int hash_obj(struct object *obj, unsigned int n)
        return hash % n;
 }
 
+static void insert_ref_hash(struct object_refs *ref, struct object_refs **hash, unsigned int size)
+{
+       int j = hash_obj(ref->base, size);
+
+       while (hash[j]) {
+               j++;
+               if (j >= size)
+                       j = 0;
+       }
+       hash[j] = ref;
+}
+
 static void grow_refs_hash(void)
 {
        int i;
        int new_hash_size = (refs_hash_size + 1000) * 3 / 2;
        struct object_refs **new_hash;
 
-       new_hash = calloc(new_hash_size, sizeof(struct object_refs *));
+       new_hash = xcalloc(new_hash_size, sizeof(struct object_refs *));
        for (i = 0; i < refs_hash_size; i++) {
-               int j;
                struct object_refs *ref = refs_hash[i];
                if (!ref)
                        continue;
-               j = hash_obj(ref->base, new_hash_size);
-               new_hash[j] = ref;
+               insert_ref_hash(ref, new_hash, new_hash_size);
        }
        free(refs_hash);
        refs_hash = new_hash;
        refs_hash_size = new_hash_size;
 }
 
-static void insert_ref_hash(struct object_refs *ref)
-{
-       int j = hash_obj(ref->base, refs_hash_size);
-
-       while (refs_hash[j]) {
-               j++;
-               if (j >= refs_hash_size)
-                       j = 0;
-       }
-       refs_hash[j] = ref;
-}
-
 static void add_object_refs(struct object *obj, struct object_refs *ref)
 {
        int nr = nr_object_refs + 1;
@@ -51,15 +49,19 @@ static void add_object_refs(struct object *obj, struct object_refs *ref)
        if (nr > refs_hash_size * 2 / 3)
                grow_refs_hash();
        ref->base = obj;
-       insert_ref_hash(ref);
+       insert_ref_hash(ref, refs_hash, refs_hash_size);
        nr_object_refs = nr;
 }
 
 struct object_refs *lookup_object_refs(struct object *obj)
 {
-       int j = hash_obj(obj, refs_hash_size);
        struct object_refs *ref;
+       int j;
 
+       /* nothing to lookup */
+       if (!refs_hash_size)
+               return NULL;
+       j = hash_obj(obj, refs_hash_size);
        while ((ref = refs_hash[j]) != NULL) {
                if (ref->base == obj)
                        break;
@@ -127,9 +129,6 @@ void mark_reachable(struct object *obj, unsigned int mask)
 
        if (!track_object_refs)
                die("cannot do reachability with object refs turned off");
-       /* nothing to lookup */
-       if (!refs_hash_size)
-               return;
        /* If we've been here already, don't bother */
        if (obj->flags & mask)
                return;