misc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_HELPER(x) #x
  44. #define MP_STRINGIFY(x) MP_STRINGIFY_HELPER(x)
  45. // Static assertion macro
  46. #define MP_STATIC_ASSERT(cond) ((void)sizeof(char[1 - 2 * !(cond)]))
  47. // In C++ things like comparing extern const pointers are not constant-expressions so cannot be used
  48. // in MP_STATIC_ASSERT. Note that not all possible compiler versions will reject this. Some gcc versions
  49. // do, others only with -Werror=vla, msvc always does.
  50. // The (void) is needed to avoid "left operand of comma operator has no effect [-Werror=unused-value]"
  51. // when using this macro on the left-hand side of a comma.
  52. #if defined(_MSC_VER) || defined(__cplusplus)
  53. #define MP_STATIC_ASSERT_NONCONSTEXPR(cond) ((void)1)
  54. #else
  55. #define MP_STATIC_ASSERT_NONCONSTEXPR(cond) MP_STATIC_ASSERT(cond)
  56. #endif
  57. // Round-up integer division
  58. #define MP_CEIL_DIVIDE(a, b) (((a) + (b) - 1) / (b))
  59. #define MP_ROUND_DIVIDE(a, b) (((a) + (b) / 2) / (b))
  60. /** memory allocation ******************************************/
  61. // TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
  62. #define m_new(type, num) ((type *)(m_malloc(sizeof(type) * (num))))
  63. #define m_new_maybe(type, num) ((type *)(m_malloc_maybe(sizeof(type) * (num))))
  64. #define m_new0(type, num) ((type *)(m_malloc0(sizeof(type) * (num))))
  65. #define m_new_obj(type) (m_new(type, 1))
  66. #define m_new_obj_maybe(type) (m_new_maybe(type, 1))
  67. #define m_new_obj_var(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
  68. #define m_new_obj_var0(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc0(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
  69. #define m_new_obj_var_maybe(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc_maybe(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
  70. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  71. #define m_renew(type, ptr, old_num, new_num) ((type *)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
  72. #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))))
  73. #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
  74. #define m_del_var(obj_type, var_field, var_type, var_num, ptr) (m_free(ptr, offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
  75. #else
  76. #define m_renew(type, ptr, old_num, new_num) ((type *)(m_realloc((ptr), sizeof(type) * (new_num))))
  77. #define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type *)(m_realloc_maybe((ptr), sizeof(type) * (new_num), (allow_move))))
  78. #define m_del(type, ptr, num) ((void)(num), m_free(ptr))
  79. #define m_del_var(obj_type, var_field, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
  80. #endif
  81. #define m_del_obj(type, ptr) (m_del(type, ptr, 1))
  82. void *m_malloc(size_t num_bytes);
  83. void *m_malloc_maybe(size_t num_bytes);
  84. void *m_malloc_with_finaliser(size_t num_bytes);
  85. void *m_malloc0(size_t num_bytes);
  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);
  90. #else
  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);
  93. void m_free(void *ptr);
  94. #endif
  95. NORETURN void m_malloc_fail(size_t num_bytes);
  96. #if MICROPY_TRACKED_ALLOC
  97. // These alloc/free functions track the pointers in a linked list so the GC does not reclaim
  98. // them. They can be used by code that requires traditional C malloc/free semantics.
  99. void *m_tracked_calloc(size_t nmemb, size_t size);
  100. void m_tracked_free(void *ptr_in);
  101. #endif
  102. #if MICROPY_MEM_STATS
  103. size_t m_get_total_bytes_allocated(void);
  104. size_t m_get_current_bytes_allocated(void);
  105. size_t m_get_peak_bytes_allocated(void);
  106. #endif
  107. /** array helpers ***********************************************/
  108. // get the number of elements in a fixed-size array
  109. #define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  110. // align ptr to the nearest multiple of "alignment"
  111. #define MP_ALIGN(ptr, alignment) (void *)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
  112. /** unichar / UTF-8 *********************************************/
  113. #if MICROPY_PY_BUILTINS_STR_UNICODE
  114. // with unicode enabled we need a type which can fit chars up to 0x10ffff
  115. typedef uint32_t unichar;
  116. #else
  117. // without unicode enabled we can only need to fit chars up to 0xff
  118. // (on 16-bit archs uint is 16-bits and more efficient than uint32_t)
  119. typedef uint unichar;
  120. #endif
  121. #if MICROPY_PY_BUILTINS_STR_UNICODE
  122. unichar utf8_get_char(const byte *s);
  123. const byte *utf8_next_char(const byte *s);
  124. size_t utf8_charlen(const byte *str, size_t len);
  125. #else
  126. static inline unichar utf8_get_char(const byte *s) {
  127. return *s;
  128. }
  129. static inline const byte *utf8_next_char(const byte *s) {
  130. return s + 1;
  131. }
  132. static inline size_t utf8_charlen(const byte *str, size_t len) {
  133. (void)str;
  134. return len;
  135. }
  136. #endif
  137. bool unichar_isspace(unichar c);
  138. bool unichar_isalpha(unichar c);
  139. bool unichar_isprint(unichar c);
  140. bool unichar_isdigit(unichar c);
  141. bool unichar_isxdigit(unichar c);
  142. bool unichar_isident(unichar c);
  143. bool unichar_isalnum(unichar c);
  144. bool unichar_isupper(unichar c);
  145. bool unichar_islower(unichar c);
  146. unichar unichar_tolower(unichar c);
  147. unichar unichar_toupper(unichar c);
  148. mp_uint_t unichar_xdigit_value(unichar c);
  149. #define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
  150. #define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
  151. /** variable string *********************************************/
  152. typedef struct _vstr_t {
  153. size_t alloc;
  154. size_t len;
  155. char *buf;
  156. bool fixed_buf;
  157. } vstr_t;
  158. // convenience macro to declare a vstr with a fixed size buffer on the stack
  159. #define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
  160. void vstr_init(vstr_t *vstr, size_t alloc);
  161. void vstr_init_len(vstr_t *vstr, size_t len);
  162. void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
  163. struct _mp_print_t;
  164. void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
  165. void vstr_clear(vstr_t *vstr);
  166. vstr_t *vstr_new(size_t alloc);
  167. void vstr_free(vstr_t *vstr);
  168. static inline void vstr_reset(vstr_t *vstr) {
  169. vstr->len = 0;
  170. }
  171. static inline char *vstr_str(vstr_t *vstr) {
  172. return vstr->buf;
  173. }
  174. static inline size_t vstr_len(vstr_t *vstr) {
  175. return vstr->len;
  176. }
  177. void vstr_hint_size(vstr_t *vstr, size_t size);
  178. char *vstr_extend(vstr_t *vstr, size_t size);
  179. char *vstr_add_len(vstr_t *vstr, size_t len);
  180. char *vstr_null_terminated_str(vstr_t *vstr);
  181. void vstr_add_byte(vstr_t *vstr, byte v);
  182. void vstr_add_char(vstr_t *vstr, unichar chr);
  183. void vstr_add_str(vstr_t *vstr, const char *str);
  184. void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
  185. void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
  186. void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
  187. void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
  188. void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
  189. void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
  190. void vstr_printf(vstr_t *vstr, const char *fmt, ...);
  191. /** non-dynamic size-bounded variable buffer/string *************/
  192. #define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
  193. #define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
  194. #define CHECKBUF_APPEND(buf, src, src_len) \
  195. { size_t l = MIN(src_len, buf##_len); \
  196. memcpy(buf##_p, src, l); \
  197. buf##_len -= l; \
  198. buf##_p += l; }
  199. #define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
  200. #define CHECKBUF_LEN(buf) (buf##_p - buf)
  201. #ifdef va_start
  202. void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
  203. #endif
  204. // Debugging helpers
  205. int DEBUG_printf(const char *fmt, ...);
  206. extern mp_uint_t mp_verbose_flag;
  207. /** float internals *************/
  208. #if MICROPY_PY_BUILTINS_FLOAT
  209. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  210. #define MP_FLOAT_EXP_BITS (11)
  211. #define MP_FLOAT_EXP_OFFSET (1023)
  212. #define MP_FLOAT_FRAC_BITS (52)
  213. typedef uint64_t mp_float_uint_t;
  214. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  215. #define MP_FLOAT_EXP_BITS (8)
  216. #define MP_FLOAT_EXP_OFFSET (127)
  217. #define MP_FLOAT_FRAC_BITS (23)
  218. typedef uint32_t mp_float_uint_t;
  219. #endif
  220. #define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
  221. typedef union _mp_float_union_t {
  222. mp_float_t f;
  223. #if MP_ENDIANNESS_LITTLE
  224. struct {
  225. mp_float_uint_t frc : MP_FLOAT_FRAC_BITS;
  226. mp_float_uint_t exp : MP_FLOAT_EXP_BITS;
  227. mp_float_uint_t sgn : 1;
  228. } p;
  229. #else
  230. struct {
  231. mp_float_uint_t sgn : 1;
  232. mp_float_uint_t exp : MP_FLOAT_EXP_BITS;
  233. mp_float_uint_t frc : MP_FLOAT_FRAC_BITS;
  234. } p;
  235. #endif
  236. mp_float_uint_t i;
  237. } mp_float_union_t;
  238. #endif // MICROPY_PY_BUILTINS_FLOAT
  239. /** ROM string compression *************/
  240. #if MICROPY_ROM_TEXT_COMPRESSION
  241. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
  242. #error "MICROPY_ERROR_REPORTING_NONE requires MICROPY_ROM_TEXT_COMPRESSION disabled"
  243. #endif
  244. #ifdef NO_QSTR
  245. // Compression enabled but doing QSTR extraction.
  246. // So leave MP_COMPRESSED_ROM_TEXT in place for makeqstrdefs.py / makecompresseddata.py to find them.
  247. #else
  248. // Compression enabled and doing a regular build.
  249. // Map MP_COMPRESSED_ROM_TEXT to the compressed strings.
  250. // Force usage of the MP_ERROR_TEXT macro by requiring an opaque type.
  251. typedef struct {
  252. #if defined(__clang__) || defined(_MSC_VER)
  253. // Fix "error: empty struct has size 0 in C, size 1 in C++", and the msvc counterpart
  254. // "C requires that a struct or union have at least one member"
  255. char dummy;
  256. #endif
  257. } *mp_rom_error_text_t;
  258. #include <string.h>
  259. inline MP_ALWAYSINLINE const char *MP_COMPRESSED_ROM_TEXT(const char *msg) {
  260. // "genhdr/compressed.data.h" contains an invocation of the MP_MATCH_COMPRESSED macro for each compressed string.
  261. // The giant if(strcmp) tree is optimized by the compiler, which turns this into a direct return of the compressed data.
  262. #define MP_MATCH_COMPRESSED(a, b) if (strcmp(msg, a) == 0) { return b; } else
  263. // It also contains a single invocation of the MP_COMPRESSED_DATA macro, we don't need that here.
  264. #define MP_COMPRESSED_DATA(x)
  265. #include "genhdr/compressed.data.h"
  266. #undef MP_COMPRESSED_DATA
  267. #undef MP_MATCH_COMPRESSED
  268. return msg;
  269. }
  270. #endif
  271. #else
  272. // Compression not enabled, just make it a no-op.
  273. typedef const char *mp_rom_error_text_t;
  274. #define MP_COMPRESSED_ROM_TEXT(x) x
  275. #endif // MICROPY_ROM_TEXT_COMPRESSION
  276. // Might add more types of compressed text in the future.
  277. // For now, forward directly to MP_COMPRESSED_ROM_TEXT.
  278. #define MP_ERROR_TEXT(x) (mp_rom_error_text_t)MP_COMPRESSED_ROM_TEXT(x)
  279. #endif // MICROPY_INCLUDED_PY_MISC_H