blob: 97e16c23ecca5790c7a242c869051b5285b2e0c1 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Damien429d7192013-10-04 19:53:11 +010027// a mini library of useful types and functions
28
29#ifndef _INCLUDED_MINILIB_H
30#define _INCLUDED_MINILIB_H
31
32/** types *******************************************************/
33
ian-v5fd8fd22014-01-06 13:51:53 -080034#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010035
36typedef unsigned char byte;
37typedef unsigned int uint;
38
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020039/** generic ops *************************************************/
40
Damien Georgee09ffa12014-02-05 23:57:48 +000041#ifndef MIN
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020042#define MIN(x, y) ((x) < (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000043#endif
44#ifndef MAX
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020045#define MAX(x, y) ((x) > (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000046#endif
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020047
Damien429d7192013-10-04 19:53:11 +010048/** memomry allocation ******************************************/
49
Damien732407f2013-12-29 19:33:23 +000050// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
51
Damien429d7192013-10-04 19:53:11 +010052#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
Damien George1b82e9a2014-05-10 17:36:41 +010053#define m_new_maybe(type, num) ((type*)(m_malloc_maybe(sizeof(type) * (num))))
Damien429d7192013-10-04 19:53:11 +010054#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
Damiend99b0522013-12-21 18:17:45 +000055#define m_new_obj(type) (m_new(type, 1))
56#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 +000057#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 +010058#if MICROPY_ENABLE_FINALISER
59#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
60#else
61#define m_new_obj_with_finaliser(type) m_new_obj(type)
62#endif
Damien732407f2013-12-29 19:33:23 +000063#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
Damien George58ba4c32014-04-10 14:27:31 +000064#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
Damien732407f2013-12-29 19:33:23 +000065#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
66#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
John R. Lenton07205ec2014-01-13 02:31:00 +000067#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien429d7192013-10-04 19:53:11 +010068
Damien429d7192013-10-04 19:53:11 +010069void *m_malloc(int num_bytes);
Damien Georgef0954e32014-04-10 14:38:25 +010070void *m_malloc_maybe(int num_bytes);
Damien George12bab722014-04-05 20:35:48 +010071void *m_malloc_with_finaliser(int num_bytes);
Damien429d7192013-10-04 19:53:11 +010072void *m_malloc0(int num_bytes);
Damien732407f2013-12-29 19:33:23 +000073void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
Damien George58ba4c32014-04-10 14:27:31 +000074void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes);
Damien732407f2013-12-29 19:33:23 +000075void m_free(void *ptr, int num_bytes);
Damien George6902eed2014-04-04 10:52:59 +000076void *m_malloc_fail(int num_bytes);
Damien429d7192013-10-04 19:53:11 +010077
Damien8b3a7c22013-10-23 20:20:17 +010078int m_get_total_bytes_allocated(void);
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020079int m_get_current_bytes_allocated(void);
Paul Sokolovsky780f5552014-01-01 23:42:21 +020080int m_get_peak_bytes_allocated(void);
Damien429d7192013-10-04 19:53:11 +010081
Damien George6d3c5e42014-04-26 10:47:29 +010082/** array helpers ***********************************************/
83
84// get the number of elements in a fixed-size array
85#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
86
Damien429d7192013-10-04 19:53:11 +010087/** unichar / UTF-8 *********************************************/
88
89typedef int unichar; // TODO
90
Paul Sokolovskyb0bb4582014-06-14 06:18:34 +030091unichar utf8_get_char(const byte *s);
92const byte *utf8_next_char(const byte *s);
Damien429d7192013-10-04 19:53:11 +010093
ian-v5fd8fd22014-01-06 13:51:53 -080094bool unichar_isspace(unichar c);
95bool unichar_isalpha(unichar c);
96bool unichar_isprint(unichar c);
97bool unichar_isdigit(unichar c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +020098bool unichar_isxdigit(unichar c);
Kim Bautersa3f4b832014-05-31 07:30:03 +010099bool unichar_isupper(unichar c);
100bool unichar_islower(unichar c);
Paul Sokolovsky69135212014-05-10 19:47:41 +0300101unichar unichar_tolower(unichar c);
102unichar unichar_toupper(unichar c);
Damien429d7192013-10-04 19:53:11 +0100103
Damien429d7192013-10-04 19:53:11 +0100104/** variable string *********************************************/
105
Damien68f59a92013-10-20 14:39:58 +0100106typedef struct _vstr_t {
Damien George280e7202014-03-15 14:33:09 +0000107 uint alloc;
108 uint len;
Damien68f59a92013-10-20 14:39:58 +0100109 char *buf;
Damien George51047752014-02-26 17:40:52 +0000110 bool had_error : 1;
111 bool fixed_buf : 1;
Damien68f59a92013-10-20 14:39:58 +0100112} vstr_t;
Damien429d7192013-10-04 19:53:11 +0100113
Damien George354d15a2014-02-06 21:11:19 +0000114// convenience macro to declare a vstr with a fixed size buffer on the stack
115#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
116
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200117void vstr_init(vstr_t *vstr, int alloc);
Damien George354d15a2014-02-06 21:11:19 +0000118void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
Damien68f59a92013-10-20 14:39:58 +0100119void vstr_clear(vstr_t *vstr);
Damien8b3a7c22013-10-23 20:20:17 +0100120vstr_t *vstr_new(void);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200121vstr_t *vstr_new_size(int alloc);
Damien429d7192013-10-04 19:53:11 +0100122void vstr_free(vstr_t *vstr);
123void vstr_reset(vstr_t *vstr);
ian-v5fd8fd22014-01-06 13:51:53 -0800124bool vstr_had_error(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +0100125char *vstr_str(vstr_t *vstr);
126int vstr_len(vstr_t *vstr);
127void vstr_hint_size(vstr_t *vstr, int size);
Damien George354d15a2014-02-06 21:11:19 +0000128char *vstr_extend(vstr_t *vstr, int size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200129bool vstr_set_size(vstr_t *vstr, int size);
130bool vstr_shrink(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +0100131char *vstr_add_len(vstr_t *vstr, int len);
Damien68f59a92013-10-20 14:39:58 +0100132void vstr_add_byte(vstr_t *vstr, byte v);
133void vstr_add_char(vstr_t *vstr, unichar chr);
Damien429d7192013-10-04 19:53:11 +0100134void vstr_add_str(vstr_t *vstr, const char *str);
135void vstr_add_strn(vstr_t *vstr, const char *str, int len);
Damien68f59a92013-10-20 14:39:58 +0100136//void vstr_add_le16(vstr_t *vstr, unsigned short v);
137//void vstr_add_le32(vstr_t *vstr, unsigned int v);
Damien George280e7202014-03-15 14:33:09 +0000138void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b);
139void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr);
140void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut);
141void vstr_cut_tail_bytes(vstr_t *vstr, uint bytes_to_cut);
142void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut);
Damien2f06c572013-11-03 18:20:56 +0000143void vstr_printf(vstr_t *vstr, const char *fmt, ...);
Damien96a0add2013-11-03 18:30:10 +0000144
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200145/** non-dynamic size-bounded variable buffer/string *************/
146
147#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
Damien Georgee09ffa12014-02-05 23:57:48 +0000148#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200149#define CHECKBUF_APPEND(buf, src, src_len) \
150 { int l = MIN(src_len, buf##_len); \
151 memcpy(buf##_p, src, l); \
152 buf##_len -= l; \
153 buf##_p += l; }
154#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
155#define CHECKBUF_LEN(buf) (buf##_p - buf)
156
Damien96a0add2013-11-03 18:30:10 +0000157#ifdef va_start
Damien2f06c572013-11-03 18:20:56 +0000158void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
Damien96a0add2013-11-03 18:30:10 +0000159#endif
Damien429d7192013-10-04 19:53:11 +0100160
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200161// Debugging helpers
162int DEBUG_printf(const char *fmt, ...);
163
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300164extern uint mp_verbose_flag;
165
Damien429d7192013-10-04 19:53:11 +0100166#endif // _INCLUDED_MINILIB_H