From 2d73ba21860e2374bb5e678d88c2593009c7cc08 Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Tue, 4 May 2021 03:35:45 -0400 Subject: [PATCH 1/2] qstr: Use `const` consistently to avoid a cast. --- py/qstr.c | 10 +++++----- py/qstr.h | 2 +- tools/mpy-tool.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/py/qstr.c b/py/qstr.c index 879fe30f22..7df83e25cb 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -123,7 +123,7 @@ void qstr_init(void) { STATIC const char *find_qstr(qstr q, qstr_attr_t *attr) { // search pool for this qstr // total_prev_len==0 in the final pool, so the loop will always terminate - qstr_pool_t *pool = MP_STATE_VM(last_pool); + const qstr_pool_t *pool = MP_STATE_VM(last_pool); while (q < pool->total_prev_len) { pool = pool->prev; } @@ -181,7 +181,7 @@ qstr qstr_find_strn(const char *str, size_t str_len) { mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len); // search pools for the data - for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { + for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { qstr_attr_t *attrs = pool->attrs; for (mp_uint_t at = 0, top = pool->len; at < top; at++) { if (attrs[at].hash == str_hash && attrs[at].len == str_len && memcmp(pool->qstrs[at], str, str_len) == 0) { @@ -290,7 +290,7 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si *n_qstr = 0; *n_str_data_bytes = 0; *n_total_bytes = 0; - for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { + for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { *n_pool += 1; *n_qstr += pool->len; for (const qstr_attr_t *q = pool->attrs, *q_top = pool->attrs + pool->len; q < q_top; q++) { @@ -310,8 +310,8 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si #if MICROPY_PY_MICROPYTHON_MEM_INFO void qstr_dump_data(void) { QSTR_ENTER(); - for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { - for (const char **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { + for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { + for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) { mp_printf(&mp_plat_print, "Q(%s)\n", *q); } } diff --git a/py/qstr.h b/py/qstr.h index bf9e30231b..6326426b0f 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -65,7 +65,7 @@ typedef struct _qstr_attr_t { } qstr_attr_t; typedef struct _qstr_pool_t { - struct _qstr_pool_t *prev; + const struct _qstr_pool_t *prev; size_t total_prev_len; size_t alloc; size_t len; diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 0fbbce40f2..90578da72b 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -878,7 +878,7 @@ def freeze_mpy(base_qstrs, raw_codes): print() print("extern const qstr_pool_t mp_qstr_const_pool;") print("const qstr_pool_t mp_qstr_frozen_const_pool = {") - print(" (qstr_pool_t*)&mp_qstr_const_pool, // previous pool") + print(" &mp_qstr_const_pool, // previous pool") print(" MP_QSTRnumber_of, // previous pool size") print(" %u, // allocated entries" % qstr_pool_alloc) print(" %u, // used entries" % len(new)) From 269d5bc80acea4f5e5f5b3598f2e22600c05b7bb Mon Sep 17 00:00:00 2001 From: Artyom Skrobov Date: Wed, 5 May 2021 09:01:47 +0300 Subject: [PATCH 2/2] qstr: One more cast could be avoided. --- py/qstr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/py/qstr.c b/py/qstr.c index 7df83e25cb..25012a3cb7 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -149,14 +149,14 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) { new_pool_length = MICROPY_ALLOC_QSTR_ENTRIES_INIT; } #endif - mp_uint_t pool_size = sizeof(qstr_pool_t) + sizeof(const char *) * new_pool_length; - void *chunk = m_malloc_maybe(pool_size + sizeof(qstr_attr_t) * new_pool_length, true); - if (chunk == NULL) { + mp_uint_t pool_size = sizeof(qstr_pool_t) + + (sizeof(const char *) + sizeof(qstr_attr_t)) * new_pool_length; + qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size, true); + if (pool == NULL) { QSTR_EXIT(); m_malloc_fail(new_pool_length); } - qstr_pool_t *pool = (qstr_pool_t *)chunk; - pool->attrs = (qstr_attr_t *)(void *)((char *)chunk + pool_size); + pool->attrs = (qstr_attr_t *)(pool->qstrs + new_pool_length); pool->prev = MP_STATE_VM(last_pool); pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len; pool->alloc = new_pool_length;