Colin Hogben / micropython

Dependents:   micropython-repl

Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2013, 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26 #ifndef __MICROPY_INCLUDED_PY_MISC_H__
pythontech 0:5868e8752d44 27 #define __MICROPY_INCLUDED_PY_MISC_H__
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 // a mini library of useful types and functions
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 /** types *******************************************************/
pythontech 0:5868e8752d44 32
pythontech 0:5868e8752d44 33 #include <stdbool.h>
pythontech 0:5868e8752d44 34 #include <stdint.h>
pythontech 0:5868e8752d44 35 #include <stddef.h>
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 typedef unsigned char byte;
pythontech 0:5868e8752d44 38 typedef unsigned int uint;
pythontech 0:5868e8752d44 39
pythontech 0:5868e8752d44 40 /** generic ops *************************************************/
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 #ifndef MIN
pythontech 0:5868e8752d44 43 #define MIN(x, y) ((x) < (y) ? (x) : (y))
pythontech 0:5868e8752d44 44 #endif
pythontech 0:5868e8752d44 45 #ifndef MAX
pythontech 0:5868e8752d44 46 #define MAX(x, y) ((x) > (y) ? (x) : (y))
pythontech 0:5868e8752d44 47 #endif
pythontech 0:5868e8752d44 48
pythontech 0:5868e8752d44 49 /** memomry allocation ******************************************/
pythontech 0:5868e8752d44 50
pythontech 0:5868e8752d44 51 // TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
pythontech 0:5868e8752d44 52
pythontech 0:5868e8752d44 53 #define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
pythontech 0:5868e8752d44 54 #define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
pythontech 0:5868e8752d44 55 #define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
pythontech 0:5868e8752d44 56 #define m_new_obj(type) (m_new(type, 1))
pythontech 0:5868e8752d44 57 #define m_new_obj_maybe(type) (m_new_maybe(type, 1))
pythontech 0:5868e8752d44 58 #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
pythontech 0:5868e8752d44 59 #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)))
pythontech 0:5868e8752d44 60 #if MICROPY_ENABLE_FINALISER
pythontech 0:5868e8752d44 61 #define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
pythontech 0:5868e8752d44 62 #else
pythontech 0:5868e8752d44 63 #define m_new_obj_with_finaliser(type) m_new_obj(type)
pythontech 0:5868e8752d44 64 #endif
pythontech 0:5868e8752d44 65 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
pythontech 0:5868e8752d44 66 #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
pythontech 0:5868e8752d44 67 #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))))
pythontech 0:5868e8752d44 68 #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
pythontech 0:5868e8752d44 69 #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
pythontech 0:5868e8752d44 70 #else
pythontech 0:5868e8752d44 71 #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (new_num))))
pythontech 0:5868e8752d44 72 #define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (new_num), (allow_move))))
pythontech 0:5868e8752d44 73 #define m_del(type, ptr, num) ((void)(num), m_free(ptr))
pythontech 0:5868e8752d44 74 #define m_del_var(obj_type, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
pythontech 0:5868e8752d44 75 #endif
pythontech 0:5868e8752d44 76 #define m_del_obj(type, ptr) (m_del(type, ptr, 1))
pythontech 0:5868e8752d44 77
pythontech 0:5868e8752d44 78 void *m_malloc(size_t num_bytes);
pythontech 0:5868e8752d44 79 void *m_malloc_maybe(size_t num_bytes);
pythontech 0:5868e8752d44 80 void *m_malloc_with_finaliser(size_t num_bytes);
pythontech 0:5868e8752d44 81 void *m_malloc0(size_t num_bytes);
pythontech 0:5868e8752d44 82 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
pythontech 0:5868e8752d44 83 void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
pythontech 0:5868e8752d44 84 void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move);
pythontech 0:5868e8752d44 85 void m_free(void *ptr, size_t num_bytes);
pythontech 0:5868e8752d44 86 #else
pythontech 0:5868e8752d44 87 void *m_realloc(void *ptr, size_t new_num_bytes);
pythontech 0:5868e8752d44 88 void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move);
pythontech 0:5868e8752d44 89 void m_free(void *ptr);
pythontech 0:5868e8752d44 90 #endif
pythontech 0:5868e8752d44 91 void *m_malloc_fail(size_t num_bytes);
pythontech 0:5868e8752d44 92
pythontech 0:5868e8752d44 93 #if MICROPY_MEM_STATS
pythontech 0:5868e8752d44 94 size_t m_get_total_bytes_allocated(void);
pythontech 0:5868e8752d44 95 size_t m_get_current_bytes_allocated(void);
pythontech 0:5868e8752d44 96 size_t m_get_peak_bytes_allocated(void);
pythontech 0:5868e8752d44 97 #endif
pythontech 0:5868e8752d44 98
pythontech 0:5868e8752d44 99 /** array helpers ***********************************************/
pythontech 0:5868e8752d44 100
pythontech 0:5868e8752d44 101 // get the number of elements in a fixed-size array
pythontech 0:5868e8752d44 102 #define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
pythontech 0:5868e8752d44 103
pythontech 0:5868e8752d44 104 // align ptr to the nearest multiple of "alignment"
pythontech 0:5868e8752d44 105 #define MP_ALIGN(ptr, alignment) (void*)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
pythontech 0:5868e8752d44 106
pythontech 0:5868e8752d44 107 /** unichar / UTF-8 *********************************************/
pythontech 0:5868e8752d44 108
pythontech 0:5868e8752d44 109 #if MICROPY_PY_BUILTINS_STR_UNICODE
pythontech 0:5868e8752d44 110 // with unicode enabled we need a type which can fit chars up to 0x10ffff
pythontech 0:5868e8752d44 111 typedef uint32_t unichar;
pythontech 0:5868e8752d44 112 #else
pythontech 0:5868e8752d44 113 // without unicode enabled we can only need to fit chars up to 0xff
pythontech 0:5868e8752d44 114 // (on 16-bit archs uint is 16-bits and more efficient than uint32_t)
pythontech 0:5868e8752d44 115 typedef uint unichar;
pythontech 0:5868e8752d44 116 #endif
pythontech 0:5868e8752d44 117
pythontech 0:5868e8752d44 118 unichar utf8_get_char(const byte *s);
pythontech 0:5868e8752d44 119 const byte *utf8_next_char(const byte *s);
pythontech 0:5868e8752d44 120
pythontech 0:5868e8752d44 121 bool unichar_isspace(unichar c);
pythontech 0:5868e8752d44 122 bool unichar_isalpha(unichar c);
pythontech 0:5868e8752d44 123 bool unichar_isprint(unichar c);
pythontech 0:5868e8752d44 124 bool unichar_isdigit(unichar c);
pythontech 0:5868e8752d44 125 bool unichar_isxdigit(unichar c);
pythontech 0:5868e8752d44 126 bool unichar_isident(unichar c);
pythontech 0:5868e8752d44 127 bool unichar_isupper(unichar c);
pythontech 0:5868e8752d44 128 bool unichar_islower(unichar c);
pythontech 0:5868e8752d44 129 unichar unichar_tolower(unichar c);
pythontech 0:5868e8752d44 130 unichar unichar_toupper(unichar c);
pythontech 0:5868e8752d44 131 mp_uint_t unichar_xdigit_value(unichar c);
pythontech 0:5868e8752d44 132 mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
pythontech 0:5868e8752d44 133 #define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
pythontech 0:5868e8752d44 134 #define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
pythontech 0:5868e8752d44 135
pythontech 0:5868e8752d44 136 /** variable string *********************************************/
pythontech 0:5868e8752d44 137
pythontech 0:5868e8752d44 138 typedef struct _vstr_t {
pythontech 0:5868e8752d44 139 size_t alloc;
pythontech 0:5868e8752d44 140 size_t len;
pythontech 0:5868e8752d44 141 char *buf;
pythontech 0:5868e8752d44 142 bool had_error : 1;
pythontech 0:5868e8752d44 143 bool fixed_buf : 1;
pythontech 0:5868e8752d44 144 } vstr_t;
pythontech 0:5868e8752d44 145
pythontech 0:5868e8752d44 146 // convenience macro to declare a vstr with a fixed size buffer on the stack
pythontech 0:5868e8752d44 147 #define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
pythontech 0:5868e8752d44 148
pythontech 0:5868e8752d44 149 void vstr_init(vstr_t *vstr, size_t alloc);
pythontech 0:5868e8752d44 150 void vstr_init_len(vstr_t *vstr, size_t len);
pythontech 0:5868e8752d44 151 void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
pythontech 0:5868e8752d44 152 struct _mp_print_t;
pythontech 0:5868e8752d44 153 void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
pythontech 0:5868e8752d44 154 void vstr_clear(vstr_t *vstr);
pythontech 0:5868e8752d44 155 vstr_t *vstr_new(void);
pythontech 0:5868e8752d44 156 vstr_t *vstr_new_size(size_t alloc);
pythontech 0:5868e8752d44 157 void vstr_free(vstr_t *vstr);
pythontech 0:5868e8752d44 158 void vstr_reset(vstr_t *vstr);
pythontech 0:5868e8752d44 159 bool vstr_had_error(vstr_t *vstr);
pythontech 0:5868e8752d44 160 char *vstr_str(vstr_t *vstr);
pythontech 0:5868e8752d44 161 size_t vstr_len(vstr_t *vstr);
pythontech 0:5868e8752d44 162 void vstr_hint_size(vstr_t *vstr, size_t size);
pythontech 0:5868e8752d44 163 char *vstr_extend(vstr_t *vstr, size_t size);
pythontech 0:5868e8752d44 164 char *vstr_add_len(vstr_t *vstr, size_t len);
pythontech 0:5868e8752d44 165 char *vstr_null_terminated_str(vstr_t *vstr);
pythontech 0:5868e8752d44 166 void vstr_add_byte(vstr_t *vstr, byte v);
pythontech 0:5868e8752d44 167 void vstr_add_char(vstr_t *vstr, unichar chr);
pythontech 0:5868e8752d44 168 void vstr_add_str(vstr_t *vstr, const char *str);
pythontech 0:5868e8752d44 169 void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
pythontech 0:5868e8752d44 170 void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
pythontech 0:5868e8752d44 171 void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
pythontech 0:5868e8752d44 172 void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
pythontech 0:5868e8752d44 173 void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
pythontech 0:5868e8752d44 174 void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
pythontech 0:5868e8752d44 175 void vstr_printf(vstr_t *vstr, const char *fmt, ...);
pythontech 0:5868e8752d44 176
pythontech 0:5868e8752d44 177 /** non-dynamic size-bounded variable buffer/string *************/
pythontech 0:5868e8752d44 178
pythontech 0:5868e8752d44 179 #define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
pythontech 0:5868e8752d44 180 #define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
pythontech 0:5868e8752d44 181 #define CHECKBUF_APPEND(buf, src, src_len) \
pythontech 0:5868e8752d44 182 { size_t l = MIN(src_len, buf##_len); \
pythontech 0:5868e8752d44 183 memcpy(buf##_p, src, l); \
pythontech 0:5868e8752d44 184 buf##_len -= l; \
pythontech 0:5868e8752d44 185 buf##_p += l; }
pythontech 0:5868e8752d44 186 #define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
pythontech 0:5868e8752d44 187 #define CHECKBUF_LEN(buf) (buf##_p - buf)
pythontech 0:5868e8752d44 188
pythontech 0:5868e8752d44 189 #ifdef va_start
pythontech 0:5868e8752d44 190 void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
pythontech 0:5868e8752d44 191 #endif
pythontech 0:5868e8752d44 192
pythontech 0:5868e8752d44 193 // Debugging helpers
pythontech 0:5868e8752d44 194 int DEBUG_printf(const char *fmt, ...);
pythontech 0:5868e8752d44 195
pythontech 0:5868e8752d44 196 extern mp_uint_t mp_verbose_flag;
pythontech 0:5868e8752d44 197
pythontech 0:5868e8752d44 198 // This is useful for unicode handling. Some CPU archs has
pythontech 0:5868e8752d44 199 // special instructions for efficient implentation of this
pythontech 0:5868e8752d44 200 // function (e.g. CLZ on ARM).
pythontech 0:5868e8752d44 201 // NOTE: this function is unused at the moment
pythontech 0:5868e8752d44 202 #ifndef count_lead_ones
pythontech 0:5868e8752d44 203 static inline mp_uint_t count_lead_ones(byte val) {
pythontech 0:5868e8752d44 204 mp_uint_t c = 0;
pythontech 0:5868e8752d44 205 for (byte mask = 0x80; val & mask; mask >>= 1) {
pythontech 0:5868e8752d44 206 c++;
pythontech 0:5868e8752d44 207 }
pythontech 0:5868e8752d44 208 return c;
pythontech 0:5868e8752d44 209 }
pythontech 0:5868e8752d44 210 #endif
pythontech 0:5868e8752d44 211
pythontech 0:5868e8752d44 212 /** float internals *************/
pythontech 0:5868e8752d44 213
pythontech 0:5868e8752d44 214 #if MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 215 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
pythontech 0:5868e8752d44 216 #define MP_FLOAT_EXP_BITS (11)
pythontech 0:5868e8752d44 217 #define MP_FLOAT_FRAC_BITS (52)
pythontech 0:5868e8752d44 218 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
pythontech 0:5868e8752d44 219 #define MP_FLOAT_EXP_BITS (8)
pythontech 0:5868e8752d44 220 #define MP_FLOAT_FRAC_BITS (23)
pythontech 0:5868e8752d44 221 #endif
pythontech 0:5868e8752d44 222 #define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
pythontech 0:5868e8752d44 223 #endif // MICROPY_PY_BUILTINS_FLOAT
pythontech 0:5868e8752d44 224
pythontech 0:5868e8752d44 225 #endif // __MICROPY_INCLUDED_PY_MISC_H__