misc.h 6.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * 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.
 */

Damien's avatar
Damien committed
27
28
29
30
31
32
33
// a mini library of useful types and functions

#ifndef _INCLUDED_MINILIB_H
#define _INCLUDED_MINILIB_H

/** types *******************************************************/

34
#include <stdbool.h>
Damien's avatar
Damien committed
35
36
37
38

typedef unsigned char byte;
typedef unsigned int uint;

39
40
/** generic ops *************************************************/

41
#ifndef MIN
42
#define MIN(x, y) ((x) < (y) ? (x) : (y))
43
44
#endif
#ifndef MAX
45
#define MAX(x, y) ((x) > (y) ? (x) : (y))
46
#endif
47

Damien's avatar
Damien committed
48
49
/** memomry allocation ******************************************/

50
51
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)

Damien's avatar
Damien committed
52
53
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
54
55
#define m_new_obj(type) (m_new(type, 1))
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
56
#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)))
57
58
59
60
61
#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
62
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
63
#define m_renew_maybe(type, ptr, old_num, new_num) ((type*)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
64
65
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
John R. Lenton's avatar
John R. Lenton committed
66
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien's avatar
Damien committed
67
68

void *m_malloc(int num_bytes);
69
void *m_malloc_maybe(int num_bytes);
70
void *m_malloc_with_finaliser(int num_bytes);
Damien's avatar
Damien committed
71
void *m_malloc0(int num_bytes);
72
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
73
void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes);
74
void m_free(void *ptr, int num_bytes);
75
void *m_malloc_fail(int num_bytes);
Damien's avatar
Damien committed
76

77
int m_get_total_bytes_allocated(void);
78
int m_get_current_bytes_allocated(void);
79
int m_get_peak_bytes_allocated(void);
Damien's avatar
Damien committed
80

81
82
83
84
85
/** array helpers ***********************************************/

// get the number of elements in a fixed-size array
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

Damien's avatar
Damien committed
86
87
88
89
/** unichar / UTF-8 *********************************************/

typedef int unichar; // TODO

90
91
unichar utf8_get_char(const char *s);
char *utf8_next_char(const char *s);
Damien's avatar
Damien committed
92

93
94
95
96
bool unichar_isspace(unichar c);
bool unichar_isalpha(unichar c);
bool unichar_isprint(unichar c);
bool unichar_isdigit(unichar c);
97
bool unichar_isxdigit(unichar c);
Damien's avatar
Damien committed
98
99
100

/** variable string *********************************************/

Damien's avatar
Damien committed
101
typedef struct _vstr_t {
102
103
    uint alloc;
    uint len;
Damien's avatar
Damien committed
104
    char *buf;
105
106
    bool had_error : 1;
    bool fixed_buf : 1;
Damien's avatar
Damien committed
107
108
} vstr_t;

109
110
111
// 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);

112
void vstr_init(vstr_t *vstr, int alloc);
113
void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
Damien's avatar
Damien committed
114
void vstr_clear(vstr_t *vstr);
115
vstr_t *vstr_new(void);
116
vstr_t *vstr_new_size(int alloc);
Damien's avatar
Damien committed
117
118
void vstr_free(vstr_t *vstr);
void vstr_reset(vstr_t *vstr);
119
bool vstr_had_error(vstr_t *vstr);
Damien's avatar
Damien committed
120
121
122
char *vstr_str(vstr_t *vstr);
int vstr_len(vstr_t *vstr);
void vstr_hint_size(vstr_t *vstr, int size);
123
char *vstr_extend(vstr_t *vstr, int size);
124
125
bool vstr_set_size(vstr_t *vstr, int size);
bool vstr_shrink(vstr_t *vstr);
Damien's avatar
Damien committed
126
char *vstr_add_len(vstr_t *vstr, int len);
Damien's avatar
Damien committed
127
128
void vstr_add_byte(vstr_t *vstr, byte v);
void vstr_add_char(vstr_t *vstr, unichar chr);
Damien's avatar
Damien committed
129
130
void vstr_add_str(vstr_t *vstr, const char *str);
void vstr_add_strn(vstr_t *vstr, const char *str, int len);
Damien's avatar
Damien committed
131
132
//void vstr_add_le16(vstr_t *vstr, unsigned short v);
//void vstr_add_le32(vstr_t *vstr, unsigned int v);
133
134
135
136
137
void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b);
void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr);
void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut);
void vstr_cut_tail_bytes(vstr_t *vstr, uint bytes_to_cut);
void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut);
138
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
139

140
141
142
/** non-dynamic size-bounded variable buffer/string *************/

#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
143
#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
144
145
146
147
148
149
150
151
#define CHECKBUF_APPEND(buf, src, src_len) \
        { int l = MIN(src_len, buf##_len); \
        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)

152
#ifdef va_start
153
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
154
#endif
Damien's avatar
Damien committed
155

156
157
158
// Debugging helpers
int DEBUG_printf(const char *fmt, ...);

Damien's avatar
Damien committed
159
#endif // _INCLUDED_MINILIB_H