summaryrefslogtreecommitdiff
path: root/tools/perf/compat-android.h
blob: b8fb93679329080a69986cb179656144fc3fe70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Android compatibility header
 * Provides missing bits in Bionic on Android, ignored
 * on regular Linux.
 *
 * Written by Bernhard.Rosenkranzer@linaro.org
 *
 * Released into the public domain. Do with this file
 * whatever you want.
 */
#ifdef ANDROID
/* Bionic has its own idea about ALIGN, and kills other definitions.
 * Done outside the multiple-inclusion wrapper to make sure we
 * can override Bionic's ALIGN by simply including compat-android.h
 * again after including Bionic headers.
 */
#undef ALIGN
#undef __ALIGN_MASK
#define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))

#ifndef _COMPAT_ANDROID_H_
#define _COMPAT_ANDROID_H_ 1
#include <stdio.h>
#include <signal.h>
#include <asm/page.h> /* for PAGE_SIZE */
#include <asm/termios.h> /* for winsize */

#ifndef __WORDSIZE
#include <stdint.h>
#define __WORDSIZE _BITSIZE
#endif

#ifndef roundup
#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
#endif

#ifndef __force
#define __force
#endif

#ifndef __le32
#define __le32 uint32_t
#endif

/* Assorted functions that are missing from Bionic */
/* Android prior to 4.2 lacks psignal().
 * What we're doing here is fairly evil - but necessary since
 * Bionic doesn't export any version identifier or the likes.
 * We do know that 4.2 is the version introducing psignal() and
 * also KLOG_CONSOLE_OFF -- completely unrelated, but something
 * we can check for...
 */
#include <sys/klog.h>
#ifndef KLOG_CONSOLE_OFF
static void psignal(int sig, const char *s)
{
	if(sig >= 0 && sig < NSIG) {
		if(s)
			fprintf(stderr, "%s: %s\n", s, sys_siglist[sig]);
		else
			fprintf(stderr, "%s\n", sys_siglist[sig]);
	} else {
		if(s)
			fprintf(stderr, "%s: invalid signal\n", s);
		else
			fputs("invalid signal\n", stderr);
	}
}
#endif

static ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
	size_t ret = 0;

	if (!lineptr || !n || !stream)
		return -1;

	if(!*lineptr) {
		*n = 128;
		*lineptr = (char*)malloc(*n);
		if(!*lineptr)
			return -1;
	}

	while(!feof(stream) && !ferror(stream)) {
		int c;
		if(ret == *n) {
			*n += 128;
			*lineptr = (char*)realloc(*lineptr, *n);
			if(!*lineptr) {
				*n = 0;
				return -1;
			}
		}
		c = fgetc(stream);
		if(c == EOF)
			break;
		*lineptr[ret++] = c;
		if(c == '\n')
			break;
	}
	*lineptr[ret] = 0;
	return ret;
}
#endif
#endif