aboutsummaryrefslogtreecommitdiff
path: root/py/malloc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-01-01 23:30:53 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-07 20:33:00 +0000
commitb4b10fd350852e321624d74983cca286091b55a1 (patch)
tree7ac4aa40d70be0170a61f649e9d73c42faa4ba33 /py/malloc.c
parentad2307c92c15f0aa90dbd0741fd2538719d0b5e1 (diff)
py: Put all global state together in state structures.
This patch consolidates all global variables in py/ core into one place, in a global structure. Root pointers are all located together to make GC tracing easier and more efficient.
Diffstat (limited to 'py/malloc.c')
-rw-r--r--py/malloc.c35
1 files changed, 16 insertions, 19 deletions
diff --git a/py/malloc.c b/py/malloc.c
index 10e3566e8..be2c0db02 100644
--- a/py/malloc.c
+++ b/py/malloc.c
@@ -30,6 +30,7 @@
#include "py/mpconfig.h"
#include "py/misc.h"
+#include "py/mpstate.h"
#if 0 // print debugging info
#define DEBUG_printf DEBUG_printf
@@ -38,11 +39,7 @@
#endif
#if MICROPY_MEM_STATS
-STATIC size_t total_bytes_allocated = 0;
-STATIC size_t current_bytes_allocated = 0;
-STATIC size_t peak_bytes_allocated = 0;
-
-#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
+#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); }
#endif
#if MICROPY_ENABLE_GC
@@ -68,8 +65,8 @@ void *m_malloc(size_t num_bytes) {
return m_malloc_fail(num_bytes);
}
#if MICROPY_MEM_STATS
- total_bytes_allocated += num_bytes;
- current_bytes_allocated += num_bytes;
+ MP_STATE_MEM(total_bytes_allocated) += num_bytes;
+ MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
@@ -79,8 +76,8 @@ void *m_malloc(size_t num_bytes) {
void *m_malloc_maybe(size_t num_bytes) {
void *ptr = malloc(num_bytes);
#if MICROPY_MEM_STATS
- total_bytes_allocated += num_bytes;
- current_bytes_allocated += num_bytes;
+ MP_STATE_MEM(total_bytes_allocated) += num_bytes;
+ MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
@@ -94,8 +91,8 @@ void *m_malloc_with_finaliser(size_t num_bytes) {
return m_malloc_fail(num_bytes);
}
#if MICROPY_MEM_STATS
- total_bytes_allocated += num_bytes;
- current_bytes_allocated += num_bytes;
+ MP_STATE_MEM(total_bytes_allocated) += num_bytes;
+ MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
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) {
// allocated total. If we process only positive increments,
// we'll count 3K.
size_t diff = new_num_bytes - old_num_bytes;
- total_bytes_allocated += diff;
- current_bytes_allocated += diff;
+ MP_STATE_MEM(total_bytes_allocated) += diff;
+ MP_STATE_MEM(current_bytes_allocated) += diff;
UPDATE_PEAK();
#endif
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) {
// Also, don't count failed reallocs.
if (!(new_ptr == NULL && new_num_bytes != 0)) {
size_t diff = new_num_bytes - old_num_bytes;
- total_bytes_allocated += diff;
- current_bytes_allocated += diff;
+ MP_STATE_MEM(total_bytes_allocated) += diff;
+ MP_STATE_MEM(current_bytes_allocated) += diff;
UPDATE_PEAK();
}
#endif
@@ -155,21 +152,21 @@ void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
void m_free(void *ptr, size_t num_bytes) {
free(ptr);
#if MICROPY_MEM_STATS
- current_bytes_allocated -= num_bytes;
+ MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
#endif
DEBUG_printf("free %p, %d\n", ptr, num_bytes);
}
#if MICROPY_MEM_STATS
size_t m_get_total_bytes_allocated(void) {
- return total_bytes_allocated;
+ return MP_STATE_MEM(total_bytes_allocated);
}
size_t m_get_current_bytes_allocated(void) {
- return current_bytes_allocated;
+ return MP_STATE_MEM(current_bytes_allocated);
}
size_t m_get_peak_bytes_allocated(void) {
- return peak_bytes_allocated;
+ return MP_STATE_MEM(peak_bytes_allocated);
}
#endif