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