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