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}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++) {47struct 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, struct object *obj, void *decoration)59{60int nr = n->nr + 1;6162if (nr > n->size * 2 / 3)63grow_decoration(n);64return insert_decoration(n, obj, decoration);65}6667/* Lookup a decoration pointer */68void *lookup_decoration(struct decoration *n, struct object *obj)69{70int j;7172/* nothing to lookup */73if (!n->size)74return NULL;75j = hash_obj(obj, n->size);76for (;;) {77struct object_decoration *ref = n->hash + j;78if (ref->base == obj)79return ref->decoration;80if (!ref->base)81return NULL;82if (++j == n->size)83j = 0;84}85}