Skip to content

Commit 6902eed

Browse files
committed
py: Add m_malloc_fail function to handle memory allocation error.
A malloc/realloc fail now throws MemoryError.
1 parent 072cf02 commit 6902eed

7 files changed

Lines changed: 20 additions & 6 deletions

File tree

py/malloc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void *m_malloc(int num_bytes) {
4141
}
4242
void *ptr = malloc(num_bytes);
4343
if (ptr == NULL) {
44-
printf("could not allocate memory, allocating %d bytes\n", num_bytes);
45-
return NULL;
44+
return m_malloc_fail(num_bytes);
4645
}
4746
#if MICROPY_MEM_STATS
4847
total_bytes_allocated += num_bytes;
@@ -68,8 +67,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
6867
}
6968
void *new_ptr = realloc(ptr, new_num_bytes);
7069
if (new_ptr == NULL) {
71-
printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
72-
return NULL;
70+
return m_malloc_fail(new_num_bytes);
7371
}
7472
#if MICROPY_MEM_STATS
7573
// At first thought, "Total bytes allocated" should only grow,

py/misc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void *m_malloc(int num_bytes);
3636
void *m_malloc0(int num_bytes);
3737
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
3838
void m_free(void *ptr, int num_bytes);
39+
void *m_malloc_fail(int num_bytes);
3940

4041
int m_get_total_bytes_allocated(void);
4142
int m_get_current_bytes_allocated(void);

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ extern const struct _mp_obj_bool_t mp_const_false_obj;
306306
extern const struct _mp_obj_bool_t mp_const_true_obj;
307307
extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
308308
extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
309+
extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
309310
extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
310311

311312
// General API for objects

py/objexcept.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ typedef struct _mp_obj_exception_t {
1717
mp_obj_tuple_t args;
1818
} mp_obj_exception_t;
1919

20+
// Instance of MemoryError exception - needed by mp_malloc_fail
21+
const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, MP_OBJ_NULL, {{&mp_type_tuple}, 0}};
22+
2023
// Instance of GeneratorExit exception - needed by generator.close()
2124
// This would belong to objgenerator.c, but to keep mp_obj_exception_t
2225
// definition module-private so far, have it here.

py/runtime.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,11 @@ void mp_globals_set(mp_map_t *m) {
10341034
map_globals = m;
10351035
}
10361036

1037+
void *m_malloc_fail(int num_bytes) {
1038+
DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
1039+
nlr_jump((mp_obj_t)&mp_const_MemoryError_obj);
1040+
}
1041+
10371042
// these must correspond to the respective enum
10381043
void *const mp_fun_table[MP_F_NUMBER_OF] = {
10391044
mp_load_const_dec,

py/vm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,8 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
849849
// set file and line number that the exception occurred at
850850
// TODO: don't set traceback for exceptions re-raised by END_FINALLY.
851851
// But consider how to handle nested exceptions.
852-
// TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj)
853-
if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj) {
852+
// TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj and MemoryError_obj)
853+
if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj && nlr.ret_val != &mp_const_MemoryError_obj) {
854854
machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
855855
qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
856856
qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);

tests/basics/memoryerror.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
l = list(range(10000))
2+
try:
3+
100000000 * l
4+
except MemoryError:
5+
print('MemoryError')
6+
print(len(l), l[0], l[-1])

0 commit comments

Comments
 (0)