diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-26 22:23:42 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-26 22:23:42 +0100 |
commit | 32bef315be8e56ad2d7f69223fe7b9606893b6ab (patch) | |
tree | d16ffb11f6cdc38d789077059eecced3bd647c98 | |
parent | dbc81df5d4822b3a0fc31ea1c5684b0101e193dd (diff) | |
download | micropython-32bef315be8e56ad2d7f69223fe7b9606893b6ab.tar.gz micropython-32bef315be8e56ad2d7f69223fe7b9606893b6ab.zip |
py, gc: Only zero out the extra bytes at the end of the heap chunk.
This is a small optimisation to zero out only the additional bytes that
the caller did not ask for.
-rw-r--r-- | py/gc.c | 12 | ||||
-rw-r--r-- | py/malloc.c | 4 |
2 files changed, 6 insertions, 10 deletions
@@ -366,17 +366,17 @@ found: // get pointer to first block void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); - // zero out the newly allocated blocks + // zero out the additional bytes of the newly allocated blocks // This is needed because the blocks may have previously held pointers // to the heap and will not be set to something else if the caller // doesn't actually use the entire block. As such they will continue // to point to the heap and may prevent other blocks from being reclaimed. - memset(ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK); + memset(ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes); #if MICROPY_ENABLE_FINALISER if (has_finaliser) { - // clear type pointer in case it is never set (now done above in memset) - //((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL; + // clear type pointer in case it is never set + ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL; // set mp_obj flag only if it has a finaliser FTB_SET(start_block); } @@ -534,8 +534,8 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) { ATB_FREE_TO_TAIL(bl); } - // zero out the newly allocated blocks (see comment above in gc_alloc) - memset(ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK); + // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc) + memset(ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes); return ptr_in; } diff --git a/py/malloc.c b/py/malloc.c index 76c3fe6cad..db2578d9ad 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -88,13 +88,9 @@ 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; } |