THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
4.9KB

  1. #include "cache.h"
  2. #include "object.h"
  3. #include "pack.h"
  4. #include "pack-objects.h"
  5. #include "packfile.h"
  6. #include "config.h"
  7. static uint32_t locate_object_entry_hash(struct packing_data *pdata,
  8. const struct object_id *oid,
  9. int *found)
  10. {
  11. uint32_t i, mask = (pdata->index_size - 1);
  12. i = oidhash(oid) & mask;
  13. while (pdata->index[i] > 0) {
  14. uint32_t pos = pdata->index[i] - 1;
  15. if (oideq(oid, &pdata->objects[pos].idx.oid)) {
  16. *found = 1;
  17. return i;
  18. }
  19. i = (i + 1) & mask;
  20. }
  21. *found = 0;
  22. return i;
  23. }
  24. static inline uint32_t closest_pow2(uint32_t v)
  25. {
  26. v = v - 1;
  27. v |= v >> 1;
  28. v |= v >> 2;
  29. v |= v >> 4;
  30. v |= v >> 8;
  31. v |= v >> 16;
  32. return v + 1;
  33. }
  34. static void rehash_objects(struct packing_data *pdata)
  35. {
  36. uint32_t i;
  37. struct object_entry *entry;
  38. pdata->index_size = closest_pow2(pdata->nr_objects * 3);
  39. if (pdata->index_size < 1024)
  40. pdata->index_size = 1024;
  41. free(pdata->index);
  42. pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
  43. entry = pdata->objects;
  44. for (i = 0; i < pdata->nr_objects; i++) {
  45. int found;
  46. uint32_t ix = locate_object_entry_hash(pdata,
  47. &entry->idx.oid,
  48. &found);
  49. if (found)
  50. BUG("Duplicate object in hash");
  51. pdata->index[ix] = i + 1;
  52. entry++;
  53. }
  54. }
  55. struct object_entry *packlist_find(struct packing_data *pdata,
  56. const struct object_id *oid)
  57. {
  58. uint32_t i;
  59. int found;
  60. if (!pdata->index_size)
  61. return NULL;
  62. i = locate_object_entry_hash(pdata, oid, &found);
  63. if (!found)
  64. return NULL;
  65. return &pdata->objects[pdata->index[i] - 1];
  66. }
  67. static void prepare_in_pack_by_idx(struct packing_data *pdata)
  68. {
  69. struct packed_git **mapping, *p;
  70. int cnt = 0, nr = 1U << OE_IN_PACK_BITS;
  71. ALLOC_ARRAY(mapping, nr);
  72. /*
  73. * oe_in_pack() on an all-zero'd object_entry
  74. * (i.e. in_pack_idx also zero) should return NULL.
  75. */
  76. mapping[cnt++] = NULL;
  77. for (p = get_all_packs(pdata->repo); p; p = p->next, cnt++) {
  78. if (cnt == nr) {
  79. free(mapping);
  80. return;
  81. }
  82. p->index = cnt;
  83. mapping[cnt] = p;
  84. }
  85. pdata->in_pack_by_idx = mapping;
  86. }
  87. /*
  88. * A new pack appears after prepare_in_pack_by_idx() has been
  89. * run. This is likely a race.
  90. *
  91. * We could map this new pack to in_pack_by_idx[] array, but then we
  92. * have to deal with full array anyway. And since it's hard to test
  93. * this fall back code, just stay simple and fall back to using
  94. * in_pack[] array.
  95. */
  96. void oe_map_new_pack(struct packing_data *pack)
  97. {
  98. uint32_t i;
  99. REALLOC_ARRAY(pack->in_pack, pack->nr_alloc);
  100. for (i = 0; i < pack->nr_objects; i++)
  101. pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);
  102. FREE_AND_NULL(pack->in_pack_by_idx);
  103. }
  104. /* assume pdata is already zero'd by caller */
  105. void prepare_packing_data(struct repository *r, struct packing_data *pdata)
  106. {
  107. pdata->repo = r;
  108. if (git_env_bool("GIT_TEST_FULL_IN_PACK_ARRAY", 0)) {
  109. /*
  110. * do not initialize in_pack_by_idx[] to force the
  111. * slow path in oe_in_pack()
  112. */
  113. } else {
  114. prepare_in_pack_by_idx(pdata);
  115. }
  116. pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
  117. 1U << OE_SIZE_BITS);
  118. pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
  119. 1UL << OE_DELTA_SIZE_BITS);
  120. init_recursive_mutex(&pdata->odb_lock);
  121. }
  122. struct object_entry *packlist_alloc(struct packing_data *pdata,
  123. const struct object_id *oid)
  124. {
  125. struct object_entry *new_entry;
  126. if (pdata->nr_objects >= pdata->nr_alloc) {
  127. pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
  128. REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
  129. if (!pdata->in_pack_by_idx)
  130. REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
  131. if (pdata->delta_size)
  132. REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
  133. if (pdata->tree_depth)
  134. REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);
  135. if (pdata->layer)
  136. REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
  137. }
  138. new_entry = pdata->objects + pdata->nr_objects++;
  139. memset(new_entry, 0, sizeof(*new_entry));
  140. oidcpy(&new_entry->idx.oid, oid);
  141. if (pdata->index_size * 3 <= pdata->nr_objects * 4)
  142. rehash_objects(pdata);
  143. else {
  144. int found;
  145. uint32_t pos = locate_object_entry_hash(pdata,
  146. &new_entry->idx.oid,
  147. &found);
  148. if (found)
  149. BUG("duplicate object inserted into hash");
  150. pdata->index[pos] = pdata->nr_objects;
  151. }
  152. if (pdata->in_pack)
  153. pdata->in_pack[pdata->nr_objects - 1] = NULL;
  154. if (pdata->tree_depth)
  155. pdata->tree_depth[pdata->nr_objects - 1] = 0;
  156. if (pdata->layer)
  157. pdata->layer[pdata->nr_objects - 1] = 0;
  158. return new_entry;
  159. }
  160. void oe_set_delta_ext(struct packing_data *pdata,
  161. struct object_entry *delta,
  162. const unsigned char *sha1)
  163. {
  164. struct object_entry *base;
  165. ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);
  166. base = &pdata->ext_bases[pdata->nr_ext++];
  167. memset(base, 0, sizeof(*base));
  168. hashcpy(base->idx.oid.hash, sha1);
  169. /* These flags mark that we are not part of the actual pack output. */
  170. base->preferred_base = 1;
  171. base->filled = 1;
  172. delta->ext_base = 1;
  173. delta->delta_idx = base - pdata->ext_bases + 1;
  174. }