13839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
23839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * inline.c --- Includes the inlined functions defined in the header
33839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * 	files as standalone functions, in case the application program
43839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * 	is compiled with inlining turned off.
5efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * Copyright (C) 1993, 1994 Theodore Ts'o.
719c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o *
819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header%
9543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * This file may be redistributed under the terms of the GNU Library
10543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * General Public License, version 2.
1119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header%
123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
14e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifndef _XOPEN_SOURCE
15e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#define _XOPEN_SOURCE 600	/* for posix_memalign() */
16e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdio.h>
193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <string.h>
204cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#if HAVE_UNISTD_H
213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <unistd.h>
224cbe8af4b0d0c72fb28bb500c1bd8a46b00fdde3Theodore Ts'o#endif
233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <fcntl.h>
243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <time.h>
251d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_STAT_H
263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/stat.h>
271d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
281d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#if HAVE_SYS_TYPES_H
293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/types.h>
301d2ff46ae7533ffd038534b189f272d2a4122e4eTheodore Ts'o#endif
31e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifdef HAVE_MALLOC_H
32e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#include <malloc.h>
33e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
35b5abe6fac9c9e7caf4710501d1657d30e4857ef6Theodore Ts'o#include "ext2_fs.h"
363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#define INCLUDE_INLINE_FUNCS
373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "ext2fs.h"
383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
39e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall/*
40e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall * We used to define this as an inline, but since we are now using
41e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall * autoconf-defined #ifdef's, we need to export this as a
42e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall * library-provided function exclusively.
43e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall */
44e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallerrcode_t ext2fs_get_memalign(unsigned long size,
45e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			      unsigned long align, void *ptr)
46e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
47e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	errcode_t retval;
48e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	void **p = ptr;
49e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
50e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (align < 8)
51e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		align = 8;
52e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifdef HAVE_POSIX_MEMALIGN
53e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	retval = posix_memalign(p, align, size);
54e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (retval) {
55e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		if (retval == ENOMEM)
56e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			return EXT2_ET_NO_MEMORY;
57e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		return retval;
58e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	}
59e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#else
60e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifdef HAVE_MEMALIGN
61e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	*p = memalign(align, size);
62e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (*p == NULL) {
63e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		if (errno)
64e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			return errno;
65e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		else
66e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			return EXT2_ET_NO_MEMORY;
67e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	}
68e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#else
69e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifdef HAVE_VALLOC
70e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (align > sizeof(long long))
71e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		*p = valloc(size);
72e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	else
73e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
74e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		*p = malloc(size);
75e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if ((unsigned long) *p & (align - 1)) {
76e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		free(*p);
77e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		*p = 0;
78e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	}
79e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (*p == 0)
80e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		return EXT2_ET_NO_MEMORY;
81e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
82e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
83e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return 0;
84e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
85e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
86e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#ifdef DEBUG
87e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallstatic int isaligned(void *ptr, unsigned long align)
88e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
89e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return (((unsigned long) ptr & (align - 1)) == 0);
90e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
91e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
92e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallstatic errcode_t test_memalign(unsigned long align)
93e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
94e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	void *ptr = 0;
95e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	errcode_t retval;
96e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
97e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	retval = ext2fs_get_memalign(32, align, &ptr);
98e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (!retval && !isaligned(ptr, align))
99e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		retval = EINVAL;
100e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	free(ptr);
101e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	printf("tst_memalign(%lu) is %s\n", align,
102e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	       retval ? error_message(retval) : "OK");
103e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return retval;
104e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
105e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
106e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrallint main(int argc, char **argv)
107e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall{
108e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	int err = 0;
109e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall
110e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (test_memalign(4))
111e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		err++;
112e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (test_memalign(32))
113e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		err++;
114e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (test_memalign(1024))
115e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		err++;
116e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	if (test_memalign(4096))
117e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		err++;
118e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	return err;
119e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall}
120e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall#endif
121