1c588184b28658f9fdaee458019b9c81e5e8f252
   1#ifndef PACK_OBJECTS_H
   2#define PACK_OBJECTS_H
   3
   4#include "object-store.h"
   5
   6#define OE_DFS_STATE_BITS       2
   7#define OE_DEPTH_BITS           12
   8#define OE_IN_PACK_BITS         10
   9#define OE_Z_DELTA_BITS         20
  10/*
  11 * Note that oe_set_size() becomes expensive when the given size is
  12 * above this limit. Don't lower it too much.
  13 */
  14#define OE_SIZE_BITS            31
  15#define OE_DELTA_SIZE_BITS      20
  16
  17/*
  18 * State flags for depth-first search used for analyzing delta cycles.
  19 *
  20 * The depth is measured in delta-links to the base (so if A is a delta
  21 * against B, then A has a depth of 1, and B a depth of 0).
  22 */
  23enum dfs_state {
  24        DFS_NONE = 0,
  25        DFS_ACTIVE,
  26        DFS_DONE,
  27        DFS_NUM_STATES
  28};
  29
  30/*
  31 * basic object info
  32 * -----------------
  33 * idx.oid is filled up before delta searching starts. idx.crc32 is
  34 * only valid after the object is written out and will be used for
  35 * generating the index. idx.offset will be both gradually set and
  36 * used in writing phase (base objects get offset first, then deltas
  37 * refer to them)
  38 *
  39 * "size" is the uncompressed object size. Compressed size of the raw
  40 * data for an object in a pack is not stored anywhere but is computed
  41 * and made available when reverse .idx is made. Note that when a
  42 * delta is reused, "size" is the uncompressed _delta_ size, not the
  43 * canonical one after the delta has been applied.
  44 *
  45 * "hash" contains a path name hash which is used for sorting the
  46 * delta list and also during delta searching. Once prepare_pack()
  47 * returns it's no longer needed.
  48 *
  49 * source pack info
  50 * ----------------
  51 * The (in_pack, in_pack_offset) tuple contains the location of the
  52 * object in the source pack. in_pack_header_size allows quickly
  53 * skipping the header and going straight to the zlib stream.
  54 *
  55 * "type" and "in_pack_type" both describe object type. in_pack_type
  56 * may contain a delta type, while type is always the canonical type.
  57 *
  58 * deltas
  59 * ------
  60 * Delta links (delta, delta_child and delta_sibling) are created to
  61 * reflect that delta graph from the source pack then updated or added
  62 * during delta searching phase when we find better deltas.
  63 *
  64 * delta_child and delta_sibling are last needed in
  65 * compute_write_order(). "delta" and "delta_size" must remain valid
  66 * at object writing phase in case the delta is not cached.
  67 *
  68 * If a delta is cached in memory and is compressed, delta_data points
  69 * to the data and z_delta_size contains the compressed size. If it's
  70 * uncompressed [1], z_delta_size must be zero. delta_size is always
  71 * the uncompressed size and must be valid even if the delta is not
  72 * cached.
  73 *
  74 * [1] during try_delta phase we don't bother with compressing because
  75 * the delta could be quickly replaced with a better one.
  76 */
  77struct object_entry {
  78        struct pack_idx_entry idx;
  79        unsigned size_:OE_SIZE_BITS;
  80        unsigned size_valid:1;
  81        unsigned in_pack_idx:OE_IN_PACK_BITS;   /* already in pack */
  82        off_t in_pack_offset;
  83        uint32_t delta_idx;     /* delta base object */
  84        uint32_t delta_child_idx; /* deltified objects who bases me */
  85        uint32_t delta_sibling_idx; /* other deltified objects who
  86                                     * uses the same base as me
  87                                     */
  88        void *delta_data;       /* cached delta (uncompressed) */
  89        unsigned delta_size_:OE_DELTA_SIZE_BITS; /* delta data size (uncompressed) */
  90        unsigned delta_size_valid:1;
  91        unsigned z_delta_size:OE_Z_DELTA_BITS;
  92        unsigned type_:TYPE_BITS;
  93        unsigned in_pack_type:TYPE_BITS; /* could be delta */
  94        unsigned type_valid:1;
  95        uint32_t hash;                  /* name hint hash */
  96        unsigned char in_pack_header_size;
  97        unsigned preferred_base:1; /*
  98                                    * we do not pack this, but is available
  99                                    * to be used as the base object to delta
 100                                    * objects against.
 101                                    */
 102        unsigned no_try_delta:1;
 103        unsigned tagged:1; /* near the very tip of refs */
 104        unsigned filled:1; /* assigned write-order */
 105        unsigned dfs_state:OE_DFS_STATE_BITS;
 106        unsigned depth:OE_DEPTH_BITS;
 107};
 108
 109struct packing_data {
 110        struct object_entry *objects;
 111        uint32_t nr_objects, nr_alloc;
 112
 113        int32_t *index;
 114        uint32_t index_size;
 115
 116        unsigned int *in_pack_pos;
 117
 118        /*
 119         * Only one of these can be non-NULL and they have different
 120         * sizes. if in_pack_by_idx is allocated, oe_in_pack() returns
 121         * the pack of an object using in_pack_idx field. If not,
 122         * in_pack[] array is used the same way as in_pack_pos[]
 123         */
 124        struct packed_git **in_pack_by_idx;
 125        struct packed_git **in_pack;
 126
 127        uintmax_t oe_size_limit;
 128};
 129
 130void prepare_packing_data(struct packing_data *pdata);
 131struct object_entry *packlist_alloc(struct packing_data *pdata,
 132                                    const unsigned char *sha1,
 133                                    uint32_t index_pos);
 134
 135struct object_entry *packlist_find(struct packing_data *pdata,
 136                                   const unsigned char *sha1,
 137                                   uint32_t *index_pos);
 138
 139static inline uint32_t pack_name_hash(const char *name)
 140{
 141        uint32_t c, hash = 0;
 142
 143        if (!name)
 144                return 0;
 145
 146        /*
 147         * This effectively just creates a sortable number from the
 148         * last sixteen non-whitespace characters. Last characters
 149         * count "most", so things that end in ".c" sort together.
 150         */
 151        while ((c = *name++) != 0) {
 152                if (isspace(c))
 153                        continue;
 154                hash = (hash >> 2) + (c << 24);
 155        }
 156        return hash;
 157}
 158
 159static inline enum object_type oe_type(const struct object_entry *e)
 160{
 161        return e->type_valid ? e->type_ : OBJ_BAD;
 162}
 163
 164static inline void oe_set_type(struct object_entry *e,
 165                               enum object_type type)
 166{
 167        if (type >= OBJ_ANY)
 168                BUG("OBJ_ANY cannot be set in pack-objects code");
 169
 170        e->type_valid = type >= OBJ_NONE;
 171        e->type_ = (unsigned)type;
 172}
 173
 174static inline unsigned int oe_in_pack_pos(const struct packing_data *pack,
 175                                          const struct object_entry *e)
 176{
 177        return pack->in_pack_pos[e - pack->objects];
 178}
 179
 180static inline void oe_set_in_pack_pos(const struct packing_data *pack,
 181                                      const struct object_entry *e,
 182                                      unsigned int pos)
 183{
 184        pack->in_pack_pos[e - pack->objects] = pos;
 185}
 186
 187static inline struct packed_git *oe_in_pack(const struct packing_data *pack,
 188                                            const struct object_entry *e)
 189{
 190        if (pack->in_pack_by_idx)
 191                return pack->in_pack_by_idx[e->in_pack_idx];
 192        else
 193                return pack->in_pack[e - pack->objects];
 194}
 195
 196void oe_map_new_pack(struct packing_data *pack,
 197                     struct packed_git *p);
 198static inline void oe_set_in_pack(struct packing_data *pack,
 199                                  struct object_entry *e,
 200                                  struct packed_git *p)
 201{
 202        if (!p->index)
 203                oe_map_new_pack(pack, p);
 204        if (pack->in_pack_by_idx)
 205                e->in_pack_idx = p->index;
 206        else
 207                pack->in_pack[e - pack->objects] = p;
 208}
 209
 210static inline struct object_entry *oe_delta(
 211                const struct packing_data *pack,
 212                const struct object_entry *e)
 213{
 214        if (e->delta_idx)
 215                return &pack->objects[e->delta_idx - 1];
 216        return NULL;
 217}
 218
 219static inline void oe_set_delta(struct packing_data *pack,
 220                                struct object_entry *e,
 221                                struct object_entry *delta)
 222{
 223        if (delta)
 224                e->delta_idx = (delta - pack->objects) + 1;
 225        else
 226                e->delta_idx = 0;
 227}
 228
 229static inline struct object_entry *oe_delta_child(
 230                const struct packing_data *pack,
 231                const struct object_entry *e)
 232{
 233        if (e->delta_child_idx)
 234                return &pack->objects[e->delta_child_idx - 1];
 235        return NULL;
 236}
 237
 238static inline void oe_set_delta_child(struct packing_data *pack,
 239                                      struct object_entry *e,
 240                                      struct object_entry *delta)
 241{
 242        if (delta)
 243                e->delta_child_idx = (delta - pack->objects) + 1;
 244        else
 245                e->delta_child_idx = 0;
 246}
 247
 248static inline struct object_entry *oe_delta_sibling(
 249                const struct packing_data *pack,
 250                const struct object_entry *e)
 251{
 252        if (e->delta_sibling_idx)
 253                return &pack->objects[e->delta_sibling_idx - 1];
 254        return NULL;
 255}
 256
 257static inline void oe_set_delta_sibling(struct packing_data *pack,
 258                                        struct object_entry *e,
 259                                        struct object_entry *delta)
 260{
 261        if (delta)
 262                e->delta_sibling_idx = (delta - pack->objects) + 1;
 263        else
 264                e->delta_sibling_idx = 0;
 265}
 266
 267unsigned long oe_get_size_slow(struct packing_data *pack,
 268                               const struct object_entry *e);
 269static inline unsigned long oe_size(struct packing_data *pack,
 270                                    const struct object_entry *e)
 271{
 272        if (e->size_valid)
 273                return e->size_;
 274
 275        return oe_get_size_slow(pack, e);
 276}
 277
 278static inline int oe_size_less_than(struct packing_data *pack,
 279                                    const struct object_entry *lhs,
 280                                    unsigned long rhs)
 281{
 282        if (lhs->size_valid)
 283                return lhs->size_ < rhs;
 284        if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
 285                return 0;
 286        return oe_get_size_slow(pack, lhs) < rhs;
 287}
 288
 289static inline int oe_size_greater_than(struct packing_data *pack,
 290                                       const struct object_entry *lhs,
 291                                       unsigned long rhs)
 292{
 293        if (lhs->size_valid)
 294                return lhs->size_ > rhs;
 295        if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
 296                return 1;
 297        return oe_get_size_slow(pack, lhs) > rhs;
 298}
 299
 300static inline void oe_set_size(struct packing_data *pack,
 301                               struct object_entry *e,
 302                               unsigned long size)
 303{
 304        if (size < pack->oe_size_limit) {
 305                e->size_ = size;
 306                e->size_valid = 1;
 307        } else {
 308                e->size_valid = 0;
 309                if (oe_get_size_slow(pack, e) != size)
 310                        BUG("'size' is supposed to be the object size!");
 311        }
 312}
 313
 314static inline unsigned long oe_delta_size(struct packing_data *pack,
 315                                          const struct object_entry *e)
 316{
 317        if (e->delta_size_valid)
 318                return e->delta_size_;
 319        return oe_size(pack, e);
 320}
 321
 322static inline void oe_set_delta_size(struct packing_data *pack,
 323                                     struct object_entry *e,
 324                                     unsigned long size)
 325{
 326        e->delta_size_ = size;
 327        e->delta_size_valid = e->delta_size_ == size;
 328        if (!e->delta_size_valid && size != oe_size(pack, e))
 329                BUG("this can only happen in check_object() "
 330                    "where delta size is the same as entry size");
 331}
 332
 333#endif