Skip to content

Commit e1199ec

Browse files
committed
py, lexer: Add allocation policy config; return NULL if can't allocate.
1 parent 1b82e9a commit e1199ec

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

py/lexer.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ STATIC void next_char(mp_lexer_t *lex) {
210210

211211
void indent_push(mp_lexer_t *lex, uint indent) {
212212
if (lex->num_indent_level >= lex->alloc_indent_level) {
213-
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level * 2);
214-
lex->alloc_indent_level *= 2;
213+
// TODO use m_renew_maybe and somehow indicate an error if it fails... probably by using MP_TOKEN_MEMORY_ERROR
214+
lex->indent_level = m_renew(uint16_t, lex->indent_level, lex->alloc_indent_level, lex->alloc_indent_level + MP_ALLOC_LEXEL_INDENT_INC);
215+
lex->alloc_indent_level += MP_ALLOC_LEXEL_INDENT_INC;
215216
}
216217
lex->indent_level[lex->num_indent_level++] = indent;
217218
}
@@ -696,7 +697,15 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, mp_token_t *tok, bool firs
696697
}
697698

698699
mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_char_t stream_next_char, mp_lexer_stream_close_t stream_close) {
699-
mp_lexer_t *lex = m_new(mp_lexer_t, 1);
700+
mp_lexer_t *lex = m_new_maybe(mp_lexer_t, 1);
701+
702+
// check for memory allocation error
703+
if (lex == NULL) {
704+
if (stream_close) {
705+
stream_close(stream_data);
706+
}
707+
return NULL;
708+
}
700709

701710
lex->source_name = src_name;
702711
lex->stream_data = stream_data;
@@ -706,12 +715,20 @@ mp_lexer_t *mp_lexer_new(qstr src_name, void *stream_data, mp_lexer_stream_next_
706715
lex->column = 1;
707716
lex->emit_dent = 0;
708717
lex->nested_bracket_level = 0;
709-
lex->alloc_indent_level = 16;
718+
lex->alloc_indent_level = MP_ALLOC_LEXER_INDENT_INIT;
710719
lex->num_indent_level = 1;
711-
lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
712-
lex->indent_level[0] = 0;
720+
lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level);
713721
vstr_init(&lex->vstr, 32);
714722

723+
// check for memory allocation error
724+
if (lex->indent_level == NULL || vstr_had_error(&lex->vstr)) {
725+
mp_lexer_free(lex);
726+
return NULL;
727+
}
728+
729+
// store sentinel for first indentation level
730+
lex->indent_level[0] = 0;
731+
715732
// preload characters
716733
lex->chr0 = stream_next_char(stream_data);
717734
lex->chr1 = stream_next_char(stream_data);

py/mpconfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
/*****************************************************************************/
3737
/* Memory allocation policy */
3838

39+
// Initial amount for lexer indentation level
40+
#ifndef MP_ALLOC_LEXER_INDENT_INIT
41+
#define MP_ALLOC_LEXER_INDENT_INIT (10)
42+
#endif
43+
44+
// Increment for lexer indentation level
45+
#ifndef MP_ALLOC_LEXEL_INDENT_INC
46+
#define MP_ALLOC_LEXEL_INDENT_INC (8)
47+
#endif
48+
3949
// Initial amount for parse rule stack
4050
#ifndef MP_ALLOC_PARSE_RULE_INIT
4151
#define MP_ALLOC_PARSE_RULE_INIT (64)

0 commit comments

Comments
 (0)