Merge pull request #4630 from BrzVlad/feature-valloc-limit
[mono-project.git] / mono / utils / mono-threads-android.c
blobc8c17a3d3cfc1cda13b0f7a25ce4d47340d2f14a
1 /**
2 * \file
3 */
5 #include <config.h>
7 #if defined(PLATFORM_ANDROID)
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <inttypes.h>
12 #include "glib.h"
14 static void
15 slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
17 char buff [1024];
18 FILE *f = fopen ("/proc/self/maps", "r");
19 if (!f)
20 g_error ("Could not determine thread bounds, failed to open /proc/self/maps");
22 while (fgets (buff, sizeof (buff), f)) {
23 intmax_t low, high;
24 char *ptr = buff;
25 char *end = NULL;
26 //each line starts with the range we want: f7648000-f7709000
27 low = strtoimax (ptr, &end, 16);
28 if (end) {
29 ptr = end + 1; //skip the dash to make sure we don't get a negative number
30 end = NULL;
31 high = strtoimax (ptr, &end, 16);
33 if (end && low <= (intmax_t)(size_t)current && high > (intmax_t)(size_t)current) {
34 *staddr = (guint8 *)(size_t)low;
35 *stsize = (size_t)(high - low);
36 fclose (f);
37 return;
40 g_error ("Could not determine thread bounds, failed to find current stack pointer in /proc/self/maps");
43 void
44 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
46 pthread_attr_t attr;
47 guint8 *current = (guint8*)&attr;
49 *staddr = NULL;
50 *stsize = (size_t)-1;
52 pthread_getattr_np (pthread_self (), &attr);
53 pthread_attr_getstack (&attr, (void**)staddr, stsize);
54 pthread_attr_destroy (&attr);
56 if (*staddr && ((current <= *staddr) || (current > *staddr + *stsize)))
57 slow_get_thread_bounds (current, staddr, stsize);
60 #endif