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 { 78struct pack_idx_entry idx; 79unsigned size_:OE_SIZE_BITS; 80unsigned size_valid:1; 81unsigned in_pack_idx:OE_IN_PACK_BITS;/* already in pack */ 82 off_t in_pack_offset; 83uint32_t delta_idx;/* delta base object */ 84uint32_t delta_child_idx;/* deltified objects who bases me */ 85uint32_t delta_sibling_idx;/* other deltified objects who 86 * uses the same base as me 87 */ 88void*delta_data;/* cached delta (uncompressed) */ 89unsigned delta_size_:OE_DELTA_SIZE_BITS;/* delta data size (uncompressed) */ 90unsigned delta_size_valid:1; 91unsigned z_delta_size:OE_Z_DELTA_BITS; 92unsigned type_:TYPE_BITS; 93unsigned in_pack_type:TYPE_BITS;/* could be delta */ 94unsigned type_valid:1; 95uint32_t hash;/* name hint hash */ 96unsigned char in_pack_header_size; 97unsigned 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 */ 102unsigned no_try_delta:1; 103unsigned tagged:1;/* near the very tip of refs */ 104unsigned filled:1;/* assigned write-order */ 105unsigned dfs_state:OE_DFS_STATE_BITS; 106unsigned depth:OE_DEPTH_BITS; 107}; 108 109struct packing_data { 110struct object_entry *objects; 111uint32_t nr_objects, nr_alloc; 112 113int32_t*index; 114uint32_t index_size; 115 116unsigned 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 */ 124struct packed_git **in_pack_by_idx; 125struct packed_git **in_pack; 126 127uintmax_t oe_size_limit; 128}; 129 130voidprepare_packing_data(struct packing_data *pdata); 131struct object_entry *packlist_alloc(struct packing_data *pdata, 132const unsigned char*sha1, 133uint32_t index_pos); 134 135struct object_entry *packlist_find(struct packing_data *pdata, 136const unsigned char*sha1, 137uint32_t*index_pos); 138 139staticinlineuint32_tpack_name_hash(const char*name) 140{ 141uint32_t c, hash =0; 142 143if(!name) 144return0; 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 */ 151while((c = *name++) !=0) { 152if(isspace(c)) 153continue; 154 hash = (hash >>2) + (c <<24); 155} 156return hash; 157} 158 159staticinlineenum object_type oe_type(const struct object_entry *e) 160{ 161return e->type_valid ? e->type_ : OBJ_BAD; 162} 163 164staticinlinevoidoe_set_type(struct object_entry *e, 165enum object_type type) 166{ 167if(type >= OBJ_ANY) 168BUG("OBJ_ANY cannot be set in pack-objects code"); 169 170 e->type_valid = type >= OBJ_NONE; 171 e->type_ = (unsigned)type; 172} 173 174staticinlineunsigned intoe_in_pack_pos(const struct packing_data *pack, 175const struct object_entry *e) 176{ 177return pack->in_pack_pos[e - pack->objects]; 178} 179 180staticinlinevoidoe_set_in_pack_pos(const struct packing_data *pack, 181const struct object_entry *e, 182unsigned int pos) 183{ 184 pack->in_pack_pos[e - pack->objects] = pos; 185} 186 187staticinlinestruct packed_git *oe_in_pack(const struct packing_data *pack, 188const struct object_entry *e) 189{ 190if(pack->in_pack_by_idx) 191return pack->in_pack_by_idx[e->in_pack_idx]; 192else 193return pack->in_pack[e - pack->objects]; 194} 195 196voidoe_map_new_pack(struct packing_data *pack, 197struct packed_git *p); 198staticinlinevoidoe_set_in_pack(struct packing_data *pack, 199struct object_entry *e, 200struct packed_git *p) 201{ 202if(!p->index) 203oe_map_new_pack(pack, p); 204if(pack->in_pack_by_idx) 205 e->in_pack_idx = p->index; 206else 207 pack->in_pack[e - pack->objects] = p; 208} 209 210staticinlinestruct object_entry *oe_delta( 211const struct packing_data *pack, 212const struct object_entry *e) 213{ 214if(e->delta_idx) 215return&pack->objects[e->delta_idx -1]; 216return NULL; 217} 218 219staticinlinevoidoe_set_delta(struct packing_data *pack, 220struct object_entry *e, 221struct object_entry *delta) 222{ 223if(delta) 224 e->delta_idx = (delta - pack->objects) +1; 225else 226 e->delta_idx =0; 227} 228 229staticinlinestruct object_entry *oe_delta_child( 230const struct packing_data *pack, 231const struct object_entry *e) 232{ 233if(e->delta_child_idx) 234return&pack->objects[e->delta_child_idx -1]; 235return NULL; 236} 237 238staticinlinevoidoe_set_delta_child(struct packing_data *pack, 239struct object_entry *e, 240struct object_entry *delta) 241{ 242if(delta) 243 e->delta_child_idx = (delta - pack->objects) +1; 244else 245 e->delta_child_idx =0; 246} 247 248staticinlinestruct object_entry *oe_delta_sibling( 249const struct packing_data *pack, 250const struct object_entry *e) 251{ 252if(e->delta_sibling_idx) 253return&pack->objects[e->delta_sibling_idx -1]; 254return NULL; 255} 256 257staticinlinevoidoe_set_delta_sibling(struct packing_data *pack, 258struct object_entry *e, 259struct object_entry *delta) 260{ 261if(delta) 262 e->delta_sibling_idx = (delta - pack->objects) +1; 263else 264 e->delta_sibling_idx =0; 265} 266 267unsigned longoe_get_size_slow(struct packing_data *pack, 268const struct object_entry *e); 269staticinlineunsigned longoe_size(struct packing_data *pack, 270const struct object_entry *e) 271{ 272if(e->size_valid) 273return e->size_; 274 275returnoe_get_size_slow(pack, e); 276} 277 278staticinlineintoe_size_less_than(struct packing_data *pack, 279const struct object_entry *lhs, 280unsigned long rhs) 281{ 282if(lhs->size_valid) 283return lhs->size_ < rhs; 284if(rhs < pack->oe_size_limit)/* rhs < 2^x <= lhs ? */ 285return0; 286returnoe_get_size_slow(pack, lhs) < rhs; 287} 288 289staticinlineintoe_size_greater_than(struct packing_data *pack, 290const struct object_entry *lhs, 291unsigned long rhs) 292{ 293if(lhs->size_valid) 294return lhs->size_ > rhs; 295if(rhs < pack->oe_size_limit)/* rhs < 2^x <= lhs ? */ 296return1; 297returnoe_get_size_slow(pack, lhs) > rhs; 298} 299 300staticinlinevoidoe_set_size(struct packing_data *pack, 301struct object_entry *e, 302unsigned long size) 303{ 304if(size < pack->oe_size_limit) { 305 e->size_ = size; 306 e->size_valid =1; 307}else{ 308 e->size_valid =0; 309if(oe_get_size_slow(pack, e) != size) 310BUG("'size' is supposed to be the object size!"); 311} 312} 313 314staticinlineunsigned longoe_delta_size(struct packing_data *pack, 315const struct object_entry *e) 316{ 317if(e->delta_size_valid) 318return e->delta_size_; 319returnoe_size(pack, e); 320} 321 322staticinlinevoidoe_set_delta_size(struct packing_data *pack, 323struct object_entry *e, 324unsigned long size) 325{ 326 e->delta_size_ = size; 327 e->delta_size_valid = e->delta_size_ == size; 328if(!e->delta_size_valid && size !=oe_size(pack, e)) 329BUG("this can only happen in check_object() " 330"where delta size is the same as entry size"); 331} 332 333#endif