Skip to content

Commit 4f7e9f5

Browse files
committed
Implement del
1 parent 094d450 commit 4f7e9f5

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

py/gc.c

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

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

8+
#include "misc.h"
9+
#include "qstr.h"
10+
#include "obj.h"
11+
712
#if MICROPY_ENABLE_GC
813

914
#if 0 // print debugging info
@@ -20,7 +25,9 @@ typedef unsigned char byte;
2025
#define STACK_SIZE (64) // tunable; minimum is 1
2126

2227
STATIC byte *gc_alloc_table_start;
28+
STATIC byte *gc_mpobj_table_start;
2329
STATIC machine_uint_t gc_alloc_table_byte_len;
30+
STATIC machine_uint_t gc_mpobj_table_byte_len;
2431
STATIC machine_uint_t *gc_pool_start;
2532
STATIC machine_uint_t *gc_pool_end;
2633

@@ -58,6 +65,10 @@ STATIC machine_uint_t *gc_sp;
5865
#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
5966
#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
6067

68+
#define ATB_SET_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] |= (1<<(block%8)); } while (0)
69+
#define ATB_CLR_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] &= (~(1<<(block%8))); } while (0)
70+
#define ATB_IS_MPOBJ(block) ((gc_mpobj_table_start[(block) / 8]>>(block%8))&0x01)
71+
6172
#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
6273
#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
6374
#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
@@ -72,14 +83,21 @@ void gc_init(void *start, void *end) {
7283
machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
7384
gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
7485
gc_alloc_table_start = (byte*)start;
75-
machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2;
86+
87+
gc_mpobj_table_byte_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2)/8;
88+
gc_mpobj_table_start = gc_alloc_table_start+gc_alloc_table_byte_len;
89+
90+
machine_uint_t gc_pool_block_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2) -(gc_mpobj_table_byte_len / BYTES_PER_BLOCK);
7691
machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
7792
gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
7893
gc_pool_end = end;
7994

8095
// clear ATBs
8196
memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
8297

98+
// clear MPOBJ flags
99+
memset(gc_mpobj_table_start, 0, gc_mpobj_table_byte_len);
100+
83101
// allocate first block because gc_pool_start points there and it will never
84102
// be freed, so allocating 1 block with null pointers will minimise memory loss
85103
ATB_FREE_TO_HEAD(0);
@@ -156,6 +174,14 @@ STATIC void gc_sweep(void) {
156174
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
157175
switch (ATB_GET_KIND(block)) {
158176
case AT_HEAD:
177+
if (ATB_IS_MPOBJ(block)) {
178+
ATB_CLR_MPOBJ(block); // clear mpobj flag
179+
mp_obj_t *self = (mp_obj_t*)PTR_FROM_BLOCK(block);
180+
mp_obj_type_t *type= mp_obj_get_type(self);
181+
if (type->del != NULL) {
182+
type->del(self);
183+
}
184+
}
159185
free_tail = 1;
160186
// fall through to free the head
161187

@@ -236,7 +262,7 @@ void gc_info(gc_info_t *info) {
236262
info->free *= BYTES_PER_BLOCK;
237263
}
238264

239-
void *gc_alloc(machine_uint_t n_bytes) {
265+
void *_gc_alloc(machine_uint_t n_bytes, bool is_mpobj) {
240266
machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
241267
DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
242268

@@ -279,6 +305,10 @@ void *gc_alloc(machine_uint_t n_bytes) {
279305
// mark first block as used head
280306
ATB_FREE_TO_HEAD(start_block);
281307

308+
if (is_mpobj) {
309+
ATB_SET_MPOBJ(start_block);
310+
}
311+
282312
// mark rest of blocks as used tail
283313
// TODO for a run of many blocks can make this more efficient
284314
for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
@@ -289,6 +319,14 @@ void *gc_alloc(machine_uint_t n_bytes) {
289319
return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
290320
}
291321

322+
void *gc_alloc(machine_uint_t n_bytes) {
323+
return _gc_alloc(n_bytes, false);
324+
}
325+
326+
void *gc_alloc_mp_obj(machine_uint_t n_bytes) {
327+
return _gc_alloc(n_bytes, true);
328+
}
329+
292330
// force the freeing of a piece of memory
293331
void gc_free(void *ptr_in) {
294332
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
@@ -53,6 +54,24 @@ void *m_malloc(int num_bytes) {
5354
return ptr;
5455
}
5556

57+
void *m_malloc_mp_obj(int num_bytes) {
58+
if (num_bytes == 0) {
59+
return NULL;
60+
}
61+
void *ptr = malloc_mp_obj(num_bytes);
62+
if (ptr == NULL) {
63+
printf("could not allocate memory, allocating %d bytes\n", num_bytes);
64+
return NULL;
65+
}
66+
#if MICROPY_MEM_STATS
67+
total_bytes_allocated += num_bytes;
68+
current_bytes_allocated += num_bytes;
69+
UPDATE_PEAK();
70+
#endif
71+
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
72+
return ptr;
73+
}
74+
5675
void *m_malloc0(int num_bytes) {
5776
void *ptr = m_malloc(num_bytes);
5877
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/obj.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
154154
typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
155155
typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded
156156
typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded
157+
typedef void (*mp_del_fun_t)(mp_obj_t self_in);
157158

158159
typedef struct _mp_method_t {
159160
qstr name;
@@ -236,6 +237,7 @@ struct _mp_obj_type_t {
236237
237238
unpack seq list tuple
238239
*/
240+
mp_del_fun_t del;
239241
};
240242

241243
typedef struct _mp_obj_type_t mp_obj_type_t;

stm/file.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ typedef struct _pyb_file_obj_t {
1313
FIL fp;
1414
} pyb_file_obj_t;
1515

16+
void file_obj_del(mp_obj_t self_in) {
17+
pyb_file_obj_t *self = self_in;
18+
f_close(&self->fp);
19+
printf("<file del called>\n");
20+
}
21+
1622
void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
1723
printf("<file %p>", self_in);
1824
}
@@ -64,13 +70,14 @@ static const mp_obj_type_t file_obj_type = {
6470
{ &mp_type_type },
6571
.name = MP_QSTR_File,
6672
.print = file_obj_print,
73+
.del = file_obj_del,
6774
.locals_dict = (mp_obj_t)&file_locals_dict,
6875
};
6976

7077
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
7178
const char *filename = mp_obj_str_get_str(o_filename);
7279
const char *mode = mp_obj_str_get_str(o_mode);
73-
pyb_file_obj_t *self = m_new_obj(pyb_file_obj_t);
80+
pyb_file_obj_t *self = m_new_mp_obj(pyb_file_obj_t);
7481
self->base.type = &file_obj_type;
7582
if (mode[0] == 'r') {
7683
// open for reading

0 commit comments

Comments
 (0)