From: Paul Eggert Date: Fri, 2 Aug 2024 16:32:11 +0000 (-0700) Subject: Use xalignalloc X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba332e36d0a308bf0fd34178928118598aff4a15;p=thirdparty%2Ftar.git Use xalignalloc It ports around issues that our handwritten code does not. * gnulib.modules: Add xalignalloc. * src/misc.c (ptr_align, page_aligned_alloc): Remove. All page_aligned_alloc callers changed to use xalignalloc. --- diff --git a/gnulib.modules b/gnulib.modules index 4dc35be4..7637b803 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -119,6 +119,7 @@ unlinkdir unlocked-io utimensat version-etc-fsf +xalignalloc xalloc xalloc-die xgetcwd diff --git a/src/buffer.c b/src/buffer.c index e76ee9b7..9688a368 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -23,6 +23,7 @@ #include +#include #include #include #include @@ -45,7 +46,6 @@ static tarlong prev_written; /* bytes written on previous volumes */ static tarlong bytes_written; /* bytes written on this volume */ static void *record_buffer[2]; /* allocated memory */ -static union block *record_buffer_aligned[2]; static int record_index; /* FIXME: The following variables should ideally be static to this @@ -665,11 +665,10 @@ xclose (int fd) static void init_buffer (void) { - if (! record_buffer_aligned[record_index]) - record_buffer_aligned[record_index] = - page_aligned_alloc (&record_buffer[record_index], record_size); + if (! record_buffer[record_index]) + record_buffer[record_index] = xalignalloc (getpagesize (), record_size); - record_start = record_buffer_aligned[record_index]; + record_start = record_buffer[record_index]; current_block = record_start; record_end = record_start + blocking_factor; } @@ -1139,8 +1138,8 @@ close_archive (void) sys_wait_for_child (child_pid, hit_eof); tar_stat_destroy (¤t_stat_info); - free (record_buffer[0]); - free (record_buffer[1]); + alignfree (record_buffer[0]); + alignfree (record_buffer[1]); bufmap_free (NULL); } diff --git a/src/common.h b/src/common.h index 085e7d96..0120bf46 100644 --- a/src/common.h +++ b/src/common.h @@ -766,7 +766,6 @@ _Noreturn void write_fatal (char const *name); pid_t xfork (void); void xpipe (int fd[2]); -void *page_aligned_alloc (void **ptr, size_t size); int set_file_atime (int fd, int parentfd, char const *file, struct timespec atime); diff --git a/src/compare.c b/src/compare.c index deb9b92e..4b741c36 100644 --- a/src/compare.c +++ b/src/compare.c @@ -31,6 +31,7 @@ #endif #include "common.h" +#include #include #include #include @@ -48,8 +49,7 @@ static char *diff_buffer; void diff_init (void) { - void *ptr; - diff_buffer = page_aligned_alloc (&ptr, record_size); + diff_buffer = xalignalloc (getpagesize (), record_size); if (listed_incremental_option) read_directory_file (); } diff --git a/src/misc.c b/src/misc.c index 87eb1b84..da88c7e4 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1216,33 +1216,6 @@ xpipe (int fd[2]) call_arg_fatal ("pipe", _("interprocess channel")); } -/* Return PTR, aligned upward to the next multiple of ALIGNMENT. - ALIGNMENT must be nonzero. The caller must arrange for ((char *) - PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable - locations. */ - -static void * -ptr_align (void *ptr, size_t alignment) -{ - char *p0 = ptr; - char *p1 = p0 + alignment - 1; - return p1 - (size_t) p1 % alignment; -} - -/* Return the address of a page-aligned buffer of at least SIZE bytes. - The caller should free *PTR when done with the buffer. */ - -void * -page_aligned_alloc (void **ptr, size_t size) -{ - size_t alignment = getpagesize (); - size_t size1; - if (ckd_add (&size1, size, alignment)) - xalloc_die (); - *ptr = xmalloc (size1); - return ptr_align (*ptr, alignment); -} - struct namebuf