diff options
author | Damien George <damien.p.george@gmail.com> | 2014-09-23 18:10:17 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-09-25 15:49:26 +0100 |
commit | b0261341d3706329ea58d8fc1a5ff9c788c9ccf7 (patch) | |
tree | 97aed92dcb6bcf516c258ea56d525f1ed7d4c50f /py/malloc.c | |
parent | ac04a8a56a1591ddbaf6558b8eab9837a7d2f3cb (diff) |
py: For malloc and vstr functions, use size_t exclusively for int type.
It seems most sensible to use size_t for measuring "number of bytes" in
malloc and vstr functions (since that's what size_t is for). We don't
use mp_uint_t because malloc and vstr are not Micro Python specific.
Diffstat (limited to 'py/malloc.c')
-rw-r--r-- | py/malloc.c | 42 |
1 files changed, 16 insertions, 26 deletions
diff --git a/py/malloc.c b/py/malloc.c index 8e90849e9..4f41c83ef 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -38,9 +38,9 @@ #endif #if MICROPY_MEM_STATS -STATIC int total_bytes_allocated = 0; -STATIC int current_bytes_allocated = 0; -STATIC int peak_bytes_allocated = 0; +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; } #endif @@ -62,7 +62,7 @@ STATIC int peak_bytes_allocated = 0; #define realloc gc_realloc #endif // MICROPY_ENABLE_GC -void *m_malloc(int num_bytes) { +void *m_malloc(size_t num_bytes) { if (num_bytes == 0) { return NULL; } @@ -79,7 +79,7 @@ void *m_malloc(int num_bytes) { return ptr; } -void *m_malloc_maybe(int num_bytes) { +void *m_malloc_maybe(size_t num_bytes) { void *ptr = malloc(num_bytes); if (ptr == NULL) { return NULL; @@ -94,7 +94,7 @@ void *m_malloc_maybe(int num_bytes) { } #if MICROPY_ENABLE_FINALISER -void *m_malloc_with_finaliser(int num_bytes) { +void *m_malloc_with_finaliser(size_t num_bytes) { if (num_bytes == 0) { return NULL; } @@ -112,7 +112,7 @@ void *m_malloc_with_finaliser(int num_bytes) { } #endif -void *m_malloc0(int num_bytes) { +void *m_malloc0(size_t num_bytes) { void *ptr = m_malloc(num_bytes); if (ptr != NULL) { memset(ptr, 0, num_bytes); @@ -120,7 +120,7 @@ void *m_malloc0(int num_bytes) { return ptr; } -void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { +void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) { if (new_num_bytes == 0) { free(ptr); return NULL; @@ -135,7 +135,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { // shrunk to 1K and then grown to 2K again. It's still 2K // allocated total. If we process only positive increments, // we'll count 3K. - int diff = new_num_bytes - old_num_bytes; + size_t diff = new_num_bytes - old_num_bytes; total_bytes_allocated += diff; current_bytes_allocated += diff; UPDATE_PEAK(); @@ -144,7 +144,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { return new_ptr; } -void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) { +void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) { void *new_ptr = realloc(ptr, new_num_bytes); if (new_ptr == NULL) { return NULL; @@ -155,7 +155,7 @@ void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) { // shrunk to 1K and then grown to 2K again. It's still 2K // allocated total. If we process only positive increments, // we'll count 3K. - int diff = new_num_bytes - old_num_bytes; + size_t diff = new_num_bytes - old_num_bytes; total_bytes_allocated += diff; current_bytes_allocated += diff; UPDATE_PEAK(); @@ -164,7 +164,7 @@ void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) { return new_ptr; } -void m_free(void *ptr, int num_bytes) { +void m_free(void *ptr, size_t num_bytes) { if (ptr != NULL) { free(ptr); } @@ -174,26 +174,16 @@ void m_free(void *ptr, int num_bytes) { DEBUG_printf("free %p, %d\n", ptr, num_bytes); } -int m_get_total_bytes_allocated(void) { #if MICROPY_MEM_STATS +size_t m_get_total_bytes_allocated(void) { return total_bytes_allocated; -#else - return -1; -#endif } -int m_get_current_bytes_allocated(void) { -#if MICROPY_MEM_STATS +size_t m_get_current_bytes_allocated(void) { return current_bytes_allocated; -#else - return -1; -#endif } -int m_get_peak_bytes_allocated(void) { -#if MICROPY_MEM_STATS +size_t m_get_peak_bytes_allocated(void) { return peak_bytes_allocated; -#else - return -1; -#endif } +#endif |