Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
py/qstr.c@0:5868e8752d44, 2016-04-16 (annotated)
- Committer:
- pythontech
- Date:
- Sat Apr 16 17:11:56 2016 +0000
- Revision:
- 0:5868e8752d44
- Child:
- 2:c89e95946844
Split off library from repl
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pythontech | 0:5868e8752d44 | 1 | /* |
pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
pythontech | 0:5868e8752d44 | 3 | * |
pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
pythontech | 0:5868e8752d44 | 5 | * |
pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
pythontech | 0:5868e8752d44 | 7 | * |
pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
pythontech | 0:5868e8752d44 | 14 | * |
pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
pythontech | 0:5868e8752d44 | 17 | * |
pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
pythontech | 0:5868e8752d44 | 25 | */ |
pythontech | 0:5868e8752d44 | 26 | |
pythontech | 0:5868e8752d44 | 27 | #include <assert.h> |
pythontech | 0:5868e8752d44 | 28 | #include <string.h> |
pythontech | 0:5868e8752d44 | 29 | #include <stdio.h> |
pythontech | 0:5868e8752d44 | 30 | |
pythontech | 0:5868e8752d44 | 31 | #include "py/mpstate.h" |
pythontech | 0:5868e8752d44 | 32 | #include "py/qstr.h" |
pythontech | 0:5868e8752d44 | 33 | #include "py/gc.h" |
pythontech | 0:5868e8752d44 | 34 | |
pythontech | 0:5868e8752d44 | 35 | // NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings) |
pythontech | 0:5868e8752d44 | 36 | // ultimately we will replace this with a static hash table of some kind |
pythontech | 0:5868e8752d44 | 37 | // also probably need to include the length in the string data, to allow null bytes in the string |
pythontech | 0:5868e8752d44 | 38 | |
pythontech | 0:5868e8752d44 | 39 | #if 0 // print debugging info |
pythontech | 0:5868e8752d44 | 40 | #define DEBUG_printf DEBUG_printf |
pythontech | 0:5868e8752d44 | 41 | #else // don't print debugging info |
pythontech | 0:5868e8752d44 | 42 | #define DEBUG_printf(...) (void)0 |
pythontech | 0:5868e8752d44 | 43 | #endif |
pythontech | 0:5868e8752d44 | 44 | |
pythontech | 0:5868e8752d44 | 45 | // A qstr is an index into the qstr pool. |
pythontech | 0:5868e8752d44 | 46 | // The data for a qstr contains (hash, length, data): |
pythontech | 0:5868e8752d44 | 47 | // - hash (configurable number of bytes) |
pythontech | 0:5868e8752d44 | 48 | // - length (configurable number of bytes) |
pythontech | 0:5868e8752d44 | 49 | // - data ("length" number of bytes) |
pythontech | 0:5868e8752d44 | 50 | // - \0 terminated (so they can be printed using printf) |
pythontech | 0:5868e8752d44 | 51 | |
pythontech | 0:5868e8752d44 | 52 | #if MICROPY_QSTR_BYTES_IN_HASH == 1 |
pythontech | 0:5868e8752d44 | 53 | #define Q_HASH_MASK (0xff) |
pythontech | 0:5868e8752d44 | 54 | #define Q_GET_HASH(q) ((mp_uint_t)(q)[0]) |
pythontech | 0:5868e8752d44 | 55 | #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); } while (0) |
pythontech | 0:5868e8752d44 | 56 | #elif MICROPY_QSTR_BYTES_IN_HASH == 2 |
pythontech | 0:5868e8752d44 | 57 | #define Q_HASH_MASK (0xffff) |
pythontech | 0:5868e8752d44 | 58 | #define Q_GET_HASH(q) ((mp_uint_t)(q)[0] | ((mp_uint_t)(q)[1] << 8)) |
pythontech | 0:5868e8752d44 | 59 | #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); (q)[1] = (hash) >> 8; } while (0) |
pythontech | 0:5868e8752d44 | 60 | #else |
pythontech | 0:5868e8752d44 | 61 | #error unimplemented qstr hash decoding |
pythontech | 0:5868e8752d44 | 62 | #endif |
pythontech | 0:5868e8752d44 | 63 | #define Q_GET_ALLOC(q) (MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1) |
pythontech | 0:5868e8752d44 | 64 | #define Q_GET_DATA(q) ((q) + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN) |
pythontech | 0:5868e8752d44 | 65 | #if MICROPY_QSTR_BYTES_IN_LEN == 1 |
pythontech | 0:5868e8752d44 | 66 | #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH]) |
pythontech | 0:5868e8752d44 | 67 | #define Q_SET_LENGTH(q, len) do { (q)[MICROPY_QSTR_BYTES_IN_HASH] = (len); } while (0) |
pythontech | 0:5868e8752d44 | 68 | #elif MICROPY_QSTR_BYTES_IN_LEN == 2 |
pythontech | 0:5868e8752d44 | 69 | #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH] | ((q)[MICROPY_QSTR_BYTES_IN_HASH + 1] << 8)) |
pythontech | 0:5868e8752d44 | 70 | #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) |
pythontech | 0:5868e8752d44 | 71 | #else |
pythontech | 0:5868e8752d44 | 72 | #error unimplemented qstr length decoding |
pythontech | 0:5868e8752d44 | 73 | #endif |
pythontech | 0:5868e8752d44 | 74 | |
pythontech | 0:5868e8752d44 | 75 | // this must match the equivalent function in makeqstrdata.py |
pythontech | 0:5868e8752d44 | 76 | mp_uint_t qstr_compute_hash(const byte *data, size_t len) { |
pythontech | 0:5868e8752d44 | 77 | // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html |
pythontech | 0:5868e8752d44 | 78 | mp_uint_t hash = 5381; |
pythontech | 0:5868e8752d44 | 79 | for (const byte *top = data + len; data < top; data++) { |
pythontech | 0:5868e8752d44 | 80 | hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data |
pythontech | 0:5868e8752d44 | 81 | } |
pythontech | 0:5868e8752d44 | 82 | hash &= Q_HASH_MASK; |
pythontech | 0:5868e8752d44 | 83 | // Make sure that valid hash is never zero, zero means "hash not computed" |
pythontech | 0:5868e8752d44 | 84 | if (hash == 0) { |
pythontech | 0:5868e8752d44 | 85 | hash++; |
pythontech | 0:5868e8752d44 | 86 | } |
pythontech | 0:5868e8752d44 | 87 | return hash; |
pythontech | 0:5868e8752d44 | 88 | } |
pythontech | 0:5868e8752d44 | 89 | |
pythontech | 0:5868e8752d44 | 90 | STATIC const qstr_pool_t const_pool = { |
pythontech | 0:5868e8752d44 | 91 | NULL, // no previous pool |
pythontech | 0:5868e8752d44 | 92 | 0, // no previous pool |
pythontech | 0:5868e8752d44 | 93 | 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below) |
pythontech | 0:5868e8752d44 | 94 | MP_QSTR_number_of, // corresponds to number of strings in array just below |
pythontech | 0:5868e8752d44 | 95 | { |
pythontech | 0:5868e8752d44 | 96 | #define QDEF(id, str) str, |
pythontech | 0:5868e8752d44 | 97 | #include "genhdr/qstrdefs.generated.h" |
pythontech | 0:5868e8752d44 | 98 | #undef QDEF |
pythontech | 0:5868e8752d44 | 99 | }, |
pythontech | 0:5868e8752d44 | 100 | }; |
pythontech | 0:5868e8752d44 | 101 | |
pythontech | 0:5868e8752d44 | 102 | void qstr_init(void) { |
pythontech | 0:5868e8752d44 | 103 | MP_STATE_VM(last_pool) = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left |
pythontech | 0:5868e8752d44 | 104 | MP_STATE_VM(qstr_last_chunk) = NULL; |
pythontech | 0:5868e8752d44 | 105 | } |
pythontech | 0:5868e8752d44 | 106 | |
pythontech | 0:5868e8752d44 | 107 | STATIC const byte *find_qstr(qstr q) { |
pythontech | 0:5868e8752d44 | 108 | // search pool for this qstr |
pythontech | 0:5868e8752d44 | 109 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { |
pythontech | 0:5868e8752d44 | 110 | if (q >= pool->total_prev_len) { |
pythontech | 0:5868e8752d44 | 111 | return pool->qstrs[q - pool->total_prev_len]; |
pythontech | 0:5868e8752d44 | 112 | } |
pythontech | 0:5868e8752d44 | 113 | } |
pythontech | 0:5868e8752d44 | 114 | |
pythontech | 0:5868e8752d44 | 115 | // not found |
pythontech | 0:5868e8752d44 | 116 | return 0; |
pythontech | 0:5868e8752d44 | 117 | } |
pythontech | 0:5868e8752d44 | 118 | |
pythontech | 0:5868e8752d44 | 119 | STATIC qstr qstr_add(const byte *q_ptr) { |
pythontech | 0:5868e8752d44 | 120 | 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)); |
pythontech | 0:5868e8752d44 | 121 | |
pythontech | 0:5868e8752d44 | 122 | // make sure we have room in the pool for a new qstr |
pythontech | 0:5868e8752d44 | 123 | if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) { |
pythontech | 0:5868e8752d44 | 124 | qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2); |
pythontech | 0:5868e8752d44 | 125 | pool->prev = MP_STATE_VM(last_pool); |
pythontech | 0:5868e8752d44 | 126 | pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len; |
pythontech | 0:5868e8752d44 | 127 | pool->alloc = MP_STATE_VM(last_pool)->alloc * 2; |
pythontech | 0:5868e8752d44 | 128 | pool->len = 0; |
pythontech | 0:5868e8752d44 | 129 | MP_STATE_VM(last_pool) = pool; |
pythontech | 0:5868e8752d44 | 130 | DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc); |
pythontech | 0:5868e8752d44 | 131 | } |
pythontech | 0:5868e8752d44 | 132 | |
pythontech | 0:5868e8752d44 | 133 | // add the new qstr |
pythontech | 0:5868e8752d44 | 134 | MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr; |
pythontech | 0:5868e8752d44 | 135 | |
pythontech | 0:5868e8752d44 | 136 | // return id for the newly-added qstr |
pythontech | 0:5868e8752d44 | 137 | return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1; |
pythontech | 0:5868e8752d44 | 138 | } |
pythontech | 0:5868e8752d44 | 139 | |
pythontech | 0:5868e8752d44 | 140 | qstr qstr_find_strn(const char *str, size_t str_len) { |
pythontech | 0:5868e8752d44 | 141 | // work out hash of str |
pythontech | 0:5868e8752d44 | 142 | mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len); |
pythontech | 0:5868e8752d44 | 143 | |
pythontech | 0:5868e8752d44 | 144 | // search pools for the data |
pythontech | 0:5868e8752d44 | 145 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) { |
pythontech | 0:5868e8752d44 | 146 | for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { |
pythontech | 0:5868e8752d44 | 147 | if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) { |
pythontech | 0:5868e8752d44 | 148 | return pool->total_prev_len + (q - pool->qstrs); |
pythontech | 0:5868e8752d44 | 149 | } |
pythontech | 0:5868e8752d44 | 150 | } |
pythontech | 0:5868e8752d44 | 151 | } |
pythontech | 0:5868e8752d44 | 152 | |
pythontech | 0:5868e8752d44 | 153 | // not found; return null qstr |
pythontech | 0:5868e8752d44 | 154 | return 0; |
pythontech | 0:5868e8752d44 | 155 | } |
pythontech | 0:5868e8752d44 | 156 | |
pythontech | 0:5868e8752d44 | 157 | qstr qstr_from_str(const char *str) { |
pythontech | 0:5868e8752d44 | 158 | return qstr_from_strn(str, strlen(str)); |
pythontech | 0:5868e8752d44 | 159 | } |
pythontech | 0:5868e8752d44 | 160 | |
pythontech | 0:5868e8752d44 | 161 | qstr qstr_from_strn(const char *str, size_t len) { |
pythontech | 0:5868e8752d44 | 162 | assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))); |
pythontech | 0:5868e8752d44 | 163 | qstr q = qstr_find_strn(str, len); |
pythontech | 0:5868e8752d44 | 164 | if (q == 0) { |
pythontech | 0:5868e8752d44 | 165 | // qstr does not exist in interned pool so need to add it |
pythontech | 0:5868e8752d44 | 166 | |
pythontech | 0:5868e8752d44 | 167 | // compute number of bytes needed to intern this string |
pythontech | 0:5868e8752d44 | 168 | size_t n_bytes = MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1; |
pythontech | 0:5868e8752d44 | 169 | |
pythontech | 0:5868e8752d44 | 170 | if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) { |
pythontech | 0:5868e8752d44 | 171 | // not enough room at end of previously interned string so try to grow |
pythontech | 0:5868e8752d44 | 172 | 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); |
pythontech | 0:5868e8752d44 | 173 | if (new_p == NULL) { |
pythontech | 0:5868e8752d44 | 174 | // could not grow existing memory; shrink it to fit previous |
pythontech | 0:5868e8752d44 | 175 | (void)m_renew(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used)); |
pythontech | 0:5868e8752d44 | 176 | MP_STATE_VM(qstr_last_chunk) = NULL; |
pythontech | 0:5868e8752d44 | 177 | } else { |
pythontech | 0:5868e8752d44 | 178 | // could grow existing memory |
pythontech | 0:5868e8752d44 | 179 | MP_STATE_VM(qstr_last_alloc) += n_bytes; |
pythontech | 0:5868e8752d44 | 180 | } |
pythontech | 0:5868e8752d44 | 181 | } |
pythontech | 0:5868e8752d44 | 182 | |
pythontech | 0:5868e8752d44 | 183 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
pythontech | 0:5868e8752d44 | 184 | // no existing memory for the interned string so allocate a new chunk |
pythontech | 0:5868e8752d44 | 185 | size_t al = n_bytes; |
pythontech | 0:5868e8752d44 | 186 | if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) { |
pythontech | 0:5868e8752d44 | 187 | al = MICROPY_ALLOC_QSTR_CHUNK_INIT; |
pythontech | 0:5868e8752d44 | 188 | } |
pythontech | 0:5868e8752d44 | 189 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, al); |
pythontech | 0:5868e8752d44 | 190 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
pythontech | 0:5868e8752d44 | 191 | // failed to allocate a large chunk so try with exact size |
pythontech | 0:5868e8752d44 | 192 | MP_STATE_VM(qstr_last_chunk) = m_new(byte, n_bytes); |
pythontech | 0:5868e8752d44 | 193 | al = n_bytes; |
pythontech | 0:5868e8752d44 | 194 | } |
pythontech | 0:5868e8752d44 | 195 | MP_STATE_VM(qstr_last_alloc) = al; |
pythontech | 0:5868e8752d44 | 196 | MP_STATE_VM(qstr_last_used) = 0; |
pythontech | 0:5868e8752d44 | 197 | } |
pythontech | 0:5868e8752d44 | 198 | |
pythontech | 0:5868e8752d44 | 199 | // allocate memory from the chunk for this new interned string's data |
pythontech | 0:5868e8752d44 | 200 | byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used); |
pythontech | 0:5868e8752d44 | 201 | MP_STATE_VM(qstr_last_used) += n_bytes; |
pythontech | 0:5868e8752d44 | 202 | |
pythontech | 0:5868e8752d44 | 203 | // store the interned strings' data |
pythontech | 0:5868e8752d44 | 204 | mp_uint_t hash = qstr_compute_hash((const byte*)str, len); |
pythontech | 0:5868e8752d44 | 205 | Q_SET_HASH(q_ptr, hash); |
pythontech | 0:5868e8752d44 | 206 | Q_SET_LENGTH(q_ptr, len); |
pythontech | 0:5868e8752d44 | 207 | memcpy(q_ptr + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN, str, len); |
pythontech | 0:5868e8752d44 | 208 | q_ptr[MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0'; |
pythontech | 0:5868e8752d44 | 209 | q = qstr_add(q_ptr); |
pythontech | 0:5868e8752d44 | 210 | } |
pythontech | 0:5868e8752d44 | 211 | return q; |
pythontech | 0:5868e8752d44 | 212 | } |
pythontech | 0:5868e8752d44 | 213 | |
pythontech | 0:5868e8752d44 | 214 | byte *qstr_build_start(size_t len, byte **q_ptr) { |
pythontech | 0:5868e8752d44 | 215 | assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))); |
pythontech | 0:5868e8752d44 | 216 | *q_ptr = m_new(byte, MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1); |
pythontech | 0:5868e8752d44 | 217 | Q_SET_LENGTH(*q_ptr, len); |
pythontech | 0:5868e8752d44 | 218 | return Q_GET_DATA(*q_ptr); |
pythontech | 0:5868e8752d44 | 219 | } |
pythontech | 0:5868e8752d44 | 220 | |
pythontech | 0:5868e8752d44 | 221 | qstr qstr_build_end(byte *q_ptr) { |
pythontech | 0:5868e8752d44 | 222 | qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr)); |
pythontech | 0:5868e8752d44 | 223 | if (q == 0) { |
pythontech | 0:5868e8752d44 | 224 | size_t len = Q_GET_LENGTH(q_ptr); |
pythontech | 0:5868e8752d44 | 225 | mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len); |
pythontech | 0:5868e8752d44 | 226 | Q_SET_HASH(q_ptr, hash); |
pythontech | 0:5868e8752d44 | 227 | q_ptr[MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0'; |
pythontech | 0:5868e8752d44 | 228 | q = qstr_add(q_ptr); |
pythontech | 0:5868e8752d44 | 229 | } else { |
pythontech | 0:5868e8752d44 | 230 | m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr)); |
pythontech | 0:5868e8752d44 | 231 | } |
pythontech | 0:5868e8752d44 | 232 | return q; |
pythontech | 0:5868e8752d44 | 233 | } |
pythontech | 0:5868e8752d44 | 234 | |
pythontech | 0:5868e8752d44 | 235 | mp_uint_t qstr_hash(qstr q) { |
pythontech | 0:5868e8752d44 | 236 | return Q_GET_HASH(find_qstr(q)); |
pythontech | 0:5868e8752d44 | 237 | } |
pythontech | 0:5868e8752d44 | 238 | |
pythontech | 0:5868e8752d44 | 239 | size_t qstr_len(qstr q) { |
pythontech | 0:5868e8752d44 | 240 | const byte *qd = find_qstr(q); |
pythontech | 0:5868e8752d44 | 241 | return Q_GET_LENGTH(qd); |
pythontech | 0:5868e8752d44 | 242 | } |
pythontech | 0:5868e8752d44 | 243 | |
pythontech | 0:5868e8752d44 | 244 | // XXX to remove! |
pythontech | 0:5868e8752d44 | 245 | const char *qstr_str(qstr q) { |
pythontech | 0:5868e8752d44 | 246 | const byte *qd = find_qstr(q); |
pythontech | 0:5868e8752d44 | 247 | return (const char*)Q_GET_DATA(qd); |
pythontech | 0:5868e8752d44 | 248 | } |
pythontech | 0:5868e8752d44 | 249 | |
pythontech | 0:5868e8752d44 | 250 | const byte *qstr_data(qstr q, size_t *len) { |
pythontech | 0:5868e8752d44 | 251 | const byte *qd = find_qstr(q); |
pythontech | 0:5868e8752d44 | 252 | *len = Q_GET_LENGTH(qd); |
pythontech | 0:5868e8752d44 | 253 | return Q_GET_DATA(qd); |
pythontech | 0:5868e8752d44 | 254 | } |
pythontech | 0:5868e8752d44 | 255 | |
pythontech | 0:5868e8752d44 | 256 | void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) { |
pythontech | 0:5868e8752d44 | 257 | *n_pool = 0; |
pythontech | 0:5868e8752d44 | 258 | *n_qstr = 0; |
pythontech | 0:5868e8752d44 | 259 | *n_str_data_bytes = 0; |
pythontech | 0:5868e8752d44 | 260 | *n_total_bytes = 0; |
pythontech | 0:5868e8752d44 | 261 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) { |
pythontech | 0:5868e8752d44 | 262 | *n_pool += 1; |
pythontech | 0:5868e8752d44 | 263 | *n_qstr += pool->len; |
pythontech | 0:5868e8752d44 | 264 | for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { |
pythontech | 0:5868e8752d44 | 265 | *n_str_data_bytes += Q_GET_ALLOC(*q); |
pythontech | 0:5868e8752d44 | 266 | } |
pythontech | 0:5868e8752d44 | 267 | #if MICROPY_ENABLE_GC |
pythontech | 0:5868e8752d44 | 268 | *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap |
pythontech | 0:5868e8752d44 | 269 | #else |
pythontech | 0:5868e8752d44 | 270 | *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc; |
pythontech | 0:5868e8752d44 | 271 | #endif |
pythontech | 0:5868e8752d44 | 272 | } |
pythontech | 0:5868e8752d44 | 273 | *n_total_bytes += *n_str_data_bytes; |
pythontech | 0:5868e8752d44 | 274 | } |
pythontech | 0:5868e8752d44 | 275 | |
pythontech | 0:5868e8752d44 | 276 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
pythontech | 0:5868e8752d44 | 277 | void qstr_dump_data(void) { |
pythontech | 0:5868e8752d44 | 278 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) { |
pythontech | 0:5868e8752d44 | 279 | for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { |
pythontech | 0:5868e8752d44 | 280 | mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q)); |
pythontech | 0:5868e8752d44 | 281 | } |
pythontech | 0:5868e8752d44 | 282 | } |
pythontech | 0:5868e8752d44 | 283 | } |
pythontech | 0:5868e8752d44 | 284 | #endif |