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(const 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, const 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}27if (++j >= size)28j = 0;29}30hash[j].base = base;31hash[j].decoration = decoration;32n->nr++;33return NULL;34}3536static void grow_decoration(struct decoration *n)37{38int i;39int old_size = n->size;40struct object_decoration *old_hash = n->hash;4142n->size = (old_size + 1000) * 3 / 2;43n->hash = xcalloc(n->size, sizeof(struct object_decoration));44n->nr = 0;4546for (i = 0; i < old_size; i++) {47const struct object *base = old_hash[i].base;48void *decoration = old_hash[i].decoration;4950if (!base)51continue;52insert_decoration(n, base, decoration);53}54free(old_hash);55}5657/* Add a decoration pointer, return any old one */58void *add_decoration(struct decoration *n, const struct object *obj,59void *decoration)60{61int nr = n->nr + 1;6263if (nr > n->size * 2 / 3)64grow_decoration(n);65return insert_decoration(n, obj, decoration);66}6768/* Lookup a decoration pointer */69void *lookup_decoration(struct decoration *n, const struct object *obj)70{71int j;7273/* nothing to lookup */74if (!n->size)75return NULL;76j = hash_obj(obj, n->size);77for (;;) {78struct object_decoration *ref = n->hash + j;79if (ref->base == obj)80return ref->decoration;81if (!ref->base)82return NULL;83if (++j == n->size)84j = 0;85}86}