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;
/*
* 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;
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);
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);
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);
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,
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);
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--;
}
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)