blob: 5d557db6fe51ce698b72e750e945e085c9485371 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_MISC_H
27#define MICROPY_INCLUDED_PY_MISC_H
Damien George04b91472014-05-03 23:27:38 +010028
Damien429d7192013-10-04 19:53:11 +010029// a mini library of useful types and functions
30
Damien429d7192013-10-04 19:53:11 +010031/** types *******************************************************/
32
ian-v5fd8fd22014-01-06 13:51:53 -080033#include <stdbool.h>
Paul Sokolovsky9f001b02015-12-07 20:08:07 +020034#include <stdint.h>
bvernouxf6f248b2014-09-28 09:54:35 +020035#include <stddef.h>
Damien429d7192013-10-04 19:53:11 +010036
37typedef unsigned char byte;
38typedef unsigned int uint;
39
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020040/** generic ops *************************************************/
41
Damien Georgee09ffa12014-02-05 23:57:48 +000042#ifndef MIN
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020043#define MIN(x, y) ((x) < (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000044#endif
45#ifndef MAX
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020046#define MAX(x, y) ((x) > (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000047#endif
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020048
Paul Sokolovsky25f44c12016-12-27 01:00:12 +030049// Classical double-indirection stringification of preprocessor macro's value
50#define _MP_STRINGIFY(x) #x
51#define MP_STRINGIFY(x) _MP_STRINGIFY(x)
52
Paul Sokolovskycf96be62016-12-27 00:55:42 +030053/** memory allocation ******************************************/
Damien429d7192013-10-04 19:53:11 +010054
Damien732407f2013-12-29 19:33:23 +000055// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
56
Damien429d7192013-10-04 19:53:11 +010057#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
Damien George1b82e9a2014-05-10 17:36:41 +010058#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
Damien429d7192013-10-04 19:53:11 +010059#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
Damiend99b0522013-12-21 18:17:45 +000060#define m_new_obj(type) (m_new(type, 1))
Damien George9bf5f282014-10-09 16:53:37 +010061#define m_new_obj_maybe(type) (m_new_maybe(type, 1))
Damiend99b0522013-12-21 18:17:45 +000062#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien George58ba4c32014-04-10 14:27:31 +000063#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)))
Damien George12bab722014-04-05 20:35:48 +010064#if MICROPY_ENABLE_FINALISER
65#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
Paul Sokolovsky75d3c042017-12-04 11:05:49 +020066#define m_new_obj_var_with_finaliser(type, var_type, var_num) ((type*)m_malloc_with_finaliser(sizeof(type) + sizeof(var_type) * (var_num)))
Damien George12bab722014-04-05 20:35:48 +010067#else
68#define m_new_obj_with_finaliser(type) m_new_obj(type)
Paul Sokolovsky75d3c042017-12-04 11:05:49 +020069#define m_new_obj_var_with_finaliser(type, var_type, var_num) m_new_obj_var(type, var_type, var_num)
Damien George12bab722014-04-05 20:35:48 +010070#endif
Damien Georged8914522015-02-27 09:54:12 +000071#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
Damien732407f2013-12-29 19:33:23 +000072#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
Damien Georgeade9a052015-06-13 21:53:22 +010073#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))))
Damien732407f2013-12-29 19:33:23 +000074#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
John R. Lenton07205ec2014-01-13 02:31:00 +000075#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien Georged8914522015-02-27 09:54:12 +000076#else
77#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (new_num))))
Damien Georgeade9a052015-06-13 21:53:22 +010078#define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (new_num), (allow_move))))
Damien Georged8914522015-02-27 09:54:12 +000079#define m_del(type, ptr, num) ((void)(num), m_free(ptr))
80#define m_del_var(obj_type, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
81#endif
82#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
Damien429d7192013-10-04 19:53:11 +010083
Damien Georgeb0261342014-09-23 18:10:17 +010084void *m_malloc(size_t num_bytes);
85void *m_malloc_maybe(size_t num_bytes);
86void *m_malloc_with_finaliser(size_t num_bytes);
87void *m_malloc0(size_t num_bytes);
Damien Georged8914522015-02-27 09:54:12 +000088#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
Damien Georgeb0261342014-09-23 18:10:17 +010089void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
Damien Georgeade9a052015-06-13 21:53:22 +010090void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move);
Damien Georgeb0261342014-09-23 18:10:17 +010091void m_free(void *ptr, size_t num_bytes);
Damien Georged8914522015-02-27 09:54:12 +000092#else
93void *m_realloc(void *ptr, size_t new_num_bytes);
Damien Georgeade9a052015-06-13 21:53:22 +010094void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move);
Damien Georged8914522015-02-27 09:54:12 +000095void m_free(void *ptr);
96#endif
Damien Georgeca21aed2017-08-31 17:00:14 +100097NORETURN void m_malloc_fail(size_t num_bytes);
Damien429d7192013-10-04 19:53:11 +010098
Damien Georgeb0261342014-09-23 18:10:17 +010099#if MICROPY_MEM_STATS
100size_t m_get_total_bytes_allocated(void);
101size_t m_get_current_bytes_allocated(void);
102size_t m_get_peak_bytes_allocated(void);
103#endif
Damien429d7192013-10-04 19:53:11 +0100104
Damien George6d3c5e42014-04-26 10:47:29 +0100105/** array helpers ***********************************************/
106
107// get the number of elements in a fixed-size array
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200108#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Damien George6d3c5e42014-04-26 10:47:29 +0100109
Paul Sokolovsky564e4642014-07-12 15:55:47 +0300110// align ptr to the nearest multiple of "alignment"
Damien George999cedb2015-11-27 17:01:44 +0000111#define MP_ALIGN(ptr, alignment) (void*)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
Paul Sokolovsky564e4642014-07-12 15:55:47 +0300112
Damien429d7192013-10-04 19:53:11 +0100113/** unichar / UTF-8 *********************************************/
114
Damien George16677ce2015-01-28 14:07:11 +0000115#if MICROPY_PY_BUILTINS_STR_UNICODE
Damien George16677ce2015-01-28 14:07:11 +0000116// with unicode enabled we need a type which can fit chars up to 0x10ffff
117typedef uint32_t unichar;
118#else
119// without unicode enabled we can only need to fit chars up to 0xff
120// (on 16-bit archs uint is 16-bits and more efficient than uint32_t)
121typedef uint unichar;
122#endif
Damien429d7192013-10-04 19:53:11 +0100123
Paul Sokolovskyb0bb4582014-06-14 06:18:34 +0300124unichar utf8_get_char(const byte *s);
125const byte *utf8_next_char(const byte *s);
Damien429d7192013-10-04 19:53:11 +0100126
ian-v5fd8fd22014-01-06 13:51:53 -0800127bool unichar_isspace(unichar c);
128bool unichar_isalpha(unichar c);
129bool unichar_isprint(unichar c);
130bool unichar_isdigit(unichar c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +0200131bool unichar_isxdigit(unichar c);
Alex March69d9e7d2016-02-16 13:36:18 +0000132bool unichar_isident(unichar c);
Kim Bautersa3f4b832014-05-31 07:30:03 +0100133bool unichar_isupper(unichar c);
134bool unichar_islower(unichar c);
Paul Sokolovsky69135212014-05-10 19:47:41 +0300135unichar unichar_tolower(unichar c);
136unichar unichar_toupper(unichar c);
Dave Hylands3ad94d62015-05-18 14:41:25 -0700137mp_uint_t unichar_xdigit_value(unichar c);
Damien George40f3c022014-07-03 13:25:24 +0100138mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
Chris Angelicoc88987c2014-06-04 05:28:12 +1000139#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
140#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
Damien429d7192013-10-04 19:53:11 +0100141
Damien429d7192013-10-04 19:53:11 +0100142/** variable string *********************************************/
143
Damien68f59a92013-10-20 14:39:58 +0100144typedef struct _vstr_t {
Damien Georgeb0261342014-09-23 18:10:17 +0100145 size_t alloc;
146 size_t len;
Damien68f59a92013-10-20 14:39:58 +0100147 char *buf;
Damien George51047752014-02-26 17:40:52 +0000148 bool fixed_buf : 1;
Damien68f59a92013-10-20 14:39:58 +0100149} vstr_t;
Damien429d7192013-10-04 19:53:11 +0100150
Damien George354d15a2014-02-06 21:11:19 +0000151// convenience macro to declare a vstr with a fixed size buffer on the stack
152#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
153
Damien Georgeb0261342014-09-23 18:10:17 +0100154void vstr_init(vstr_t *vstr, size_t alloc);
Damien George05005f62015-01-21 22:48:37 +0000155void vstr_init_len(vstr_t *vstr, size_t len);
Damien Georgeb0261342014-09-23 18:10:17 +0100156void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
Damien George7f9d1d62015-04-09 23:56:15 +0100157struct _mp_print_t;
158void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
Damien68f59a92013-10-20 14:39:58 +0100159void vstr_clear(vstr_t *vstr);
Damien George824f5c52016-10-14 16:46:34 +1100160vstr_t *vstr_new(size_t alloc);
Damien429d7192013-10-04 19:53:11 +0100161void vstr_free(vstr_t *vstr);
Damien George5da0d292016-09-19 11:17:02 +1000162static inline void vstr_reset(vstr_t *vstr) { vstr->len = 0; }
163static inline char *vstr_str(vstr_t *vstr) { return vstr->buf; }
164static inline size_t vstr_len(vstr_t *vstr) { return vstr->len; }
Damien Georgeb0261342014-09-23 18:10:17 +0100165void vstr_hint_size(vstr_t *vstr, size_t size);
166char *vstr_extend(vstr_t *vstr, size_t size);
Damien Georgeb0261342014-09-23 18:10:17 +0100167char *vstr_add_len(vstr_t *vstr, size_t len);
Damien George827b0f72015-01-29 13:57:23 +0000168char *vstr_null_terminated_str(vstr_t *vstr);
Damien68f59a92013-10-20 14:39:58 +0100169void vstr_add_byte(vstr_t *vstr, byte v);
170void vstr_add_char(vstr_t *vstr, unichar chr);
Damien429d7192013-10-04 19:53:11 +0100171void vstr_add_str(vstr_t *vstr, const char *str);
Damien Georgeb0261342014-09-23 18:10:17 +0100172void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
173void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
174void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
175void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
176void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
177void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
Damien2f06c572013-11-03 18:20:56 +0000178void vstr_printf(vstr_t *vstr, const char *fmt, ...);
Damien96a0add2013-11-03 18:30:10 +0000179
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200180/** non-dynamic size-bounded variable buffer/string *************/
181
Damien Georgeb0261342014-09-23 18:10:17 +0100182#define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
Damien Georgee09ffa12014-02-05 23:57:48 +0000183#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200184#define CHECKBUF_APPEND(buf, src, src_len) \
Damien Georgeb0261342014-09-23 18:10:17 +0100185 { size_t l = MIN(src_len, buf##_len); \
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200186 memcpy(buf##_p, src, l); \
187 buf##_len -= l; \
188 buf##_p += l; }
189#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
190#define CHECKBUF_LEN(buf) (buf##_p - buf)
191
Damien96a0add2013-11-03 18:30:10 +0000192#ifdef va_start
Damien2f06c572013-11-03 18:20:56 +0000193void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
Damien96a0add2013-11-03 18:30:10 +0000194#endif
Damien429d7192013-10-04 19:53:11 +0100195
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200196// Debugging helpers
197int DEBUG_printf(const char *fmt, ...);
198
Damien Georgeb0261342014-09-23 18:10:17 +0100199extern mp_uint_t mp_verbose_flag;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300200
Paul Sokolovskyce813122014-06-15 23:13:41 +0300201// This is useful for unicode handling. Some CPU archs has
Ville Skyttäca16c382017-05-29 10:08:14 +0300202// special instructions for efficient implementation of this
Paul Sokolovskyce813122014-06-15 23:13:41 +0300203// function (e.g. CLZ on ARM).
Damien Georgee04a44e2014-06-28 10:27:23 +0100204// NOTE: this function is unused at the moment
Paul Sokolovskyce813122014-06-15 23:13:41 +0300205#ifndef count_lead_ones
Damien Georgeb0261342014-09-23 18:10:17 +0100206static inline mp_uint_t count_lead_ones(byte val) {
207 mp_uint_t c = 0;
Paul Sokolovskyce813122014-06-15 23:13:41 +0300208 for (byte mask = 0x80; val & mask; mask >>= 1) {
209 c++;
210 }
211 return c;
212}
213#endif
214
David Steinbergc585ad12015-01-13 15:19:37 +0000215/** float internals *************/
216
217#if MICROPY_PY_BUILTINS_FLOAT
218#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
219#define MP_FLOAT_EXP_BITS (11)
220#define MP_FLOAT_FRAC_BITS (52)
221#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
222#define MP_FLOAT_EXP_BITS (8)
223#define MP_FLOAT_FRAC_BITS (23)
224#endif
225#define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
226#endif // MICROPY_PY_BUILTINS_FLOAT
227
Alexander Steffen299bc622017-06-29 23:14:58 +0200228#endif // MICROPY_INCLUDED_PY_MISC_H