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 | |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 36 | // NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings) |
| 37 | // ultimately we will replace this with a static hash table of some kind |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 38 | |
Stefan Naumann | ace9fb5 | 2017-07-24 18:55:14 +0200 | [diff] [blame] | 39 | #if MICROPY_DEBUG_VERBOSE // print debugging info |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 40 | #define DEBUG_printf DEBUG_printf |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 41 | #else // don't print debugging info |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 42 | #define DEBUG_printf(...) (void)0 |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 45 | // A qstr is an index into the qstr pool. |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 46 | // 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] | 47 | |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 48 | #define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 49 | |
Damien George | a1c93a6 | 2016-05-26 10:53:34 +0000 | [diff] [blame] | 50 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 51 | #define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1) |
| 52 | #define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex)) |
| 53 | #else |
| 54 | #define QSTR_ENTER() |
| 55 | #define QSTR_EXIT() |
| 56 | #endif |
| 57 | |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 58 | // Initial number of entries for qstr pool, set so that the first dynamically |
| 59 | // allocated pool is twice this size. The value here must be <= MP_QSTRnumber_of. |
| 60 | #define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10) |
| 61 | |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 62 | // this must match the equivalent function in makeqstrdata.py |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 63 | mp_uint_t qstr_compute_hash(const byte *data, size_t len) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 64 | // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 65 | mp_uint_t hash = 5381; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 66 | for (const byte *top = data + len; data < top; data++) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 67 | hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 68 | } |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame] | 69 | hash &= Q_HASH_MASK; |
Paul Sokolovsky | 59e269c | 2014-04-14 01:43:01 +0300 | [diff] [blame] | 70 | // Make sure that valid hash is never zero, zero means "hash not computed" |
| 71 | if (hash == 0) { |
| 72 | hash++; |
| 73 | } |
| 74 | return hash; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 77 | const qstr_hash_t mp_qstr_const_hashes[] = { |
| 78 | #ifndef NO_QSTR |
| 79 | #define QDEF(id, hash, len, str) hash, |
| 80 | #include "genhdr/qstrdefs.generated.h" |
| 81 | #undef QDEF |
| 82 | #endif |
| 83 | }; |
| 84 | |
| 85 | const qstr_len_t mp_qstr_const_lengths[] = { |
| 86 | #ifndef NO_QSTR |
| 87 | #define QDEF(id, hash, len, str) len, |
| 88 | #include "genhdr/qstrdefs.generated.h" |
| 89 | #undef QDEF |
| 90 | #endif |
| 91 | }; |
| 92 | |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 93 | const qstr_pool_t mp_qstr_const_pool = { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 94 | NULL, // no previous pool |
| 95 | 0, // no previous pool |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 96 | MICROPY_ALLOC_QSTR_ENTRIES_INIT, |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 97 | MP_QSTRnumber_of, // corresponds to number of strings in array just below |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 98 | (qstr_hash_t *)mp_qstr_const_hashes, |
| 99 | (qstr_len_t *)mp_qstr_const_lengths, |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 100 | { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 101 | #ifndef NO_QSTR |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 102 | #define QDEF(id, hash, len, str) str, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 103 | #include "genhdr/qstrdefs.generated.h" |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 104 | #undef QDEF |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 105 | #endif |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 106 | }, |
| 107 | }; |
| 108 | |
Damien George | 0a2e965 | 2016-01-31 22:24:16 +0000 | [diff] [blame] | 109 | #ifdef MICROPY_QSTR_EXTRA_POOL |
| 110 | extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL; |
| 111 | #define CONST_POOL MICROPY_QSTR_EXTRA_POOL |
| 112 | #else |
| 113 | #define CONST_POOL mp_qstr_const_pool |
| 114 | #endif |
| 115 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 116 | void qstr_init(void) { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 117 | 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] | 118 | MP_STATE_VM(qstr_last_chunk) = NULL; |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 119 | |
David Lechner | edbb73a | 2020-01-22 11:19:37 -0600 | [diff] [blame] | 120 | #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 121 | mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex)); |
| 122 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 125 | STATIC const qstr_pool_t *find_qstr(qstr *q) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 126 | // search pool for this qstr |
Damien George | 8e323b8 | 2017-11-29 16:58:27 +1100 | [diff] [blame] | 127 | // 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^] | 128 | const qstr_pool_t *pool = MP_STATE_VM(last_pool); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 129 | while (*q < pool->total_prev_len) { |
Damien George | 8e323b8 | 2017-11-29 16:58:27 +1100 | [diff] [blame] | 130 | pool = pool->prev; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 131 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 132 | *q -= pool->total_prev_len; |
| 133 | assert(*q < pool->len); |
| 134 | return pool; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 137 | // qstr_mutex must be taken while in this function |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 138 | STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) { |
| 139 | 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] | 140 | |
| 141 | // make sure we have room in the pool for a new qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 142 | 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] | 143 | size_t new_alloc = MP_STATE_VM(last_pool)->alloc * 2; |
| 144 | #ifdef MICROPY_QSTR_EXTRA_POOL |
| 145 | // Put a lower bound on the allocation size in case the extra qstr pool has few entries |
| 146 | new_alloc = MAX(MICROPY_ALLOC_QSTR_ENTRIES_INIT, new_alloc); |
| 147 | #endif |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 148 | mp_uint_t pool_size = sizeof(qstr_pool_t) |
| 149 | + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * new_alloc; |
| 150 | qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size); |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 151 | if (pool == NULL) { |
Emilie Feral | 58b56b9 | 2021-12-27 15:39:34 +0100 | [diff] [blame] | 152 | // Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned |
| 153 | // at garbage collection since it's reachable from a qstr_pool_t. And the caller of |
| 154 | // this function expects q_ptr to be stored in a qstr_pool_t so it can be reached |
| 155 | // by the collector. If qstr_pool_t allocation failed, qstr_last_chunk needs to be |
| 156 | // NULL'd. Otherwise it may become a dangling pointer at the next garbage collection. |
| 157 | MP_STATE_VM(qstr_last_chunk) = NULL; |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 158 | QSTR_EXIT(); |
Damien George | 0d165fe | 2018-12-13 17:08:26 +1100 | [diff] [blame] | 159 | m_malloc_fail(new_alloc); |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 160 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 161 | pool->hashes = (qstr_hash_t *)(pool->qstrs + new_alloc); |
| 162 | pool->lengths = (qstr_len_t *)(pool->hashes + new_alloc); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 163 | pool->prev = MP_STATE_VM(last_pool); |
| 164 | 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] | 165 | pool->alloc = new_alloc; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 166 | pool->len = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 167 | MP_STATE_VM(last_pool) = pool; |
| 168 | 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] | 169 | } |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 170 | |
| 171 | // add the new qstr |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 172 | mp_uint_t at = MP_STATE_VM(last_pool)->len; |
| 173 | MP_STATE_VM(last_pool)->hashes[at] = hash; |
| 174 | MP_STATE_VM(last_pool)->lengths[at] = len; |
| 175 | MP_STATE_VM(last_pool)->qstrs[at] = q_ptr; |
| 176 | MP_STATE_VM(last_pool)->len++; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 177 | |
| 178 | // return id for the newly-added qstr |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 179 | return MP_STATE_VM(last_pool)->total_prev_len + at; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 182 | qstr qstr_find_strn(const char *str, size_t str_len) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 183 | // work out hash of str |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 184 | mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 185 | |
| 186 | // search pools for the data |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 187 | for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 188 | for (mp_uint_t at = 0, top = pool->len; at < top; at++) { |
| 189 | if (pool->hashes[at] == str_hash && pool->lengths[at] == str_len |
| 190 | && memcmp(pool->qstrs[at], str, str_len) == 0) { |
| 191 | return pool->total_prev_len + at; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 192 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 195 | |
| 196 | // not found; return null qstr |
| 197 | return 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 200 | qstr qstr_from_str(const char *str) { |
| 201 | return qstr_from_strn(str, strlen(str)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 204 | qstr qstr_from_strn(const char *str, size_t len) { |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 205 | QSTR_ENTER(); |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 206 | qstr q = qstr_find_strn(str, len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 207 | if (q == 0) { |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 208 | // qstr does not exist in interned pool so need to add it |
| 209 | |
Léa Saviot | bc129f1 | 2019-11-22 10:07:08 +0100 | [diff] [blame] | 210 | // check that len is not too big |
| 211 | if (len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) { |
| 212 | QSTR_EXIT(); |
Jim Mussared | def76fe | 2020-03-02 22:35:22 +1100 | [diff] [blame] | 213 | 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] | 214 | } |
| 215 | |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 216 | // compute number of bytes needed to intern this string |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 217 | size_t n_bytes = len + 1; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 218 | |
| 219 | if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) { |
| 220 | // 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] | 221 | 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] | 222 | if (new_p == NULL) { |
| 223 | // could not grow existing memory; shrink it to fit previous |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 224 | (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] | 225 | MP_STATE_VM(qstr_last_chunk) = NULL; |
| 226 | } else { |
| 227 | // could grow existing memory |
| 228 | MP_STATE_VM(qstr_last_alloc) += n_bytes; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 233 | // no existing memory for the interned string so allocate a new chunk |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 234 | size_t al = n_bytes; |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 235 | if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) { |
| 236 | al = MICROPY_ALLOC_QSTR_CHUNK_INIT; |
| 237 | } |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 238 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, al); |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 239 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 240 | // failed to allocate a large chunk so try with exact size |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 241 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, n_bytes); |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 242 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 243 | QSTR_EXIT(); |
| 244 | m_malloc_fail(n_bytes); |
| 245 | } |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 246 | al = n_bytes; |
| 247 | } |
| 248 | MP_STATE_VM(qstr_last_alloc) = al; |
| 249 | MP_STATE_VM(qstr_last_used) = 0; |
| 250 | } |
| 251 | |
| 252 | // allocate memory from the chunk for this new interned string's data |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 253 | 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] | 254 | MP_STATE_VM(qstr_last_used) += n_bytes; |
| 255 | |
| 256 | // store the interned strings' data |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 257 | mp_uint_t hash = qstr_compute_hash((const byte *)str, len); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 258 | memcpy(q_ptr, str, len); |
| 259 | q_ptr[len] = '\0'; |
| 260 | q = qstr_add(hash, len, q_ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 261 | } |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 262 | QSTR_EXIT(); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 263 | return q; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 266 | mp_uint_t qstr_hash(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 267 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 268 | return pool->hashes[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 271 | size_t qstr_len(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 272 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 273 | return pool->lengths[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 276 | const char *qstr_str(qstr q) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 277 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 278 | return pool->qstrs[q]; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 281 | const byte *qstr_data(qstr q, size_t *len) { |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 282 | const qstr_pool_t *pool = find_qstr(&q); |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 283 | *len = pool->lengths[q]; |
| 284 | return (byte *)pool->qstrs[q]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 285 | } |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 286 | |
Damien George | 2578485 | 2015-12-17 12:41:40 +0000 | [diff] [blame] | 287 | 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] | 288 | QSTR_ENTER(); |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 289 | *n_pool = 0; |
| 290 | *n_qstr = 0; |
| 291 | *n_str_data_bytes = 0; |
| 292 | *n_total_bytes = 0; |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 293 | 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] | 294 | *n_pool += 1; |
| 295 | *n_qstr += pool->len; |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 296 | for (qstr_len_t *l = pool->lengths, *l_top = pool->lengths + pool->len; l < l_top; l++) { |
| 297 | *n_str_data_bytes += *l + 1; |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 298 | } |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 299 | #if MICROPY_ENABLE_GC |
| 300 | *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap |
| 301 | #else |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 302 | *n_total_bytes += sizeof(qstr_pool_t) |
| 303 | + (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] | 304 | #endif |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 305 | } |
| 306 | *n_total_bytes += *n_str_data_bytes; |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 307 | QSTR_EXIT(); |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 308 | } |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 309 | |
| 310 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
| 311 | void qstr_dump_data(void) { |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 312 | QSTR_ENTER(); |
Artyom Skrobov | f46a714 | 2021-05-04 03:35:45 -0400 | [diff] [blame^] | 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++) { |
Artyom Skrobov | 18b1ba0 | 2021-05-03 14:17:36 -0400 | [diff] [blame] | 315 | mp_printf(&mp_plat_print, "Q(%s)\n", *q); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
Damien George | 1f54ad2 | 2016-05-26 09:06:46 +0000 | [diff] [blame] | 318 | QSTR_EXIT(); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 319 | } |
| 320 | #endif |
Jim Mussared | 154b4eb | 2019-09-26 22:19:29 +1000 | [diff] [blame] | 321 | |
| 322 | #if MICROPY_ROM_TEXT_COMPRESSION |
| 323 | |
| 324 | #ifdef NO_QSTR |
| 325 | |
| 326 | // If NO_QSTR is set, it means we're doing QSTR extraction. |
| 327 | // So we won't yet have "genhdr/compressed.data.h" |
| 328 | |
| 329 | #else |
| 330 | |
| 331 | // Emit the compressed_string_data string. |
| 332 | #define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x; |
| 333 | #define MP_MATCH_COMPRESSED(a, b) |
| 334 | #include "genhdr/compressed.data.h" |
| 335 | #undef MP_COMPRESSED_DATA |
| 336 | #undef MP_MATCH_COMPRESSED |
| 337 | |
| 338 | #endif // NO_QSTR |
| 339 | |
| 340 | // This implements the "common word" compression scheme (see makecompresseddata.py) where the most |
| 341 | // common 128 words in error messages are replaced by their index into the list of common words. |
| 342 | |
| 343 | // The compressed string data is delimited by setting high bit in the final char of each word. |
| 344 | // e.g. aaaa<0x80|a>bbbbbb<0x80|b>.... |
| 345 | // This method finds the n'th string. |
| 346 | STATIC const byte *find_uncompressed_string(uint8_t n) { |
| 347 | const byte *c = (byte *)compressed_string_data; |
| 348 | while (n > 0) { |
| 349 | while ((*c & 0x80) == 0) { |
| 350 | ++c; |
| 351 | } |
| 352 | ++c; |
| 353 | --n; |
| 354 | } |
| 355 | return c; |
| 356 | } |
| 357 | |
| 358 | // Given a compressed string in src, decompresses it into dst. |
| 359 | // dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1). |
| 360 | void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src_chr) { |
| 361 | // Skip past the 0xff marker. |
| 362 | const byte *src = (byte *)src_chr + 1; |
| 363 | // Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2). |
| 364 | // 0 = start, 1 = compressed, 2 = regular. |
| 365 | int state = 0; |
| 366 | while (*src) { |
| 367 | if ((byte) * src >= 128) { |
| 368 | if (state != 0) { |
| 369 | *dst++ = ' '; |
| 370 | } |
| 371 | state = 1; |
| 372 | |
| 373 | // High bit set, replace with common word. |
| 374 | const byte *word = find_uncompressed_string(*src & 0x7f); |
| 375 | // The word is terminated by the final char having its high bit set. |
| 376 | while ((*word & 0x80) == 0) { |
| 377 | *dst++ = *word++; |
| 378 | } |
| 379 | *dst++ = (*word & 0x7f); |
| 380 | } else { |
| 381 | // Otherwise just copy one char. |
| 382 | if (state == 1) { |
| 383 | *dst++ = ' '; |
| 384 | } |
| 385 | state = 2; |
| 386 | |
| 387 | *dst++ = *src; |
| 388 | } |
| 389 | ++src; |
| 390 | } |
| 391 | // Add null-terminator. |
| 392 | *dst = 0; |
| 393 | } |
| 394 | |
| 395 | #endif // MICROPY_ROM_TEXT_COMPRESSION |