pack-objects: move static inline from a header to the sole consumer

Move the code that is only used in builtin/pack-objects.c out of pack-objects.h. This fixes an issue where Solaris's SunCC hasn't been able to compile git since 483fa7f42d9 (t/helper/test-bitmap.c: initial commit, 2021-03-31). The real origin of that issue is that in 898eba5e630 (pack-objects: refer to delta objects by index instead of pointer, 2018-04-14) utility functions only needed by builtin/pack-objects.c were added to pack-objects.h. Since then the header has been used in a few other places, but 483fa7f42d9 was the first time it was used by test helper. Since Solaris is stricter about linking and the oe_get_size_slow() function lives in builtin/pack-objects.c the build started failing with: Undefined first referenced symbol in file oe_get_size_slow t/helper/test-bitmap.o ld: fatal: symbol referencing errors. No output written to t/helper/test-tool On other platforms this is presumably OK because the compiler and/or linker detects that the "static inline" functions that reference oe_get_size_slow() aren't used. Let's solve this by moving the relevant code from pack-objects.h to builtin/pack-objects.c. This is almost entirely a code-only move, but because of the early macro definitions in that file referencing some of these inline functions we need to move the definition of "static struct packing_data to_pack" earlier, and declare these inline functions above the macros. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed May 27, 2021 at 02:52 UTC 7d089fb9b73a0d7f9bf43a81f284fd4f499d4886
2 files changed +167 -166
builtin/pack-objects.c
+167 -7
@@ -37,6 +37,134 @@
37 #include "shallow.h"
38 #include "promisor-remote.h"
39
40 +/*
41 + * Objects we are going to pack are collected in the `to_pack` structure.
42 + * It contains an array (dynamically expanded) of the object data, and a map
43 + * that can resolve SHA1s to their position in the array.
44 + */
45 +static struct packing_data to_pack;
46 +
47 +static inline struct object_entry *oe_delta(
48 + const struct packing_data *pack,
49 + const struct object_entry *e)
50 +{
51 + if (!e->delta_idx)
52 + return NULL;
53 + if (e->ext_base)
54 + return &pack->ext_bases[e->delta_idx - 1];
55 + else
56 + return &pack->objects[e->delta_idx - 1];
57 +}
58 +
59 +static inline unsigned long oe_delta_size(struct packing_data *pack,
60 + const struct object_entry *e)
61 +{
62 + if (e->delta_size_valid)
63 + return e->delta_size_;
64 +
65 + /*
66 + * pack->delta_size[] can't be NULL because oe_set_delta_size()
67 + * must have been called when a new delta is saved with
68 + * oe_set_delta().
69 + * If oe_delta() returns NULL (i.e. default state, which means
70 + * delta_size_valid is also false), then the caller must never
71 + * call oe_delta_size().
72 + */
73 + return pack->delta_size[e - pack->objects];
74 +}
75 +
76 +unsigned long oe_get_size_slow(struct packing_data *pack,
77 + const struct object_entry *e);
78 +
79 +static inline unsigned long oe_size(struct packing_data *pack,
80 + const struct object_entry *e)
81 +{
82 + if (e->size_valid)
83 + return e->size_;
84 +
85 + return oe_get_size_slow(pack, e);
86 +}
87 +
88 +static inline void oe_set_delta(struct packing_data *pack,
89 + struct object_entry *e,
90 + struct object_entry *delta)
91 +{
92 + if (delta)
93 + e->delta_idx = (delta - pack->objects) + 1;
94 + else
95 + e->delta_idx = 0;
96 +}
97 +
98 +static inline struct object_entry *oe_delta_sibling(
99 + const struct packing_data *pack,
100 + const struct object_entry *e)
101 +{
102 + if (e->delta_sibling_idx)
103 + return &pack->objects[e->delta_sibling_idx - 1];
104 + return NULL;
105 +}
106 +
107 +static inline struct object_entry *oe_delta_child(
108 + const struct packing_data *pack,
109 + const struct object_entry *e)
110 +{
111 + if (e->delta_child_idx)
112 + return &pack->objects[e->delta_child_idx - 1];
113 + return NULL;
114 +}
115 +
116 +static inline void oe_set_delta_child(struct packing_data *pack,
117 + struct object_entry *e,
118 + struct object_entry *delta)
119 +{
120 + if (delta)
121 + e->delta_child_idx = (delta - pack->objects) + 1;
122 + else
123 + e->delta_child_idx = 0;
124 +}
125 +
126 +static inline void oe_set_delta_sibling(struct packing_data *pack,
127 + struct object_entry *e,
128 + struct object_entry *delta)
129 +{
130 + if (delta)
131 + e->delta_sibling_idx = (delta - pack->objects) + 1;
132 + else
133 + e->delta_sibling_idx = 0;
134 +}
135 +
136 +static inline void oe_set_size(struct packing_data *pack,
137 + struct object_entry *e,
138 + unsigned long size)
139 +{
140 + if (size < pack->oe_size_limit) {
141 + e->size_ = size;
142 + e->size_valid = 1;
143 + } else {
144 + e->size_valid = 0;
145 + if (oe_get_size_slow(pack, e) != size)
146 + BUG("'size' is supposed to be the object size!");
147 + }
148 +}
149 +
150 +static inline void oe_set_delta_size(struct packing_data *pack,
151 + struct object_entry *e,
152 + unsigned long size)
153 +{
154 + if (size < pack->oe_delta_size_limit) {
155 + e->delta_size_ = size;
156 + e->delta_size_valid = 1;
157 + } else {
158 + packing_data_lock(pack);
159 + if (!pack->delta_size)
160 + ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
161 + packing_data_unlock(pack);
162 +
163 + pack->delta_size[e - pack->objects] = size;
164 + e->delta_size_valid = 0;
165 + }
166 +}
167 +
168 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
169 #define SIZE(obj) oe_size(&to_pack, obj)
170 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
@@ -56,13 +184,6 @@ static const char *pack_usage[] = {
184 NULL
185 };
186
59 -/*
60 - * Objects we are going to pack are collected in the `to_pack` structure.
61 - * It contains an array (dynamically expanded) of the object data, and a map
62 - * that can resolve SHA1s to their position in the array.
63 - */
64 -static struct packing_data to_pack;
65 -
187 static struct pack_idx_entry **written_list;
188 static uint32_t nr_result, nr_written, nr_seen;
189 static struct bitmap_index *bitmap_git;
@@ -301,6 +422,17 @@ static void copy_pack_data(struct hashfile *f,
422 }
423 }
424
425 +static inline int oe_size_greater_than(struct packing_data *pack,
426 + const struct object_entry *lhs,
427 + unsigned long rhs)
428 +{
429 + if (lhs->size_valid)
430 + return lhs->size_ > rhs;
431 + if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
432 + return 1;
433 + return oe_get_size_slow(pack, lhs) > rhs;
434 +}
435 +
436 /* Return 0 if we will bust the pack-size limit */
437 static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
438 unsigned long limit, int usable_delta)
@@ -642,6 +774,14 @@ static int mark_tagged(const char *path, const struct object_id *oid, int flag,
774 return 0;
775 }
776
777 +static inline unsigned char oe_layer(struct packing_data *pack,
778 + struct object_entry *e)
779 +{
780 + if (!pack->layer)
781 + return 0;
782 + return pack->layer[e - pack->objects];
783 +}
784 +
785 static inline void add_to_write_order(struct object_entry **wo,
786 unsigned int *endp,
787 struct object_entry *e)
@@ -2231,6 +2371,26 @@ static pthread_mutex_t progress_mutex;
2371 * progress_mutex for protection.
2372 */
2373
2374 +static inline int oe_size_less_than(struct packing_data *pack,
2375 + const struct object_entry *lhs,
2376 + unsigned long rhs)
2377 +{
2378 + if (lhs->size_valid)
2379 + return lhs->size_ < rhs;
2380 + if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2381 + return 0;
2382 + return oe_get_size_slow(pack, lhs) < rhs;
2383 +}
2384 +
2385 +static inline void oe_set_tree_depth(struct packing_data *pack,
2386 + struct object_entry *e,
2387 + unsigned int tree_depth)
2388 +{
2389 + if (!pack->tree_depth)
2390 + CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2391 + pack->tree_depth[e - pack->objects] = tree_depth;
2392 +}
2393 +
2394 /*
2395 * Return the size of the object without doing any delta
2396 * reconstruction (so non-deltas are true object sizes, but deltas
pack-objects.h
-159
@@ -268,152 +268,10 @@ static inline void oe_set_in_pack(struct packing_data *pack,
268 pack->in_pack[e - pack->objects] = p;
269 }
270
271 -static inline struct object_entry *oe_delta(
272 - const struct packing_data *pack,
273 - const struct object_entry *e)
274 -{
275 - if (!e->delta_idx)
276 - return NULL;
277 - if (e->ext_base)
278 - return &pack->ext_bases[e->delta_idx - 1];
279 - else
280 - return &pack->objects[e->delta_idx - 1];
281 -}
282 -
283 -static inline void oe_set_delta(struct packing_data *pack,
284 - struct object_entry *e,
285 - struct object_entry *delta)
286 -{
287 - if (delta)
288 - e->delta_idx = (delta - pack->objects) + 1;
289 - else
290 - e->delta_idx = 0;
291 -}
292 -
271 void oe_set_delta_ext(struct packing_data *pack,
272 struct object_entry *e,
273 const struct object_id *oid);
274
297 -static inline struct object_entry *oe_delta_child(
298 - const struct packing_data *pack,
299 - const struct object_entry *e)
300 -{
301 - if (e->delta_child_idx)
302 - return &pack->objects[e->delta_child_idx - 1];
303 - return NULL;
304 -}
305 -
306 -static inline void oe_set_delta_child(struct packing_data *pack,
307 - struct object_entry *e,
308 - struct object_entry *delta)
309 -{
310 - if (delta)
311 - e->delta_child_idx = (delta - pack->objects) + 1;
312 - else
313 - e->delta_child_idx = 0;
314 -}
315 -
316 -static inline struct object_entry *oe_delta_sibling(
317 - const struct packing_data *pack,
318 - const struct object_entry *e)
319 -{
320 - if (e->delta_sibling_idx)
321 - return &pack->objects[e->delta_sibling_idx - 1];
322 - return NULL;
323 -}
324 -
325 -static inline void oe_set_delta_sibling(struct packing_data *pack,
326 - struct object_entry *e,
327 - struct object_entry *delta)
328 -{
329 - if (delta)
330 - e->delta_sibling_idx = (delta - pack->objects) + 1;
331 - else
332 - e->delta_sibling_idx = 0;
333 -}
334 -
335 -unsigned long oe_get_size_slow(struct packing_data *pack,
336 - const struct object_entry *e);
337 -static inline unsigned long oe_size(struct packing_data *pack,
338 - const struct object_entry *e)
339 -{
340 - if (e->size_valid)
341 - return e->size_;
342 -
343 - return oe_get_size_slow(pack, e);
344 -}
345 -
346 -static inline int oe_size_less_than(struct packing_data *pack,
347 - const struct object_entry *lhs,
348 - unsigned long rhs)
349 -{
350 - if (lhs->size_valid)
351 - return lhs->size_ < rhs;
352 - if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
353 - return 0;
354 - return oe_get_size_slow(pack, lhs) < rhs;
355 -}
356 -
357 -static inline int oe_size_greater_than(struct packing_data *pack,
358 - const struct object_entry *lhs,
359 - unsigned long rhs)
360 -{
361 - if (lhs->size_valid)
362 - return lhs->size_ > rhs;
363 - if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
364 - return 1;
365 - return oe_get_size_slow(pack, lhs) > rhs;
366 -}
367 -
368 -static inline void oe_set_size(struct packing_data *pack,
369 - struct object_entry *e,
370 - unsigned long size)
371 -{
372 - if (size < pack->oe_size_limit) {
373 - e->size_ = size;
374 - e->size_valid = 1;
375 - } else {
376 - e->size_valid = 0;
377 - if (oe_get_size_slow(pack, e) != size)
378 - BUG("'size' is supposed to be the object size!");
379 - }
380 -}
381 -
382 -static inline unsigned long oe_delta_size(struct packing_data *pack,
383 - const struct object_entry *e)
384 -{
385 - if (e->delta_size_valid)
386 - return e->delta_size_;
387 -
388 - /*
389 - * pack->delta_size[] can't be NULL because oe_set_delta_size()
390 - * must have been called when a new delta is saved with
391 - * oe_set_delta().
392 - * If oe_delta() returns NULL (i.e. default state, which means
393 - * delta_size_valid is also false), then the caller must never
394 - * call oe_delta_size().
395 - */
396 - return pack->delta_size[e - pack->objects];
397 -}
398 -
399 -static inline void oe_set_delta_size(struct packing_data *pack,
400 - struct object_entry *e,
401 - unsigned long size)
402 -{
403 - if (size < pack->oe_delta_size_limit) {
404 - e->delta_size_ = size;
405 - e->delta_size_valid = 1;
406 - } else {
407 - packing_data_lock(pack);
408 - if (!pack->delta_size)
409 - ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
410 - packing_data_unlock(pack);
411 -
412 - pack->delta_size[e - pack->objects] = size;
413 - e->delta_size_valid = 0;
414 - }
415 -}
416 -
275 static inline unsigned int oe_tree_depth(struct packing_data *pack,
276 struct object_entry *e)
277 {
@@ -422,23 +280,6 @@ static inline unsigned int oe_tree_depth(struct packing_data *pack,
280 return pack->tree_depth[e - pack->objects];
281 }
282
425 -static inline void oe_set_tree_depth(struct packing_data *pack,
426 - struct object_entry *e,
427 - unsigned int tree_depth)
428 -{
429 - if (!pack->tree_depth)
430 - CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
431 - pack->tree_depth[e - pack->objects] = tree_depth;
432 -}
433 -
434 -static inline unsigned char oe_layer(struct packing_data *pack,
435 - struct object_entry *e)
436 -{
437 - if (!pack->layer)
438 - return 0;
439 - return pack->layer[e - pack->objects];
440 -}
441 -
283 static inline void oe_set_layer(struct packing_data *pack,
284 struct object_entry *e,
285 unsigned char layer)