Skip to content

Commit 8825e7f

Browse files
authored
Merge pull request #6397 from jepler/extra-memset
Some flash size optimizations related to string0.c (implementation of str/mem functions)
2 parents 7e4b2a0 + 162fa6e commit 8825e7f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

py/gc.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ void gc_init(void *start, void *end) {
138138
MP_STATE_MEM(gc_alloc_table_start) = (byte *)start;
139139

140140
#if MICROPY_ENABLE_FINALISER
141-
size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
142141
MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len) + 1;
143142
#endif
144143

@@ -147,18 +146,16 @@ void gc_init(void *start, void *end) {
147146
MP_STATE_MEM(gc_pool_end) = end;
148147

149148
#if MICROPY_ENABLE_FINALISER
149+
size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
150+
(void)gc_finaliser_table_byte_len; // avoid unused variable diagnostic if asserts are disabled
150151
assert(MP_STATE_MEM(gc_pool_start) >= MP_STATE_MEM(gc_finaliser_table_start) + gc_finaliser_table_byte_len);
151152
#endif
152153

153-
// Clear ATBs plus one more byte. The extra byte might be read when we read the final ATB and
154-
// then try to count its tail. Clearing the byte ensures it is 0 and ends the chain. Without an
155-
// FTB, it'll just clear the pool byte early.
156-
memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_alloc_table_byte_len) + 1);
157-
158-
#if MICROPY_ENABLE_FINALISER
159-
// clear FTBs
160-
memset(MP_STATE_MEM(gc_finaliser_table_start), 0, gc_finaliser_table_byte_len);
161-
#endif
154+
// Clear ATBs & finalisers (if enabled). This also clears the extra byte
155+
// which appears between ATBs and finalisers that ensures every chain in
156+
// the ATB terminates, rather than erroneously using bits from the
157+
// finalisers.
158+
memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_pool_start) - MP_STATE_MEM(gc_alloc_table_start));
162159

163160
// Set first free ATB index to the start of the heap.
164161
for (size_t i = 0; i < MICROPY_ATB_INDICES; i++) {

shared/libc/string0.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
#include <stddef.h>
2929
#include <string.h>
3030

31+
#include "py/mpconfig.h"
32+
3133
#ifndef likely
3234
#define likely(x) __builtin_expect((x), 1)
3335
#endif
3436

3537
#pragma GCC diagnostic push
3638
#pragma GCC diagnostic ignored "-Wcast-align"
3739
void *memcpy(void *dst, const void *src, size_t n) {
40+
#if CIRCUITPY_FULL_BUILD
3841
if (likely(!(((uintptr_t)dst) & 3) && !(((uintptr_t)src) & 3))) {
3942
// pointers aligned
4043
uint32_t *d = dst;
@@ -56,7 +59,9 @@ void *memcpy(void *dst, const void *src, size_t n) {
5659
// copy byte
5760
*((uint8_t*)d) = *((const uint8_t*)s);
5861
}
59-
} else {
62+
} else
63+
#endif
64+
{
6065
// unaligned access, copy bytes
6166
uint8_t *d = dst;
6267
const uint8_t *s = src;
@@ -93,6 +98,7 @@ void *memmove(void *dest, const void *src, size_t n) {
9398
}
9499

95100
void *memset(void *s, int c, size_t n) {
101+
#if CIRCUITPY_FULL_BUILD
96102
if (c == 0 && ((uintptr_t)s & 3) == 0) {
97103
// aligned store of 0
98104
uint32_t *s32 = s;
@@ -106,7 +112,9 @@ void *memset(void *s, int c, size_t n) {
106112
if (n & 1) {
107113
*((uint8_t*)s32) = 0;
108114
}
109-
} else {
115+
} else
116+
#endif
117+
{
110118
uint8_t *s2 = s;
111119
for (; n > 0; n--) {
112120
*s2++ = c;

0 commit comments

Comments
 (0)