Skip to content

Commit b026134

Browse files
committed
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.
1 parent ac04a8a commit b026134

File tree

5 files changed

+81
-102
lines changed

5 files changed

+81
-102
lines changed

py/malloc.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
#endif
3939

4040
#if MICROPY_MEM_STATS
41-
STATIC int total_bytes_allocated = 0;
42-
STATIC int current_bytes_allocated = 0;
43-
STATIC int peak_bytes_allocated = 0;
41+
STATIC size_t total_bytes_allocated = 0;
42+
STATIC size_t current_bytes_allocated = 0;
43+
STATIC size_t peak_bytes_allocated = 0;
4444

4545
#define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = current_bytes_allocated; }
4646
#endif
@@ -62,7 +62,7 @@ STATIC int peak_bytes_allocated = 0;
6262
#define realloc gc_realloc
6363
#endif // MICROPY_ENABLE_GC
6464

65-
void *m_malloc(int num_bytes) {
65+
void *m_malloc(size_t num_bytes) {
6666
if (num_bytes == 0) {
6767
return NULL;
6868
}
@@ -79,7 +79,7 @@ void *m_malloc(int num_bytes) {
7979
return ptr;
8080
}
8181

82-
void *m_malloc_maybe(int num_bytes) {
82+
void *m_malloc_maybe(size_t num_bytes) {
8383
void *ptr = malloc(num_bytes);
8484
if (ptr == NULL) {
8585
return NULL;
@@ -94,7 +94,7 @@ void *m_malloc_maybe(int num_bytes) {
9494
}
9595

9696
#if MICROPY_ENABLE_FINALISER
97-
void *m_malloc_with_finaliser(int num_bytes) {
97+
void *m_malloc_with_finaliser(size_t num_bytes) {
9898
if (num_bytes == 0) {
9999
return NULL;
100100
}
@@ -112,15 +112,15 @@ void *m_malloc_with_finaliser(int num_bytes) {
112112
}
113113
#endif
114114

115-
void *m_malloc0(int num_bytes) {
115+
void *m_malloc0(size_t num_bytes) {
116116
void *ptr = m_malloc(num_bytes);
117117
if (ptr != NULL) {
118118
memset(ptr, 0, num_bytes);
119119
}
120120
return ptr;
121121
}
122122

123-
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
123+
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
124124
if (new_num_bytes == 0) {
125125
free(ptr);
126126
return NULL;
@@ -135,7 +135,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
135135
// shrunk to 1K and then grown to 2K again. It's still 2K
136136
// allocated total. If we process only positive increments,
137137
// we'll count 3K.
138-
int diff = new_num_bytes - old_num_bytes;
138+
size_t diff = new_num_bytes - old_num_bytes;
139139
total_bytes_allocated += diff;
140140
current_bytes_allocated += diff;
141141
UPDATE_PEAK();
@@ -144,7 +144,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
144144
return new_ptr;
145145
}
146146

147-
void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) {
147+
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
148148
void *new_ptr = realloc(ptr, new_num_bytes);
149149
if (new_ptr == NULL) {
150150
return NULL;
@@ -155,7 +155,7 @@ void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) {
155155
// shrunk to 1K and then grown to 2K again. It's still 2K
156156
// allocated total. If we process only positive increments,
157157
// we'll count 3K.
158-
int diff = new_num_bytes - old_num_bytes;
158+
size_t diff = new_num_bytes - old_num_bytes;
159159
total_bytes_allocated += diff;
160160
current_bytes_allocated += diff;
161161
UPDATE_PEAK();
@@ -164,7 +164,7 @@ void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) {
164164
return new_ptr;
165165
}
166166

167-
void m_free(void *ptr, int num_bytes) {
167+
void m_free(void *ptr, size_t num_bytes) {
168168
if (ptr != NULL) {
169169
free(ptr);
170170
}
@@ -174,26 +174,16 @@ void m_free(void *ptr, int num_bytes) {
174174
DEBUG_printf("free %p, %d\n", ptr, num_bytes);
175175
}
176176

177-
int m_get_total_bytes_allocated(void) {
178177
#if MICROPY_MEM_STATS
178+
size_t m_get_total_bytes_allocated(void) {
179179
return total_bytes_allocated;
180-
#else
181-
return -1;
182-
#endif
183180
}
184181

185-
int m_get_current_bytes_allocated(void) {
186-
#if MICROPY_MEM_STATS
182+
size_t m_get_current_bytes_allocated(void) {
187183
return current_bytes_allocated;
188-
#else
189-
return -1;
190-
#endif
191184
}
192185

193-
int m_get_peak_bytes_allocated(void) {
194-
#if MICROPY_MEM_STATS
186+
size_t m_get_peak_bytes_allocated(void) {
195187
return peak_bytes_allocated;
196-
#else
197-
return -1;
198-
#endif
199188
}
189+
#endif

py/misc.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ typedef unsigned int uint;
6666
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
6767
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
6868

69-
void *m_malloc(int num_bytes);
70-
void *m_malloc_maybe(int num_bytes);
71-
void *m_malloc_with_finaliser(int num_bytes);
72-
void *m_malloc0(int num_bytes);
73-
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
74-
void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes);
75-
void m_free(void *ptr, int num_bytes);
76-
void *m_malloc_fail(int num_bytes);
77-
78-
int m_get_total_bytes_allocated(void);
79-
int m_get_current_bytes_allocated(void);
80-
int m_get_peak_bytes_allocated(void);
69+
void *m_malloc(size_t num_bytes);
70+
void *m_malloc_maybe(size_t num_bytes);
71+
void *m_malloc_with_finaliser(size_t num_bytes);
72+
void *m_malloc0(size_t num_bytes);
73+
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
74+
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
75+
void m_free(void *ptr, size_t num_bytes);
76+
void *m_malloc_fail(size_t num_bytes);
77+
78+
#if MICROPY_MEM_STATS
79+
size_t m_get_total_bytes_allocated(void);
80+
size_t m_get_current_bytes_allocated(void);
81+
size_t m_get_peak_bytes_allocated(void);
82+
#endif
8183

8284
/** array helpers ***********************************************/
8385

@@ -110,8 +112,8 @@ mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
110112
/** variable string *********************************************/
111113

112114
typedef struct _vstr_t {
113-
uint alloc;
114-
uint len;
115+
size_t alloc;
116+
size_t len;
115117
char *buf;
116118
bool had_error : 1;
117119
bool fixed_buf : 1;
@@ -120,40 +122,38 @@ typedef struct _vstr_t {
120122
// convenience macro to declare a vstr with a fixed size buffer on the stack
121123
#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
122124

123-
void vstr_init(vstr_t *vstr, int alloc);
124-
void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
125+
void vstr_init(vstr_t *vstr, size_t alloc);
126+
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
125127
void vstr_clear(vstr_t *vstr);
126128
vstr_t *vstr_new(void);
127-
vstr_t *vstr_new_size(int alloc);
129+
vstr_t *vstr_new_size(size_t alloc);
128130
void vstr_free(vstr_t *vstr);
129131
void vstr_reset(vstr_t *vstr);
130132
bool vstr_had_error(vstr_t *vstr);
131133
char *vstr_str(vstr_t *vstr);
132-
int vstr_len(vstr_t *vstr);
133-
void vstr_hint_size(vstr_t *vstr, int size);
134-
char *vstr_extend(vstr_t *vstr, int size);
135-
bool vstr_set_size(vstr_t *vstr, int size);
134+
size_t vstr_len(vstr_t *vstr);
135+
void vstr_hint_size(vstr_t *vstr, size_t size);
136+
char *vstr_extend(vstr_t *vstr, size_t size);
137+
bool vstr_set_size(vstr_t *vstr, size_t size);
136138
bool vstr_shrink(vstr_t *vstr);
137-
char *vstr_add_len(vstr_t *vstr, int len);
139+
char *vstr_add_len(vstr_t *vstr, size_t len);
138140
void vstr_add_byte(vstr_t *vstr, byte v);
139141
void vstr_add_char(vstr_t *vstr, unichar chr);
140142
void vstr_add_str(vstr_t *vstr, const char *str);
141-
void vstr_add_strn(vstr_t *vstr, const char *str, int len);
142-
//void vstr_add_le16(vstr_t *vstr, unsigned short v);
143-
//void vstr_add_le32(vstr_t *vstr, unsigned int v);
144-
void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b);
145-
void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr);
146-
void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut);
147-
void vstr_cut_tail_bytes(vstr_t *vstr, uint bytes_to_cut);
148-
void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut);
143+
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
144+
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
145+
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
146+
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
147+
void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
148+
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
149149
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
150150

151151
/** non-dynamic size-bounded variable buffer/string *************/
152152

153-
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
153+
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
154154
#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
155155
#define CHECKBUF_APPEND(buf, src, src_len) \
156-
{ int l = MIN(src_len, buf##_len); \
156+
{ size_t l = MIN(src_len, buf##_len); \
157157
memcpy(buf##_p, src, l); \
158158
buf##_len -= l; \
159159
buf##_p += l; }
@@ -167,15 +167,15 @@ void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
167167
// Debugging helpers
168168
int DEBUG_printf(const char *fmt, ...);
169169

170-
extern uint mp_verbose_flag;
170+
extern mp_uint_t mp_verbose_flag;
171171

172172
// This is useful for unicode handling. Some CPU archs has
173173
// special instructions for efficient implentation of this
174174
// function (e.g. CLZ on ARM).
175175
// NOTE: this function is unused at the moment
176176
#ifndef count_lead_ones
177-
static inline uint count_lead_ones(byte val) {
178-
uint c = 0;
177+
static inline mp_uint_t count_lead_ones(byte val) {
178+
mp_uint_t c = 0;
179179
for (byte mask = 0x80; val & mask; mask >>= 1) {
180180
c++;
181181
}

py/runtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ void mp_globals_set(mp_obj_dict_t *d) {
11461146
dict_globals = d;
11471147
}
11481148

1149-
void *m_malloc_fail(int num_bytes) {
1150-
DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
1149+
void *m_malloc_fail(size_t num_bytes) {
1150+
DEBUG_printf("memory allocation failed, allocating " UINT_FMT " bytes\n", num_bytes);
11511151
nlr_raise((mp_obj_t)&mp_const_MemoryError_obj);
11521152
}
11531153

0 commit comments

Comments
 (0)