@matheusbsbs / matheus-bagliano / commits / 136f2e548a
Fork 0

Make "struct tree" contain the pointer to the tree buffer

This allows us to avoid allocating information for names etc, because we can just use the information from the tree buffer directly. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 29, 2006 at 12:16 UTC 136f2e548a34f1f504b0f062f87ddf33e8d6e83b
6 files changed +36 -34
builtin-read-tree.c
+2 -2
@@ -39,7 +39,7 @@ static struct tree_entry_list df_conflict_list = {
39
40 typedef int (*merge_fn_t)(struct cache_entry **src);
41
42 -static int entcmp(char *name1, int dir1, char *name2, int dir2)
42 +static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
43 {
44 int len1 = strlen(name1);
45 int len2 = strlen(name2);
@@ -67,7 +67,7 @@ static int unpack_trees_rec(struct tree_entry_list **posns, int len,
67 int src_size = len + 1;
68 do {
69 int i;
70 - char *first;
70 + const char *first;
71 int firstdir = 0;
72 int pathlen;
73 unsigned ce_size;
builtin-rev-list.c
+2 -1
@@ -136,10 +136,11 @@ static struct object_list **process_tree(struct tree *tree,
136 p = process_tree(entry->item.tree, p, &me, entry->name);
137 else
138 p = process_blob(entry->item.blob, p, &me, entry->name);
139 - free(entry->name);
139 free(entry);
140 entry = next;
141 }
142 + free(tree->buffer);
143 + tree->buffer = NULL;
144 return p;
145 }
146
fsck-objects.c
+3 -4
@@ -198,17 +198,16 @@ static int fsck_tree(struct tree *item)
198 default:
199 break;
200 }
201 - free(last->name);
201 free(last);
202 }
203
204 last = entry;
205 }
207 - if (last) {
208 - free(last->name);
206 + if (last)
207 free(last);
210 - }
208 item->entries = NULL;
209 + free(item->buffer);
210 + item->buffer = NULL;
211
212 retval = 0;
213 if (has_full_path) {
object.c
+4 -1
@@ -200,8 +200,11 @@ struct object *parse_object(const unsigned char *sha1)
200 obj = &blob->object;
201 } else if (!strcmp(type, tree_type)) {
202 struct tree *tree = lookup_tree(sha1);
203 - parse_tree_buffer(tree, buffer, size);
203 obj = &tree->object;
204 + if (!tree->object.parsed) {
205 + parse_tree_buffer(tree, buffer, size);
206 + buffer = NULL;
207 + }
208 } else if (!strcmp(type, commit_type)) {
209 struct commit *commit = lookup_commit(sha1);
210 parse_commit_buffer(commit, buffer, size);
tree.c
+22 -25
@@ -3,6 +3,7 @@
3 #include "blob.h"
4 #include "commit.h"
5 #include "tag.h"
6 +#include "tree-walk.h"
7 #include <stdlib.h>
8
9 const char *tree_type = "tree";
@@ -145,46 +146,45 @@ struct tree *lookup_tree(const unsigned char *sha1)
146
147 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
148 {
148 - void *bufptr = buffer;
149 + struct tree_desc desc;
150 struct tree_entry_list **list_p;
151 int n_refs = 0;
152
153 if (item->object.parsed)
154 return 0;
155 item->object.parsed = 1;
156 + item->buffer = buffer;
157 + item->size = size;
158 +
159 + desc.buf = buffer;
160 + desc.size = size;
161 +
162 list_p = &item->entries;
156 - while (size) {
157 - struct object *obj;
163 + while (desc.size) {
164 + unsigned mode;
165 + const char *path;
166 + const unsigned char *sha1;
167 struct tree_entry_list *entry;
159 - int len = 1+strlen(bufptr);
160 - unsigned char *file_sha1 = bufptr + len;
161 - char *path = strchr(bufptr, ' ');
162 - unsigned int mode;
163 - if (size < len + 20 || !path ||
164 - sscanf(bufptr, "%o", &mode) != 1)
165 - return -1;
168 +
169 + sha1 = tree_entry_extract(&desc, &path, &mode);
170
171 entry = xmalloc(sizeof(struct tree_entry_list));
168 - entry->name = strdup(path + 1);
172 + entry->name = path;
173 + entry->mode = mode;
174 entry->directory = S_ISDIR(mode) != 0;
175 entry->executable = (mode & S_IXUSR) != 0;
176 entry->symlink = S_ISLNK(mode) != 0;
172 - entry->zeropad = *(char *)bufptr == '0';
173 - entry->mode = mode;
177 + entry->zeropad = *(const char *)(desc.buf) == '0';
178 entry->next = NULL;
179
176 - bufptr += len + 20;
177 - size -= len + 20;
180 + update_tree_entry(&desc);
181
182 if (entry->directory) {
180 - entry->item.tree = lookup_tree(file_sha1);
181 - obj = &entry->item.tree->object;
183 + entry->item.tree = lookup_tree(sha1);
184 } else {
183 - entry->item.blob = lookup_blob(file_sha1);
184 - obj = &entry->item.blob->object;
185 + entry->item.blob = lookup_blob(sha1);
186 }
186 - if (obj)
187 - n_refs++;
187 + n_refs++;
188 *list_p = entry;
189 list_p = &entry->next;
190 }
@@ -206,7 +206,6 @@ int parse_tree(struct tree *item)
206 char type[20];
207 void *buffer;
208 unsigned long size;
209 - int ret;
209
210 if (item->object.parsed)
211 return 0;
@@ -219,9 +218,7 @@ int parse_tree(struct tree *item)
218 return error("Object %s not a tree",
219 sha1_to_hex(item->object.sha1));
220 }
222 - ret = parse_tree_buffer(item, buffer, size);
223 - free(buffer);
224 - return ret;
221 + return parse_tree_buffer(item, buffer, size);
222 }
223
224 struct tree *parse_tree_indirect(const unsigned char *sha1)
tree.h
+3 -1
@@ -12,7 +12,7 @@ struct tree_entry_list {
12 unsigned symlink : 1;
13 unsigned zeropad : 1;
14 unsigned int mode;
15 - char *name;
15 + const char *name;
16 union {
17 struct object *any;
18 struct tree *tree;
@@ -22,6 +22,8 @@ struct tree_entry_list {
22
23 struct tree {
24 struct object object;
25 + void *buffer;
26 + unsigned long size;
27 struct tree_entry_list *entries;
28 };
29