1/*2* decorate.c - decorate a git object with some arbitrary3* data.4*/5#include "cache.h"6#include "object.h"7#include "decorate.h"89static unsigned int hash_obj(struct object *obj, unsigned int n)10{11unsigned int hash = *(unsigned int *)obj->sha1;12return hash % n;13}1415static void *insert_decoration(struct decoration *n, struct object *base, void *decoration)16{17int size = n->size;18struct object_decoration *hash = n->hash;19int j = hash_obj(base, size);2021while (hash[j].base) {22if (hash[j].base == base) {23void *old = hash[j].decoration;24hash[j].decoration = decoration;25return old;26}27j++;28if (++j >= size)29j = 0;30}31hash[j].base = base;32hash[j].decoration = decoration;33n->nr++;34return NULL;35}3637static void grow_decoration(struct decoration *n)38{39int i;40int old_size = n->size;41struct object_decoration *old_hash;4243old_size = n->size;44old_hash = n->hash;4546n->size = (old_size + 1000) * 3 / 2;47n->hash = xcalloc(n->size, sizeof(struct object_decoration));48n->nr = 0;4950for (i = 0; i < old_size; i++) {51struct object *base = old_hash[i].base;52void *decoration = old_hash[i].decoration;5354if (!base)55continue;56insert_decoration(n, base, decoration);57}58free(old_hash);59}6061/* Add a decoration pointer, return any old one */62void *add_decoration(struct decoration *n, struct object *obj, void *decoration)63{64int nr = n->nr + 1;6566if (nr > n->size * 2 / 3)67grow_decoration(n);68return insert_decoration(n, obj, decoration);69}7071/* Lookup a decoration pointer */72void *lookup_decoration(struct decoration *n, struct object *obj)73{74int j;7576/* nothing to lookup */77if (!n->size)78return NULL;79j = hash_obj(obj, n->size);80for (;;) {81struct object_decoration *ref = n->hash + j;82if (ref->base == obj)83return ref->decoration;84if (!ref->base)85return NULL;86if (++j == n->size)87j = 0;88}89}