From b0261341d3706329ea58d8fc1a5ff9c788c9ccf7 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 23 Sep 2014 18:10:17 +0100
Subject: [PATCH] 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.
---
 py/malloc.c  | 42 ++++++++++++-------------------
 py/misc.h    | 70 ++++++++++++++++++++++++++--------------------------
 py/runtime.c |  4 +--
 py/vstr.c    | 55 +++++++++++++++--------------------------
 unix/main.c  | 12 ++++++---
 5 files changed, 81 insertions(+), 102 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
diff --git a/py/misc.h b/py/misc.h
index 94a3ffaaf..860215d62 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -66,18 +66,20 @@ typedef unsigned int uint;
 #define m_del_obj(type, ptr) (m_del(type, ptr, 1))
 #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
 
-void *m_malloc(int num_bytes);
-void *m_malloc_maybe(int num_bytes);
-void *m_malloc_with_finaliser(int num_bytes);
-void *m_malloc0(int num_bytes);
-void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
-void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes);
-void m_free(void *ptr, int num_bytes);
-void *m_malloc_fail(int num_bytes);
-
-int m_get_total_bytes_allocated(void);
-int m_get_current_bytes_allocated(void);
-int m_get_peak_bytes_allocated(void);
+void *m_malloc(size_t num_bytes);
+void *m_malloc_maybe(size_t num_bytes);
+void *m_malloc_with_finaliser(size_t num_bytes);
+void *m_malloc0(size_t num_bytes);
+void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
+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);
+void *m_malloc_fail(size_t num_bytes);
+
+#if MICROPY_MEM_STATS
+size_t m_get_total_bytes_allocated(void);
+size_t m_get_current_bytes_allocated(void);
+size_t m_get_peak_bytes_allocated(void);
+#endif
 
 /** array helpers ***********************************************/
 
@@ -110,8 +112,8 @@ mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
 /** variable string *********************************************/
 
 typedef struct _vstr_t {
-    uint alloc;
-    uint len;
+    size_t alloc;
+    size_t len;
     char *buf;
     bool had_error : 1;
     bool fixed_buf : 1;
@@ -120,40 +122,38 @@ typedef struct _vstr_t {
 // convenience macro to declare a vstr with a fixed size buffer on the stack
 #define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
 
-void vstr_init(vstr_t *vstr, int alloc);
-void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
+void vstr_init(vstr_t *vstr, size_t alloc);
+void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
 void vstr_clear(vstr_t *vstr);
 vstr_t *vstr_new(void);
-vstr_t *vstr_new_size(int alloc);
+vstr_t *vstr_new_size(size_t alloc);
 void vstr_free(vstr_t *vstr);
 void vstr_reset(vstr_t *vstr);
 bool vstr_had_error(vstr_t *vstr);
 char *vstr_str(vstr_t *vstr);
-int vstr_len(vstr_t *vstr);
-void vstr_hint_size(vstr_t *vstr, int size);
-char *vstr_extend(vstr_t *vstr, int size);
-bool vstr_set_size(vstr_t *vstr, int size);
+size_t vstr_len(vstr_t *vstr);
+void vstr_hint_size(vstr_t *vstr, size_t size);
+char *vstr_extend(vstr_t *vstr, size_t size);
+bool vstr_set_size(vstr_t *vstr, size_t size);
 bool vstr_shrink(vstr_t *vstr);
-char *vstr_add_len(vstr_t *vstr, int len);
+char *vstr_add_len(vstr_t *vstr, size_t len);
 void vstr_add_byte(vstr_t *vstr, byte v);
 void vstr_add_char(vstr_t *vstr, unichar chr);
 void vstr_add_str(vstr_t *vstr, const char *str);
-void vstr_add_strn(vstr_t *vstr, const char *str, int len);
-//void vstr_add_le16(vstr_t *vstr, unsigned short v);
-//void vstr_add_le32(vstr_t *vstr, unsigned int v);
-void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b);
-void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr);
-void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut);
-void vstr_cut_tail_bytes(vstr_t *vstr, uint bytes_to_cut);
-void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut);
+void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
+void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
+void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
+void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
+void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
+void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
 void vstr_printf(vstr_t *vstr, const char *fmt, ...);
 
 /** non-dynamic size-bounded variable buffer/string *************/
 
-#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
+#define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
 #define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
 #define CHECKBUF_APPEND(buf, src, src_len) \
