lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * inline.c --- Includes the inlined functions defined in the header |
| 3 | * files as standalone functions, in case the application program |
| 4 | * is compiled with inlining turned off. |
| 5 | * |
| 6 | * Copyright (C) 1993, 1994 Theodore Ts'o. |
| 7 | * |
| 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Library |
| 10 | * General Public License, version 2. |
| 11 | * %End-Header% |
| 12 | */ |
| 13 | |
| 14 | #ifndef _XOPEN_SOURCE |
| 15 | #define _XOPEN_SOURCE 600 /* for posix_memalign() */ |
| 16 | #endif |
| 17 | |
| 18 | #include "config.h" |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #if HAVE_UNISTD_H |
| 22 | #include <unistd.h> |
| 23 | #endif |
| 24 | #include <fcntl.h> |
| 25 | #include <time.h> |
| 26 | #if HAVE_SYS_STAT_H |
| 27 | #include <sys/stat.h> |
| 28 | #endif |
| 29 | #if HAVE_SYS_TYPES_H |
| 30 | #include <sys/types.h> |
| 31 | #endif |
| 32 | #if HAVE_MALLOC_H |
| 33 | #include <malloc.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "ext2_fs.h" |
| 37 | #define INCLUDE_INLINE_FUNCS |
| 38 | #include "ext2fs.h" |
| 39 | |
| 40 | /* |
| 41 | * We used to define this as an inline, but since we are now using |
| 42 | * autoconf-defined #ifdef's, we need to export this as a |
| 43 | * library-provided function exclusively. |
| 44 | */ |
| 45 | errcode_t ext2fs_get_memalign(unsigned long size, |
| 46 | unsigned long align, void *ptr) |
| 47 | { |
| 48 | errcode_t retval; |
| 49 | void **p = ptr; |
| 50 | |
| 51 | if (align < 8) |
| 52 | align = 8; |
| 53 | #ifdef HAVE_POSIX_MEMALIGN |
| 54 | retval = posix_memalign(p, align, size); |
| 55 | if (retval) { |
| 56 | if (retval == ENOMEM) |
| 57 | return EXT2_ET_NO_MEMORY; |
| 58 | return retval; |
| 59 | } |
| 60 | #else |
| 61 | #ifdef HAVE_MEMALIGN |
| 62 | *p = memalign(align, size); |
| 63 | if (*p == NULL) { |
| 64 | if (errno) |
| 65 | return errno; |
| 66 | else |
| 67 | return EXT2_ET_NO_MEMORY; |
| 68 | } |
| 69 | #else |
| 70 | #ifdef HAVE_VALLOC |
| 71 | if (align > sizeof(long long)) |
| 72 | *p = valloc(size); |
| 73 | else |
| 74 | #endif |
| 75 | *p = malloc(size); |
| 76 | if ((unsigned long) *p & (align - 1)) { |
| 77 | free(*p); |
| 78 | *p = 0; |
| 79 | } |
| 80 | if (*p == 0) |
| 81 | return EXT2_ET_NO_MEMORY; |
| 82 | #endif |
| 83 | #endif |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | #ifdef DEBUG |
| 88 | static int isaligned(void *ptr, unsigned long align) |
| 89 | { |
| 90 | return (((unsigned long) ptr & (align - 1)) == 0); |
| 91 | } |
| 92 | |
| 93 | static errcode_t test_memalign(unsigned long align) |
| 94 | { |
| 95 | void *ptr = 0; |
| 96 | errcode_t retval; |
| 97 | |
| 98 | retval = ext2fs_get_memalign(32, align, &ptr); |
| 99 | if (!retval && !isaligned(ptr, align)) |
| 100 | retval = EINVAL; |
| 101 | free(ptr); |
| 102 | printf("tst_memalign(%lu) is %s\n", align, |
| 103 | retval ? error_message(retval) : "OK"); |
| 104 | return retval; |
| 105 | } |
| 106 | |
| 107 | int main(int argc, char **argv) |
| 108 | { |
| 109 | int err = 0; |
| 110 | |
| 111 | if (test_memalign(4)) |
| 112 | err++; |
| 113 | if (test_memalign(32)) |
| 114 | err++; |
| 115 | if (test_memalign(1024)) |
| 116 | err++; |
| 117 | if (test_memalign(4096)) |
| 118 | err++; |
| 119 | return err; |
| 120 | } |
| 121 | #endif |