summaryrefslogtreecommitdiffstatshomepage
path: root/py/malloc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-25 23:37:55 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-25 23:37:55 +0100
commitdaab651c5ca0d7198f6db2fa0abe2399fc08c69e (patch)
tree674cc7e1bacf4e0a12fabd72da995aa7c2a85315 /py/malloc.c
parent5be40afb4c50e6a71a0b6c7d0e7b63c4abe5b7b5 (diff)
downloadmicropython-daab651c5ca0d7198f6db2fa0abe2399fc08c69e.tar.gz
micropython-daab651c5ca0d7198f6db2fa0abe2399fc08c69e.zip
py, gc: Zero out newly allocated blocks in the GC.
Also add some more debugging output to gc_dump_alloc_table(). Now that newly allocated heap is always zero'd, maybe we just make this a policy for the uPy API to keep it simple (ie any new implementation of memory allocation must zero all allocations). This follows the D language philosophy. Before this patch, a previously used memory block which had pointers in it may still retain those pointers if the new user of that block does not actually use the entire block. Eg, if I want 5 blocks worth of heap, I actually get 8 (round up to nearest 4). Then I never use the last 3, so they keep their old values, which may be pointers pointing to the heap, hence preventing GC. In rare (or maybe not that rare) cases, this leads to long, unintentional "linked lists" within the GC'd heap, filling it up completely. It's pretty rare, because you have to reuse exactly that memory which is part of this "linked list", and reuse it in just the right way. This should fix issue #522, and might have something to do with issue #510.
Diffstat (limited to 'py/malloc.c')
-rw-r--r--py/malloc.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/py/malloc.c b/py/malloc.c
index db2578d9ad..76c3fe6cad 100644
--- a/py/malloc.c
+++ b/py/malloc.c
@@ -88,9 +88,13 @@ void *m_malloc_with_finaliser(int num_bytes) {
void *m_malloc0(int num_bytes) {
void *ptr = m_malloc(num_bytes);
+#if MICROPY_ENABLE_GC
+ // the GC already zeros out all memory
+#else
if (ptr != NULL) {
memset(ptr, 0, num_bytes);
}
+#endif
return ptr;
}