@@ -43,14 +43,32 @@ int type_from_string(const char *str)
4343 die ("invalid object type \"%s\"" , str );
4444}
4545
46+ /*
47+ * Return a numerical hash value between 0 and n-1 for the object with
48+ * the specified sha1. n must be a power of 2. Please note that the
49+ * return value is *not* consistent across computer architectures.
50+ */
4651static unsigned int hash_obj (const unsigned char * sha1 , unsigned int n )
4752{
4853 unsigned int hash ;
54+
55+ /*
56+ * Since the sha1 is essentially random, we just take the
57+ * required number of bits directly from the first
58+ * sizeof(unsigned int) bytes of sha1. First we have to copy
59+ * the bytes into a properly aligned integer. If we cared
60+ * about getting consistent results across architectures, we
61+ * would have to call ntohl() here, too.
62+ */
4963 memcpy (& hash , sha1 , sizeof (unsigned int ));
50- /* Assumes power-of-2 hash sizes in grow_object_hash */
5164 return hash & (n - 1 );
5265}
5366
67+ /*
68+ * Insert obj into the hash table hash, which has length size (which
69+ * must be a power of 2). On collisions, simply overflow to the next
70+ * empty bucket.
71+ */
5472static void insert_obj_hash (struct object * obj , struct object * * hash , unsigned int size )
5573{
5674 unsigned int j = hash_obj (obj -> sha1 , size );
@@ -63,6 +81,10 @@ static void insert_obj_hash(struct object *obj, struct object **hash, unsigned i
6381 hash [j ] = obj ;
6482}
6583
84+ /*
85+ * Look up the record for the given sha1 in the hash map stored in
86+ * obj_hash. Return NULL if it was not found.
87+ */
6688struct object * lookup_object (const unsigned char * sha1 )
6789{
6890 unsigned int i , first ;
@@ -92,6 +114,11 @@ struct object *lookup_object(const unsigned char *sha1)
92114 return obj ;
93115}
94116
117+ /*
118+ * Increase the size of the hash map stored in obj_hash to the next
119+ * power of 2 (but at least 32). Copy the existing values to the new
120+ * hash map.
121+ */
95122static void grow_object_hash (void )
96123{
97124 int i ;
0 commit comments