Skip to content

Commit ddd3e31

Browse files
jonathantanmygitster
authored andcommitted
decorate: clean up and document API
Improve the names of the identifiers in decorate.h, document them, and add an example of how to use these functions. The example is compiled and run as part of the test suite. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95ec6b1 commit ddd3e31

File tree

8 files changed

+146
-25
lines changed

8 files changed

+146
-25
lines changed

Documentation/technical/api-decorate.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ TEST_PROGRAMS_NEED_X += test-dump-cache-tree
651651
TEST_PROGRAMS_NEED_X += test-dump-fsmonitor
652652
TEST_PROGRAMS_NEED_X += test-dump-split-index
653653
TEST_PROGRAMS_NEED_X += test-dump-untracked-cache
654+
TEST_PROGRAMS_NEED_X += test-example-decorate
654655
TEST_PROGRAMS_NEED_X += test-fake-ssh
655656
TEST_PROGRAMS_NEED_X += test-genrandom
656657
TEST_PROGRAMS_NEED_X += test-hashmap

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ static void export_marks(char *file)
895895
{
896896
unsigned int i;
897897
uint32_t mark;
898-
struct object_decoration *deco = idnums.hash;
898+
struct decoration_entry *deco = idnums.entries;
899899
FILE *f;
900900
int e = 0;
901901

decorate.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ static unsigned int hash_obj(const struct object *obj, unsigned int n)
1414
static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
1515
{
1616
int size = n->size;
17-
struct object_decoration *hash = n->hash;
17+
struct decoration_entry *entries = n->entries;
1818
unsigned int j = hash_obj(base, size);
1919

20-
while (hash[j].base) {
21-
if (hash[j].base == base) {
22-
void *old = hash[j].decoration;
23-
hash[j].decoration = decoration;
20+
while (entries[j].base) {
21+
if (entries[j].base == base) {
22+
void *old = entries[j].decoration;
23+
entries[j].decoration = decoration;
2424
return old;
2525
}
2626
if (++j >= size)
2727
j = 0;
2828
}
29-
hash[j].base = base;
30-
hash[j].decoration = decoration;
29+
entries[j].base = base;
30+
entries[j].decoration = decoration;
3131
n->nr++;
3232
return NULL;
3333
}
@@ -36,24 +36,23 @@ static void grow_decoration(struct decoration *n)
3636
{
3737
int i;
3838
int old_size = n->size;
39-
struct object_decoration *old_hash = n->hash;
39+
struct decoration_entry *old_entries = n->entries;
4040

4141
n->size = (old_size + 1000) * 3 / 2;
42-
n->hash = xcalloc(n->size, sizeof(struct object_decoration));
42+
n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
4343
n->nr = 0;
4444

4545
for (i = 0; i < old_size; i++) {
46-
const struct object *base = old_hash[i].base;
47-
void *decoration = old_hash[i].decoration;
46+
const struct object *base = old_entries[i].base;
47+
void *decoration = old_entries[i].decoration;
4848

4949
if (!decoration)
5050
continue;
5151
insert_decoration(n, base, decoration);
5252
}
53-
free(old_hash);
53+
free(old_entries);
5454
}
5555

56-
/* Add a decoration pointer, return any old one */
5756
void *add_decoration(struct decoration *n, const struct object *obj,
5857
void *decoration)
5958
{
@@ -64,7 +63,6 @@ void *add_decoration(struct decoration *n, const struct object *obj,
6463
return insert_decoration(n, obj, decoration);
6564
}
6665

67-
/* Lookup a decoration pointer */
6866
void *lookup_decoration(struct decoration *n, const struct object *obj)
6967
{
7068
unsigned int j;
@@ -74,7 +72,7 @@ void *lookup_decoration(struct decoration *n, const struct object *obj)
7472
return NULL;
7573
j = hash_obj(obj, n->size);
7674
for (;;) {
77-
struct object_decoration *ref = n->hash + j;
75+
struct decoration_entry *ref = n->entries + j;
7876
if (ref->base == obj)
7977
return ref->decoration;
8078
if (!ref->base)

decorate.h

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,61 @@
11
#ifndef DECORATE_H
22
#define DECORATE_H
33

4-
struct object_decoration {
4+
/*
5+
* A data structure that associates Git objects to void pointers. See
6+
* t/helper/test-example-decorate.c for a demonstration of how to use these
7+
* functions.
8+
*/
9+
10+
/*
11+
* An entry in the data structure.
12+
*/
13+
struct decoration_entry {
514
const struct object *base;
615
void *decoration;
716
};
817

18+
/*
19+
* The data structure.
20+
*
21+
* This data structure must be zero-initialized.
22+
*/
923
struct decoration {
24+
/*
25+
* Not used by the decoration mechanism. Clients may use this for
26+
* whatever they want.
27+
*/
1028
const char *name;
11-
unsigned int size, nr;
12-
struct object_decoration *hash;
29+
30+
/*
31+
* The capacity of "entries".
32+
*/
33+
unsigned int size;
34+
35+
/*
36+
* The number of real Git objects (that is, entries with non-NULL
37+
* "base").
38+
*/
39+
unsigned int nr;
40+
41+
/*
42+
* The entries. This is an array of size "size", containing nr entries
43+
* with non-NULL "base" and (size - nr) entries with NULL "base".
44+
*/
45+
struct decoration_entry *entries;
1346
};
1447

48+
/*
49+
* Add an association from the given object to the given pointer (which may be
50+
* NULL), returning the previously associated pointer. If there is no previous
51+
* association, this function returns NULL.
52+
*/
1553
extern void *add_decoration(struct decoration *n, const struct object *obj, void *decoration);
54+
55+
/*
56+
* Return the pointer associated to the given object. If there is no
57+
* association, this function returns NULL.
58+
*/
1659
extern void *lookup_decoration(struct decoration *n, const struct object *obj);
1760

1861
#endif

t/helper/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/test-dump-fsmonitor
99
/test-dump-split-index
1010
/test-dump-untracked-cache
11+
/test-example-decorate
1112
/test-fake-ssh
1213
/test-scrap-cache-tree
1314
/test-genrandom

t/helper/test-example-decorate.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "cache.h"
2+
#include "object.h"
3+
#include "decorate.h"
4+
5+
int cmd_main(int argc, const char **argv)
6+
{
7+
struct decoration n;
8+
struct object_id one_oid = { {1} };
9+
struct object_id two_oid = { {2} };
10+
struct object_id three_oid = { {3} };
11+
struct object *one, *two, *three;
12+
13+
int decoration_a, decoration_b;
14+
15+
void *ret;
16+
17+
int i, objects_noticed = 0;
18+
19+
/*
20+
* The struct must be zero-initialized.
21+
*/
22+
memset(&n, 0, sizeof(n));
23+
24+
/*
25+
* Add 2 objects, one with a non-NULL decoration and one with a NULL
26+
* decoration.
27+
*/
28+
one = lookup_unknown_object(one_oid.hash);
29+
two = lookup_unknown_object(two_oid.hash);
30+
ret = add_decoration(&n, one, &decoration_a);
31+
if (ret)
32+
die("BUG: when adding a brand-new object, NULL should be returned");
33+
ret = add_decoration(&n, two, NULL);
34+
if (ret)
35+
die("BUG: when adding a brand-new object, NULL should be returned");
36+
37+
/*
38+
* When re-adding an already existing object, the old decoration is
39+
* returned.
40+
*/
41+
ret = add_decoration(&n, one, NULL);
42+
if (ret != &decoration_a)
43+
die("BUG: when readding an already existing object, existing decoration should be returned");
44+
ret = add_decoration(&n, two, &decoration_b);
45+
if (ret)
46+
die("BUG: when readding an already existing object, existing decoration should be returned");
47+
48+
/*
49+
* Lookup returns the added declarations, or NULL if the object was
50+
* never added.
51+
*/
52+
ret = lookup_decoration(&n, one);
53+
if (ret)
54+
die("BUG: lookup should return added declaration");
55+
ret = lookup_decoration(&n, two);
56+
if (ret != &decoration_b)
57+
die("BUG: lookup should return added declaration");
58+
three = lookup_unknown_object(three_oid.hash);
59+
ret = lookup_decoration(&n, three);
60+
if (ret)
61+
die("BUG: lookup for unknown object should return NULL");
62+
63+
/*
64+
* The user can also loop through all entries.
65+
*/
66+
for (i = 0; i < n.size; i++) {
67+
if (n.entries[i].base)
68+
objects_noticed++;
69+
}
70+
if (objects_noticed != 2)
71+
die("BUG: should have 2 objects");
72+
73+
return 0;
74+
}

t/t9004-example.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
test_description='check that example code compiles and runs'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'decorate' '
7+
test-example-decorate
8+
'
9+
10+
test_done

0 commit comments

Comments
 (0)