blob: dc89013f86f86461f41567bb13eb1c2256c7dda4 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28#include <string.h>
Damien Georgeea0461d2015-02-10 11:02:28 +000029#include <stdio.h>
Damien429d7192013-10-04 19:53:11 +010030
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/qstr.h"
33#include "py/gc.h"
Léa Saviotbc129f12019-11-22 10:07:08 +010034#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010035
Stefan Naumannace9fb52017-07-24 18:55:14 +020036#if MICROPY_DEBUG_VERBOSE // print debugging info
Paul Sokolovsky44739e22014-02-16 18:11:42 +020037#define DEBUG_printf DEBUG_printf
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000038#else // don't print debugging info
Damien George1dc76af2014-02-26 16:57:08 +000039#define DEBUG_printf(...) (void)0
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000040#endif
41
Damien George55baff42014-01-21 21:40:13 +000042// A qstr is an index into the qstr pool.
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040043// The data for a qstr is \0 terminated (so they can be printed using printf)
Damien George55baff42014-01-21 21:40:13 +000044
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040045#define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1)
Damien George55baff42014-01-21 21:40:13 +000046
Damien Georgea1c93a62016-05-26 10:53:34 +000047#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +000048#define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
49#define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex))
50#else
51#define QSTR_ENTER()
52#define QSTR_EXIT()
53#endif
54
Damien George0d165fe2018-12-13 17:08:26 +110055// Initial number of entries for qstr pool, set so that the first dynamically
56// allocated pool is twice this size. The value here must be <= MP_QSTRnumber_of.
57#define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10)
58
Damien George6e628c42014-03-25 15:27:15 +000059// this must match the equivalent function in makeqstrdata.py
Damien George82b35002022-08-11 16:34:02 +100060size_t qstr_compute_hash(const byte *data, size_t len) {
Damien George6e628c42014-03-25 15:27:15 +000061 // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
Damien George82b35002022-08-11 16:34:02 +100062 size_t hash = 5381;
Damien George55baff42014-01-21 21:40:13 +000063 for (const byte *top = data + len; data < top; data++) {
Damien George6e628c42014-03-25 15:27:15 +000064 hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
Damien George55baff42014-01-21 21:40:13 +000065 }
Damien Georgec3bd9412015-07-20 11:03:13 +000066 hash &= Q_HASH_MASK;
Paul Sokolovsky59e269c2014-04-14 01:43:01 +030067 // Make sure that valid hash is never zero, zero means "hash not computed"
68 if (hash == 0) {
69 hash++;
70 }
71 return hash;
Damien George55baff42014-01-21 21:40:13 +000072}
73
Jim Mussared64c79a52023-02-15 16:09:04 +110074// The first pool is the static qstr table. The contents must remain stable as
75// it is part of the .mpy ABI. See the top of py/persistentcode.c and
76// static_qstr_list in makeqstrdata.py. This pool is unsorted (although in a
77// future .mpy version we could re-order them and make it sorted). It also
78// contains additional qstrs that must have IDs <256, see operator_qstr_list
79// in makeqstrdata.py.
80const qstr_hash_t mp_qstr_const_hashes_static[] = {
81 #ifndef NO_QSTR
82#define QDEF0(id, hash, len, str) hash,
83#define QDEF1(id, hash, len, str)
84 #include "genhdr/qstrdefs.generated.h"
85#undef QDEF0
86#undef QDEF1
87 #endif
88};
89
90const qstr_len_t mp_qstr_const_lengths_static[] = {
91 #ifndef NO_QSTR
92#define QDEF0(id, hash, len, str) len,
93#define QDEF1(id, hash, len, str)
94 #include "genhdr/qstrdefs.generated.h"
95#undef QDEF0
96#undef QDEF1
97 #endif
98};
99
100const qstr_pool_t mp_qstr_const_pool_static = {
101 NULL, // no previous pool
102 0, // no previous pool
103 false, // is_sorted
104 MICROPY_ALLOC_QSTR_ENTRIES_INIT,
105 MP_QSTRnumber_of_static, // corresponds to number of strings in array just below
106 (qstr_hash_t *)mp_qstr_const_hashes_static,
107 (qstr_len_t *)mp_qstr_const_lengths_static,
108 {
109 #ifndef NO_QSTR
110#define QDEF0(id, hash, len, str) str,
111#define QDEF1(id, hash, len, str)
112 #include "genhdr/qstrdefs.generated.h"
113#undef QDEF0
114#undef QDEF1
115 #endif
116 },
117};
118
119// The next pool is the remainder of the qstrs defined in the firmware. This
120// is sorted.
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400121const qstr_hash_t mp_qstr_const_hashes[] = {
122 #ifndef NO_QSTR
Jim Mussared64c79a52023-02-15 16:09:04 +1100123#define QDEF0(id, hash, len, str)
124#define QDEF1(id, hash, len, str) hash,
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400125 #include "genhdr/qstrdefs.generated.h"
Jim Mussared64c79a52023-02-15 16:09:04 +1100126#undef QDEF0
127#undef QDEF1
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400128 #endif
129};
130
131const qstr_len_t mp_qstr_const_lengths[] = {
132 #ifndef NO_QSTR
Jim Mussared64c79a52023-02-15 16:09:04 +1100133#define QDEF0(id, hash, len, str)
134#define QDEF1(id, hash, len, str) len,
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400135 #include "genhdr/qstrdefs.generated.h"
Jim Mussared64c79a52023-02-15 16:09:04 +1100136#undef QDEF0
137#undef QDEF1
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400138 #endif
139};
140
Damien George0a2e9652016-01-31 22:24:16 +0000141const qstr_pool_t mp_qstr_const_pool = {
Jim Mussared64c79a52023-02-15 16:09:04 +1100142 &mp_qstr_const_pool_static,
143 MP_QSTRnumber_of_static,
144 true, // is_sorted
Damien George0d165fe2018-12-13 17:08:26 +1100145 MICROPY_ALLOC_QSTR_ENTRIES_INIT,
Jim Mussared64c79a52023-02-15 16:09:04 +1100146 MP_QSTRnumber_of - MP_QSTRnumber_of_static, // corresponds to number of strings in array just below
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400147 (qstr_hash_t *)mp_qstr_const_hashes,
148 (qstr_len_t *)mp_qstr_const_lengths,
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000149 {
Damien George69661f32020-02-27 15:36:53 +1100150 #ifndef NO_QSTR
Jim Mussared64c79a52023-02-15 16:09:04 +1100151#define QDEF0(id, hash, len, str)
152#define QDEF1(id, hash, len, str) str,
Damien George69661f32020-02-27 15:36:53 +1100153 #include "genhdr/qstrdefs.generated.h"
Jim Mussared64c79a52023-02-15 16:09:04 +1100154#undef QDEF0
155#undef QDEF1
Damien George69661f32020-02-27 15:36:53 +1100156 #endif
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000157 },
158};
159
Jim Mussared64c79a52023-02-15 16:09:04 +1100160// If frozen code is enabled, then there is an additional, sorted, ROM pool
161// containing additional qstrs required by the frozen code.
Damien George0a2e9652016-01-31 22:24:16 +0000162#ifdef MICROPY_QSTR_EXTRA_POOL
163extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL;
164#define CONST_POOL MICROPY_QSTR_EXTRA_POOL
165#else
166#define CONST_POOL mp_qstr_const_pool
167#endif
168
Damien8b3a7c22013-10-23 20:20:17 +0100169void qstr_init(void) {
Damien George69661f32020-02-27 15:36:53 +1100170 MP_STATE_VM(last_pool) = (qstr_pool_t *)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
Damien Georgeade9a052015-06-13 21:53:22 +0100171 MP_STATE_VM(qstr_last_chunk) = NULL;
Damien George1f54ad22016-05-26 09:06:46 +0000172
David Lechneredbb73a2020-01-22 11:19:37 -0600173 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +0000174 mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
175 #endif
Damien429d7192013-10-04 19:53:11 +0100176}
177
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400178STATIC const qstr_pool_t *find_qstr(qstr *q) {
Damien George55baff42014-01-21 21:40:13 +0000179 // search pool for this qstr
Damien George8e323b82017-11-29 16:58:27 +1100180 // total_prev_len==0 in the final pool, so the loop will always terminate
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400181 const qstr_pool_t *pool = MP_STATE_VM(last_pool);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400182 while (*q < pool->total_prev_len) {
Damien George8e323b82017-11-29 16:58:27 +1100183 pool = pool->prev;
Damien George55baff42014-01-21 21:40:13 +0000184 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400185 *q -= pool->total_prev_len;
186 assert(*q < pool->len);
187 return pool;
Damien George55baff42014-01-21 21:40:13 +0000188}
189
Damien George1f54ad22016-05-26 09:06:46 +0000190// qstr_mutex must be taken while in this function
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400191STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
192 DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", hash, len, len, q_ptr);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000193
194 // make sure we have room in the pool for a new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000195 if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
Damien George0d165fe2018-12-13 17:08:26 +1100196 size_t new_alloc = MP_STATE_VM(last_pool)->alloc * 2;
197 #ifdef MICROPY_QSTR_EXTRA_POOL
198 // Put a lower bound on the allocation size in case the extra qstr pool has few entries
199 new_alloc = MAX(MICROPY_ALLOC_QSTR_ENTRIES_INIT, new_alloc);
200 #endif
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400201 mp_uint_t pool_size = sizeof(qstr_pool_t)
202 + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * new_alloc;
203 qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size);
Damien George1f54ad22016-05-26 09:06:46 +0000204 if (pool == NULL) {
Emilie Feral58b56b92021-12-27 15:39:34 +0100205 // Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned
206 // at garbage collection since it's reachable from a qstr_pool_t. And the caller of
207 // this function expects q_ptr to be stored in a qstr_pool_t so it can be reached
208 // by the collector. If qstr_pool_t allocation failed, qstr_last_chunk needs to be
209 // NULL'd. Otherwise it may become a dangling pointer at the next garbage collection.
210 MP_STATE_VM(qstr_last_chunk) = NULL;
Damien George1f54ad22016-05-26 09:06:46 +0000211 QSTR_EXIT();
Damien George0d165fe2018-12-13 17:08:26 +1100212 m_malloc_fail(new_alloc);
Damien George1f54ad22016-05-26 09:06:46 +0000213 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400214 pool->hashes = (qstr_hash_t *)(pool->qstrs + new_alloc);
215 pool->lengths = (qstr_len_t *)(pool->hashes + new_alloc);
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000216 pool->prev = MP_STATE_VM(last_pool);
217 pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
Damien George0d165fe2018-12-13 17:08:26 +1100218 pool->alloc = new_alloc;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000219 pool->len = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000220 MP_STATE_VM(last_pool) = pool;
221 DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
Damien429d7192013-10-04 19:53:11 +0100222 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000223
224 // add the new qstr
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400225 mp_uint_t at = MP_STATE_VM(last_pool)->len;
226 MP_STATE_VM(last_pool)->hashes[at] = hash;
227 MP_STATE_VM(last_pool)->lengths[at] = len;
228 MP_STATE_VM(last_pool)->qstrs[at] = q_ptr;
229 MP_STATE_VM(last_pool)->len++;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000230
231 // return id for the newly-added qstr
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400232 return MP_STATE_VM(last_pool)->total_prev_len + at;
Damien429d7192013-10-04 19:53:11 +0100233}
234
Damien Georgec3f64d92015-11-27 12:23:18 +0000235qstr qstr_find_strn(const char *str, size_t str_len) {
Jim Mussared42127992023-11-06 15:08:11 +1100236 if (str_len == 0) {
237 // strncmp behaviour is undefined for str==NULL.
238 return MP_QSTR_;
239 }
240
Damien George55baff42014-01-21 21:40:13 +0000241 // work out hash of str
Damien George82b35002022-08-11 16:34:02 +1000242 size_t str_hash = qstr_compute_hash((const byte *)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000243
244 // search pools for the data
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400245 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Jim Mussared64c79a52023-02-15 16:09:04 +1100246 size_t low = 0;
247 size_t high = pool->len - 1;
248
249 // binary search inside the pool
250 if (pool->is_sorted) {
251 while (high - low > 1) {
252 size_t mid = (low + high) / 2;
253 int cmp = strncmp(str, pool->qstrs[mid], str_len);
254 if (cmp <= 0) {
255 high = mid;
256 } else {
257 low = mid;
258 }
259 }
260 }
261
262 // sequential search for the remaining strings
263 for (mp_uint_t at = low; at < high + 1; at++) {
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400264 if (pool->hashes[at] == str_hash && pool->lengths[at] == str_len
265 && memcmp(pool->qstrs[at], str, str_len) == 0) {
266 return pool->total_prev_len + at;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000267 }
Damien429d7192013-10-04 19:53:11 +0100268 }
269 }
Damien George55baff42014-01-21 21:40:13 +0000270
271 // not found; return null qstr
Jim Mussared64c79a52023-02-15 16:09:04 +1100272 return MP_QSTRnull;
Damien429d7192013-10-04 19:53:11 +0100273}
274
Damien George55baff42014-01-21 21:40:13 +0000275qstr qstr_from_str(const char *str) {
276 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100277}
278
Damien Georgec3f64d92015-11-27 12:23:18 +0000279qstr qstr_from_strn(const char *str, size_t len) {
Damien George1f54ad22016-05-26 09:06:46 +0000280 QSTR_ENTER();
Damien George2617eeb2014-05-25 22:27:57 +0100281 qstr q = qstr_find_strn(str, len);
Damien George55baff42014-01-21 21:40:13 +0000282 if (q == 0) {
Damien Georgeade9a052015-06-13 21:53:22 +0100283 // qstr does not exist in interned pool so need to add it
284
Léa Saviotbc129f12019-11-22 10:07:08 +0100285 // check that len is not too big
286 if (len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) {
287 QSTR_EXIT();
Jim Mussareddef76fe2020-03-02 22:35:22 +1100288 mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("name too long"));
Léa Saviotbc129f12019-11-22 10:07:08 +0100289 }
290
Damien Georgeade9a052015-06-13 21:53:22 +0100291 // compute number of bytes needed to intern this string
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400292 size_t n_bytes = len + 1;
Damien Georgeade9a052015-06-13 21:53:22 +0100293
294 if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
295 // not enough room at end of previously interned string so try to grow
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400296 char *new_p = m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_alloc) + n_bytes, false);
Damien Georgeade9a052015-06-13 21:53:22 +0100297 if (new_p == NULL) {
298 // could not grow existing memory; shrink it to fit previous
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400299 (void)m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
Damien Georgeade9a052015-06-13 21:53:22 +0100300 MP_STATE_VM(qstr_last_chunk) = NULL;
301 } else {
302 // could grow existing memory
303 MP_STATE_VM(qstr_last_alloc) += n_bytes;
304 }
305 }
306
307 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
308 // no existing memory for the interned string so allocate a new chunk
Damien Georgec3f64d92015-11-27 12:23:18 +0000309 size_t al = n_bytes;
Damien Georgeade9a052015-06-13 21:53:22 +0100310 if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
311 al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
312 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400313 MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, al);
Damien Georgeade9a052015-06-13 21:53:22 +0100314 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
315 // failed to allocate a large chunk so try with exact size
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400316 MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, n_bytes);
Damien George1f54ad22016-05-26 09:06:46 +0000317 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
318 QSTR_EXIT();
319 m_malloc_fail(n_bytes);
320 }
Damien Georgeade9a052015-06-13 21:53:22 +0100321 al = n_bytes;
322 }
323 MP_STATE_VM(qstr_last_alloc) = al;
324 MP_STATE_VM(qstr_last_used) = 0;
325 }
326
327 // allocate memory from the chunk for this new interned string's data
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400328 char *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
Damien Georgeade9a052015-06-13 21:53:22 +0100329 MP_STATE_VM(qstr_last_used) += n_bytes;
330
331 // store the interned strings' data
Damien George82b35002022-08-11 16:34:02 +1000332 size_t hash = qstr_compute_hash((const byte *)str, len);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400333 memcpy(q_ptr, str, len);
334 q_ptr[len] = '\0';
335 q = qstr_add(hash, len, q_ptr);
Damien429d7192013-10-04 19:53:11 +0100336 }
Damien George1f54ad22016-05-26 09:06:46 +0000337 QSTR_EXIT();
Damien George55baff42014-01-21 21:40:13 +0000338 return q;
Damien429d7192013-10-04 19:53:11 +0100339}
340
Damien George40f3c022014-07-03 13:25:24 +0100341mp_uint_t qstr_hash(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400342 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400343 return pool->hashes[q];
Damien George55baff42014-01-21 21:40:13 +0000344}
345
Damien Georgec3f64d92015-11-27 12:23:18 +0000346size_t qstr_len(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400347 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400348 return pool->lengths[q];
Damien George55baff42014-01-21 21:40:13 +0000349}
350
Damien George55baff42014-01-21 21:40:13 +0000351const char *qstr_str(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400352 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400353 return pool->qstrs[q];
Damien George55baff42014-01-21 21:40:13 +0000354}
355
Damien Georgec3f64d92015-11-27 12:23:18 +0000356const byte *qstr_data(qstr q, size_t *len) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400357 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400358 *len = pool->lengths[q];
359 return (byte *)pool->qstrs[q];
Damien429d7192013-10-04 19:53:11 +0100360}
Damien George4d5b28c2014-01-29 18:56:46 +0000361
Damien George25784852015-12-17 12:41:40 +0000362void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
Damien George1f54ad22016-05-26 09:06:46 +0000363 QSTR_ENTER();
Damien George4d5b28c2014-01-29 18:56:46 +0000364 *n_pool = 0;
365 *n_qstr = 0;
366 *n_str_data_bytes = 0;
367 *n_total_bytes = 0;
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400368 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
Damien George4d5b28c2014-01-29 18:56:46 +0000369 *n_pool += 1;
370 *n_qstr += pool->len;
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400371 for (qstr_len_t *l = pool->lengths, *l_top = pool->lengths + pool->len; l < l_top; l++) {
372 *n_str_data_bytes += *l + 1;
Damien George4d5b28c2014-01-29 18:56:46 +0000373 }
Damien George0b13f3e2014-10-24 23:12:25 +0100374 #if MICROPY_ENABLE_GC
375 *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
376 #else
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400377 *n_total_bytes += sizeof(qstr_pool_t)
378 + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * pool->alloc;
Damien George0b13f3e2014-10-24 23:12:25 +0100379 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000380 }
381 *n_total_bytes += *n_str_data_bytes;
Damien George1f54ad22016-05-26 09:06:46 +0000382 QSTR_EXIT();
Damien George4d5b28c2014-01-29 18:56:46 +0000383}
Damien Georgeea0461d2015-02-10 11:02:28 +0000384
385#if MICROPY_PY_MICROPYTHON_MEM_INFO
386void qstr_dump_data(void) {
Damien George1f54ad22016-05-26 09:06:46 +0000387 QSTR_ENTER();
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400388 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
389 for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) {
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400390 mp_printf(&mp_plat_print, "Q(%s)\n", *q);
Damien Georgeea0461d2015-02-10 11:02:28 +0000391 }
392 }
Damien George1f54ad22016-05-26 09:06:46 +0000393 QSTR_EXIT();
Damien Georgeea0461d2015-02-10 11:02:28 +0000394}
395#endif
Jim Mussared154b4eb2019-09-26 22:19:29 +1000396
397#if MICROPY_ROM_TEXT_COMPRESSION
398
399#ifdef NO_QSTR
400
401// If NO_QSTR is set, it means we're doing QSTR extraction.
402// So we won't yet have "genhdr/compressed.data.h"
403
404#else
405
406// Emit the compressed_string_data string.
407#define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x;
408#define MP_MATCH_COMPRESSED(a, b)
409#include "genhdr/compressed.data.h"
410#undef MP_COMPRESSED_DATA
411#undef MP_MATCH_COMPRESSED
412
413#endif // NO_QSTR
414
415// This implements the "common word" compression scheme (see makecompresseddata.py) where the most
416// common 128 words in error messages are replaced by their index into the list of common words.
417
418// The compressed string data is delimited by setting high bit in the final char of each word.
419// e.g. aaaa<0x80|a>bbbbbb<0x80|b>....
420// This method finds the n'th string.
421STATIC const byte *find_uncompressed_string(uint8_t n) {
422 const byte *c = (byte *)compressed_string_data;
423 while (n > 0) {
424 while ((*c & 0x80) == 0) {
425 ++c;
426 }
427 ++c;
428 --n;
429 }
430 return c;
431}
432
433// Given a compressed string in src, decompresses it into dst.
434// dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1).
435void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src_chr) {
436 // Skip past the 0xff marker.
437 const byte *src = (byte *)src_chr + 1;
438 // Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2).
439 // 0 = start, 1 = compressed, 2 = regular.
440 int state = 0;
441 while (*src) {
442 if ((byte) * src >= 128) {
443 if (state != 0) {
444 *dst++ = ' ';
445 }
446 state = 1;
447
448 // High bit set, replace with common word.
449 const byte *word = find_uncompressed_string(*src & 0x7f);
450 // The word is terminated by the final char having its high bit set.
451 while ((*word & 0x80) == 0) {
452 *dst++ = *word++;
453 }
454 *dst++ = (*word & 0x7f);
455 } else {
456 // Otherwise just copy one char.
457 if (state == 1) {
458 *dst++ = ' ';
459 }
460 state = 2;
461
462 *dst++ = *src;
463 }
464 ++src;
465 }
466 // Add null-terminator.
467 *dst = 0;
468}
469
470#endif // MICROPY_ROM_TEXT_COMPRESSION