Use proper object allocators for unknown object nodes too
[git.git] / object.c
blob153ebac66d86fb49df449edc4d92c9db4428f44a
1 #include "cache.h"
2 #include "object.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "tag.h"
8 static struct object **obj_hash;
9 static int nr_objs, obj_hash_size;
11 unsigned int get_max_object_index(void)
13 return obj_hash_size;
16 struct object *get_indexed_object(unsigned int idx)
18 return obj_hash[idx];
21 static const char *object_type_strings[] = {
22 NULL, /* OBJ_NONE = 0 */
23 "commit", /* OBJ_COMMIT = 1 */
24 "tree", /* OBJ_TREE = 2 */
25 "blob", /* OBJ_BLOB = 3 */
26 "tag", /* OBJ_TAG = 4 */
29 const char *typename(unsigned int type)
31 if (type >= ARRAY_SIZE(object_type_strings))
32 return NULL;
33 return object_type_strings[type];
36 int type_from_string(const char *str)
38 int i;
40 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
41 if (!strcmp(str, object_type_strings[i]))
42 return i;
43 die("invalid object type \"%s\"", str);
46 static unsigned int hash_obj(struct object *obj, unsigned int n)
48 unsigned int hash = *(unsigned int *)obj->sha1;
49 return hash % n;
52 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54 int j = hash_obj(obj, size);
56 while (hash[j]) {
57 j++;
58 if (j >= size)
59 j = 0;
61 hash[j] = obj;
64 static int hashtable_index(const unsigned char *sha1)
66 unsigned int i;
67 memcpy(&i, sha1, sizeof(unsigned int));
68 return (int)(i % obj_hash_size);
71 struct object *lookup_object(const unsigned char *sha1)
73 int i;
74 struct object *obj;
76 if (!obj_hash)
77 return NULL;
79 i = hashtable_index(sha1);
80 while ((obj = obj_hash[i]) != NULL) {
81 if (!hashcmp(sha1, obj->sha1))
82 break;
83 i++;
84 if (i == obj_hash_size)
85 i = 0;
87 return obj;
90 static void grow_object_hash(void)
92 int i;
93 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
94 struct object **new_hash;
96 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
97 for (i = 0; i < obj_hash_size; i++) {
98 struct object *obj = obj_hash[i];
99 if (!obj)
100 continue;
101 insert_obj_hash(obj, new_hash, new_hash_size);
103 free(obj_hash);
104 obj_hash = new_hash;
105 obj_hash_size = new_hash_size;
108 void created_object(const unsigned char *sha1, struct object *obj)
110 obj->parsed = 0;
111 obj->used = 0;
112 obj->type = OBJ_NONE;
113 obj->flags = 0;
114 hashcpy(obj->sha1, sha1);
116 if (obj_hash_size - 1 <= nr_objs * 2)
117 grow_object_hash();
119 insert_obj_hash(obj, obj_hash, obj_hash_size);
120 nr_objs++;
123 struct object *lookup_unknown_object(const unsigned char *sha1)
125 struct object *obj = lookup_object(sha1);
126 if (!obj) {
127 obj = alloc_object_node();
128 created_object(sha1, obj);
129 obj->type = OBJ_NONE;
131 return obj;
134 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
136 struct object *obj;
137 int eaten = 0;
139 if (type == OBJ_BLOB) {
140 struct blob *blob = lookup_blob(sha1);
141 parse_blob_buffer(blob, buffer, size);
142 obj = &blob->object;
143 } else if (type == OBJ_TREE) {
144 struct tree *tree = lookup_tree(sha1);
145 obj = &tree->object;
146 if (!tree->object.parsed) {
147 parse_tree_buffer(tree, buffer, size);
148 eaten = 1;
150 } else if (type == OBJ_COMMIT) {
151 struct commit *commit = lookup_commit(sha1);
152 parse_commit_buffer(commit, buffer, size);
153 if (!commit->buffer) {
154 commit->buffer = buffer;
155 eaten = 1;
157 obj = &commit->object;
158 } else if (type == OBJ_TAG) {
159 struct tag *tag = lookup_tag(sha1);
160 parse_tag_buffer(tag, buffer, size);
161 obj = &tag->object;
162 } else {
163 obj = NULL;
165 *eaten_p = eaten;
166 return obj;
169 struct object *parse_object(const unsigned char *sha1)
171 unsigned long size;
172 enum object_type type;
173 int eaten;
174 void *buffer = read_sha1_file(sha1, &type, &size);
176 if (buffer) {
177 struct object *obj;
178 if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) {
179 error("sha1 mismatch %s\n", sha1_to_hex(sha1));
180 return NULL;
183 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
184 if (!eaten)
185 free(buffer);
186 return obj;
188 return NULL;
191 struct object_list *object_list_insert(struct object *item,
192 struct object_list **list_p)
194 struct object_list *new_list = xmalloc(sizeof(struct object_list));
195 new_list->item = item;
196 new_list->next = *list_p;
197 *list_p = new_list;
198 return new_list;
201 void object_list_append(struct object *item,
202 struct object_list **list_p)
204 while (*list_p) {
205 list_p = &((*list_p)->next);
207 *list_p = xmalloc(sizeof(struct object_list));
208 (*list_p)->next = NULL;
209 (*list_p)->item = item;
212 unsigned object_list_length(struct object_list *list)
214 unsigned ret = 0;
215 while (list) {
216 list = list->next;
217 ret++;
219 return ret;
222 int object_list_contains(struct object_list *list, struct object *obj)
224 while (list) {
225 if (list->item == obj)
226 return 1;
227 list = list->next;
229 return 0;
232 void add_object_array(struct object *obj, const char *name, struct object_array *array)
234 unsigned nr = array->nr;
235 unsigned alloc = array->alloc;
236 struct object_array_entry *objects = array->objects;
238 if (nr >= alloc) {
239 alloc = (alloc + 32) * 2;
240 objects = xrealloc(objects, alloc * sizeof(*objects));
241 array->alloc = alloc;
242 array->objects = objects;
244 objects[nr].item = obj;
245 objects[nr].name = name;
246 array->nr = ++nr;