Skip to content

Commit 8123a33

Browse files
committed
Merge pull request adafruit#425 from iabdalkader/del
Implement del
2 parents ea13f40 + cc849f7 commit 8123a33

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

py/gc.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include <stdio.h>
22
#include <string.h>
3+
#include <stdbool.h>
34

45
#include "mpconfig.h"
56
#include "misc.h"
67
#include "gc.h"
78

9+
#include "misc.h"
10+
#include "qstr.h"
11+
#include "obj.h"
12+
#include "runtime.h"
13+
814
#if MICROPY_ENABLE_GC
915

1016
#if 0 // print debugging info
@@ -21,7 +27,9 @@ typedef unsigned char byte;
2127
#define STACK_SIZE (64) // tunable; minimum is 1
2228

2329
STATIC byte *gc_alloc_table_start;
30+
STATIC byte *gc_mpobj_table_start;
2431
STATIC machine_uint_t gc_alloc_table_byte_len;
32+
STATIC machine_uint_t gc_mpobj_table_byte_len;
2533
STATIC machine_uint_t *gc_pool_start;
2634
STATIC machine_uint_t *gc_pool_end;
2735

@@ -59,6 +67,10 @@ STATIC machine_uint_t *gc_sp;
5967
#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
6068
#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
6169

70+
#define ATB_SET_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] |= (1<<(block%8)); } while (0)
71+
#define ATB_CLR_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] &= (~(1<<(block%8))); } while (0)
72+
#define ATB_IS_MPOBJ(block) ((gc_mpobj_table_start[(block) / 8]>>(block%8))&0x01)
73+
6274
#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
6375
#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
6476
#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
@@ -73,14 +85,21 @@ void gc_init(void *start, void *end) {
7385
machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
7486
gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
7587
gc_alloc_table_start = (byte*)start;
76-
machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2;
88+
89+
gc_mpobj_table_byte_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2)/8;
90+
gc_mpobj_table_start = gc_alloc_table_start+gc_alloc_table_byte_len;
91+
92+
machine_uint_t gc_pool_block_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2) -(gc_mpobj_table_byte_len / BYTES_PER_BLOCK);
7793
machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
7894
gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
7995
gc_pool_end = end;
8096

8197
// clear ATBs
8298
memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
8399

