xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/inline.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * inline.c --- Includes the inlined functions defined in the header
3*6a54128fSAndroid Build Coastguard Worker  * 	files as standalone functions, in case the application program
4*6a54128fSAndroid Build Coastguard Worker  * 	is compiled with inlining turned off.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993, 1994 Theodore Ts'o.
7*6a54128fSAndroid Build Coastguard Worker  *
8*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
9*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
10*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
11*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
12*6a54128fSAndroid Build Coastguard Worker  */
13*6a54128fSAndroid Build Coastguard Worker 
14*6a54128fSAndroid Build Coastguard Worker #ifndef _XOPEN_SOURCE
15*6a54128fSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 600	/* for posix_memalign() */
16*6a54128fSAndroid Build Coastguard Worker #endif
17*6a54128fSAndroid Build Coastguard Worker 
18*6a54128fSAndroid Build Coastguard Worker #include "config.h"
19*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
20*6a54128fSAndroid Build Coastguard Worker #include <string.h>
21*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
22*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
23*6a54128fSAndroid Build Coastguard Worker #endif
24*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
25*6a54128fSAndroid Build Coastguard Worker #include <time.h>
26*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
27*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
28*6a54128fSAndroid Build Coastguard Worker #endif
29*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
30*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
31*6a54128fSAndroid Build Coastguard Worker #endif
32*6a54128fSAndroid Build Coastguard Worker #if HAVE_MALLOC_H
33*6a54128fSAndroid Build Coastguard Worker #include <malloc.h>
34*6a54128fSAndroid Build Coastguard Worker #endif
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
37*6a54128fSAndroid Build Coastguard Worker #define INCLUDE_INLINE_FUNCS
38*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
39*6a54128fSAndroid Build Coastguard Worker 
40*6a54128fSAndroid Build Coastguard Worker /*
41*6a54128fSAndroid Build Coastguard Worker  * We used to define this as an inline, but since we are now using
42*6a54128fSAndroid Build Coastguard Worker  * autoconf-defined #ifdef's, we need to export this as a
43*6a54128fSAndroid Build Coastguard Worker  * library-provided function exclusively.
44*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_get_memalign(unsigned long size,unsigned long align,void * ptr)45*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_get_memalign(unsigned long size,
46*6a54128fSAndroid Build Coastguard Worker 			      unsigned long align, void *ptr)
47*6a54128fSAndroid Build Coastguard Worker {
48*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
49*6a54128fSAndroid Build Coastguard Worker 	void **p = ptr;
50*6a54128fSAndroid Build Coastguard Worker 
51*6a54128fSAndroid Build Coastguard Worker 	if (align < 8)
52*6a54128fSAndroid Build Coastguard Worker 		align = 8;
53*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_POSIX_MEMALIGN
54*6a54128fSAndroid Build Coastguard Worker 	retval = posix_memalign(p, align, size);
55*6a54128fSAndroid Build Coastguard Worker 	if (retval == ENOMEM)
56*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_MEMORY;
57*6a54128fSAndroid Build Coastguard Worker #else  /* !HAVE_POSIX_MEMALIGN */
58*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MEMALIGN
59*6a54128fSAndroid Build Coastguard Worker 	*p = memalign(align, size);
60*6a54128fSAndroid Build Coastguard Worker 	if (*p == NULL) {
61*6a54128fSAndroid Build Coastguard Worker 		if (errno)
62*6a54128fSAndroid Build Coastguard Worker 			return errno;
63*6a54128fSAndroid Build Coastguard Worker 		else
64*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_NO_MEMORY;
65*6a54128fSAndroid Build Coastguard Worker 	}
66*6a54128fSAndroid Build Coastguard Worker #else  /* !HAVE_MEMALIGN */
67*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_VALLOC
68*6a54128fSAndroid Build Coastguard Worker 	if (align > sizeof(long long))
69*6a54128fSAndroid Build Coastguard Worker 		*p = valloc(size);
70*6a54128fSAndroid Build Coastguard Worker 	else
71*6a54128fSAndroid Build Coastguard Worker #endif
72*6a54128fSAndroid Build Coastguard Worker 		*p = malloc(size);
73*6a54128fSAndroid Build Coastguard Worker 	if ((uintptr_t) *p & (align - 1)) {
74*6a54128fSAndroid Build Coastguard Worker 		free(*p);
75*6a54128fSAndroid Build Coastguard Worker 		*p = 0;
76*6a54128fSAndroid Build Coastguard Worker 	}
77*6a54128fSAndroid Build Coastguard Worker 	if (*p == 0)
78*6a54128fSAndroid Build Coastguard Worker 		return EXT2_ET_NO_MEMORY;
79*6a54128fSAndroid Build Coastguard Worker #endif	/* HAVE_MEMALIGN */
80*6a54128fSAndroid Build Coastguard Worker #endif	/* HAVE_POSIX_MEMALIGN */
81*6a54128fSAndroid Build Coastguard Worker 	return retval;
82*6a54128fSAndroid Build Coastguard Worker }
83*6a54128fSAndroid Build Coastguard Worker 
84*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
isaligned(void * ptr,unsigned long align)85*6a54128fSAndroid Build Coastguard Worker static int isaligned(void *ptr, unsigned long align)
86*6a54128fSAndroid Build Coastguard Worker {
87*6a54128fSAndroid Build Coastguard Worker 	return (((unsigned long) ptr & (align - 1)) == 0);
88*6a54128fSAndroid Build Coastguard Worker }
89*6a54128fSAndroid Build Coastguard Worker 
test_memalign(unsigned long align)90*6a54128fSAndroid Build Coastguard Worker static errcode_t test_memalign(unsigned long align)
91*6a54128fSAndroid Build Coastguard Worker {
92*6a54128fSAndroid Build Coastguard Worker 	void *ptr = 0;
93*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
94*6a54128fSAndroid Build Coastguard Worker 
95*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_get_memalign(32, align, &ptr);
96*6a54128fSAndroid Build Coastguard Worker 	if (!retval && !isaligned(ptr, align))
97*6a54128fSAndroid Build Coastguard Worker 		retval = EINVAL;
98*6a54128fSAndroid Build Coastguard Worker 	free(ptr);
99*6a54128fSAndroid Build Coastguard Worker 	printf("tst_memalign(%lu) is %s\n", align,
100*6a54128fSAndroid Build Coastguard Worker 	       retval ? error_message(retval) : "OK");
101*6a54128fSAndroid Build Coastguard Worker 	return retval;
102*6a54128fSAndroid Build Coastguard Worker }
103*6a54128fSAndroid Build Coastguard Worker 
main(int argc,char ** argv)104*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
105*6a54128fSAndroid Build Coastguard Worker {
106*6a54128fSAndroid Build Coastguard Worker 	int err = 0;
107*6a54128fSAndroid Build Coastguard Worker 
108*6a54128fSAndroid Build Coastguard Worker 	if (test_memalign(4))
109*6a54128fSAndroid Build Coastguard Worker 		err++;
110*6a54128fSAndroid Build Coastguard Worker 	if (test_memalign(32))
111*6a54128fSAndroid Build Coastguard Worker 		err++;
112*6a54128fSAndroid Build Coastguard Worker 	if (test_memalign(1024))
113*6a54128fSAndroid Build Coastguard Worker 		err++;
114*6a54128fSAndroid Build Coastguard Worker 	if (test_memalign(4096))
115*6a54128fSAndroid Build Coastguard Worker 		err++;
116*6a54128fSAndroid Build Coastguard Worker 	return err;
117*6a54128fSAndroid Build Coastguard Worker }
118*6a54128fSAndroid Build Coastguard Worker #endif
119