apply: introduce macro and function to init images
We're about to convert the `struct image` to gain a `struct strbuf` member, which requires more careful initialization than just memsetting it to zeros. Introduce the `IMAGE_INIT` macro and `image_init()` function to prepare for this change. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Sep 17, 2024 at 12:08 UTC
7db28d0133a8b828c8d7a829b587711acf81cddc
1 file changed
+14
-8
apply.c
+14
-8
@@ -284,11 +284,19 @@ struct image {
284
struct line *line_allocated;
285
struct line *line;
286
};
287
+#define IMAGE_INIT { 0 }
288
+
289
+static void image_init(struct image *image)
290
+{
291
+ struct image empty = IMAGE_INIT;
292
+ memcpy(image, &empty, sizeof(*image));
293
+}
294
295
static void image_clear(struct image *image)
296
{
297
free(image->buf);
298
free(image->line_allocated);
299
+ image_init(image);
300
}
301
302
static uint32_t hash_line(const char *cp, size_t len)
@@ -322,7 +330,7 @@ static void image_prepare(struct image *image, char *buf, size_t len,
330
{
331
const char *cp, *ep;
332
325
- memset(image, 0, sizeof(*image));
333
+ image_clear(image);
334
image->buf = buf;
335
image->len = len;
336
@@ -2314,7 +2322,7 @@ static void update_pre_post_images(struct image *preimage,
2322
{
2323
int i, ctx, reduced;
2324
char *new_buf, *old_buf, *fixed;
2317
- struct image fixed_preimage;
2325
+ struct image fixed_preimage = IMAGE_INIT;
2326
2327
/*
2328
* Update the preimage with whitespace fixes. Note that we
@@ -2910,11 +2918,9 @@ static int apply_one_fragment(struct apply_state *state,
2918
int hunk_linenr = frag->linenr;
2919
unsigned long leading, trailing;
2920
int pos, applied_pos;
2913
- struct image preimage;
2914
- struct image postimage;
2921
+ struct image preimage = IMAGE_INIT;
2922
+ struct image postimage = IMAGE_INIT;
2923
2916
- memset(&preimage, 0, sizeof(preimage));
2917
- memset(&postimage, 0, sizeof(postimage));
2924
oldlines = xmalloc(size);
2925
strbuf_init(&newlines, size);
2926
@@ -3650,7 +3656,7 @@ static int try_threeway(struct apply_state *state,
3656
size_t len;
3657
int status;
3658
char *img;
3653
- struct image tmp_image;
3659
+ struct image tmp_image = IMAGE_INIT;
3660
3661
/* No point falling back to 3-way merge in these cases */
3662
if (patch->is_delete ||
@@ -3727,7 +3733,7 @@ static int try_threeway(struct apply_state *state,
3733
static int apply_data(struct apply_state *state, struct patch *patch,
3734
struct stat *st, const struct cache_entry *ce)
3735
{
3730
- struct image image;
3736
+ struct image image = IMAGE_INIT;
3737
3738
if (load_preimage(state, &image, patch, st, ce) < 0)
3739
return -1;