Branch data Line data Source code
1 : : /* 2 : : * This file is part of the MicroPython project, http://micropython.org/ 3 : : * 4 : : * The MIT License (MIT) 5 : : * 6 : : * Copyright (c) 2014 Paul Sokolovsky 7 : : * Copyright (c) 2024 Angus Gratton 8 : : * 9 : : * Permission is hereby granted, free of charge, to any person obtaining a copy 10 : : * of this software and associated documentation files (the "Software"), to deal 11 : : * in the Software without restriction, including without limitation the rights 12 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 : : * copies of the Software, and to permit persons to whom the Software is 14 : : * furnished to do so, subject to the following conditions: 15 : : * 16 : : * The above copyright notice and this permission notice shall be included in 17 : : * all copies or substantial portions of the Software. 18 : : * 19 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 : : * THE SOFTWARE. 26 : : */ 27 : : #ifndef MICROPY_INCLUDED_PY_CSTACK_H 28 : : #define MICROPY_INCLUDED_PY_CSTACK_H 29 : : 30 : : #include "py/mpstate.h" 31 : : 32 : : // Both init functions below accept the full stack size. Set the 33 : : // MICROPY_STACK_CHECK_MARGIN to the number of bytes subtracted to account 34 : : // for stack usage between checks. 35 : : 36 : : void mp_cstack_init_with_sp_here(size_t stack_size); 37 : : 38 : 4010 : inline static void mp_cstack_init_with_top(void *top, size_t stack_size) { 39 : 4010 : MP_STATE_THREAD(stack_top) = (char *)top; 40 : : 41 : : #if MICROPY_STACK_CHECK 42 [ - + ]: 4010 : assert(stack_size > MICROPY_STACK_CHECK_MARGIN); // Should be enforced by port 43 : 4010 : MP_STATE_THREAD(stack_limit) = stack_size - MICROPY_STACK_CHECK_MARGIN; 44 : : #else 45 : : (void)stack_size; 46 : : #endif 47 : 4010 : } 48 : : 49 : : mp_uint_t mp_cstack_usage(void); 50 : : 51 : : #if MICROPY_STACK_CHECK 52 : : 53 : : void mp_cstack_check(void); 54 : : 55 : : #else 56 : : 57 : : inline static void mp_cstack_check(void) { 58 : : // No-op when stack checking is disabled 59 : : } 60 : : 61 : : #endif 62 : : 63 : : #endif // MICROPY_INCLUDED_PY_CSTACK_H