Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
gc.cpp@0:4ab1392a0142, 2012-10-07 (annotated)
- Committer:
- gignops
- Date:
- Sun Oct 07 11:49:09 2012 +0000
- Revision:
- 0:4ab1392a0142
This project is an adaptation of tinypy code. The aim is to run python code on Mbed.; ; Have fun !
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| gignops | 0:4ab1392a0142 | 1 | #include "gc.h" |
| gignops | 0:4ab1392a0142 | 2 | |
| gignops | 0:4ab1392a0142 | 3 | |
| gignops | 0:4ab1392a0142 | 4 | |
| gignops | 0:4ab1392a0142 | 5 | void tp_grey(TP,tp_obj v) { |
| gignops | 0:4ab1392a0142 | 6 | if (v.type < TP_STRING || (!v.gci.data) || *v.gci.data) { return; } |
| gignops | 0:4ab1392a0142 | 7 | *v.gci.data = 1; |
| gignops | 0:4ab1392a0142 | 8 | if (v.type == TP_STRING || v.type == TP_DATA) { |
| gignops | 0:4ab1392a0142 | 9 | _tp_list_appendx(tp,tp->black,v); |
| gignops | 0:4ab1392a0142 | 10 | return; |
| gignops | 0:4ab1392a0142 | 11 | } |
| gignops | 0:4ab1392a0142 | 12 | _tp_list_appendx(tp,tp->grey,v); |
| gignops | 0:4ab1392a0142 | 13 | } |
| gignops | 0:4ab1392a0142 | 14 | |
| gignops | 0:4ab1392a0142 | 15 | void tp_follow(TP,tp_obj v) { |
| gignops | 0:4ab1392a0142 | 16 | int type = v.type; |
| gignops | 0:4ab1392a0142 | 17 | if (type == TP_LIST) { |
| gignops | 0:4ab1392a0142 | 18 | int n; |
| gignops | 0:4ab1392a0142 | 19 | for (n=0; n<v.list.val->len; n++) { |
| gignops | 0:4ab1392a0142 | 20 | tp_grey(tp,v.list.val->items[n]); |
| gignops | 0:4ab1392a0142 | 21 | } |
| gignops | 0:4ab1392a0142 | 22 | } |
| gignops | 0:4ab1392a0142 | 23 | if (type == TP_DICT) { |
| gignops | 0:4ab1392a0142 | 24 | int i; |
| gignops | 0:4ab1392a0142 | 25 | for (i=0; i<v.dict.val->len; i++) { |
| gignops | 0:4ab1392a0142 | 26 | int n = _tp_dict_next(tp,v.dict.val); |
| gignops | 0:4ab1392a0142 | 27 | tp_grey(tp,v.dict.val->items[n].key); |
| gignops | 0:4ab1392a0142 | 28 | tp_grey(tp,v.dict.val->items[n].val); |
| gignops | 0:4ab1392a0142 | 29 | } |
| gignops | 0:4ab1392a0142 | 30 | } |
| gignops | 0:4ab1392a0142 | 31 | if (type == TP_FNC) { |
| gignops | 0:4ab1392a0142 | 32 | tp_grey(tp,v.fnc.info->self); |
| gignops | 0:4ab1392a0142 | 33 | tp_grey(tp,v.fnc.info->globals); |
| gignops | 0:4ab1392a0142 | 34 | } |
| gignops | 0:4ab1392a0142 | 35 | } |
| gignops | 0:4ab1392a0142 | 36 | |
| gignops | 0:4ab1392a0142 | 37 | void tp_reset(TP) { |
| gignops | 0:4ab1392a0142 | 38 | int n; |
| gignops | 0:4ab1392a0142 | 39 | _tp_list *tmp; |
| gignops | 0:4ab1392a0142 | 40 | for (n=0; n<tp->black->len; n++) { |
| gignops | 0:4ab1392a0142 | 41 | *tp->black->items[n].gci.data = 0; |
| gignops | 0:4ab1392a0142 | 42 | } |
| gignops | 0:4ab1392a0142 | 43 | tmp = tp->white; |
| gignops | 0:4ab1392a0142 | 44 | tp->white = tp->black; |
| gignops | 0:4ab1392a0142 | 45 | tp->black = tmp; |
| gignops | 0:4ab1392a0142 | 46 | } |
| gignops | 0:4ab1392a0142 | 47 | |
| gignops | 0:4ab1392a0142 | 48 | void tp_gc_init(TP) { |
| gignops | 0:4ab1392a0142 | 49 | tp->white = _tp_list_new(); |
| gignops | 0:4ab1392a0142 | 50 | tp->strings = _tp_dict_new(); |
| gignops | 0:4ab1392a0142 | 51 | tp->grey = _tp_list_new(); |
| gignops | 0:4ab1392a0142 | 52 | tp->black = _tp_list_new(); |
| gignops | 0:4ab1392a0142 | 53 | tp->steps = 0; |
| gignops | 0:4ab1392a0142 | 54 | } |
| gignops | 0:4ab1392a0142 | 55 | |
| gignops | 0:4ab1392a0142 | 56 | void tp_gc_deinit(TP) { |
| gignops | 0:4ab1392a0142 | 57 | _tp_list_free(tp->white); |
| gignops | 0:4ab1392a0142 | 58 | _tp_dict_free(tp->strings); |
| gignops | 0:4ab1392a0142 | 59 | _tp_list_free(tp->grey); |
| gignops | 0:4ab1392a0142 | 60 | _tp_list_free(tp->black); |
| gignops | 0:4ab1392a0142 | 61 | } |
| gignops | 0:4ab1392a0142 | 62 | |
| gignops | 0:4ab1392a0142 | 63 | void tp_delete(TP,tp_obj v) { |
| gignops | 0:4ab1392a0142 | 64 | int type = v.type; |
| gignops | 0:4ab1392a0142 | 65 | if (type == TP_LIST) { |
| gignops | 0:4ab1392a0142 | 66 | _tp_list_free(v.list.val); |
| gignops | 0:4ab1392a0142 | 67 | return; |
| gignops | 0:4ab1392a0142 | 68 | } else if (type == TP_DICT) { |
| gignops | 0:4ab1392a0142 | 69 | _tp_dict_free(v.dict.val); |
| gignops | 0:4ab1392a0142 | 70 | return; |
| gignops | 0:4ab1392a0142 | 71 | } else if (type == TP_STRING) { |
| gignops | 0:4ab1392a0142 | 72 | tp_free(v.string.info); |
| gignops | 0:4ab1392a0142 | 73 | return; |
| gignops | 0:4ab1392a0142 | 74 | } else if (type == TP_DATA) { |
| gignops | 0:4ab1392a0142 | 75 | if (v.data.info->free) { |
| gignops | 0:4ab1392a0142 | 76 | v.data.info->free(tp,v); |
| gignops | 0:4ab1392a0142 | 77 | } |
| gignops | 0:4ab1392a0142 | 78 | tp_free(v.data.info); |
| gignops | 0:4ab1392a0142 | 79 | return; |
| gignops | 0:4ab1392a0142 | 80 | } else if (type == TP_FNC) { |
| gignops | 0:4ab1392a0142 | 81 | tp_free(v.fnc.info); |
| gignops | 0:4ab1392a0142 | 82 | return; |
| gignops | 0:4ab1392a0142 | 83 | } |
| gignops | 0:4ab1392a0142 | 84 | tp_raise(,"tp_delete(%s)",TP_CSTR(v)); |
| gignops | 0:4ab1392a0142 | 85 | } |
| gignops | 0:4ab1392a0142 | 86 | |
| gignops | 0:4ab1392a0142 | 87 | void tp_collect(TP) { |
| gignops | 0:4ab1392a0142 | 88 | int n; |
| gignops | 0:4ab1392a0142 | 89 | for (n=0; n<tp->white->len; n++) { |
| gignops | 0:4ab1392a0142 | 90 | tp_obj r = tp->white->items[n]; |
| gignops | 0:4ab1392a0142 | 91 | if (*r.gci.data) { continue; } |
| gignops | 0:4ab1392a0142 | 92 | if (r.type == TP_STRING) { |
| gignops | 0:4ab1392a0142 | 93 | /*this can't be moved into tp_delete, because tp_delete is |
| gignops | 0:4ab1392a0142 | 94 | also used by tp_track_s to delete redundant strings*/ |
| gignops | 0:4ab1392a0142 | 95 | _tp_dict_del(tp,tp->strings,r,"tp_collect"); |
| gignops | 0:4ab1392a0142 | 96 | } |
| gignops | 0:4ab1392a0142 | 97 | tp_delete(tp,r); |
| gignops | 0:4ab1392a0142 | 98 | } |
| gignops | 0:4ab1392a0142 | 99 | tp->white->len = 0; |
| gignops | 0:4ab1392a0142 | 100 | tp_reset(tp); |
| gignops | 0:4ab1392a0142 | 101 | } |
| gignops | 0:4ab1392a0142 | 102 | |
| gignops | 0:4ab1392a0142 | 103 | void _tp_gcinc(TP) { |
| gignops | 0:4ab1392a0142 | 104 | tp_obj v; |
| gignops | 0:4ab1392a0142 | 105 | if (!tp->grey->len) { |
| gignops | 0:4ab1392a0142 | 106 | return; |
| gignops | 0:4ab1392a0142 | 107 | } |
| gignops | 0:4ab1392a0142 | 108 | v = _tp_list_pop(tp,tp->grey,tp->grey->len-1,"_tp_gcinc"); |
| gignops | 0:4ab1392a0142 | 109 | tp_follow(tp,v); |
| gignops | 0:4ab1392a0142 | 110 | _tp_list_appendx(tp,tp->black,v); |
| gignops | 0:4ab1392a0142 | 111 | } |
| gignops | 0:4ab1392a0142 | 112 | |
| gignops | 0:4ab1392a0142 | 113 | void tp_full(TP) { |
| gignops | 0:4ab1392a0142 | 114 | while (tp->grey->len) { |
| gignops | 0:4ab1392a0142 | 115 | _tp_gcinc(tp); |
| gignops | 0:4ab1392a0142 | 116 | } |
| gignops | 0:4ab1392a0142 | 117 | tp_collect(tp); |
| gignops | 0:4ab1392a0142 | 118 | tp_follow(tp,tp->root); |
| gignops | 0:4ab1392a0142 | 119 | } |
| gignops | 0:4ab1392a0142 | 120 | |
| gignops | 0:4ab1392a0142 | 121 | void tp_gcinc(TP) { |
| gignops | 0:4ab1392a0142 | 122 | tp->steps += 1; |
| gignops | 0:4ab1392a0142 | 123 | if (tp->steps < TP_GCMAX || tp->grey->len > 0) { |
| gignops | 0:4ab1392a0142 | 124 | _tp_gcinc(tp); _tp_gcinc(tp); |
| gignops | 0:4ab1392a0142 | 125 | } |
| gignops | 0:4ab1392a0142 | 126 | if (tp->steps < TP_GCMAX || tp->grey->len > 0) { return; } |
| gignops | 0:4ab1392a0142 | 127 | tp->steps = 0; |
| gignops | 0:4ab1392a0142 | 128 | tp_full(tp); |
| gignops | 0:4ab1392a0142 | 129 | return; |
| gignops | 0:4ab1392a0142 | 130 | } |
| gignops | 0:4ab1392a0142 | 131 | |
| gignops | 0:4ab1392a0142 | 132 | tp_obj tp_track(TP,tp_obj v) { |
| gignops | 0:4ab1392a0142 | 133 | if (v.type == TP_STRING) { |
| gignops | 0:4ab1392a0142 | 134 | int i = _tp_dict_find(tp,tp->strings,v); |
| gignops | 0:4ab1392a0142 | 135 | if (i != -1) { |
| gignops | 0:4ab1392a0142 | 136 | tp_delete(tp,v); |
| gignops | 0:4ab1392a0142 | 137 | v = tp->strings->items[i].key; |
| gignops | 0:4ab1392a0142 | 138 | tp_grey(tp,v); |
| gignops | 0:4ab1392a0142 | 139 | return v; |
| gignops | 0:4ab1392a0142 | 140 | } |
| gignops | 0:4ab1392a0142 | 141 | _tp_dict_setx(tp,tp->strings,v,tp_True); |
| gignops | 0:4ab1392a0142 | 142 | } |
| gignops | 0:4ab1392a0142 | 143 | tp_gcinc(tp); |
| gignops | 0:4ab1392a0142 | 144 | tp_grey(tp,v); |
| gignops | 0:4ab1392a0142 | 145 | return v; |
| gignops | 0:4ab1392a0142 | 146 | } |
| gignops | 0:4ab1392a0142 | 147 | |
| gignops | 0:4ab1392a0142 | 148 | /**/ |
| gignops | 0:4ab1392a0142 | 149 |