100+
// clear MPOBJ flags
101+
memset(gc_mpobj_table_start, 0, gc_mpobj_table_byte_len);
102+
84103
// allocate first block because gc_pool_start points there and it will never
85104
// be freed, so allocating 1 block with null pointers will minimise memory loss
86105
ATB_FREE_TO_HEAD(0);
@@ -157,6 +176,16 @@ STATIC void gc_sweep(void) {
157176
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
158177
switch (ATB_GET_KIND(block)) {
159178
case AT_HEAD:
179+
if (ATB_IS_MPOBJ(block)) {
180+
mp_obj_t dest[2];
181+
mp_load_method((mp_obj_t*)PTR_FROM_BLOCK(block), MP_QSTR___del__, dest);
182+
// load_method returned a method
183+
if (dest[1] != MP_OBJ_NULL) {
184+
mp_call_method_n_kw(0, 0, dest);
185+
}
186+
// clear mpobj flag
187+
ATB_CLR_MPOBJ(block);
188+
}
160189
free_tail = 1;
161190
// fall through to free the head
162191

@@ -237,7 +266,7 @@ void gc_info(gc_info_t *info) {
237266
info->free *= BYTES_PER_BLOCK;
238267
}
239268

240-
void *gc_alloc(machine_uint_t n_bytes) {
269+
void *_gc_alloc(machine_uint_t n_bytes, bool is_mpobj) {
241270
machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
242271
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
243272

@@ -286,10 +315,23 @@ void *gc_alloc(machine_uint_t n_bytes) {
286315
ATB_FREE_TO_TAIL(bl);
287316
}
288317

318+
if (is_mpobj) {
319+
// set mp_obj flag only if it has del
320+
ATB_SET_MPOBJ(start_block);
321+
}
322+
289323
// return pointer to first block
290324
return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
291325
}
292326

327+
void *gc_alloc(machine_uint_t n_bytes) {
328+
return _gc_alloc(n_bytes, false);
329+
}
330+
331+
void *gc_alloc_mp_obj(machine_uint_t n_bytes) {
332+
return _gc_alloc(n_bytes, true);
333+
}
334+
293335
// force the freeing of a piece of memory
294336
void gc_free(void *ptr_in) {
295337
machine_uint_t ptr = (machine_uint_t)ptr_in;

py/gc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ void gc_collect_root(void **ptrs, machine_uint_t len);
44
void gc_collect_end(void);
55
void gc_collect(void);
66
void *gc_alloc(machine_uint_t n_bytes);
7+
void *gc_alloc_mp_obj(machine_uint_t n_bytes);
78
void gc_free(void *ptr);
89
machine_uint_t gc_nbytes(void *ptr);
910
void *gc_realloc(void *ptr, machine_uint_t n_bytes);

py/malloc.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ STATIC int peak_bytes_allocated = 0;
3131
#undef free
3232
#undef realloc
3333
#define malloc gc_alloc
34+
#define malloc_mp_obj gc_alloc_mp_obj
3435
#define free gc_free
3536
#define realloc gc_realloc
3637
#endif // MICROPY_ENABLE_GC
@@ -52,6 +53,24 @@ void *m_malloc(int num_bytes) {
5253
return ptr;
5354
}
5455

56+
void *m_malloc_mp_obj(int num_bytes) {
57+
if (num_bytes == 0) {
58+
return NULL;
59+
}
60+
void *ptr = malloc_mp_obj(num_bytes);
61+
if (ptr == NULL) {
62+
printf("could not allocate memory, allocating %d bytes\n", num_bytes);
63+
return NULL;
64+
}
65+
#if MICROPY_MEM_STATS
66+
total_bytes_allocated += num_bytes;
67+
current_bytes_allocated += num_bytes;
68+
UPDATE_PEAK();
69+
#endif
70+
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
71+
return ptr;
72+
}
73+
5574
void *m_malloc0(int num_bytes) {
5675
void *ptr = m_malloc(num_bytes);
5776
if (ptr != NULL) {

py/misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ typedef unsigned int uint;
2727
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
2828
#define m_new_obj(type) (m_new(type, 1))
2929
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
30+
#define m_new_mp_obj(type)((type*)(m_malloc_mp_obj(sizeof(type))))
3031
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
3132
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
3233
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
3334
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
3435

3536
void *m_malloc(int num_bytes);
37+
void *m_malloc_mp_obj(int num_bytes);
3638
void *m_malloc0(int num_bytes);
3739
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
3840
void m_free(void *ptr, int num_bytes);

py/qstrdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Q(__sub__)
2727
Q(__repr__)
2828
Q(__str__)
2929
Q(__getattr__)
30+
Q(__del__)
3031

3132
Q(micropython)
3233
Q(byte_code)

stm/file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ STATIC const mp_map_elem_t file_locals_dict_table[] = {
5656
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&file_obj_read_obj },
5757
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&file_obj_write_obj },
5858
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&file_obj_close_obj },
59+
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&file_obj_close_obj },
5960
};
6061

6162
STATIC MP_DEFINE_CONST_DICT(file_locals_dict, file_locals_dict_table);
@@ -70,7 +71,7 @@ static const mp_obj_type_t file_obj_type = {
7071
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
7172
const char *filename = mp_obj_str_get_str(o_filename);
7273
const char *mode = mp_obj_str_get_str(o_mode);
73-
pyb_file_obj_t *self = m_new_obj(pyb_file_obj_t);
74+
pyb_file_obj_t *self = m_new_mp_obj(pyb_file_obj_t);
7475
self->base.type = &file_obj_type;
7576
if (mode[0] == 'r') {
7677
// open for reading

0 commit comments

Comments
 (0)