698517be |
1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2008-2010 |
3 | * |
4 | * This work is licensed under the terms of any of these licenses |
5 | * (at your option): |
6 | * - GNU GPL, version 2 or later. |
7 | * - GNU LGPL, version 2.1 or later. |
8 | * See the COPYING file in the top-level directory. |
9 | */ |
10 | |
7badc935 |
11 | #define _GNU_SOURCE 1 |
698517be |
12 | #include <stdio.h> |
13 | #include <string.h> |
14 | #include <stdarg.h> |
15 | #include <dirent.h> |
16 | #include <sys/time.h> |
17 | #include <time.h> |
18 | #include <unistd.h> |
19 | #include <sys/mman.h> |
9ee0fd5b |
20 | #include <errno.h> |
698517be |
21 | |
22 | #include "../common/plat.h" |
23 | |
9ee0fd5b |
24 | /* XXX: maybe unhardcode pagesize? */ |
25 | #define HUGETLB_PAGESIZE (2 * 1024 * 1024) |
26 | #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2) |
27 | #ifndef MAP_HUGETLB |
28 | #define MAP_HUGETLB 0x40000 /* arch specific */ |
29 | #endif |
30 | |
698517be |
31 | |
32 | int plat_is_dir(const char *path) |
33 | { |
34 | DIR *dir; |
35 | if ((dir = opendir(path))) { |
36 | closedir(dir); |
37 | return 1; |
38 | } |
39 | return 0; |
40 | } |
41 | |
42 | int plat_get_root_dir(char *dst, int len) |
43 | { |
44 | int j, ret; |
45 | |
46 | ret = readlink("/proc/self/exe", dst, len - 1); |
47 | if (ret < 0) { |
48 | perror("readlink"); |
49 | ret = 0; |
50 | } |
51 | dst[ret] = 0; |
52 | |
53 | for (j = strlen(dst); j > 0; j--) |
54 | if (dst[j] == '/') { |
55 | dst[++j] = 0; |
56 | break; |
57 | } |
58 | |
69d41dfb |
59 | return j; |
698517be |
60 | } |
61 | |
62 | #ifdef __GP2X__ |
63 | /* Wiz has a borked gettimeofday().. */ |
64 | #define plat_get_ticks_ms plat_get_ticks_ms_good |
65 | #define plat_get_ticks_us plat_get_ticks_us_good |
66 | #endif |
67 | |
68 | unsigned int plat_get_ticks_ms(void) |
69 | { |
70 | struct timeval tv; |
71 | unsigned int ret; |
72 | |
73 | gettimeofday(&tv, NULL); |
74 | |
75 | ret = (unsigned)tv.tv_sec * 1000; |
76 | /* approximate /= 1000 */ |
77 | ret += ((unsigned)tv.tv_usec * 4195) >> 22; |
78 | |
79 | return ret; |
80 | } |
81 | |
82 | unsigned int plat_get_ticks_us(void) |
83 | { |
84 | struct timeval tv; |
85 | unsigned int ret; |
86 | |
87 | gettimeofday(&tv, NULL); |
88 | |
89 | ret = (unsigned)tv.tv_sec * 1000000; |
90 | ret += (unsigned)tv.tv_usec; |
91 | |
92 | return ret; |
93 | } |
94 | |
95 | void plat_sleep_ms(int ms) |
96 | { |
97 | usleep(ms * 1000); |
98 | } |
99 | |
100 | int plat_wait_event(int *fds_hnds, int count, int timeout_ms) |
101 | { |
102 | struct timeval tv, *timeout = NULL; |
103 | int i, ret, fdmax = -1; |
104 | fd_set fdset; |
105 | |
106 | if (timeout_ms >= 0) { |
107 | tv.tv_sec = timeout_ms / 1000; |
108 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
109 | timeout = &tv; |
110 | } |
111 | |
112 | FD_ZERO(&fdset); |
113 | for (i = 0; i < count; i++) { |
114 | if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i]; |
115 | FD_SET(fds_hnds[i], &fdset); |
116 | } |
117 | |
118 | ret = select(fdmax + 1, &fdset, NULL, NULL, timeout); |
119 | if (ret == -1) |
120 | { |
121 | perror("plat_wait_event: select failed"); |
122 | sleep(1); |
123 | return -1; |
124 | } |
125 | |
126 | if (ret == 0) |
127 | return -1; /* timeout */ |
128 | |
129 | ret = -1; |
130 | for (i = 0; i < count; i++) |
131 | if (FD_ISSET(fds_hnds[i], &fdset)) |
132 | ret = fds_hnds[i]; |
133 | |
134 | return ret; |
135 | } |
136 | |
9ee0fd5b |
137 | void *plat_mmap(unsigned long addr, size_t size, int need_exec) |
698517be |
138 | { |
9ee0fd5b |
139 | static int hugetlb_disabled; |
140 | int prot = PROT_READ | PROT_WRITE; |
141 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
698517be |
142 | void *req, *ret; |
143 | |
144 | req = (void *)addr; |
9ee0fd5b |
145 | if (need_exec) |
146 | prot |= PROT_EXEC; |
147 | if (size >= HUGETLB_THRESHOLD && !hugetlb_disabled) |
148 | flags |= MAP_HUGETLB; |
149 | |
150 | ret = mmap(req, size, prot, flags, -1, 0); |
151 | if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) { |
152 | fprintf(stderr, |
153 | "warning: failed to do hugetlb mmap (%p, %zu): %d\n", |
154 | req, size, errno); |
155 | hugetlb_disabled = 1; |
156 | flags &= ~MAP_HUGETLB; |
157 | ret = mmap(req, size, prot, flags, -1, 0); |
158 | } |
698517be |
159 | if (ret == MAP_FAILED) |
160 | return NULL; |
9ee0fd5b |
161 | |
162 | if (req != NULL && ret != req) |
163 | fprintf(stderr, |
164 | "warning: mmaped to %p, requested %p\n", ret, req); |
698517be |
165 | |
166 | return ret; |
167 | } |
168 | |
169 | void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) |
170 | { |
171 | void *ret; |
172 | |
173 | ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE); |
174 | if (ret == MAP_FAILED) |
175 | return NULL; |
176 | if (ret != ptr) |
177 | printf("warning: mremap moved: %p -> %p\n", ptr, ret); |
178 | |
179 | return ret; |
180 | } |
181 | |
182 | void plat_munmap(void *ptr, size_t size) |
183 | { |
9ee0fd5b |
184 | int ret; |
185 | |
186 | ret = munmap(ptr, size); |
187 | if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) { |
188 | // prehaps an autorounded hugetlb mapping? |
189 | size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1); |
190 | ret = munmap(ptr, size); |
191 | } |
192 | if (ret != 0) { |
193 | fprintf(stderr, |
194 | "munmap(%p, %zu) failed: %d\n", ptr, size, errno); |
195 | } |
698517be |
196 | } |
197 | |
198 | /* lprintf */ |
199 | void lprintf(const char *fmt, ...) |
200 | { |
201 | va_list vl; |
202 | |
203 | va_start(vl, fmt); |
204 | vprintf(fmt, vl); |
205 | va_end(vl); |
206 | } |
207 | |