Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 3 | * |
| 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 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | #include <assert.h> |
| 28 | #include <string.h> |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 31 | #include "py/mpstate.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/qstr.h" |
| 33 | #include "py/gc.h" |
Léa Saviot | bc129f1 | 2019-11-22 10:07:08 +0100 | [diff] [blame] | 34 | #include "py/runtime.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | |
Stefan Naumann | ace9fb5 | 2017-07-24 18:55:14 +0200 | [diff] [blame] | 36 | #if MICROPY_DEBUG_VERBOSE // print debugging info |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 37 | #define DEBUG_printf DEBUG_printf |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 38 | #else // don't print debugging info |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 39 | #define DEBUG_printf(...) (void)0 |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 42 | // A qstr is an index into the qstr pool. |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 43 | // The data for a qstr is \0 terminated (so they can be printed using printf) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 44 | |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 45 | #define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 46 | |
Damien George | a1c93a6 | 2016-05-26 10:53:34 +0000 | [diff] [blame] | 47 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 48 | #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 George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 55 | // 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 George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 59 | // this must match the equivalent function in makeqstrdata.py |
Damien George | 82b3500 | 2022-08-11 16:34:02 +1000 | [diff] [blame] | 60 | size_t qstr_compute_hash(const byte *data, size_t len) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 61 | // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html |
Damien George | 82b3500 | 2022-08-11 16:34:02 +1000 | [diff] [blame] | 62 | size_t hash = 5381; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 63 | for (const byte *top = data + len; data < top; data++) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 64 | hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 65 | } |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame] | 66 | hash &= Q_HASH_MASK; |
Paul Sokolovsky | 59e269c | 2014-04-14 01:43:01 +0300 | [diff] [blame] | 67 | // Make sure that valid hash is never zero, zero means "hash not computed" |
| 68 | if (hash == 0) { |
| 69 | hash++; |
| 70 | } |
| 71 | return hash; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 74 | // 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. |
| 80 | const 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 | |
| 90 | const 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 | |
| 100 | const 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 Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 121 | const qstr_hash_t mp_qstr_const_hashes[] = { |
| 122 | #ifndef NO_QSTR |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 123 | #define QDEF0(id, hash, len, str) |
| 124 | #define QDEF1(id, hash, len, str) hash, |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 125 | #include "genhdr/qstrdefs.generated.h" |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 126 | #undef QDEF0 |
| 127 | #undef QDEF1 |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 128 | #endif |
| 129 | }; |
| 130 | |
| 131 | const qstr_len_t mp_qstr_const_lengths[] = { |
| 132 | #ifndef NO_QSTR |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 133 | #define QDEF0(id, hash, len, str) |
| 134 | #define QDEF1(id, hash, len, str) len, |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 135 | #include "genhdr/qstrdefs.generated.h" |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 136 | #undef QDEF0 |
| 137 | #undef QDEF1 |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 138 | #endif |
| 139 | }; |
| 140 | |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 141 | const qstr_pool_t mp_qstr_const_pool = { |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 142 | &mp_qstr_const_pool_static, |
| 143 | MP_QSTRnumber_of_static, |
| 144 | true, // is_sorted |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 145 | MICROPY_ALLOC_QSTR_ENTRIES_INIT, |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 146 | MP_QSTRnumber_of - MP_QSTRnumber_of_static, // corresponds to number of strings in array just below |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 147 | (qstr_hash_t *)mp_qstr_const_hashes, |
| 148 | (qstr_len_t *)mp_qstr_const_lengths, |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 149 | { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 150 | #ifndef NO_QSTR |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 151 | #define QDEF0(id, hash, len, str) |
| 152 | #define QDEF1(id, hash, len, str) str, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 153 | #include "genhdr/qstrdefs.generated.h" |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 154 | #undef QDEF0 |
| 155 | #undef QDEF1 |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 156 | #endif |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 157 | }, |
| 158 | }; |
| 159 | |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 160 | // If frozen code is enabled, then there is an additional, sorted, ROM pool |
| 161 | // containing additional qstrs required by the frozen code. |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 162 | #ifdef MICROPY_QSTR_EXTRA_POOL |
| 163 | extern 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 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 169 | void qstr_init(void) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 170 | 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 George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 171 | MP_STATE_VM(qstr_last_chunk) = NULL; |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 172 | |
David Lechner | edbb73a | 2020-01-22 11:19:37 -0600 | [diff] [blame] | 173 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 174 | mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex)); |
| 175 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 178 | STATIC const qstr_pool_t *find_qstr(qstr *q) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 179 | // search pool for this qstr |
Damien George | 8e323b8 | 2017-11-29 16:58:27 +1100 | [diff] [blame] | 180 | // total_prev_len==0 in the final pool, so the loop will always terminate |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 181 | const qstr_pool_t *pool = MP_STATE_VM(last_pool); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 182 | while (*q < pool->total_prev_len) { |
Damien George | 8e323b8 | 2017-11-29 16:58:27 +1100 | [diff] [blame] | 183 | pool = pool->prev; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 184 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 185 | *q -= pool->total_prev_len; |
| 186 | assert(*q < pool->len); |
| 187 | return pool; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 190 | // qstr_mutex must be taken while in this function |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 191 | STATIC 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 George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 193 | |
| 194 | // make sure we have room in the pool for a new qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 195 | if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) { |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 196 | 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 Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 201 | 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 George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 204 | if (pool == NULL) { |
Emilie Feral | 58b56b9 | 2021-12-27 15:39:34 +0100 | [diff] [blame] | 205 | // 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 George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 211 | QSTR_EXIT(); |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 212 | m_malloc_fail(new_alloc); |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 213 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 214 | pool->hashes = (qstr_hash_t *)(pool->qstrs + new_alloc); |
| 215 | pool->lengths = (qstr_len_t *)(pool->hashes + new_alloc); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 216 | 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 George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 218 | pool->alloc = new_alloc; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 219 | pool->len = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 220 | MP_STATE_VM(last_pool) = pool; |
| 221 | DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 222 | } |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 223 | |
| 224 | // add the new qstr |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 225 | 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 George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 230 | |
| 231 | // return id for the newly-added qstr |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 232 | return MP_STATE_VM(last_pool)->total_prev_len + at; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 235 | qstr qstr_find_strn(const char *str, size_t str_len) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 236 | // work out hash of str |
Damien George | 82b3500 | 2022-08-11 16:34:02 +1000 | [diff] [blame] | 237 | size_t str_hash = qstr_compute_hash((const byte *)str, str_len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 238 | |
| 239 | // search pools for the data |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 240 | for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 241 | size_t low = 0; |
| 242 | size_t high = pool->len - 1; |
| 243 | |
| 244 | // binary search inside the pool |
| 245 | if (pool->is_sorted) { |
| 246 | while (high - low > 1) { |
| 247 | size_t mid = (low + high) / 2; |
| 248 | int cmp = strncmp(str, pool->qstrs[mid], str_len); |
| 249 | if (cmp <= 0) { |
| 250 | high = mid; |
| 251 | } else { |
| 252 | low = mid; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // sequential search for the remaining strings |
| 258 | for (mp_uint_t at = low; at < high + 1; at++) { |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 259 | if (pool->hashes[at] == str_hash && pool->lengths[at] == str_len |
| 260 | && memcmp(pool->qstrs[at], str, str_len) == 0) { |
| 261 | return pool->total_prev_len + at; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 262 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 263 | } |
| 264 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 265 | |
| 266 | // not found; return null qstr |
Jim Mussared | 64c79a5 | 2023-02-15 16:09:04 +1100 | [diff] [blame] | 267 | return MP_QSTRnull; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 270 | qstr qstr_from_str(const char *str) { |
| 271 | return qstr_from_strn(str, strlen(str)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 274 | qstr qstr_from_strn(const char *str, size_t len) { |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 275 | QSTR_ENTER(); |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 276 | qstr q = qstr_find_strn(str, len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 277 | if (q == 0) { |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 278 | // qstr does not exist in interned pool so need to add it |
| 279 | |
Léa Saviot | bc129f1 | 2019-11-22 10:07:08 +0100 | [diff] [blame] | 280 | // check that len is not too big |
| 281 | if (len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) { |
| 282 | QSTR_EXIT(); |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 283 | mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("name too long")); |
Léa Saviot | bc129f1 | 2019-11-22 10:07:08 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 286 | // compute number of bytes needed to intern this string |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 287 | size_t n_bytes = len + 1; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 288 | |
| 289 | if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) { |
| 290 | // not enough room at end of previously interned string so try to grow |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 291 | 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 George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 292 | if (new_p == NULL) { |
| 293 | // could not grow existing memory; shrink it to fit previous |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 294 | (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 George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 295 | MP_STATE_VM(qstr_last_chunk) = NULL; |
| 296 | } else { |
| 297 | // could grow existing memory |
| 298 | MP_STATE_VM(qstr_last_alloc) += n_bytes; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 303 | // no existing memory for the interned string so allocate a new chunk |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 304 | size_t al = n_bytes; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 305 | if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) { |
| 306 | al = MICROPY_ALLOC_QSTR_CHUNK_INIT; |
| 307 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 308 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, al); |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 309 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 310 | // failed to allocate a large chunk so try with exact size |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 311 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, n_bytes); |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 312 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 313 | QSTR_EXIT(); |
| 314 | m_malloc_fail(n_bytes); |
| 315 | } |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 316 | al = n_bytes; |
| 317 | } |
| 318 | MP_STATE_VM(qstr_last_alloc) = al; |
| 319 | MP_STATE_VM(qstr_last_used) = 0; |
| 320 | } |
| 321 | |
| 322 | // allocate memory from the chunk for this new interned string's data |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 323 | char *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used); |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 324 | MP_STATE_VM(qstr_last_used) += n_bytes; |
| 325 | |
| 326 | // store the interned strings' data |
Damien George | 82b3500 | 2022-08-11 16:34:02 +1000 | [diff] [blame] | 327 | size_t hash = qstr_compute_hash((const byte *)str, len); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 328 | memcpy(q_ptr, str, len); |
| 329 | q_ptr[len] = '\0'; |
| 330 | q = qstr_add(hash, len, q_ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 331 | } |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 332 | QSTR_EXIT(); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 333 | return q; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 336 | mp_uint_t qstr_hash(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 337 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 338 | return pool->hashes[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 341 | size_t qstr_len(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 342 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 343 | return pool->lengths[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 346 | const char *qstr_str(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 347 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 348 | return pool->qstrs[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 351 | const byte *qstr_data(qstr q, size_t *len) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 352 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 353 | *len = pool->lengths[q]; |
| 354 | return (byte *)pool->qstrs[q]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 355 | } |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 356 | |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 357 | void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) { |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 358 | QSTR_ENTER(); |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 359 | *n_pool = 0; |
| 360 | *n_qstr = 0; |
| 361 | *n_str_data_bytes = 0; |
| 362 | *n_total_bytes = 0; |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 363 | for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 364 | *n_pool += 1; |
| 365 | *n_qstr += pool->len; |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 366 | for (qstr_len_t *l = pool->lengths, *l_top = pool->lengths + pool->len; l < l_top; l++) { |
| 367 | *n_str_data_bytes += *l + 1; |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 368 | } |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 369 | #if MICROPY_ENABLE_GC |
| 370 | *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap |
| 371 | #else |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 372 | *n_total_bytes += sizeof(qstr_pool_t) |
| 373 | + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * pool->alloc; |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 374 | #endif |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 375 | } |
| 376 | *n_total_bytes += *n_str_data_bytes; |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 377 | QSTR_EXIT(); |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 378 | } |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 379 | |
| 380 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
| 381 | void qstr_dump_data(void) { |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 382 | QSTR_ENTER(); |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame] | 383 | for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) { |
| 384 | for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) { |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 385 | mp_printf(&mp_plat_print, "Q(%s)\n", *q); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 388 | QSTR_EXIT(); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 389 | } |
| 390 | #endif |
Jim Mussared | 154b4eb | 2019-09-26 22:19:29 +1000 | [diff] [blame] | 391 | |
| 392 | #if MICROPY_ROM_TEXT_COMPRESSION |
| 393 | |
| 394 | #ifdef NO_QSTR |
| 395 | |
| 396 | // If NO_QSTR is set, it means we're doing QSTR extraction. |
| 397 | // So we won't yet have "genhdr/compressed.data.h" |
| 398 | |
| 399 | #else |
| 400 | |
| 401 | // Emit the compressed_string_data string. |
| 402 | #define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x; |
| 403 | #define MP_MATCH_COMPRESSED(a, b) |
| 404 | #include "genhdr/compressed.data.h" |
| 405 | #undef MP_COMPRESSED_DATA |
| 406 | #undef MP_MATCH_COMPRESSED |
| 407 | |
| 408 | #endif // NO_QSTR |
| 409 | |
| 410 | // This implements the "common word" compression scheme (see makecompresseddata.py) where the most |
| 411 | // common 128 words in error messages are replaced by their index into the list of common words. |
| 412 | |
| 413 | // The compressed string data is delimited by setting high bit in the final char of each word. |
| 414 | // e.g. aaaa<0x80|a>bbbbbb<0x80|b>.... |
| 415 | // This method finds the n'th string. |
| 416 | STATIC const byte *find_uncompressed_string(uint8_t n) { |
| 417 | const byte *c = (byte *)compressed_string_data; |
| 418 | while (n > 0) { |
| 419 | while ((*c & 0x80) == 0) { |
| 420 | ++c; |
| 421 | } |
| 422 | ++c; |
| 423 | --n; |
| 424 | } |
| 425 | return c; |
| 426 | } |
| 427 | |
| 428 | // Given a compressed string in src, decompresses it into dst. |
| 429 | // dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1). |
| 430 | void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src_chr) { |
| 431 | // Skip past the 0xff marker. |
| 432 | const byte *src = (byte *)src_chr + 1; |
| 433 | // Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2). |
| 434 | // 0 = start, 1 = compressed, 2 = regular. |
| 435 | int state = 0; |
| 436 | while (*src) { |
| 437 | if ((byte) * src >= 128) { |
| 438 | if (state != 0) { |
| 439 | *dst++ = ' '; |
| 440 | } |
| 441 | state = 1; |
| 442 | |
| 443 | // High bit set, replace with common word. |
| 444 | const byte *word = find_uncompressed_string(*src & 0x7f); |
| 445 | // The word is terminated by the final char having its high bit set. |
| 446 | while ((*word & 0x80) == 0) { |
| 447 | *dst++ = *word++; |
| 448 | } |
| 449 | *dst++ = (*word & 0x7f); |
| 450 | } else { |
| 451 | // Otherwise just copy one char. |
| 452 | if (state == 1) { |
| 453 | *dst++ = ' '; |
| 454 | } |
| 455 | state = 2; |
| 456 | |
| 457 | *dst++ = *src; |
| 458 | } |
| 459 | ++src; |
| 460 | } |
| 461 | // Add null-terminator. |
| 462 | *dst = 0; |
| 463 | } |
| 464 | |
| 465 | #endif // MICROPY_ROM_TEXT_COMPRESSION |