minor refactoring (lprintf)
[libpicofe.git] / linux / plat.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <dirent.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10
11 #include "../common/plat.h"
12
13
14 int plat_is_dir(const char *path)
15 {
16         DIR *dir;
17         if ((dir = opendir(path))) {
18                 closedir(dir);
19                 return 1;
20         }
21         return 0;
22 }
23
24 int plat_get_root_dir(char *dst, int len)
25 {
26         int j, ret;
27
28         ret = readlink("/proc/self/exe", dst, len - 1);
29         if (ret < 0) {
30                 perror("readlink");
31                 ret = 0;
32         }
33         dst[ret] = 0;
34
35         for (j = strlen(dst); j > 0; j--)
36                 if (dst[j] == '/') {
37                         dst[++j] = 0;
38                         break;
39                 }
40
41         return 0;
42 }
43
44 #ifdef __GP2X__
45 /* Wiz has a borked gettimeofday().. */
46 #define plat_get_ticks_ms plat_get_ticks_ms_good
47 #define plat_get_ticks_us plat_get_ticks_us_good
48 #endif
49
50 unsigned int plat_get_ticks_ms(void)
51 {
52         struct timeval tv;
53         unsigned int ret;
54
55         gettimeofday(&tv, NULL);
56
57         ret = (unsigned)tv.tv_sec * 1000;
58         /* approximate /= 1000 */
59         ret += ((unsigned)tv.tv_usec * 4195) >> 22;
60
61         return ret;
62 }
63
64 unsigned int plat_get_ticks_us(void)
65 {
66         struct timeval tv;
67         unsigned int ret;
68
69         gettimeofday(&tv, NULL);
70
71         ret = (unsigned)tv.tv_sec * 1000000;
72         ret += (unsigned)tv.tv_usec;
73
74         return ret;
75 }
76
77 void plat_sleep_ms(int ms)
78 {
79         usleep(ms * 1000);
80 }
81
82 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
83 {
84         struct timeval tv, *timeout = NULL;
85         int i, ret, fdmax = -1;
86         fd_set fdset;
87
88         if (timeout_ms >= 0) {
89                 tv.tv_sec = timeout_ms / 1000;
90                 tv.tv_usec = (timeout_ms % 1000) * 1000;
91                 timeout = &tv;
92         }
93
94         FD_ZERO(&fdset);
95         for (i = 0; i < count; i++) {
96                 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
97                 FD_SET(fds_hnds[i], &fdset);
98         }
99
100         ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
101         if (ret == -1)
102         {
103                 perror("plat_wait_event: select failed");
104                 sleep(1);
105                 return -1;
106         }
107
108         if (ret == 0)
109                 return -1; /* timeout */
110
111         ret = -1;
112         for (i = 0; i < count; i++)
113                 if (FD_ISSET(fds_hnds[i], &fdset))
114                         ret = fds_hnds[i];
115
116         return ret;
117 }
118
119 void *plat_mmap(unsigned long addr, size_t size)
120 {
121         void *req, *ret;
122
123         req = (void *)addr;
124         ret = mmap(req, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
125         if (ret == MAP_FAILED)
126                 return NULL;
127         if (ret != req)
128                 printf("warning: mmaped to %p, requested %p\n", ret, req);
129
130         return ret;
131 }
132
133 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
134 {
135         void *ret;
136
137         ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
138         if (ret == MAP_FAILED)
139                 return NULL;
140         if (ret != ptr)
141                 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
142
143         return ret;
144 }
145
146 void plat_munmap(void *ptr, size_t size)
147 {
148         munmap(ptr, size);
149 }
150
151 /* lprintf */
152 void lprintf(const char *fmt, ...)
153 {
154         va_list vl;
155
156         va_start(vl, fmt);
157         vprintf(fmt, vl);
158         va_end(vl);
159 }
160