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.
This commit is contained in:
Damien George
2014-09-23 18:10:17 +01:00
parent ac04a8a56a
commit b0261341d3
5 changed files with 80 additions and 101 deletions

View File

@@ -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