blob: 4d0f4ad161ab76599d1cbd3218090a9762bc1298 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00006 * Copyright (C) 1993, 1994 Theodore Ts'o.
7 *
8 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04009 * This file may be redistributed under the terms of the GNU Library
10 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000011 * %End-Header%
Theodore Ts'o3839e651997-04-26 13:21:57 +000012 */
13
JP Abgralle0ed7402014-03-19 19:08:39 -070014#ifndef _XOPEN_SOURCE
15#define _XOPEN_SOURCE 600 /* for posix_memalign() */
16#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017
18#include <stdio.h>
19#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000020#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000021#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000022#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000023#include <fcntl.h>
24#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000027#endif
28#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000029#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000030#endif
JP Abgralle0ed7402014-03-19 19:08:39 -070031#ifdef HAVE_MALLOC_H
32#include <malloc.h>
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000035#include "ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000036#define INCLUDE_INLINE_FUNCS
Theodore Ts'o3839e651997-04-26 13:21:57 +000037#include "ext2fs.h"
38
JP Abgralle0ed7402014-03-19 19:08:39 -070039/*
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 */
44errcode_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
87static int isaligned(void *ptr, unsigned long align)
88{
89 return (((unsigned long) ptr & (align - 1)) == 0);
90}
91
92static 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
106int 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