9 #define ONEMASK ((size_t)(-1) / 0xFF)
25 for (s = _s; (uintptr_t)(s) & (
sizeof(size_t) - 1); s++) {
27 if (b ==
'\0')
goto done;
28 count += (b >> 7) & ((~b) >> 6);
31 for (; ; s +=
sizeof(size_t)) {
32 __builtin_prefetch(&s[256], 0, 0);
37 u = ((u & (
ONEMASK * 0x80)) >> 7) & ((~u) >> 6);
38 count += (u *
ONEMASK) >> ((
sizeof(
size_t) - 1) * 8);
45 count += (b >> 7) & ((~b) >> 6);
48 return ((s - _s) - count);
53 #include <sys/unistd.h>
54 #define PN_ALIGN(o, x) (((((o) - 1) / (x)) + 1) * (x))
58 void *mem = VirtualAlloc(NULL, length, MEM_COMMIT,
59 exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
63 SYSTEM_INFO SystemInfo;
66 GetSystemInfo(&SystemInfo);
67 psz = SystemInfo.dwAllocationGranularity;
68 high = (size_t)SystemInfo.lpMaximumApplicationAddress -
PN_ALIGN(length, psz) + 1;
70 fprintf(stderr,
"** potion_mmap(%ld%s) failed, try last page at 0x%x\n", length, exec ?
",exec" :
"", high);
72 mem = VirtualAlloc((
void*)high, length, MEM_COMMIT,
73 exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
75 fprintf(stderr,
"** potion_mmap(%ld%s) failed\n", length, exec ?
",exec" :
"");
83 return VirtualFree(mem, len, MEM_DECOMMIT) != 0 ? 0 : -1;
91 int prot = exec ? PROT_EXEC : 0;
92 void *mem = mmap(NULL, length, prot|PROT_READ|PROT_WRITE,
93 (MAP_PRIVATE|MAP_ANON), -1, 0);
94 if (mem == MAP_FAILED) {
95 fprintf(stderr,
"** potion_mmap(%ld%s) failed\n", (
long)length, exec ?
",exec" :
"");
103 return munmap(mem, len);
109 int vasprintf (
char **strp,
const char *fmt, va_list ap)
114 int len = vsnprintf (NULL, 0, fmt, ap) + 1;
115 char *res = (
char *)malloc((
unsigned int)len);
119 return vsnprintf(res, (
unsigned int)len, fmt, ap);
124 asprintf (
char **string_ptr,
const char *format, ...)
129 va_start (arg, format);
130 done = vasprintf (string_ptr, format, arg);
void * potion_mmap(size_t length, const char exec)
int potion_munmap(void *mem, size_t len)
size_t potion_cp_strlen_utf8(const char *_s)
wonderful utf-8 counting trickery by colin percival