misc.h 8.8 KB

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