blob: 3f538b98eb310e7559dc5dfb9c76bc3ae62d3239 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001// a mini library of useful types and functions
2
3#ifndef _INCLUDED_MINILIB_H
4#define _INCLUDED_MINILIB_H
5
6/** types *******************************************************/
7
ian-v5fd8fd22014-01-06 13:51:53 -08008#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01009
10typedef unsigned char byte;
11typedef unsigned int uint;
12
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020013/** generic ops *************************************************/
14
Damien Georgee09ffa12014-02-05 23:57:48 +000015#ifndef MIN
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020016#define MIN(x, y) ((x) < (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000017#endif
18#ifndef MAX
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020019#define MAX(x, y) ((x) > (y) ? (x) : (y))
Damien Georgee09ffa12014-02-05 23:57:48 +000020#endif
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020021
Damien429d7192013-10-04 19:53:11 +010022/** memomry allocation ******************************************/
23
Damien732407f2013-12-29 19:33:23 +000024// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
25
Damien429d7192013-10-04 19:53:11 +010026#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
27#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
Damiend99b0522013-12-21 18:17:45 +000028#define m_new_obj(type) (m_new(type, 1))
29#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien George12bab722014-04-05 20:35:48 +010030#if MICROPY_ENABLE_FINALISER
31#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type))))
32#else
33#define m_new_obj_with_finaliser(type) m_new_obj(type)
34#endif
Damien732407f2013-12-29 19:33:23 +000035#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
36#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
37#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
John R. Lenton07205ec2014-01-13 02:31:00 +000038#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 +010039
Damien429d7192013-10-04 19:53:11 +010040void *m_malloc(int num_bytes);
Damien Georgef0954e32014-04-10 14:38:25 +010041void *m_malloc_maybe(int num_bytes);
Damien George12bab722014-04-05 20:35:48 +010042void *m_malloc_with_finaliser(int num_bytes);
Damien429d7192013-10-04 19:53:11 +010043void *m_malloc0(int num_bytes);
Damien732407f2013-12-29 19:33:23 +000044void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
45void m_free(void *ptr, int num_bytes);
Damien George6902eed2014-04-04 10:52:59 +000046void *m_malloc_fail(int num_bytes);
Damien429d7192013-10-04 19:53:11 +010047
Damien8b3a7c22013-10-23 20:20:17 +010048int m_get_total_bytes_allocated(void);
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020049int m_get_current_bytes_allocated(void);
Paul Sokolovsky780f5552014-01-01 23:42:21 +020050int m_get_peak_bytes_allocated(void);
Damien429d7192013-10-04 19:53:11 +010051
52/** unichar / UTF-8 *********************************************/
53
54typedef int unichar; // TODO
55
Damien George8cc96a32013-12-30 18:23:50 +000056unichar utf8_get_char(const char *s);
57char *utf8_next_char(const char *s);
Damien429d7192013-10-04 19:53:11 +010058
ian-v5fd8fd22014-01-06 13:51:53 -080059bool unichar_isspace(unichar c);
60bool unichar_isalpha(unichar c);
61bool unichar_isprint(unichar c);
62bool unichar_isdigit(unichar c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +020063bool unichar_isxdigit(unichar c);
Damien429d7192013-10-04 19:53:11 +010064
Damien429d7192013-10-04 19:53:11 +010065/** variable string *********************************************/
66
Damien68f59a92013-10-20 14:39:58 +010067typedef struct _vstr_t {
Damien George280e7202014-03-15 14:33:09 +000068 uint alloc;
69 uint len;
Damien68f59a92013-10-20 14:39:58 +010070 char *buf;
Damien George51047752014-02-26 17:40:52 +000071 bool had_error : 1;
72 bool fixed_buf : 1;
Damien68f59a92013-10-20 14:39:58 +010073} vstr_t;
Damien429d7192013-10-04 19:53:11 +010074
Damien George354d15a2014-02-06 21:11:19 +000075// convenience macro to declare a vstr with a fixed size buffer on the stack
76#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
77
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020078void vstr_init(vstr_t *vstr, int alloc);
Damien George354d15a2014-02-06 21:11:19 +000079void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
Damien68f59a92013-10-20 14:39:58 +010080void vstr_clear(vstr_t *vstr);
Damien8b3a7c22013-10-23 20:20:17 +010081vstr_t *vstr_new(void);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020082vstr_t *vstr_new_size(int alloc);
Damien429d7192013-10-04 19:53:11 +010083void vstr_free(vstr_t *vstr);
84void vstr_reset(vstr_t *vstr);
ian-v5fd8fd22014-01-06 13:51:53 -080085bool vstr_had_error(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +010086char *vstr_str(vstr_t *vstr);
87int vstr_len(vstr_t *vstr);
88void vstr_hint_size(vstr_t *vstr, int size);
Damien George354d15a2014-02-06 21:11:19 +000089char *vstr_extend(vstr_t *vstr, int size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020090bool vstr_set_size(vstr_t *vstr, int size);
91bool vstr_shrink(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +010092char *vstr_add_len(vstr_t *vstr, int len);
Damien68f59a92013-10-20 14:39:58 +010093void vstr_add_byte(vstr_t *vstr, byte v);
94void vstr_add_char(vstr_t *vstr, unichar chr);
Damien429d7192013-10-04 19:53:11 +010095void vstr_add_str(vstr_t *vstr, const char *str);
96void vstr_add_strn(vstr_t *vstr, const char *str, int len);
Damien68f59a92013-10-20 14:39:58 +010097//void vstr_add_le16(vstr_t *vstr, unsigned short v);
98//void vstr_add_le32(vstr_t *vstr, unsigned int v);
Damien George280e7202014-03-15 14:33:09 +000099void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b);
100void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr);
101void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut);
102void vstr_cut_tail_bytes(vstr_t *vstr, uint bytes_to_cut);
103void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut);
Damien2f06c572013-11-03 18:20:56 +0000104void vstr_printf(vstr_t *vstr, const char *fmt, ...);
Damien96a0add2013-11-03 18:30:10 +0000105
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200106/** non-dynamic size-bounded variable buffer/string *************/
107
108#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 +0000109#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200110#define CHECKBUF_APPEND(buf, src, src_len) \
111 { int l = MIN(src_len, buf##_len); \
112 memcpy(buf##_p, src, l); \
113 buf##_len -= l; \
114 buf##_p += l; }
115#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
116#define CHECKBUF_LEN(buf) (buf##_p - buf)
117
Damien96a0add2013-11-03 18:30:10 +0000118#ifdef va_start
Damien2f06c572013-11-03 18:20:56 +0000119void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
Damien96a0add2013-11-03 18:30:10 +0000120#endif
Damien429d7192013-10-04 19:53:11 +0100121
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200122// Debugging helpers
123int DEBUG_printf(const char *fmt, ...);
124
Damien429d7192013-10-04 19:53:11 +0100125#endif // _INCLUDED_MINILIB_H