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