2 * (C) GraÅžvydas "notaz" Ignotas, 2008-2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
26 /* XXX: maybe unhardcode pagesize? */
27 #define HUGETLB_PAGESIZE (2 * 1024 * 1024)
28 #define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2)
30 #define MAP_HUGETLB 0x40000 /* arch specific */
34 int plat_is_dir(const char *path)
37 if ((dir = opendir(path))) {
44 static int plat_get_data_dir(char *dst, int len)
47 memcpy(dst, PICO_DATA_DIR, sizeof PICO_DATA_DIR);
48 return sizeof(PICO_DATA_DIR) - 1;
50 int j, ret = readlink("/proc/self/exe", dst, len - 1);
57 for (j = ret - 1; j > 0; j--)
66 int plat_get_skin_dir(char *dst, int len)
68 int ret = plat_get_data_dir(dst, len);
72 memcpy(dst + ret, "skin/", sizeof "skin/");
73 return ret + sizeof("skin/") - 1;
77 #define PICO_HOME_DIR "/.picodrive/"
79 int plat_get_root_dir(char *dst, int len)
81 #if defined(__GP2X__) || defined(PANDORA)
82 return plat_get_data_dir(dst, len);
84 char *home = getenv("HOME");
85 size_t nb = strlen(home);
87 memcpy(dst, home, nb);
88 memcpy(dst + nb, PICO_HOME_DIR, sizeof PICO_HOME_DIR);
90 return nb + sizeof(PICO_HOME_DIR) - 1;
95 /* Wiz has a borked gettimeofday().. */
96 #define plat_get_ticks_ms plat_get_ticks_ms_good
97 #define plat_get_ticks_us plat_get_ticks_us_good
100 unsigned int plat_get_ticks_ms(void)
105 gettimeofday(&tv, NULL);
107 ret = (unsigned)tv.tv_sec * 1000;
108 /* approximate /= 1000 */
109 ret += ((unsigned)tv.tv_usec * 4195) >> 22;
114 unsigned int plat_get_ticks_us(void)
119 gettimeofday(&tv, NULL);
121 ret = (unsigned)tv.tv_sec * 1000000;
122 ret += (unsigned)tv.tv_usec;
127 void plat_sleep_ms(int ms)
132 int plat_wait_event(int *fds_hnds, int count, int timeout_ms)
134 struct timeval tv, *timeout = NULL;
135 int i, ret, fdmax = -1;
138 if (timeout_ms >= 0) {
139 tv.tv_sec = timeout_ms / 1000;
140 tv.tv_usec = (timeout_ms % 1000) * 1000;
145 for (i = 0; i < count; i++) {
146 if (fds_hnds[i] > fdmax) fdmax = fds_hnds[i];
147 FD_SET(fds_hnds[i], &fdset);
150 ret = select(fdmax + 1, &fdset, NULL, NULL, timeout);
153 perror("plat_wait_event: select failed");
159 return -1; /* timeout */
162 for (i = 0; i < count; i++)
163 if (FD_ISSET(fds_hnds[i], &fdset))
169 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
171 static int hugetlb_warned;
172 int prot = PROT_READ | PROT_WRITE;
173 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
181 if (size >= HUGETLB_THRESHOLD)
182 flags |= MAP_HUGETLB;
184 ret = mmap(req, size, prot, flags, -1, 0);
185 if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) {
186 if (!hugetlb_warned) {
188 "warning: failed to do hugetlb mmap (%p, %zu): %d\n",
192 flags &= ~MAP_HUGETLB;
193 ret = mmap(req, size, prot, flags, -1, 0);
195 if (ret == MAP_FAILED)
198 if (req != NULL && ret != req)
200 "warning: mmaped to %p, requested %p\n", ret, req);
205 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
209 ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE);
210 if (ret == MAP_FAILED)
213 printf("warning: mremap moved: %p -> %p\n", ptr, ret);
218 void plat_munmap(void *ptr, size_t size)
222 ret = munmap(ptr, size);
223 if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) {
224 // prehaps an autorounded hugetlb mapping?
225 size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1);
226 ret = munmap(ptr, size);
230 "munmap(%p, %zu) failed: %d\n", ptr, size, errno);
234 int plat_mem_set_exec(void *ptr, size_t size)
236 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
238 fprintf(stderr, "mprotect(%p, %zd) failed: %d\n",
245 void lprintf(const char *fmt, ...)