Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 86 lines (74 sloc) 1.835 kb
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
1 /*
2 * decorate.c - decorate a git object with some arbitrary
3 * data.
4 */
5 #include "cache.h"
6 #include "object.h"
7 #include "decorate.h"
8
54988bd Jeff King decorate: allow const objects to be decorated
peff authored
9 static unsigned int hash_obj(const struct object *obj, unsigned int n)
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
10 {
039dc71 hashmap: factor out getting a hash code from a SHA1
Karsten Blees authored
11 return sha1hash(obj->sha1) % n;
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
12 }
13
54988bd Jeff King decorate: allow const objects to be decorated
peff authored
14 static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
15 {
16 int size = n->size;
17 struct object_decoration *hash = n->hash;
91fe2f9 Dan McGee Unify signedness in hashing calls
toofishes authored
18 unsigned int j = hash_obj(base, size);
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
19
20 while (hash[j].base) {
21 if (hash[j].base == base) {
22 void *old = hash[j].decoration;
23 hash[j].decoration = decoration;
24 return old;
25 }
26 if (++j >= size)
27 j = 0;
28 }
29 hash[j].base = base;
30 hash[j].decoration = decoration;
31 n->nr++;
32 return NULL;
33 }
34
35 static void grow_decoration(struct decoration *n)
36 {
37 int i;
38 int old_size = n->size;
6991357 Junio C Hamano fast-export --export-marks: fix off by one error
gitster authored
39 struct object_decoration *old_hash = n->hash;
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
40
41 n->size = (old_size + 1000) * 3 / 2;
42 n->hash = xcalloc(n->size, sizeof(struct object_decoration));
43 n->nr = 0;
44
45 for (i = 0; i < old_size; i++) {
54988bd Jeff King decorate: allow const objects to be decorated
peff authored
46 const struct object *base = old_hash[i].base;
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
47 void *decoration = old_hash[i].decoration;
48
83f0412 decorate.c: compact table when growing
Kevin Bracey authored
49 if (!decoration)
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
50 continue;
51 insert_decoration(n, base, decoration);
52 }
53 free(old_hash);
54 }
55
56 /* Add a decoration pointer, return any old one */
54988bd Jeff King decorate: allow const objects to be decorated
peff authored
57 void *add_decoration(struct decoration *n, const struct object *obj,
58 void *decoration)
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
59 {
60 int nr = n->nr + 1;
61
62 if (nr > n->size * 2 / 3)
63 grow_decoration(n);
64 return insert_decoration(n, obj, decoration);
65 }
66
67 /* Lookup a decoration pointer */
54988bd Jeff King decorate: allow const objects to be decorated
peff authored
68 void *lookup_decoration(struct decoration *n, const struct object *obj)
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
69 {
91fe2f9 Dan McGee Unify signedness in hashing calls
toofishes authored
70 unsigned int j;
a59b276 Linus Torvalds Add a generic "object decorator" interface, and make object refs use it
torvalds authored
71
72 /* nothing to lookup */
73 if (!n->size)
74 return NULL;
75 j = hash_obj(obj, n->size);
76 for (;;) {
77 struct object_decoration *ref = n->hash + j;
78 if (ref->base == obj)
79 return ref->decoration;
80 if (!ref->base)
81 return NULL;
82 if (++j == n->size)
83 j = 0;
84 }
85 }
Something went wrong with that request. Please try again.