1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2017 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 #ifndef MICROPY_INCLUDED_PY_PYSTACK_H
27 #define MICROPY_INCLUDED_PY_PYSTACK_H
28 
29 #include "py/mpstate.h"
30 
31 // Enable this debugging option to check that the amount of memory freed is
32 // consistent with amounts that were previously allocated.
33 #define MP_PYSTACK_DEBUG (0)
34 
35 #if MICROPY_ENABLE_PYSTACK
36 
37 void mp_pystack_init(void *start, void *end);
38 void *mp_pystack_alloc(size_t n_bytes);
39 
40 // This function can free multiple continuous blocks at once: just pass the
41 // pointer to the block that was allocated first and it and all subsequently
42 // allocated blocks will be freed.
mp_pystack_free(void * ptr)43 static inline void mp_pystack_free(void *ptr) {
44     assert((uint8_t *)ptr >= MP_STATE_THREAD(pystack_start));
45     assert((uint8_t *)ptr <= MP_STATE_THREAD(pystack_cur));
46     #if MP_PYSTACK_DEBUG
47     size_t n_bytes_to_free = MP_STATE_THREAD(pystack_cur) - (uint8_t *)ptr;
48     size_t n_bytes = *(size_t *)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN);
49     while (n_bytes < n_bytes_to_free) {
50         n_bytes += *(size_t *)(MP_STATE_THREAD(pystack_cur) - n_bytes - MICROPY_PYSTACK_ALIGN);
51     }
52     if (n_bytes != n_bytes_to_free) {
53         mp_printf(&mp_plat_print, "mp_pystack_free() failed: %u != %u\n", (uint)n_bytes_to_free,
54             (uint)*(size_t *)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN));
55         assert(0);
56     }
57     #endif
58     MP_STATE_THREAD(pystack_cur) = (uint8_t *)ptr;
59 }
60 
mp_pystack_realloc(void * ptr,size_t n_bytes)61 static inline void mp_pystack_realloc(void *ptr, size_t n_bytes) {
62     mp_pystack_free(ptr);
63     mp_pystack_alloc(n_bytes);
64 }
65 
mp_pystack_usage(void)66 static inline size_t mp_pystack_usage(void) {
67     return MP_STATE_THREAD(pystack_cur) - MP_STATE_THREAD(pystack_start);
68 }
69 
mp_pystack_limit(void)70 static inline size_t mp_pystack_limit(void) {
71     return MP_STATE_THREAD(pystack_end) - MP_STATE_THREAD(pystack_start);
72 }
73 
74 #endif
75 
76 #if !MICROPY_ENABLE_PYSTACK
77 
78 #define mp_local_alloc(n_bytes) alloca(n_bytes)
79 
mp_local_free(void * ptr)80 static inline void mp_local_free(void *ptr) {
81     (void)ptr;
82 }
83 
mp_nonlocal_alloc(size_t n_bytes)84 static inline void *mp_nonlocal_alloc(size_t n_bytes) {
85     return m_new(uint8_t, n_bytes);
86 }
87 
mp_nonlocal_realloc(void * ptr,size_t old_n_bytes,size_t new_n_bytes)88 static inline void *mp_nonlocal_realloc(void *ptr, size_t old_n_bytes, size_t new_n_bytes) {
89     return m_renew(uint8_t, ptr, old_n_bytes, new_n_bytes);
90 }
91 
mp_nonlocal_free(void * ptr,size_t n_bytes)92 static inline void mp_nonlocal_free(void *ptr, size_t n_bytes) {
93     m_del(uint8_t, ptr, n_bytes);
94 }
95 
96 #else
97 
mp_local_alloc(size_t n_bytes)98 static inline void *mp_local_alloc(size_t n_bytes) {
99     return mp_pystack_alloc(n_bytes);
100 }
101 
mp_local_free(void * ptr)102 static inline void mp_local_free(void *ptr) {
103     mp_pystack_free(ptr);
104 }
105 
mp_nonlocal_alloc(size_t n_bytes)106 static inline void *mp_nonlocal_alloc(size_t n_bytes) {
107     return mp_pystack_alloc(n_bytes);
108 }
109 
mp_nonlocal_realloc(void * ptr,size_t old_n_bytes,size_t new_n_bytes)110 static inline void *mp_nonlocal_realloc(void *ptr, size_t old_n_bytes, size_t new_n_bytes) {
111     (void)old_n_bytes;
112     mp_pystack_realloc(ptr, new_n_bytes);
113     return ptr;
114 }
115 
mp_nonlocal_free(void * ptr,size_t n_bytes)116 static inline void mp_nonlocal_free(void *ptr, size_t n_bytes) {
117     (void)n_bytes;
118     mp_pystack_free(ptr);
119 }
120 
121 #endif
122 
123 #endif // MICROPY_INCLUDED_PY_PYSTACK_H
124