Home | History | Annotate | Download | only in ext2fs
      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 <stdio.h>
     19 #include <string.h>
     20 #if HAVE_UNISTD_H
     21 #include <unistd.h>
     22 #endif
     23 #include <fcntl.h>
     24 #include <time.h>
     25 #if HAVE_SYS_STAT_H
     26 #include <sys/stat.h>
     27 #endif
     28 #if HAVE_SYS_TYPES_H
     29 #include <sys/types.h>
     30 #endif
     31 #ifdef HAVE_MALLOC_H
     32 #include <malloc.h>
     33 #endif
     34 
     35 #include "ext2_fs.h"
     36 #define INCLUDE_INLINE_FUNCS
     37 #include "ext2fs.h"
     38 
     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
    121