blob: a5c43c0c1461761bb808b4d6b833222b181843a6 [file] [log] [blame]
Linus Torvaldsa59b2762007-04-16 16:03:15 -07001/*
2 * decorate.c - decorate a git object with some arbitrary
3 * data.
4 */
Elijah Newrenfc7bd512023-02-24 00:09:34 +00005#include "git-compat-util.h"
Linus Torvaldsa59b2762007-04-16 16:03:15 -07006#include "object.h"
7#include "decorate.h"
8
Jeff King54988bd2008-08-20 13:55:33 -04009static unsigned int hash_obj(const struct object *obj, unsigned int n)
Linus Torvaldsa59b2762007-04-16 16:03:15 -070010{
Jeff Kingd40abc82019-06-20 03:41:49 -040011 return oidhash(&obj->oid) % n;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070012}
13
Jeff King54988bd2008-08-20 13:55:33 -040014static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
Linus Torvaldsa59b2762007-04-16 16:03:15 -070015{
16 int size = n->size;
Jonathan Tanddd3e312017-12-07 16:14:24 -080017 struct decoration_entry *entries = n->entries;
Dan McGee91fe2f92009-05-18 23:34:02 -050018 unsigned int j = hash_obj(base, size);
Linus Torvaldsa59b2762007-04-16 16:03:15 -070019
Jonathan Tanddd3e312017-12-07 16:14:24 -080020 while (entries[j].base) {
21 if (entries[j].base == base) {
22 void *old = entries[j].decoration;
23 entries[j].decoration = decoration;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070024 return old;
25 }
Linus Torvaldsa59b2762007-04-16 16:03:15 -070026 if (++j >= size)
27 j = 0;
28 }
Jonathan Tanddd3e312017-12-07 16:14:24 -080029 entries[j].base = base;
30 entries[j].decoration = decoration;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070031 n->nr++;
32 return NULL;
33}
34
35static void grow_decoration(struct decoration *n)
36{
37 int i;
38 int old_size = n->size;
Jonathan Tanddd3e312017-12-07 16:14:24 -080039 struct decoration_entry *old_entries = n->entries;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070040
41 n->size = (old_size + 1000) * 3 / 2;
René Scharfeca56dad2021-03-13 17:17:22 +010042 CALLOC_ARRAY(n->entries, n->size);
Linus Torvaldsa59b2762007-04-16 16:03:15 -070043 n->nr = 0;
44
45 for (i = 0; i < old_size; i++) {
Jonathan Tanddd3e312017-12-07 16:14:24 -080046 const struct object *base = old_entries[i].base;
47 void *decoration = old_entries[i].decoration;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070048
Kevin Bracey83f04122013-05-16 18:32:27 +030049 if (!decoration)
Linus Torvaldsa59b2762007-04-16 16:03:15 -070050 continue;
51 insert_decoration(n, base, decoration);
52 }
Jonathan Tanddd3e312017-12-07 16:14:24 -080053 free(old_entries);
Linus Torvaldsa59b2762007-04-16 16:03:15 -070054}
55
Jeff King54988bd2008-08-20 13:55:33 -040056void *add_decoration(struct decoration *n, const struct object *obj,
57 void *decoration)
Linus Torvaldsa59b2762007-04-16 16:03:15 -070058{
59 int nr = n->nr + 1;
60
61 if (nr > n->size * 2 / 3)
62 grow_decoration(n);
63 return insert_decoration(n, obj, decoration);
64}
65
Jeff King54988bd2008-08-20 13:55:33 -040066void *lookup_decoration(struct decoration *n, const struct object *obj)
Linus Torvaldsa59b2762007-04-16 16:03:15 -070067{
Dan McGee91fe2f92009-05-18 23:34:02 -050068 unsigned int j;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070069
70 /* nothing to lookup */
71 if (!n->size)
72 return NULL;
73 j = hash_obj(obj, n->size);
74 for (;;) {
Jonathan Tanddd3e312017-12-07 16:14:24 -080075 struct decoration_entry *ref = n->entries + j;
Linus Torvaldsa59b2762007-04-16 16:03:15 -070076 if (ref->base == obj)
77 return ref->decoration;
78 if (!ref->base)
79 return NULL;
80 if (++j == n->size)
81 j = 0;
82 }
83}