git
/
pack-objects.c
242 строки · 5.4 Кб
1#include "git-compat-util.h"2#include "object.h"3#include "pack.h"4#include "pack-objects.h"5#include "packfile.h"6#include "parse.h"7
8static uint32_t locate_object_entry_hash(struct packing_data *pdata,9const struct object_id *oid,10int *found)11{
12uint32_t i, mask = (pdata->index_size - 1);13
14i = oidhash(oid) & mask;15
16while (pdata->index[i] > 0) {17uint32_t pos = pdata->index[i] - 1;18
19if (oideq(oid, &pdata->objects[pos].idx.oid)) {20*found = 1;21return i;22}23
24i = (i + 1) & mask;25}26
27*found = 0;28return i;29}
30
31static inline uint32_t closest_pow2(uint32_t v)32{
33v = v - 1;34v |= v >> 1;35v |= v >> 2;36v |= v >> 4;37v |= v >> 8;38v |= v >> 16;39return v + 1;40}
41
42static void rehash_objects(struct packing_data *pdata)43{
44uint32_t i;45struct object_entry *entry;46
47pdata->index_size = closest_pow2(pdata->nr_objects * 3);48if (pdata->index_size < 1024)49pdata->index_size = 1024;50
51free(pdata->index);52CALLOC_ARRAY(pdata->index, pdata->index_size);53
54entry = pdata->objects;55
56for (i = 0; i < pdata->nr_objects; i++) {57int found;58uint32_t ix = locate_object_entry_hash(pdata,59&entry->idx.oid,60&found);61
62if (found)63BUG("Duplicate object in hash");64
65pdata->index[ix] = i + 1;66entry++;67}68}
69
70struct object_entry *packlist_find(struct packing_data *pdata,71const struct object_id *oid)72{
73uint32_t i;74int found;75
76if (!pdata->index_size)77return NULL;78
79i = locate_object_entry_hash(pdata, oid, &found);80
81if (!found)82return NULL;83
84return &pdata->objects[pdata->index[i] - 1];85}
86
87static void prepare_in_pack_by_idx(struct packing_data *pdata)88{
89struct packed_git **mapping, *p;90int cnt = 0, nr = 1U << OE_IN_PACK_BITS;91
92ALLOC_ARRAY(mapping, nr);93/*94* oe_in_pack() on an all-zero'd object_entry
95* (i.e. in_pack_idx also zero) should return NULL.
96*/
97mapping[cnt++] = NULL;98for (p = get_all_packs(pdata->repo); p; p = p->next, cnt++) {99if (cnt == nr) {100free(mapping);101return;102}103p->index = cnt;104mapping[cnt] = p;105}106pdata->in_pack_by_idx = mapping;107}
108
109/*
110* A new pack appears after prepare_in_pack_by_idx() has been
111* run. This is likely a race.
112*
113* We could map this new pack to in_pack_by_idx[] array, but then we
114* have to deal with full array anyway. And since it's hard to test
115* this fall back code, just stay simple and fall back to using
116* in_pack[] array.
117*/
118void oe_map_new_pack(struct packing_data *pack)119{
120uint32_t i;121
122if (pack->in_pack)123BUG("packing_data has already been converted to pack array");124
125ALLOC_ARRAY(pack->in_pack, pack->nr_alloc);126
127for (i = 0; i < pack->nr_objects; i++)128pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);129
130FREE_AND_NULL(pack->in_pack_by_idx);131}
132
133/* assume pdata is already zero'd by caller */
134void prepare_packing_data(struct repository *r, struct packing_data *pdata)135{
136pdata->repo = r;137
138if (git_env_bool("GIT_TEST_FULL_IN_PACK_ARRAY", 0)) {139/*140* do not initialize in_pack_by_idx[] to force the
141* slow path in oe_in_pack()
142*/
143} else {144prepare_in_pack_by_idx(pdata);145}146
147pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",1481U << OE_SIZE_BITS);149pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",1501UL << OE_DELTA_SIZE_BITS);151init_recursive_mutex(&pdata->odb_lock);152}
153
154void clear_packing_data(struct packing_data *pdata)155{
156if (!pdata)157return;158
159free(pdata->cruft_mtime);160free(pdata->in_pack);161free(pdata->in_pack_by_idx);162free(pdata->in_pack_pos);163free(pdata->index);164free(pdata->layer);165free(pdata->objects);166free(pdata->tree_depth);167}
168
169struct object_entry *packlist_alloc(struct packing_data *pdata,170const struct object_id *oid)171{
172struct object_entry *new_entry;173
174if (pdata->nr_objects >= pdata->nr_alloc) {175pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;176REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);177
178if (!pdata->in_pack_by_idx)179REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);180if (pdata->delta_size)181REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);182
183if (pdata->tree_depth)184REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);185
186if (pdata->layer)187REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);188
189if (pdata->cruft_mtime)190REALLOC_ARRAY(pdata->cruft_mtime, pdata->nr_alloc);191}192
193new_entry = pdata->objects + pdata->nr_objects++;194
195memset(new_entry, 0, sizeof(*new_entry));196oidcpy(&new_entry->idx.oid, oid);197
198if (pdata->index_size * 3 <= pdata->nr_objects * 4)199rehash_objects(pdata);200else {201int found;202uint32_t pos = locate_object_entry_hash(pdata,203&new_entry->idx.oid,204&found);205if (found)206BUG("duplicate object inserted into hash");207pdata->index[pos] = pdata->nr_objects;208}209
210if (pdata->in_pack)211pdata->in_pack[pdata->nr_objects - 1] = NULL;212
213if (pdata->tree_depth)214pdata->tree_depth[pdata->nr_objects - 1] = 0;215
216if (pdata->layer)217pdata->layer[pdata->nr_objects - 1] = 0;218
219if (pdata->cruft_mtime)220pdata->cruft_mtime[pdata->nr_objects - 1] = 0;221
222return new_entry;223}
224
225void oe_set_delta_ext(struct packing_data *pdata,226struct object_entry *delta,227const struct object_id *oid)228{
229struct object_entry *base;230
231ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);232base = &pdata->ext_bases[pdata->nr_ext++];233memset(base, 0, sizeof(*base));234oidcpy(&base->idx.oid, oid);235
236/* These flags mark that we are not part of the actual pack output. */237base->preferred_base = 1;238base->filled = 1;239
240delta->ext_base = 1;241delta->delta_idx = base - pdata->ext_bases + 1;242}
243