Cleaned up memory allocation for object_entry structs.
Although its easy to ask the user to tell us how many objects they will need, its probably better to dynamically grow the object table in large units. But if the user can give us a hint as to roughly how many objects then we can still use it during startup. Also stopped printing the SHA1 strings to stdout as no user is currently making use of that facility. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Aug 8, 2006 at 00:03 UTC
27d6d29035473f01ba5bb3b52c86ee4181d251fe
1 file changed
+46
-47
fast-import.c
+46
-47
@@ -6,59 +6,64 @@
6
#include "pack.h"
7
#include "csum-file.h"
8
9
+struct object_entry
10
+{
11
+ struct object_entry *next;
12
+ unsigned long offset;
13
+ unsigned char sha1[20];
14
+};
15
+
16
+struct object_entry_block
17
+{
18
+ struct object_entry_block *next_block;
19
+ struct object_entry *next_free;
20
+ struct object_entry *end;
21
+ struct object_entry entries[0];
22
+};
23
+
24
static int max_depth = 10;
25
+static unsigned long alloc_count;
26
static unsigned long object_count;
27
static unsigned long duplicate_count;
28
static unsigned long packoff;
13
-static unsigned long overflow_count;
29
static int packfd;
30
static int current_depth;
31
static void *lastdat;
32
static unsigned long lastdatlen;
33
static unsigned char lastsha1[20];
34
static unsigned char packsha1[20];
35
+struct object_entry *object_table[1 << 16];
36
+struct object_entry_block *blocks;
37
21
-struct object_entry
38
+static void alloc_objects(int cnt)
39
{
23
- struct object_entry *next;
24
- unsigned long offset;
25
- unsigned char sha1[20];
26
-};
40
+ struct object_entry_block *b;
41
+
42
+ b = xmalloc(sizeof(struct object_entry_block)
43
+ + cnt * sizeof(struct object_entry));
44
+ b->next_block = blocks;
45
+ b->next_free = b->entries;
46
+ b->end = b->entries + cnt;
47
+ blocks = b;
48
+ alloc_count += cnt;
49
+}
50
28
-struct overflow_object_entry
51
+static struct object_entry* new_object(unsigned char *sha1)
52
{
30
- struct overflow_object_entry *next;
31
- struct object_entry oe;
32
-};
53
+ struct object_entry *e;
54
34
-struct object_entry *pool_start;
35
-struct object_entry *pool_next;
36
-struct object_entry *pool_end;
37
-struct overflow_object_entry *overflow;
38
-struct object_entry *table[1 << 16];
55
+ if (blocks->next_free == blocks->end)
56
+ alloc_objects(1000);
57
40
-static struct object_entry* new_object(unsigned char *sha1)
41
-{
42
- if (pool_next != pool_end) {
43
- struct object_entry *e = pool_next++;
44
- memcpy(e->sha1, sha1, sizeof(e->sha1));
45
- return e;
46
- } else {
47
- struct overflow_object_entry *e;
48
-
49
- e = xmalloc(sizeof(struct overflow_object_entry));
50
- e->next = overflow;
51
- memcpy(e->oe.sha1, sha1, sizeof(e->oe.sha1));
52
- overflow = e;
53
- overflow_count++;
54
- return &e->oe;
55
- }
58
+ e = blocks->next_free++;
59
+ memcpy(e->sha1, sha1, sizeof(e->sha1));
60
+ return e;
61
}
62
63
static struct object_entry* insert_object(unsigned char *sha1)
64
{
65
unsigned int h = sha1[0] << 8 | sha1[1];
61
- struct object_entry *e = table[h];
66
+ struct object_entry *e = object_table[h];
67
struct object_entry *p = 0;
68
69
while (e) {
@@ -74,7 +79,7 @@ static struct object_entry* insert_object(unsigned char *sha1)
79
if (p)
80
p->next = e;
81
else
77
- table[h] = e;
82
+ object_table[h] = e;
83
return e;
84
}
85
@@ -246,17 +251,16 @@ static void write_index(const char *idx_name)
251
struct sha1file *f;
252
struct object_entry **idx, **c, **last;
253
struct object_entry *e;
249
- struct overflow_object_entry *o;
254
+ struct object_entry_block *o;
255
unsigned int array[256];
256
int i;
257
258
/* Build the sorted table of object IDs. */
259
idx = xmalloc(object_count * sizeof(struct object_entry*));
260
c = idx;
256
- for (e = pool_start; e != pool_next; e++)
257
- *c++ = e;
258
- for (o = overflow; o; o = o->next)
259
- *c++ = &o->oe;
261
+ for (o = blocks; o; o = o->next_block)
262
+ for (e = o->entries; e != o->next_free; e++)
263
+ *c++ = e;
264
last = idx + object_count;
265
qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
266
@@ -297,14 +301,11 @@ int main(int argc, const char **argv)
301
idx_name = xmalloc(strlen(base_name) + 5);
302
sprintf(idx_name, "%s.idx", base_name);
303
300
- packfd = open(pack_name, O_RDWR|O_CREAT|O_TRUNC, 0666);
304
+ packfd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
305
if (packfd < 0)
306
die("Can't create pack file %s: %s", pack_name, strerror(errno));
307
304
- pool_start = xmalloc(est_obj_cnt * sizeof(struct object_entry));
305
- pool_next = pool_start;
306
- pool_end = pool_start + est_obj_cnt;
307
-
308
+ alloc_objects(est_obj_cnt);
309
init_pack_header();
310
for (;;) {
311
unsigned long datlen;
@@ -334,8 +335,6 @@ int main(int argc, const char **argv)
335
e->offset = packoff;
336
write_blob(dat, datlen);
337
object_count++;
337
- printf("%s\n", sha1_to_hex(sha1));
338
- fflush(stdout);
338
339
if (lastdat)
340
free(lastdat);
@@ -351,8 +350,8 @@ int main(int argc, const char **argv)
350
close(packfd);
351
write_index(idx_name);
352
354
- fprintf(stderr, "%lu objects, %lu duplicates, %lu pool overflow\n",
355
- object_count, duplicate_count, overflow_count);
353
+ fprintf(stderr, "%lu objects, %lu duplicates, %lu allocated (%lu overflow)\n",
354
+ object_count, duplicate_count, alloc_count, alloc_count - est_obj_cnt);
355
356
return 0;
357
}