localize window memory usage accounting
authorNicolas Pitre <nico@cam.org>
Thu, 6 Sep 2007 06:13:09 +0000 (02:13 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 6 Sep 2007 06:49:28 +0000 (23:49 -0700)
This is to help threadification of delta searching.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-pack-objects.c

index b1c64bec3ed0ae233241ea023208e9326d619350..b8495bf9247f69c58b1186b3965085b9532ed39e 100644 (file)
@@ -78,7 +78,6 @@ static unsigned long delta_cache_size = 0;
 static unsigned long max_delta_cache_size = 0;
 static unsigned long cache_max_small_delta_size = 1000;
 
-static unsigned long window_memory_usage = 0;
 static unsigned long window_memory_limit = 0;
 
 /*
@@ -1300,7 +1299,7 @@ static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
  * one.
  */
 static int try_delta(struct unpacked *trg, struct unpacked *src,
-                    unsigned max_depth)
+                    unsigned max_depth, unsigned long *mem_usage)
 {
        struct object_entry *trg_entry = trg->entry;
        struct object_entry *src_entry = src->entry;
@@ -1356,7 +1355,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
                if (sz != trg_size)
                        die("object %s inconsistent object length (%lu vs %lu)",
                            sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
-               window_memory_usage += sz;
+               *mem_usage += sz;
        }
        if (!src->data) {
                src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
@@ -1366,7 +1365,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
                if (sz != src_size)
                        die("object %s inconsistent object length (%lu vs %lu)",
                            sha1_to_hex(src_entry->idx.sha1), sz, src_size);
-               window_memory_usage += sz;
+               *mem_usage += sz;
        }
        if (!src->index) {
                src->index = create_delta_index(src->data, src_size);
@@ -1376,7 +1375,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
                                warning("suboptimal pack - out of memory");
                        return 0;
                }
-               window_memory_usage += sizeof_delta_index(src->index);
+               *mem_usage += sizeof_delta_index(src->index);
        }
 
        delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
@@ -1423,18 +1422,19 @@ static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
        return m;
 }
 
-static void free_unpacked(struct unpacked *n)
+static unsigned long free_unpacked(struct unpacked *n)
 {
-       window_memory_usage -= sizeof_delta_index(n->index);
+       unsigned long freed_mem = sizeof_delta_index(n->index);
        free_delta_index(n->index);
        n->index = NULL;
        if (n->data) {
+               freed_mem += n->entry->size;
                free(n->data);
                n->data = NULL;
-               window_memory_usage -= n->entry->size;
        }
        n->entry = NULL;
        n->depth = 0;
+       return freed_mem;
 }
 
 static void find_deltas(struct object_entry **list, unsigned list_size,
@@ -1443,7 +1443,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
        uint32_t i = list_size, idx = 0, count = 0, processed = 0;
        unsigned int array_size = window * sizeof(struct unpacked);
        struct unpacked *array;
-       int max_depth;
+       unsigned long mem_usage = 0;
 
        array = xmalloc(array_size);
        memset(array, 0, array_size);
@@ -1453,16 +1453,16 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
        do {
                struct object_entry *entry = list[--i];
                struct unpacked *n = array + idx;
-               int j, best_base = -1;
+               int j, max_depth, best_base = -1;
 
-               free_unpacked(n);
+               mem_usage -= free_unpacked(n);
                n->entry = entry;
 
                while (window_memory_limit &&
-                      window_memory_usage > window_memory_limit &&
+                      mem_usage > window_memory_limit &&
                       count > 1) {
                        uint32_t tail = (idx + window - count) % window;
-                       free_unpacked(array + tail);
+                       mem_usage -= free_unpacked(array + tail);
                        count--;
                }
 
@@ -1497,7 +1497,7 @@ static void find_deltas(struct object_entry **list, unsigned list_size,
                        m = array + other_idx;
                        if (!m->entry)
                                break;
-                       ret = try_delta(n, m, max_depth);
+                       ret = try_delta(n, m, max_depth, &mem_usage);
                        if (ret < 0)
                                break;
                        else if (ret > 0)