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.
This commit is contained in:
Paul Eggert
2024-08-02 09:32:11 -07:00
parent 61656ef35b
commit ba332e36d0
5 changed files with 9 additions and 37 deletions

View File

@@ -119,6 +119,7 @@ unlinkdir
unlocked-io unlocked-io
utimensat utimensat
version-etc-fsf version-etc-fsf
xalignalloc
xalloc xalloc
xalloc-die xalloc-die
xgetcwd xgetcwd

View File

@@ -23,6 +23,7 @@
#include <signal.h> #include <signal.h>
#include <alignalloc.h>
#include <c-ctype.h> #include <c-ctype.h>
#include <closeout.h> #include <closeout.h>
#include <fnmatch.h> #include <fnmatch.h>
@@ -45,7 +46,6 @@
static tarlong prev_written; /* bytes written on previous volumes */ static tarlong prev_written; /* bytes written on previous volumes */
static tarlong bytes_written; /* bytes written on this volume */ static tarlong bytes_written; /* bytes written on this volume */
static void *record_buffer[2]; /* allocated memory */ static void *record_buffer[2]; /* allocated memory */
static union block *record_buffer_aligned[2];
static int record_index; static int record_index;
/* FIXME: The following variables should ideally be static to this /* FIXME: The following variables should ideally be static to this
@@ -665,11 +665,10 @@ xclose (int fd)
static void static void
init_buffer (void) init_buffer (void)
{ {
if (! record_buffer_aligned[record_index]) if (! record_buffer[record_index])
record_buffer_aligned[record_index] = record_buffer[record_index] = xalignalloc (getpagesize (), record_size);
page_aligned_alloc (&record_buffer[record_index], record_size);
record_start = record_buffer_aligned[record_index]; record_start = record_buffer[record_index];
current_block = record_start; current_block = record_start;
record_end = record_start + blocking_factor; record_end = record_start + blocking_factor;
} }
@@ -1139,8 +1138,8 @@ close_archive (void)
sys_wait_for_child (child_pid, hit_eof); sys_wait_for_child (child_pid, hit_eof);
tar_stat_destroy (&current_stat_info); tar_stat_destroy (&current_stat_info);
free (record_buffer[0]); alignfree (record_buffer[0]);
free (record_buffer[1]); alignfree (record_buffer[1]);
bufmap_free (NULL); bufmap_free (NULL);
} }

View File

@@ -766,7 +766,6 @@ _Noreturn void write_fatal (char const *name);
pid_t xfork (void); pid_t xfork (void);
void xpipe (int fd[2]); 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, int set_file_atime (int fd, int parentfd, char const *file,
struct timespec atime); struct timespec atime);

View File

@@ -31,6 +31,7 @@
#endif #endif
#include "common.h" #include "common.h"
#include <alignalloc.h>
#include <quotearg.h> #include <quotearg.h>
#include <rmt.h> #include <rmt.h>
#include <stdarg.h> #include <stdarg.h>
@@ -48,8 +49,7 @@ static char *diff_buffer;
void void
diff_init (void) diff_init (void)
{ {
void *ptr; diff_buffer = xalignalloc (getpagesize (), record_size);
diff_buffer = page_aligned_alloc (&ptr, record_size);
if (listed_incremental_option) if (listed_incremental_option)
read_directory_file (); read_directory_file ();
} }

View File

@@ -1216,33 +1216,6 @@ xpipe (int fd[2])
call_arg_fatal ("pipe", _("interprocess channel")); 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 struct namebuf