-        { int l = MIN(src_len, buf##_len); \
+        { size_t l = MIN(src_len, buf##_len); \
         memcpy(buf##_p, src, l); \
         buf##_len -= l; \
         buf##_p += l; }
@@ -167,15 +167,15 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
 // Debugging helpers
 int DEBUG_printf(const char *fmt, ...);
 
-extern uint mp_verbose_flag;
+extern mp_uint_t mp_verbose_flag;
 
 // This is useful for unicode handling. Some CPU archs has
 // special instructions for efficient implentation of this
 // function (e.g. CLZ on ARM).
 // NOTE: this function is unused at the moment
 #ifndef count_lead_ones
-static inline uint count_lead_ones(byte val) {
-    uint c = 0;
+static inline mp_uint_t count_lead_ones(byte val) {
+    mp_uint_t c = 0;
     for (byte mask = 0x80; val & mask; mask >>= 1) {
         c++;
     }
diff --git a/py/runtime.c b/py/runtime.c
index 0db8093bc..403e98c7c 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -1146,8 +1146,8 @@ void mp_globals_set(mp_obj_dict_t *d) {
     dict_globals = d;
 }
 
-void *m_malloc_fail(int num_bytes) {
-    DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
+void *m_malloc_fail(size_t num_bytes) {
+    DEBUG_printf("memory allocation failed, allocating " UINT_FMT " bytes\n", num_bytes);
     nlr_raise((mp_obj_t)&mp_const_MemoryError_obj);
 }
 
diff --git a/py/vstr.c b/py/vstr.c
index 9ccc95d49..3a021a9ae 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -35,7 +35,7 @@
 // returned value is always at least 1 greater than argument
 #define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
 
-void vstr_init(vstr_t *vstr, int alloc) {
+void vstr_init(vstr_t *vstr, size_t alloc) {
     if (alloc < 2) {
         // need at least 1 byte for the null byte at the end
         alloc = 2;
@@ -52,7 +52,7 @@ void vstr_init(vstr_t *vstr, int alloc) {
     vstr->fixed_buf = false;
 }
 
-void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf) {
+void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
     assert(alloc > 0); // need at least room for the null byte
     vstr->alloc = alloc;
     vstr->len = 0;
@@ -78,7 +78,7 @@ vstr_t *vstr_new(void) {
     return vstr;
 }
 
-vstr_t *vstr_new_size(int alloc) {
+vstr_t *vstr_new_size(size_t alloc) {
     vstr_t *vstr = m_new(vstr_t, 1);
     if (vstr == NULL) {
         return NULL;
@@ -113,15 +113,15 @@ char *vstr_str(vstr_t *vstr) {
     return vstr->buf;
 }
 
-int vstr_len(vstr_t *vstr) {
+size_t vstr_len(vstr_t *vstr) {
     if (vstr->had_error) {
         return 0;
     }
     return vstr->len;
 }
 
-// Extend vstr strictly to by requested size, return pointer to newly added chunk
-char *vstr_extend(vstr_t *vstr, int size) {
+// Extend vstr strictly by requested size, return pointer to newly added chunk
+char *vstr_extend(vstr_t *vstr, size_t size) {
     if (vstr->fixed_buf) {
         return NULL;
     }
@@ -137,7 +137,7 @@ char *vstr_extend(vstr_t *vstr, int size) {
 }
 
 // Shrink vstr to be given size
-bool vstr_set_size(vstr_t *vstr, int size) {
+bool vstr_set_size(vstr_t *vstr, size_t size) {
     if (vstr->fixed_buf) {
         return false;
     }
@@ -156,12 +156,12 @@ bool vstr_shrink(vstr_t *vstr) {
     return vstr_set_size(vstr, vstr->len);
 }
 
-STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) {
+STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
     if (vstr->len + size + 1 > vstr->alloc) {
         if (vstr->fixed_buf) {
             return false;
         }
-        int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
+        size_t new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
         char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
         if (new_buf == NULL) {
             vstr->had_error = true;
@@ -173,14 +173,14 @@ STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) {
     return true;
 }
 
-void vstr_hint_size(vstr_t *vstr, int size) {
+void vstr_hint_size(vstr_t *vstr, size_t size) {
     // it's not an error if we fail to allocate for the size hint
     bool er = vstr->had_error;
     vstr_ensure_extra(vstr, size);
     vstr->had_error = er;
 }
 
-char *vstr_add_len(vstr_t *vstr, int len) {
+char *vstr_add_len(vstr_t *vstr, size_t len) {
     if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
         return NULL;
     }
@@ -247,7 +247,7 @@ void vstr_add_str(vstr_t *vstr, const char *str) {
     vstr_add_strn(vstr, str, strlen(str));
 }
 
-void vstr_add_strn(vstr_t *vstr, const char *str, int len) {
+void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
     if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
         // if buf is fixed, we got here because there isn't enough room left
         // so just try to copy as much as we can, with room for null byte
@@ -281,11 +281,11 @@ void vstr_add_le32(vstr_t *vstr, unsigned int v) {
 }
 */
 
-char *vstr_ins_blank_bytes(vstr_t *vstr, uint byte_pos, uint byte_len) {
+char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
     if (vstr->had_error) {
         return NULL;
     }
-    uint l = vstr->len;
+    size_t l = vstr->len;
     if (byte_pos > l) {
         byte_pos = l;
     }
@@ -302,14 +302,14 @@ char *vstr_ins_blank_bytes(vstr_t *vstr, uint byte_pos, uint byte_len) {
     return vstr->buf + byte_pos;
 }
 
-void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b) {
+void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
     char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
     if (s != NULL) {
         *s = b;
     }
 }
 
-void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) {
+void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
     // TODO UNICODE
     char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
     if (s != NULL) {
@@ -317,11 +317,11 @@ void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) {
     }
 }
 
-void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut) {
+void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
     vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
 }
 
-void vstr_cut_tail_bytes(vstr_t *vstr, uint len) {
+void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
     if (vstr->had_error) {
         return;
     }
@@ -333,7 +333,7 @@ void vstr_cut_tail_bytes(vstr_t *vstr, uint len) {
     vstr->buf[vstr->len] = 0;
 }
 
-void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut) {
+void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
     if (vstr->had_error || byte_pos >= vstr->len) {
         return;
     } else if (byte_pos + bytes_to_cut >= vstr->len) {
@@ -361,7 +361,7 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
     while (1) {
         // try to print in the allocated space
         // need to make a copy of the va_list because we may call vsnprintf multiple times
-        int size = vstr->alloc - vstr->len;
+        size_t size = vstr->alloc - vstr->len;
         va_list ap2;
         va_copy(ap2, ap);
         int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2);
@@ -387,18 +387,3 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
         }
     }
 }
-
-/** testing *****************************************************/
-
-/*
-int main(void) {
-    vstr_t *vstr = vstr_new();
-    int i;
-    for (i = 0; i < 10; i++) {
-        vstr_printf(vstr, "%d) this is a test %d %s\n", i, 1234, "'a string'");
-        vstr_add_str(vstr, "-----");
-        vstr_printf(vstr, "this is another test %d %s\n", 1234, "'a second string'");
-        printf("%s", vstr->buf);
-    }
-}
-*/
diff --git a/unix/main.c b/unix/main.c
index f00bf3d33..983afd2a3 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -54,9 +54,9 @@
 #include "stackctrl.h"
 
 // Command line options, with their defaults
-bool compile_only = false;
-uint emit_opt = MP_EMIT_OPT_NONE;
-uint mp_verbose_flag;
+STATIC bool compile_only = false;
+STATIC uint emit_opt = MP_EMIT_OPT_NONE;
+mp_uint_t mp_verbose_flag = 0;
 
 #if MICROPY_ENABLE_GC
 // Heap size of GC heap (if enabled)
@@ -222,8 +222,9 @@ int usage(char **argv) {
     return 1;
 }
 
+#if MICROPY_MEM_STATS
 STATIC mp_obj_t mem_info(void) {
-    printf("mem: total=%d, current=%d, peak=%d\n",
+    printf("mem: total=" UINT_FMT ", current=" UINT_FMT ", peak=" UINT_FMT "\n",
         m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
     printf("stack: " UINT_FMT "\n", mp_stack_usage());
 #if MICROPY_ENABLE_GC
@@ -232,6 +233,7 @@ STATIC mp_obj_t mem_info(void) {
     return mp_const_none;
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info);
+#endif
 
 STATIC mp_obj_t qstr_info(void) {
     uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
@@ -325,7 +327,9 @@ int main(int argc, char **argv) {
 
     mp_obj_list_init(mp_sys_argv, 0);
 
+    #if MICROPY_MEM_STATS
     mp_store_name(qstr_from_str("mem_info"), (mp_obj_t*)&mem_info_obj);
+    #endif
     mp_store_name(qstr_from_str("qstr_info"), (mp_obj_t*)&qstr_info_obj);
 
     // Here is some example code to create a class and instance of that class.
-- 
GitLab