Refactor parts of in_delta_base_cache/cache_or_unpack_entry

The delta base cache lookup and test were shared. Refactor them; we'll need both parts again. Also, we'll use the clearing routine later. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Mar 27, 2013 at 21:03 UTC 84dd81c12622119e7f1d3eaf2047b910352b36bb
1 file changed +32 -13
sha1_file.c
+32 -13
@@ -1756,32 +1756,51 @@ static unsigned long pack_entry_hash(struct packed_git *p, off_t base_offset)
1756 return hash % MAX_DELTA_CACHE;
1757 }
1758
1759 -static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
1759 +static struct delta_base_cache_entry *
1760 +get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
1761 {
1762 unsigned long hash = pack_entry_hash(p, base_offset);
1762 - struct delta_base_cache_entry *ent = delta_base_cache + hash;
1763 + return delta_base_cache + hash;
1764 +}
1765 +
1766 +static int eq_delta_base_cache_entry(struct delta_base_cache_entry *ent,
1767 + struct packed_git *p, off_t base_offset)
1768 +{
1769 return (ent->data && ent->p == p && ent->base_offset == base_offset);
1770 }
1771
1772 +static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
1773 +{
1774 + struct delta_base_cache_entry *ent;
1775 + ent = get_delta_base_cache_entry(p, base_offset);
1776 + return eq_delta_base_cache_entry(ent, p, base_offset);
1777 +}
1778 +
1779 +static void clear_delta_base_cache_entry(struct delta_base_cache_entry *ent)
1780 +{
1781 + ent->data = NULL;
1782 + ent->lru.next->prev = ent->lru.prev;
1783 + ent->lru.prev->next = ent->lru.next;
1784 + delta_base_cached -= ent->size;
1785 +}
1786 +
1787 static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,
1788 unsigned long *base_size, enum object_type *type, int keep_cache)
1789 {
1790 + struct delta_base_cache_entry *ent;
1791 void *ret;
1770 - unsigned long hash = pack_entry_hash(p, base_offset);
1771 - struct delta_base_cache_entry *ent = delta_base_cache + hash;
1792
1773 - ret = ent->data;
1774 - if (!ret || ent->p != p || ent->base_offset != base_offset)
1793 + ent = get_delta_base_cache_entry(p, base_offset);
1794 +
1795 + if (!eq_delta_base_cache_entry(ent, p, base_offset))
1796 return unpack_entry(p, base_offset, type, base_size);
1797
1777 - if (!keep_cache) {
1778 - ent->data = NULL;
1779 - ent->lru.next->prev = ent->lru.prev;
1780 - ent->lru.prev->next = ent->lru.next;
1781 - delta_base_cached -= ent->size;
1782 - } else {
1798 + ret = ent->data;
1799 +
1800 + if (!keep_cache)
1801 + clear_delta_base_cache_entry(ent);
1802 + else
1803 ret = xmemdupz(ent->data, ent->size);
1784 - }
1804 *type = ent->type;
1805 *base_size = ent->size;
1806 return ret;