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 = 0; 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 == ENOMEM) 56 return EXT2_ET_NO_MEMORY; 57 #else /* !HAVE_POSIX_MEMALIGN */ 58 #ifdef HAVE_MEMALIGN 59 *p = memalign(align, size); 60 if (*p == NULL) { 61 if (errno) 62 return errno; 63 else 64 return EXT2_ET_NO_MEMORY; 65 } 66 #else /* !HAVE_MEMALIGN */ 67 #ifdef HAVE_VALLOC 68 if (align > sizeof(long long)) 69 *p = valloc(size); 70 else 71 #endif 72 *p = malloc(size); 73 if ((uintptr_t) *p & (align - 1)) { 74 free(*p); 75 *p = 0; 76 } 77 if (*p == 0) 78 return EXT2_ET_NO_MEMORY; 79 #endif /* HAVE_MEMALIGN */ 80 #endif /* HAVE_POSIX_MEMALIGN */ 81 return retval; 82 } 83 84 #ifdef DEBUG 85 static int isaligned(void *ptr, unsigned long align) 86 { 87 return (((unsigned long) ptr & (align - 1)) == 0); 88 } 89 90 static errcode_t test_memalign(unsigned long align) 91 { 92 void *ptr = 0; 93 errcode_t retval; 94 95 retval = ext2fs_get_memalign(32, align, &ptr); 96 if (!retval && !isaligned(ptr, align)) 97 retval = EINVAL; 98 free(ptr); 99 printf("tst_memalign(%lu) is %s\n", align, 100 retval ? error_message(retval) : "OK"); 101 return retval; 102 } 103 104 int main(int argc, char **argv) 105 { 106 int err = 0; 107 108 if (test_memalign(4)) 109 err++; 110 if (test_memalign(32)) 111 err++; 112 if (test_memalign(1024)) 113 err++; 114 if (test_memalign(4096)) 115 err++; 116 return err; 117 } 118 #endif 119