f8a751837e3e67fef57e3de9670e5a2defc6652a
   1#include "cache.h"
   2#include "diff.h"
   3#include "diffcore.h"
   4
   5/*
   6 * Idea here is very simple.
   7 *
   8 * We have total of (sz-N+1) N-byte overlapping sequences in buf whose
   9 * size is sz.  If the same N-byte sequence appears in both source and
  10 * destination, we say the byte that starts that sequence is shared
  11 * between them (i.e. copied from source to destination).
  12 *
  13 * For each possible N-byte sequence, if the source buffer has more
  14 * instances of it than the destination buffer, that means the
  15 * difference are the number of bytes not copied from source to
  16 * destination.  If the counts are the same, everything was copied
  17 * from source to destination.  If the destination has more,
  18 * everything was copied, and destination added more.
  19 *
  20 * We are doing an approximation so we do not really have to waste
  21 * memory by actually storing the sequence.  We just hash them into
  22 * somewhere around 2^16 hashbuckets and count the occurrences.
  23 *
  24 * The length of the sequence is arbitrarily set to 8 for now.
  25 */
  26
  27/* Wild guess at the initial hash size */
  28#define INITIAL_HASH_SIZE 9
  29#define HASHBASE 65537 /* next_prime(2^16) */
  30/* We leave more room in smaller hash but do not let it
  31 * grow to have unused hole too much.
  32 */
  33#define INITIAL_FREE(sz_log2) ((1<<(sz_log2))*(sz_log2-3)/(sz_log2))
  34
  35struct spanhash {
  36        unsigned long hashval;
  37        unsigned long cnt;
  38};
  39struct spanhash_top {
  40        int alloc_log2;
  41        int free;
  42        struct spanhash data[FLEX_ARRAY];
  43};
  44
  45static struct spanhash *spanhash_find(struct spanhash_top *top,
  46                                      unsigned long hashval)
  47{
  48        int sz = 1 << top->alloc_log2;
  49        int bucket = hashval & (sz - 1);
  50        while (1) {
  51                struct spanhash *h = &(top->data[bucket++]);
  52                if (!h->cnt)
  53                        return NULL;
  54                if (h->hashval == hashval)
  55                        return h;
  56                if (sz <= bucket)
  57                        bucket = 0;
  58        }
  59}
  60
  61static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
  62{
  63        struct spanhash_top *new;
  64        int i;
  65        int osz = 1 << orig->alloc_log2;
  66        int sz = osz << 1;
  67
  68        new = xmalloc(sizeof(*orig) + sizeof(struct spanhash) * sz);
  69        new->alloc_log2 = orig->alloc_log2 + 1;
  70        new->free = INITIAL_FREE(new->alloc_log2);
  71        memset(new->data, 0, sizeof(struct spanhash) * sz);
  72        for (i = 0; i < osz; i++) {
  73                struct spanhash *o = &(orig->data[i]);
  74                int bucket;
  75                if (!o->cnt)
  76                        continue;
  77                bucket = o->hashval & (sz - 1);
  78                while (1) {
  79                        struct spanhash *h = &(new->data[bucket++]);
  80                        if (!h->cnt) {
  81                                h->hashval = o->hashval;
  82                                h->cnt = o->cnt;
  83                                new->free--;
  84                                break;
  85                        }
  86                        if (sz <= bucket)
  87                                bucket = 0;
  88                }
  89        }
  90        free(orig);
  91        return new;
  92}
  93
  94static struct spanhash_top *add_spanhash(struct spanhash_top *top,
  95                                         unsigned long hashval)
  96{
  97        int bucket, lim;
  98        struct spanhash *h;
  99
 100        lim = (1 << top->alloc_log2);
 101        bucket = hashval & (lim - 1);
 102        while (1) {
 103                h = &(top->data[bucket++]);
 104                if (!h->cnt) {
 105                        h->hashval = hashval;
 106                        h->cnt = 1;
 107                        top->free--;
 108                        if (top->free < 0)
 109                                return spanhash_rehash(top);
 110                        return top;
 111                }
 112                if (h->hashval == hashval) {
 113                        h->cnt++;
 114                        return top;
 115                }
 116                if (lim <= bucket)
 117                        bucket = 0;
 118        }
 119}
 120
 121static struct spanhash_top *hash_chars(unsigned char *buf, unsigned long sz)
 122{
 123        int i;
 124        unsigned long accum1, accum2, hashval;
 125        struct spanhash_top *hash;
 126
 127        i = INITIAL_HASH_SIZE;
 128        hash = xmalloc(sizeof(*hash) + sizeof(struct spanhash) * (1<<i));
 129        hash->alloc_log2 = i;
 130        hash->free = INITIAL_FREE(i);
 131        memset(hash->data, 0, sizeof(struct spanhash) * (1<<i));
 132
 133        /* an 8-byte shift register made of accum1 and accum2.  New
 134         * bytes come at LSB of accum2, and shifted up to accum1
 135         */
 136        for (i = accum1 = accum2 = 0; i < 7; i++, sz--) {
 137                accum1 = (accum1 << 8) | (accum2 >> 24);
 138                accum2 = (accum2 << 8) | *buf++;
 139        }
 140        while (sz) {
 141                accum1 = (accum1 << 8) | (accum2 >> 24);
 142                accum2 = (accum2 << 8) | *buf++;
 143                hashval = (accum1 + accum2 * 0x61) % HASHBASE;
 144                hash = add_spanhash(hash, hashval);
 145                sz--;
 146        }
 147        return hash;
 148}
 149
 150int diffcore_count_changes(void *src, unsigned long src_size,
 151                           void *dst, unsigned long dst_size,
 152                           void **src_count_p,
 153                           void **dst_count_p,
 154                           unsigned long delta_limit,
 155                           unsigned long *src_copied,
 156                           unsigned long *literal_added)
 157{
 158        int i, ssz;
 159        struct spanhash_top *src_count, *dst_count;
 160        unsigned long sc, la;
 161
 162        if (src_size < 8 || dst_size < 8)
 163                return -1;
 164
 165        src_count = dst_count = NULL;
 166        if (src_count_p)
 167                src_count = *src_count_p;
 168        if (!src_count) {
 169                src_count = hash_chars(src, src_size);
 170                if (src_count_p)
 171                        *src_count_p = src_count;
 172        }
 173        if (dst_count_p)
 174                dst_count = *dst_count_p;
 175        if (!dst_count) {
 176                dst_count = hash_chars(dst, dst_size);
 177                if (dst_count_p)
 178                        *dst_count_p = dst_count;
 179        }
 180        sc = la = 0;
 181
 182        ssz = 1 << src_count->alloc_log2;
 183        for (i = 0; i < ssz; i++) {
 184                struct spanhash *s = &(src_count->data[i]);
 185                struct spanhash *d;
 186                unsigned dst_cnt, src_cnt;
 187                if (!s->cnt)
 188                        continue;
 189                src_cnt = s->cnt;
 190                d = spanhash_find(dst_count, s->hashval);
 191                dst_cnt = d ? d->cnt : 0;
 192                if (src_cnt < dst_cnt) {
 193                        la += dst_cnt - src_cnt;
 194                        sc += src_cnt;
 195                }
 196                else
 197                        sc += dst_cnt;
 198        }
 199
 200        if (!src_count_p)
 201                free(src_count);
 202        if (!dst_count_p)
 203                free(dst_count);
 204        *src_copied = sc;
 205        *literal_added = la;
 206        return 0;
 207}