X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=linux%2Fplat.c;h=04ebd12fe6f7997a1695a510999e21903e897226;hb=f4750ef3f52e445cc2a810b0eb45989b8a82c446;hp=d777f23b2a09f29d011bc54ea4133715e37ac20c;hpb=d2f29611d1379a249c8f3f955ed16a336f9290cd;p=libpicofe.git diff --git a/linux/plat.c b/linux/plat.c index d777f23..04ebd12 100644 --- a/linux/plat.c +++ b/linux/plat.c @@ -1,11 +1,33 @@ +/* + * (C) Gražvydas "notaz" Ignotas, 2008-2012 + * + * This work is licensed under the terms of any of these licenses + * (at your option): + * - GNU GPL, version 2 or later. + * - GNU LGPL, version 2.1 or later. + * - MAME license. + * See the COPYING file in the top-level directory. + */ + +#define _GNU_SOURCE 1 #include #include +#include #include #include #include #include +#include +#include -#include "../common/plat.h" +#include "../plat.h" + +/* XXX: maybe unhardcode pagesize? */ +#define HUGETLB_PAGESIZE (2 * 1024 * 1024) +#define HUGETLB_THRESHOLD (HUGETLB_PAGESIZE / 2) +#ifndef MAP_HUGETLB +#define MAP_HUGETLB 0x40000 /* arch specific */ +#endif int plat_is_dir(const char *path) @@ -20,17 +42,22 @@ int plat_is_dir(const char *path) int plat_get_root_dir(char *dst, int len) { - extern char **g_argv; - int j; + int j, ret; + + ret = readlink("/proc/self/exe", dst, len - 1); + if (ret < 0) { + perror("readlink"); + ret = 0; + } + dst[ret] = 0; - strncpy(dst, g_argv[0], len); - len -= 32; // reserve - if (len < 0) len = 0; - dst[len] = 0; for (j = strlen(dst); j > 0; j--) - if (dst[j] == '/') { dst[j+1] = 0; break; } + if (dst[j] == '/') { + dst[++j] = 0; + break; + } - return j + 1; + return j; } #ifdef __GP2X__ @@ -108,3 +135,78 @@ int plat_wait_event(int *fds_hnds, int count, int timeout_ms) return ret; } +void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) +{ + static int hugetlb_warned; + int prot = PROT_READ | PROT_WRITE; + int flags = MAP_PRIVATE | MAP_ANONYMOUS; + void *req, *ret; + + req = (void *)addr; + if (need_exec) + prot |= PROT_EXEC; + if (is_fixed) + flags |= MAP_FIXED; + if (size >= HUGETLB_THRESHOLD) + flags |= MAP_HUGETLB; + + ret = mmap(req, size, prot, flags, -1, 0); + if (ret == MAP_FAILED && (flags & MAP_HUGETLB)) { + if (!hugetlb_warned) { + fprintf(stderr, + "warning: failed to do hugetlb mmap (%p, %zu): %d\n", + req, size, errno); + hugetlb_warned = 1; + } + flags &= ~MAP_HUGETLB; + ret = mmap(req, size, prot, flags, -1, 0); + } + if (ret == MAP_FAILED) + return NULL; + + if (req != NULL && ret != req) + fprintf(stderr, + "warning: mmaped to %p, requested %p\n", ret, req); + + return ret; +} + +void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) +{ + void *ret; + + ret = mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE); + if (ret == MAP_FAILED) + return NULL; + if (ret != ptr) + printf("warning: mremap moved: %p -> %p\n", ptr, ret); + + return ret; +} + +void plat_munmap(void *ptr, size_t size) +{ + int ret; + + ret = munmap(ptr, size); + if (ret != 0 && (size & (HUGETLB_PAGESIZE - 1))) { + // prehaps an autorounded hugetlb mapping? + size = (size + HUGETLB_PAGESIZE - 1) & ~(HUGETLB_PAGESIZE - 1); + ret = munmap(ptr, size); + } + if (ret != 0) { + fprintf(stderr, + "munmap(%p, %zu) failed: %d\n", ptr, size, errno); + } +} + +/* lprintf */ +void lprintf(const char *fmt, ...) +{ + va_list vl; + + va_start(vl, fmt); + vprintf(fmt, vl); + va_end(vl); +} +