2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2014-12-28 20:02:19 -05:00
|
|
|
#ifndef __MICROPY_INCLUDED_PY_MISC_H__
|
|
|
|
#define __MICROPY_INCLUDED_PY_MISC_H__
|
2014-05-03 18:27:38 -04:00
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
// a mini library of useful types and functions
|
|
|
|
|
|
|
|
/** types *******************************************************/
|
|
|
|
|
2014-01-06 16:51:53 -05:00
|
|
|
#include <stdbool.h>
|
2014-09-28 03:54:35 -04:00
|
|
|
#include <stddef.h>
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
typedef unsigned char byte;
|
|
|
|
typedef unsigned int uint;
|
|
|
|
|
2014-02-04 17:44:55 -05:00
|
|
|
/** generic ops *************************************************/
|
|
|
|
|
2014-02-05 18:57:48 -05:00
|
|
|
#ifndef MIN
|
2014-02-04 17:44:55 -05:00
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
2014-02-05 18:57:48 -05:00
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
2014-02-04 17:44:55 -05:00
|
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
2014-02-05 18:57:48 -05:00
|
|
|
#endif
|
2014-02-04 17:44:55 -05:00
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
/** memomry allocation ******************************************/
|
|
|
|
|
2013-12-29 14:33:23 -05:00
|
|
|
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
|
2014-05-10 12:36:41 -04:00
|
|
|
#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
|
2013-10-04 14:53:11 -04:00
|
|
|
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
|
2013-12-21 13:17:45 -05:00
|
|
|
#define m_new_obj(type) (m_new(type, 1))
|
2014-10-09 11:53:37 -04:00
|
|
|
#define m_new_obj_maybe(type) (m_new_maybe(type, 1))
|
2013-12-21 13:17:45 -05:00
|
|
|
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
|
2014-04-10 10:27:31 -04:00
|
|
|
#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)))
|
2014-04-05 15:35:48 -04:00
|
|
|
#if MICROPY_ENABLE_FINALISER
|
|
|
|
#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
|
|
|
|
#else
|
|
|
|
#define m_new_obj_with_finaliser(type) m_new_obj(type)
|
|
|
|
#endif
|
2015-02-27 04:54:12 -05:00
|
|
|
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
|
2013-12-29 14:33:23 -05:00
|
|
|
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
|
2014-04-10 10:27:31 -04:00
|
|
|
#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
|
2013-12-29 14:33:23 -05:00
|
|
|
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
|
2014-01-12 21:31:00 -05:00
|
|
|
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
|
2015-02-27 04:54:12 -05:00
|
|
|
#else
|
|
|
|
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (new_num))))
|
|
|
|
#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (new_num))))
|
|
|
|
#define m_del(type, ptr, num) ((void)(num), m_free(ptr))
|
|
|
|
#define m_del_var(obj_type, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
|
|
|
|
#endif
|
|
|
|
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void *m_malloc(size_t num_bytes);
|
|
|
|
void *m_malloc_maybe(size_t num_bytes);
|
|
|
|
void *m_malloc_with_finaliser(size_t num_bytes);
|
|
|
|
void *m_malloc0(size_t num_bytes);
|
2015-02-27 04:54:12 -05:00
|
|
|
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
|
2014-09-23 13:10:17 -04:00
|
|
|
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
|
|
|
|
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
|
|
|
|
void m_free(void *ptr, size_t num_bytes);
|
2015-02-27 04:54:12 -05:00
|
|
|
#else
|
|
|
|
void *m_realloc(void *ptr, size_t new_num_bytes);
|
|
|
|
void *m_realloc_maybe(void *ptr, size_t new_num_bytes);
|
|
|
|
void m_free(void *ptr);
|
|
|
|
#endif
|
2014-09-23 13:10:17 -04:00
|
|
|
void *m_malloc_fail(size_t num_bytes);
|
|
|
|
|
|
|
|
#if MICROPY_MEM_STATS
|
|
|
|
size_t m_get_total_bytes_allocated(void);
|
|
|
|
size_t m_get_current_bytes_allocated(void);
|
|
|
|
size_t m_get_peak_bytes_allocated(void);
|
|
|
|
#endif
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-04-26 05:47:29 -04:00
|
|
|
/** array helpers ***********************************************/
|
|
|
|
|
|
|
|
// get the number of elements in a fixed-size array
|
2014-06-19 12:54:34 -04:00
|
|
|
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
2014-04-26 05:47:29 -04:00
|
|
|
|
2014-07-12 08:55:47 -04:00
|
|
|
// align ptr to the nearest multiple of "alignment"
|
|
|
|
#define MP_ALIGN(ptr, alignment) (void*)(((mp_uint_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
/** unichar / UTF-8 *********************************************/
|
|
|
|
|
2015-01-28 09:07:11 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
#include <stdint.h> // only include if we need it
|
|
|
|
// with unicode enabled we need a type which can fit chars up to 0x10ffff
|
|
|
|
typedef uint32_t unichar;
|
|
|
|
#else
|
|
|
|
// without unicode enabled we can only need to fit chars up to 0xff
|
|
|
|
// (on 16-bit archs uint is 16-bits and more efficient than uint32_t)
|
|
|
|
typedef uint unichar;
|
|
|
|
#endif
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-06-13 23:18:34 -04:00
|
|
|
unichar utf8_get_char(const byte *s);
|
|
|
|
const byte *utf8_next_char(const byte *s);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-01-06 16:51:53 -05:00
|
|
|
bool unichar_isspace(unichar c);
|
|
|
|
bool unichar_isalpha(unichar c);
|
|
|
|
bool unichar_isprint(unichar c);
|
|
|
|
bool unichar_isdigit(unichar c);
|
2014-01-22 15:40:02 -05:00
|
|
|
bool unichar_isxdigit(unichar c);
|
2014-05-31 02:30:03 -04:00
|
|
|
bool unichar_isupper(unichar c);
|
|
|
|
bool unichar_islower(unichar c);
|
2014-05-10 12:47:41 -04:00
|
|
|
unichar unichar_tolower(unichar c);
|
|
|
|
unichar unichar_toupper(unichar c);
|
2015-05-18 17:41:25 -04:00
|
|
|
mp_uint_t unichar_xdigit_value(unichar c);
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t unichar_charlen(const char *str, mp_uint_t len);
|
2014-06-03 15:28:12 -04:00
|
|
|
#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
|
|
|
|
#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
/** variable string *********************************************/
|
|
|
|
|
2013-10-20 09:39:58 -04:00
|
|
|
typedef struct _vstr_t {
|
2014-09-23 13:10:17 -04:00
|
|
|
size_t alloc;
|
|
|
|
size_t len;
|
2013-10-20 09:39:58 -04:00
|
|
|
char *buf;
|
2014-02-26 12:40:52 -05:00
|
|
|
bool had_error : 1;
|
|
|
|
bool fixed_buf : 1;
|
2013-10-20 09:39:58 -04:00
|
|
|
} vstr_t;
|
|
|
|
|
2014-02-06 16:11:19 -05:00
|
|
|
// convenience macro to declare a vstr with a fixed size buffer on the stack
|
|
|
|
#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_init(vstr_t *vstr, size_t alloc);
|
2015-01-21 17:48:37 -05:00
|
|
|
void vstr_init_len(vstr_t *vstr, size_t len);
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
|
2015-04-09 18:56:15 -04:00
|
|
|
struct _mp_print_t;
|
|
|
|
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_clear(vstr_t *vstr);
|
2013-10-23 15:20:17 -04:00
|
|
|
vstr_t *vstr_new(void);
|
2014-09-23 13:10:17 -04:00
|
|
|
vstr_t *vstr_new_size(size_t alloc);
|
2013-10-04 14:53:11 -04:00
|
|
|
void vstr_free(vstr_t *vstr);
|
|
|
|
void vstr_reset(vstr_t *vstr);
|
2014-01-06 16:51:53 -05:00
|
|
|
bool vstr_had_error(vstr_t *vstr);
|
2013-10-04 14:53:11 -04:00
|
|
|
char *vstr_str(vstr_t *vstr);
|
2014-09-23 13:10:17 -04:00
|
|
|
size_t vstr_len(vstr_t *vstr);
|
|
|
|
void vstr_hint_size(vstr_t *vstr, size_t size);
|
|
|
|
char *vstr_extend(vstr_t *vstr, size_t size);
|
|
|
|
char *vstr_add_len(vstr_t *vstr, size_t len);
|
2015-01-29 08:57:23 -05:00
|
|
|
char *vstr_null_terminated_str(vstr_t *vstr);
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_add_byte(vstr_t *vstr, byte v);
|
|
|
|
void vstr_add_char(vstr_t *vstr, unichar chr);
|
2013-10-04 14:53:11 -04:00
|
|
|
void vstr_add_str(vstr_t *vstr, const char *str);
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
|
|
|
|
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
|
|
|
|
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
|
|
|
|
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
|
|
|
|
void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
|
|
|
|
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
|
2013-11-03 13:20:56 -05:00
|
|
|
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
|
2013-11-03 13:30:10 -05:00
|
|
|
|
2014-02-04 17:47:06 -05:00
|
|
|
/** non-dynamic size-bounded variable buffer/string *************/
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
|
2014-02-05 18:57:48 -05:00
|
|
|
#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
|
2014-02-04 17:47:06 -05:00
|
|
|
#define CHECKBUF_APPEND(buf, src, src_len) \
|
2014-09-23 13:10:17 -04:00
|
|
|
{ size_t l = MIN(src_len, buf##_len); \
|
2014-02-04 17:47:06 -05:00
|
|
|
memcpy(buf##_p, src, l); \
|
|
|
|
buf##_len -= l; \
|
|
|
|
buf##_p += l; }
|
|
|
|
#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
|
|
|
|
#define CHECKBUF_LEN(buf) (buf##_p - buf)
|
|
|
|
|
2013-11-03 13:30:10 -05:00
|
|
|
#ifdef va_start
|
2013-11-03 13:20:56 -05:00
|
|
|
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
|
2013-11-03 13:30:10 -05:00
|
|
|
#endif
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-02-16 11:11:42 -05:00
|
|
|
// Debugging helpers
|
|
|
|
int DEBUG_printf(const char *fmt, ...);
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
extern mp_uint_t mp_verbose_flag;
|
2014-05-04 17:50:05 -04:00
|
|
|
|
2014-06-15 16:13:41 -04:00
|
|
|
// This is useful for unicode handling. Some CPU archs has
|
|
|
|
// special instructions for efficient implentation of this
|
|
|
|
// function (e.g. CLZ on ARM).
|
2014-06-28 05:27:23 -04:00
|
|
|
// NOTE: this function is unused at the moment
|
2014-06-15 16:13:41 -04:00
|
|
|
#ifndef count_lead_ones
|
2014-09-23 13:10:17 -04:00
|
|
|
static inline mp_uint_t count_lead_ones(byte val) {
|
|
|
|
mp_uint_t c = 0;
|
2014-06-15 16:13:41 -04:00
|
|
|
for (byte mask = 0x80; val & mask; mask >>= 1) {
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-13 10:19:37 -05:00
|
|
|
/** float internals *************/
|
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
#define MP_FLOAT_EXP_BITS (11)
|
|
|
|
#define MP_FLOAT_FRAC_BITS (52)
|
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
|
|
|
#define MP_FLOAT_EXP_BITS (8)
|
|
|
|
#define MP_FLOAT_FRAC_BITS (23)
|
|
|
|
#endif
|
|
|
|
#define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
|
|
|
|
#endif // MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
|
2014-12-28 20:02:19 -05:00
|
|
|
#endif // __MICROPY_INCLUDED_PY_MISC_H__
|