| 1 | /* dex-platform.c |
| 2 | * |
| 3 | * Copyright 2022 Christian Hergert <[email protected]> |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Lesser General Public License as |
| 7 | * published by the Free Software Foundation; either version 2.1 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include "dex-platform.h" |
| 24 | |
| 25 | #ifdef G_OS_WIN32 |
| 26 | # include <windows.h> |
| 27 | #else |
| 28 | # include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | gsize |
| 32 | dex_get_page_size (void) |
| 33 | { |
| 34 | static gsize page_size; |
| 35 | |
| 36 | if G_UNLIKELY (page_size == 0) |
| 37 | { |
| 38 | #ifdef G_OS_WIN32 |
| 39 | SYSTEM_INFO si; |
| 40 | GetSystemInfo (&si); |
| 41 | page_size = si.dwPageSize; |
| 42 | #else |
| 43 | page_size = sysconf (_SC_PAGESIZE); |
| 44 | #endif |
| 45 | } |
| 46 | |
| 47 | return page_size; |
| 48 | } |
| 49 | |
| 50 | gsize |
| 51 | dex_get_min_stack_size (void) |
| 52 | { |
| 53 | static gsize min_stack_size; |
| 54 | if G_UNLIKELY (min_stack_size == 0) |
| 55 | { |
| 56 | #ifdef G_OS_WIN32 |
| 57 | /* Probably need to base this on granularity or something, |
| 58 | * because the default stack size of 1MB is likely too much. |
| 59 | */ |
| 60 | min_stack_size = 4096*16; |
| 61 | #else |
| 62 | long sc_thread_stack_min; |
| 63 | |
| 64 | /* On FreeBSD we can get 2048 for min-stack-size which |
| 65 | * doesn't really work well for pretty much anything. |
| 66 | * Even when we round up to 4096 we trivially hit the |
| 67 | * guard page. Perhaps something still needs to be |
| 68 | * fixed in DexStack to ensure we don't write somewhere |
| 69 | * we shouldn't, but the easiest thing to do right now |
| 70 | * is to just enforce 2+pages + guard page. |
| 71 | */ |
| 72 | min_stack_size = dex_get_page_size () * 2; |
| 73 | |
| 74 | sc_thread_stack_min = sysconf (_SC_THREAD_STACK_MIN); |
| 75 | if (sc_thread_stack_min != -1 && sc_thread_stack_min > min_stack_size) |
| 76 | min_stack_size = sc_thread_stack_min; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | return min_stack_size; |
| 81 | } |