inline.c revision d71520751ecfb9e0196ff154122f54f2e0f12d0f
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
15#include "config.h"
16#include <stdio.h>
17#include <string.h>
18#if HAVE_UNISTD_H
19#include <unistd.h>
20#endif
21#include <fcntl.h>
22#include <time.h>
23#if HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#if HAVE_SYS_TYPES_H
27#include <sys/types.h>
28#endif
29#if HAVE_MALLOC_H
30#include <malloc.h>
31#endif
32
33#include "ext2_fs.h"
34#define INCLUDE_INLINE_FUNCS
35#include "ext2fs.h"
36
37/*
38 * We used to define this as an inline, but since we are now using
39 * autoconf-defined #ifdef's, we need to export this as a
40 * library-provided function exclusively.
41 */
42errcode_t ext2fs_get_memalign(unsigned long size,
43			      unsigned long align, void *ptr)
44{
45	errcode_t retval;
46	void **p = ptr;
47
48	if (align < 8)
49		align = 8;
50#ifdef HAVE_POSIX_MEMALIGN
51	retval = posix_memalign(p, align, size);
52	if (retval) {
53		if (retval == ENOMEM)
54			return EXT2_ET_NO_MEMORY;
55		return retval;
56	}
57#else
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
67#ifdef HAVE_VALLOC
68	if (align > sizeof(long long))
69		*p = valloc(size);
70	else
71#endif
72		*p = malloc(size);
73	if ((unsigned long) *p & (align - 1)) {
74		free(*p);
75		*p = 0;
76	}
77	if (*p == 0)
78		return EXT2_ET_NO_MEMORY;
79#endif
80#endif
81	return 0;
82}
83
84#ifdef DEBUG
85static int isaligned(void *ptr, unsigned long align)
86{
87	return (((unsigned long) ptr & (align - 1)) == 0);
88}
89
90static 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_memliagn(%lu): %s\n", align,
100	       retval ? error_message(retval) : "OK");
101	return retval;
102}
103
104int 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