26 #ifndef MICROPY_INCLUDED_PY_MISC_H 27 #define MICROPY_INCLUDED_PY_MISC_H 37 typedef unsigned char byte;
43 #define MIN(x, y) ((x) < (y) ? (x) : (y)) 46 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 50 #define _MP_STRINGIFY(x) #x 51 #define MP_STRINGIFY(x) _MP_STRINGIFY(x) 57 #define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num)))) 58 #define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num)))) 59 #define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num)))) 60 #define m_new_obj(type) (m_new(type, 1)) 61 #define m_new_obj_maybe(type) (m_new_maybe(type, 1)) 62 #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num))) 63 #define m_new_obj_var_maybe(obj_type, var_type, var_num) ((obj_type*)m_malloc_maybe(sizeof(obj_type) + sizeof(var_type) * (var_num))) 64 #if MICROPY_ENABLE_FINALISER 65 #define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type)))) 67 #define m_new_obj_with_finaliser(type) m_new_obj(type) 69 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE 70 #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num)))) 71 #define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num), (allow_move)))) 72 #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num)) 73 #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num))) 75 #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (new_num)))) 76 #define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (new_num), (allow_move)))) 77 #define m_del(type, ptr, num) ((void)(num), m_free(ptr)) 78 #define m_del_var(obj_type, var_type, var_num, ptr) ((void)(var_num), m_free(ptr)) 80 #define m_del_obj(type, ptr) (m_del(type, ptr, 1)) 86 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE 87 void *
m_realloc(
void *ptr,
size_t old_num_bytes,
size_t new_num_bytes);
88 void *
m_realloc_maybe(
void *ptr,
size_t old_num_bytes,
size_t new_num_bytes,
bool allow_move);
89 void m_free(
void *ptr,
size_t num_bytes);
91 void *
m_realloc(
void *ptr,
size_t new_num_bytes);
92 void *
m_realloc_maybe(
void *ptr,
size_t new_num_bytes,
bool allow_move);
98 size_t m_get_total_bytes_allocated(
void);
99 size_t m_get_current_bytes_allocated(
void);
100 size_t m_get_peak_bytes_allocated(
void);
106 #define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 109 #define MP_ALIGN(ptr, alignment) (void*)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1)) 113 #if MICROPY_PY_BUILTINS_STR_UNICODE 137 #define UTF8_IS_NONASCII(ch) ((ch) & 0x80) 138 #define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80) 150 #define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf); 160 static inline void vstr_reset(
vstr_t *vstr) { vstr->
len = 0; }
161 static inline char *vstr_str(
vstr_t *vstr) {
return vstr->
buf; }
162 static inline size_t vstr_len(
vstr_t *vstr) {
return vstr->
len; }
180 #define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf; 181 #define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf; 182 #define CHECKBUF_APPEND(buf, src, src_len) \ 183 { size_t l = MIN(src_len, buf##_len); \ 184 memcpy(buf##_p, src, l); \ 187 #define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; } 188 #define CHECKBUF_LEN(buf) (buf##_p - buf) 203 #ifndef count_lead_ones 206 for (
byte mask = 0x80; val & mask; mask >>= 1) {
215 #if MICROPY_PY_BUILTINS_FLOAT 216 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 217 #define MP_FLOAT_EXP_BITS (11) 218 #define MP_FLOAT_FRAC_BITS (52) 219 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 220 #define MP_FLOAT_EXP_BITS (8) 221 #define MP_FLOAT_FRAC_BITS (23) 223 #define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1) 224 #endif // MICROPY_PY_BUILTINS_FLOAT 226 #endif // MICROPY_INCLUDED_PY_MISC_H void * m_realloc(void *ptr, size_t new_num_bytes)
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf)
char * vstr_extend(vstr_t *vstr, size_t size)
int DEBUG_printf(const char *fmt,...)
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len)
bool unichar_isalpha(unichar c)
void vstr_add_char(vstr_t *vstr, unichar chr)
void vstr_init_len(vstr_t *vstr, size_t len)
void * m_malloc_maybe(size_t num_bytes)
void vstr_init(vstr_t *vstr, size_t alloc)
vstr_t * vstr_new(size_t alloc)
bool unichar_isxdigit(unichar c)
unichar unichar_tolower(unichar c)
void vstr_add_byte(vstr_t *vstr, byte v)
const byte * utf8_next_char(const byte *s)
void vstr_hint_size(vstr_t *vstr, size_t size)
void vstr_add_str(vstr_t *vstr, const char *str)
bool unichar_isdigit(unichar c)
char * vstr_add_len(vstr_t *vstr, size_t len)
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr)
void * m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move)
bool unichar_isspace(unichar c)
void * m_malloc_with_finaliser(size_t num_bytes)
mp_uint_t unichar_xdigit_value(unichar c)
bool unichar_islower(unichar c)
bool unichar_isident(unichar c)
mp_uint_t mp_verbose_flag
void * m_malloc0(size_t num_bytes)
unichar utf8_get_char(const byte *s)
NORETURN void m_malloc_fail(size_t num_bytes)
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut)
char * vstr_null_terminated_str(vstr_t *vstr)
bool unichar_isprint(unichar c)
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap)
__builtin_va_list va_list
unichar unichar_toupper(unichar c)
void vstr_free(vstr_t *vstr)
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut)
mp_uint_t unichar_charlen(const char *str, mp_uint_t len)
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print)
void vstr_clear(vstr_t *vstr)
void * m_malloc(size_t num_bytes)
bool unichar_isupper(unichar c)
void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut)
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b)
void vstr_printf(vstr_t *vstr, const char *fmt,...)