From 464df1d5e5ab1322e2dd0a7796939fff1aeefa9a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:49:25 +0200 Subject: Adding upstream version 1.47.0. Signed-off-by: Daniel Baumann --- lib/ext2fs/inline.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 lib/ext2fs/inline.c (limited to 'lib/ext2fs/inline.c') diff --git a/lib/ext2fs/inline.c b/lib/ext2fs/inline.c new file mode 100644 index 0000000..ae2ae6e --- /dev/null +++ b/lib/ext2fs/inline.c @@ -0,0 +1,118 @@ +/* + * inline.c --- Includes the inlined functions defined in the header + * files as standalone functions, in case the application program + * is compiled with inlining turned off. + * + * Copyright (C) 1993, 1994 Theodore Ts'o. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. + * %End-Header% + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 /* for posix_memalign() */ +#endif + +#include "config.h" +#include +#include +#if HAVE_UNISTD_H +#include +#endif +#include +#include +#if HAVE_SYS_STAT_H +#include +#endif +#if HAVE_SYS_TYPES_H +#include +#endif +#if HAVE_MALLOC_H +#include +#endif + +#include "ext2_fs.h" +#define INCLUDE_INLINE_FUNCS +#include "ext2fs.h" + +/* + * We used to define this as an inline, but since we are now using + * autoconf-defined #ifdef's, we need to export this as a + * library-provided function exclusively. + */ +errcode_t ext2fs_get_memalign(unsigned long size, + unsigned long align, void *ptr) +{ + errcode_t retval = 0; + void **p = ptr; + + if (align < 8) + align = 8; +#ifdef HAVE_POSIX_MEMALIGN + retval = posix_memalign(p, align, size); + if (retval == ENOMEM) + return EXT2_ET_NO_MEMORY; +#else /* !HAVE_POSIX_MEMALIGN */ +#ifdef HAVE_MEMALIGN + *p = memalign(align, size); + if (*p == NULL) { + if (errno) + return errno; + else + return EXT2_ET_NO_MEMORY; + } +#else /* !HAVE_MEMALIGN */ +#ifdef HAVE_VALLOC + if (align > sizeof(long long)) + *p = valloc(size); + else +#endif + *p = malloc(size); + if ((uintptr_t) *p & (align - 1)) { + free(*p); + *p = 0; + } + if (*p == 0) + return EXT2_ET_NO_MEMORY; +#endif /* HAVE_MEMALIGN */ +#endif /* HAVE_POSIX_MEMALIGN */ + return retval; +} + +#ifdef DEBUG +static int isaligned(void *ptr, unsigned long align) +{ + return (((unsigned long) ptr & (align - 1)) == 0); +} + +static errcode_t test_memalign(unsigned long align) +{ + void *ptr = 0; + errcode_t retval; + + retval = ext2fs_get_memalign(32, align, &ptr); + if (!retval && !isaligned(ptr, align)) + retval = EINVAL; + free(ptr); + printf("tst_memalign(%lu) is %s\n", align, + retval ? error_message(retval) : "OK"); + return retval; +} + +int main(int argc, char **argv) +{ + int err = 0; + + if (test_memalign(4)) + err++; + if (test_memalign(32)) + err++; + if (test_memalign(1024)) + err++; + if (test_memalign(4096)) + err++; + return err; +} +#endif -- cgit v1.2.3