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