summaryrefslogtreecommitdiff
path: root/object.c
blob: 91bbc6e5e2eadfb0a66b14d992eac260d07267f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "object.h"
#include "cache.h"
#include <stdlib.h>
#include <string.h>
 
struct object **objs;
int nr_objs;
static int obj_allocs;
 
static int find_object(unsigned char *sha1)
{
	int first = 0, last = nr_objs;
 
        while (first < last) {
                int next = (first + last) / 2;
                struct object *obj = objs[next];
                int cmp;
 
                cmp = memcmp(sha1, obj->sha1, 20);
                if (!cmp)
                        return next;
                if (cmp < 0) {
                        last = next;
                        continue;
                }
                first = next+1;
        }
        return -first-1;
}
 
struct object *lookup_object(unsigned char *sha1)
{
	int pos = find_object(sha1);
	if (pos >= 0)
		return objs[pos];
	return NULL;
}
 
void created_object(unsigned char *sha1, struct object *obj)
{
	int pos = find_object(sha1);
 
	obj->parsed = 0;
	memcpy(obj->sha1, sha1, 20);
	obj->type = NULL;
	obj->refs = NULL;
	obj->used = 0;
 
	if (pos >= 0)
		die("Inserting %s twice\n", sha1_to_hex(sha1));
	pos = -pos-1;
 
	if (obj_allocs == nr_objs) {
		obj_allocs = alloc_nr(obj_allocs);
		objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
	}
 
	/* Insert it into the right place */
	memmove(objs + pos + 1, objs + pos, (nr_objs - pos) * 
		sizeof(struct object *));
 
	objs[pos] = obj;
	nr_objs++;
}
 
void add_ref(struct object *refer, struct object *target)
{
	struct object_list **pp = &refer->refs;
	struct object_list *p;
 
	while ((p = *pp) != NULL) {
		if (p->item == target)
			return;
		pp = &p->next;
	}
 
	target->used = 1;
	p = xmalloc(sizeof(*p));
	p->item = target;
	p->next = NULL;
	*pp = p;
}
 
void mark_reachable(struct object *obj, unsigned int mask)
{
	struct object_list *p = obj->refs;
 
	/* If we've been here already, don't bother */
	if (obj->flags & mask)
		return;
	obj->flags |= mask;
	while (p) {
		mark_reachable(p->item, mask);
		p = p->next;
	}
}