Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 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 | |
Jeff King | 54988bd | 2008-08-20 13:55:33 -0400 | [diff] [blame] | 9 | static unsigned int hash_obj(const struct object *obj, unsigned int n) |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 10 | { |
| 11 | unsigned int hash = *(unsigned int *)obj->sha1; |
| 12 | return hash % n; |
| 13 | } |
| 14 | |
Jeff King | 54988bd | 2008-08-20 13:55:33 -0400 | [diff] [blame] | 15 | static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration) |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 16 | { |
| 17 | int size = n->size; |
| 18 | struct object_decoration *hash = n->hash; |
| 19 | int j = hash_obj(base, size); |
| 20 | |
| 21 | while (hash[j].base) { |
| 22 | if (hash[j].base == base) { |
| 23 | void *old = hash[j].decoration; |
| 24 | hash[j].decoration = decoration; |
| 25 | return old; |
| 26 | } |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 27 | if (++j >= size) |
| 28 | j = 0; |
| 29 | } |
| 30 | hash[j].base = base; |
| 31 | hash[j].decoration = decoration; |
| 32 | n->nr++; |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | static void grow_decoration(struct decoration *n) |
| 37 | { |
| 38 | int i; |
| 39 | int old_size = n->size; |
Junio C Hamano | 6991357 | 2008-07-03 00:25:23 -0700 | [diff] [blame] | 40 | struct object_decoration *old_hash = n->hash; |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 41 | |
| 42 | n->size = (old_size + 1000) * 3 / 2; |
| 43 | n->hash = xcalloc(n->size, sizeof(struct object_decoration)); |
| 44 | n->nr = 0; |
| 45 | |
| 46 | for (i = 0; i < old_size; i++) { |
Jeff King | 54988bd | 2008-08-20 13:55:33 -0400 | [diff] [blame] | 47 | const struct object *base = old_hash[i].base; |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 48 | void *decoration = old_hash[i].decoration; |
| 49 | |
| 50 | if (!base) |
| 51 | continue; |
| 52 | insert_decoration(n, base, decoration); |
| 53 | } |
| 54 | free(old_hash); |
| 55 | } |
| 56 | |
| 57 | /* Add a decoration pointer, return any old one */ |
Jeff King | 54988bd | 2008-08-20 13:55:33 -0400 | [diff] [blame] | 58 | void *add_decoration(struct decoration *n, const struct object *obj, |
| 59 | void *decoration) |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 60 | { |
| 61 | int nr = n->nr + 1; |
| 62 | |
| 63 | if (nr > n->size * 2 / 3) |
| 64 | grow_decoration(n); |
| 65 | return insert_decoration(n, obj, decoration); |
| 66 | } |
| 67 | |
| 68 | /* Lookup a decoration pointer */ |
Jeff King | 54988bd | 2008-08-20 13:55:33 -0400 | [diff] [blame] | 69 | void *lookup_decoration(struct decoration *n, const struct object *obj) |
Linus Torvalds | a59b276 | 2007-04-16 16:03:15 -0700 | [diff] [blame] | 70 | { |
| 71 | int j; |
| 72 | |
| 73 | /* nothing to lookup */ |
| 74 | if (!n->size) |
| 75 | return NULL; |
| 76 | j = hash_obj(obj, n->size); |
| 77 | for (;;) { |
| 78 | struct object_decoration *ref = n->hash + j; |
| 79 | if (ref->base == obj) |
| 80 | return ref->decoration; |
| 81 | if (!ref->base) |
| 82 | return NULL; |
| 83 | if (++j == n->size) |
| 84 | j = 0; |
| 85 | } |
| 86 | } |