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