3030
3131#include "py/mpconfig.h"
3232#include "py/misc.h"
33+ #include "py/mpstate.h"
3334
3435#if 0 // print debugging info
3536#define DEBUG_printf DEBUG_printf
3839#endif
3940
4041#if MICROPY_MEM_STATS
41- STATIC size_t total_bytes_allocated = 0 ;
42- STATIC size_t current_bytes_allocated = 0 ;
43- STATIC size_t peak_bytes_allocated = 0 ;
44-
45- #define UPDATE_PEAK () { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
42+ #define UPDATE_PEAK () { if (MP_STATE_MEM(current_bytes_allocated) > MP_STATE_MEM(peak_bytes_allocated)) MP_STATE_MEM(peak_bytes_allocated) = MP_STATE_MEM(current_bytes_allocated); }
4643#endif
4744
4845#if MICROPY_ENABLE_GC
@@ -68,8 +65,8 @@ void *m_malloc(size_t num_bytes) {
6865 return m_malloc_fail (num_bytes );
6966 }
7067#if MICROPY_MEM_STATS
71- total_bytes_allocated += num_bytes ;
72- current_bytes_allocated += num_bytes ;
68+ MP_STATE_MEM ( total_bytes_allocated ) += num_bytes ;
69+ MP_STATE_MEM ( current_bytes_allocated ) += num_bytes ;
7370 UPDATE_PEAK ();
7471#endif
7572 DEBUG_printf ("malloc %d : %p\n" , num_bytes , ptr );
@@ -79,8 +76,8 @@ void *m_malloc(size_t num_bytes) {
7976void * m_malloc_maybe (size_t num_bytes ) {
8077 void * ptr = malloc (num_bytes );
8178#if MICROPY_MEM_STATS
82- total_bytes_allocated += num_bytes ;
83- current_bytes_allocated += num_bytes ;
79+ MP_STATE_MEM ( total_bytes_allocated ) += num_bytes ;
80+ MP_STATE_MEM ( current_bytes_allocated ) += num_bytes ;
8481 UPDATE_PEAK ();
8582#endif
8683 DEBUG_printf ("malloc %d : %p\n" , num_bytes , ptr );
@@ -94,8 +91,8 @@ void *m_malloc_with_finaliser(size_t num_bytes) {
9491 return m_malloc_fail (num_bytes );
9592 }
9693#if MICROPY_MEM_STATS
97- total_bytes_allocated += num_bytes ;
98- current_bytes_allocated += num_bytes ;
94+ MP_STATE_MEM ( total_bytes_allocated ) += num_bytes ;
95+ MP_STATE_MEM ( current_bytes_allocated ) += num_bytes ;
9996 UPDATE_PEAK ();
10097#endif
10198 DEBUG_printf ("malloc %d : %p\n" , num_bytes , ptr );
@@ -124,8 +121,8 @@ void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
124121 // allocated total. If we process only positive increments,
125122 // we'll count 3K.
126123 size_t diff = new_num_bytes - old_num_bytes ;
127- total_bytes_allocated += diff ;
128- current_bytes_allocated += diff ;
124+ MP_STATE_MEM ( total_bytes_allocated ) += diff ;
125+ MP_STATE_MEM ( current_bytes_allocated ) += diff ;
129126 UPDATE_PEAK ();
130127#endif
131128 DEBUG_printf ("realloc %p, %d, %d : %p\n" , ptr , old_num_bytes , new_num_bytes , new_ptr );
@@ -143,8 +140,8 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
143140 // Also, don't count failed reallocs.
144141 if (!(new_ptr == NULL && new_num_bytes != 0 )) {
145142 size_t diff = new_num_bytes - old_num_bytes ;
146- total_bytes_allocated += diff ;
147- current_bytes_allocated += diff ;
143+ MP_STATE_MEM ( total_bytes_allocated ) += diff ;
144+ MP_STATE_MEM ( current_bytes_allocated ) += diff ;
148145 UPDATE_PEAK ();
149146 }
150147#endif
@@ -155,21 +152,21 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
155152void m_free (void * ptr , size_t num_bytes ) {
156153 free (ptr );
157154#if MICROPY_MEM_STATS
158- current_bytes_allocated -= num_bytes ;
155+ MP_STATE_MEM ( current_bytes_allocated ) -= num_bytes ;
159156#endif
160157 DEBUG_printf ("free %p, %d\n" , ptr , num_bytes );
161158}
162159
163160#if MICROPY_MEM_STATS
164161size_t m_get_total_bytes_allocated (void ) {
165- return total_bytes_allocated ;
162+ return MP_STATE_MEM ( total_bytes_allocated ) ;
166163}
167164
168165size_t m_get_current_bytes_allocated (void ) {
169- return current_bytes_allocated ;
166+ return MP_STATE_MEM ( current_bytes_allocated ) ;
170167}
171168
172169size_t m_get_peak_bytes_allocated (void ) {
173- return peak_bytes_allocated ;
170+ return MP_STATE_MEM ( peak_bytes_allocated ) ;
174171}
175172#endif
0 commit comments