Skip to content

Commit 1d385e1

Browse files
authored
Merge pull request #4707 from tyomitch/patch-4
qstr: Use `const` consistently to avoid a cast.
2 parents 1668e9c + 269d5bc commit 1d385e1

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

py/qstr.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void qstr_init(void) {
123123
STATIC const char *find_qstr(qstr q, qstr_attr_t *attr) {
124124
// search pool for this qstr
125125
// total_prev_len==0 in the final pool, so the loop will always terminate
126-
qstr_pool_t *pool = MP_STATE_VM(last_pool);
126+
const qstr_pool_t *pool = MP_STATE_VM(last_pool);
127127
while (q < pool->total_prev_len) {
128128
pool = pool->prev;
129129
}
@@ -149,14 +149,14 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
149149
new_pool_length = MICROPY_ALLOC_QSTR_ENTRIES_INIT;
150150
}
151151
#endif
152-
mp_uint_t pool_size = sizeof(qstr_pool_t) + sizeof(const char *) * new_pool_length;
153-
void *chunk = m_malloc_maybe(pool_size + sizeof(qstr_attr_t) * new_pool_length, true);
154-
if (chunk == NULL) {
152+
mp_uint_t pool_size = sizeof(qstr_pool_t)
153+
+ (sizeof(const char *) + sizeof(qstr_attr_t)) * new_pool_length;
154+
qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size, true);
155+
if (pool == NULL) {
155156
QSTR_EXIT();
156157
m_malloc_fail(new_pool_length);
157158
}
158-
qstr_pool_t *pool = (qstr_pool_t *)chunk;
159-
pool->attrs = (qstr_attr_t *)(void *)((char *)chunk + pool_size);
159+
pool->attrs = (qstr_attr_t *)(pool->qstrs + new_pool_length);
160160
pool->prev = MP_STATE_VM(last_pool);
161161
pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
162162
pool->alloc = new_pool_length;
@@ -181,7 +181,7 @@ qstr qstr_find_strn(const char *str, size_t str_len) {
181181
mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len);
182182

183183
// search pools for the data
184-
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
184+
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
185185
qstr_attr_t *attrs = pool->attrs;
186186
for (mp_uint_t at = 0, top = pool->len; at < top; at++) {
187187
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
290290
*n_qstr = 0;
291291
*n_str_data_bytes = 0;
292292
*n_total_bytes = 0;
293-
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
293+
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
294294
*n_pool += 1;
295295
*n_qstr += pool->len;
296296
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
310310
#if MICROPY_PY_MICROPYTHON_MEM_INFO
311311
void qstr_dump_data(void) {
312312
QSTR_ENTER();
313-
for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
314-
for (const char **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
313+
for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
314+
for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) {
315315
mp_printf(&mp_plat_print, "Q(%s)\n", *q);
316316
}
317317
}

py/qstr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ typedef struct _qstr_attr_t {
6565
} qstr_attr_t;
6666

6767
typedef struct _qstr_pool_t {
68-
struct _qstr_pool_t *prev;
68+
const struct _qstr_pool_t *prev;
6969
size_t total_prev_len;
7070
size_t alloc;
7171
size_t len;

tools/mpy-tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ def freeze_mpy(base_qstrs, raw_codes):
878878
print()
879879
print("extern const qstr_pool_t mp_qstr_const_pool;")
880880
print("const qstr_pool_t mp_qstr_frozen_const_pool = {")
881-
print(" (qstr_pool_t*)&mp_qstr_const_pool, // previous pool")
881+
print(" &mp_qstr_const_pool, // previous pool")
882882
print(" MP_QSTRnumber_of, // previous pool size")
883883
print(" %u, // allocated entries" % qstr_pool_alloc)
884884
print(" %u, // used entries" % len(new))

0 commit comments

Comments
 (0)