少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * \file
 */
 
#include <config.h>
 
#if defined(HOST_ANDROID)
 
#include <pthread.h>
#include <stdio.h>
#include <inttypes.h>
#include "glib.h"
 
static void
slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
{
    char buff [1024];
    FILE *f = fopen ("/proc/self/maps", "r");
    if (!f)
        g_error ("Could not determine thread bounds, failed to open /proc/self/maps");
 
    while (fgets (buff, sizeof (buff), f)) {
        intmax_t low, high;
        char *ptr = buff;
        char *end = NULL;
        //each line starts with the range we want: f7648000-f7709000
        low = strtoimax (ptr, &end, 16);
        if (end) {
            ptr = end + 1; //skip the dash to make sure we don't get a negative number
            end = NULL;
            high = strtoimax (ptr, &end, 16);
        }
        if (end && low <= (intmax_t)(size_t)current && high > (intmax_t)(size_t)current) {
            *staddr = (guint8 *)(size_t)low;
            *stsize = (size_t)(high - low);
            fclose (f);
            return;
        }
    }
    g_error ("Could not determine thread bounds, failed to find current stack pointer in /proc/self/maps");
}
 
void
mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
{
    pthread_attr_t attr;
    guint8 *current = (guint8*)&attr;
 
    *staddr = NULL;
    *stsize = (size_t)-1;
 
    pthread_getattr_np (pthread_self (), &attr);
    pthread_attr_getstack (&attr, (void**)staddr, stsize);
    pthread_attr_destroy (&attr);
 
    if (*staddr && ((current <= *staddr) || (current > *staddr + *stsize)))
        slow_get_thread_bounds (current, staddr, stsize);
}
 
#endif