Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 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" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 34 | |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 35 | // NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings) |
| 36 | // ultimately we will replace this with a static hash table of some kind |
| 37 | // also probably need to include the length in the string data, to allow null bytes in the string |
| 38 | |
| 39 | #if 0 // 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. |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 46 | // The data for a qstr contains (hash, length, data): |
| 47 | // - hash (configurable number of bytes) |
| 48 | // - length (configurable number of bytes) |
| 49 | // - data ("length" number of bytes) |
| 50 | // - \0 terminated (so they can be printed using printf) |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 51 | |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 52 | #if MICROPY_QSTR_BYTES_IN_HASH == 1 |
| 53 | #define Q_HASH_MASK (0xff) |
| 54 | #define Q_GET_HASH(q) ((mp_uint_t)(q)[0]) |
| 55 | #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); } while (0) |
| 56 | #elif MICROPY_QSTR_BYTES_IN_HASH == 2 |
| 57 | #define Q_HASH_MASK (0xffff) |
| 58 | #define Q_GET_HASH(q) ((mp_uint_t)(q)[0] | ((mp_uint_t)(q)[1] << 8)) |
| 59 | #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); (q)[1] = (hash) >> 8; } while (0) |
| 60 | #else |
| 61 | #error unimplemented qstr hash decoding |
| 62 | #endif |
| 63 | #define Q_GET_ALLOC(q) (MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1) |
| 64 | #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] | 65 | #if MICROPY_QSTR_BYTES_IN_LEN == 1 |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 66 | #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH]) |
| 67 | #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] | 68 | #elif MICROPY_QSTR_BYTES_IN_LEN == 2 |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 69 | #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH] | ((q)[MICROPY_QSTR_BYTES_IN_HASH + 1] << 8)) |
| 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) |
Damien George | 95836f8 | 2015-01-11 22:27:30 +0000 | [diff] [blame] | 71 | #else |
| 72 | #error unimplemented qstr length decoding |
| 73 | #endif |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 74 | |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 75 | // this must match the equivalent function in makeqstrdata.py |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 76 | mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 77 | // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 78 | mp_uint_t hash = 5381; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 79 | for (const byte *top = data + len; data < top; data++) { |
Damien George | 6e628c4 | 2014-03-25 15:27:15 +0000 | [diff] [blame] | 80 | hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 81 | } |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 82 | hash &= Q_HASH_MASK; |
Paul Sokolovsky | 59e269c | 2014-04-14 01:43:01 +0300 | [diff] [blame] | 83 | // Make sure that valid hash is never zero, zero means "hash not computed" |
| 84 | if (hash == 0) { |
| 85 | hash++; |
| 86 | } |
| 87 | return hash; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Damien George | 73c98d8 | 2014-06-11 19:18:03 +0100 | [diff] [blame] | 90 | STATIC const qstr_pool_t const_pool = { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 91 | NULL, // no previous pool |
| 92 | 0, // no previous pool |
| 93 | 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below) |
| 94 | MP_QSTR_number_of, // corresponds to number of strings in array just below |
| 95 | { |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 96 | #define QDEF(id, str) str, |
Damien George | d553be5 | 2014-04-17 18:03:27 +0100 | [diff] [blame] | 97 | #include "genhdr/qstrdefs.generated.h" |
Damien George | 6942f80 | 2015-01-11 17:52:45 +0000 | [diff] [blame] | 98 | #undef QDEF |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 99 | }, |
| 100 | }; |
| 101 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 102 | void qstr_init(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 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 |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 104 | MP_STATE_VM(qstr_last_chunk) = NULL; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 107 | STATIC const byte *find_qstr(qstr q) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 108 | // search pool for this qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 109 | 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] | 110 | if (q >= pool->total_prev_len) { |
| 111 | return pool->qstrs[q - pool->total_prev_len]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // not found |
| 116 | return 0; |
| 117 | } |
| 118 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 119 | STATIC qstr qstr_add(const byte *q_ptr) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 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)); |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 121 | |
| 122 | // make sure we have room in the pool for a new qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 123 | if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) { |
| 124 | qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2); |
| 125 | pool->prev = MP_STATE_VM(last_pool); |
| 126 | pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len; |
| 127 | pool->alloc = MP_STATE_VM(last_pool)->alloc * 2; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 128 | pool->len = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 129 | MP_STATE_VM(last_pool) = pool; |
| 130 | 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] | 131 | } |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 132 | |
| 133 | // add the new qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 134 | 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] | 135 | |
| 136 | // return id for the newly-added qstr |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 137 | 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] | 138 | } |
| 139 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 140 | qstr qstr_find_strn(const char *str, mp_uint_t str_len) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 141 | // work out hash of str |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 142 | 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] | 143 | |
| 144 | // search pools for the data |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 145 | 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] | 146 | 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] | 147 | 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] | 148 | return pool->total_prev_len + (q - pool->qstrs); |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 149 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 152 | |
| 153 | // not found; return null qstr |
| 154 | return 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 157 | qstr qstr_from_str(const char *str) { |
| 158 | return qstr_from_strn(str, strlen(str)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 161 | qstr qstr_from_strn(const char *str, mp_uint_t len) { |
Damien George | 4c81ba8 | 2015-01-13 16:21:23 +0000 | [diff] [blame] | 162 | assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))); |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 163 | qstr q = qstr_find_strn(str, len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 164 | if (q == 0) { |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 165 | // qstr does not exist in interned pool so need to add it |
| 166 | |
| 167 | // compute number of bytes needed to intern this string |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 168 | mp_uint_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] | 169 | |
| 170 | if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) { |
| 171 | // not enough room at end of previously interned string so try to grow |
| 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); |
| 173 | if (new_p == NULL) { |
| 174 | // could not grow existing memory; shrink it to fit previous |
| 175 | (void)m_renew(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used)); |
| 176 | MP_STATE_VM(qstr_last_chunk) = NULL; |
| 177 | } else { |
| 178 | // could grow existing memory |
| 179 | MP_STATE_VM(qstr_last_alloc) += n_bytes; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 184 | // no existing memory for the interned string so allocate a new chunk |
| 185 | mp_uint_t al = n_bytes; |
| 186 | if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) { |
| 187 | al = MICROPY_ALLOC_QSTR_CHUNK_INIT; |
| 188 | } |
| 189 | MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, al); |
| 190 | if (MP_STATE_VM(qstr_last_chunk) == NULL) { |
| 191 | // failed to allocate a large chunk so try with exact size |
| 192 | MP_STATE_VM(qstr_last_chunk) = m_new(byte, n_bytes); |
| 193 | al = n_bytes; |
| 194 | } |
| 195 | MP_STATE_VM(qstr_last_alloc) = al; |
| 196 | MP_STATE_VM(qstr_last_used) = 0; |
| 197 | } |
| 198 | |
| 199 | // allocate memory from the chunk for this new interned string's data |
| 200 | byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used); |
| 201 | MP_STATE_VM(qstr_last_used) += n_bytes; |
| 202 | |
| 203 | // store the interned strings' data |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 204 | mp_uint_t hash = qstr_compute_hash((const byte*)str, len); |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 205 | Q_SET_HASH(q_ptr, hash); |
Damien George | 95836f8 | 2015-01-11 22:27:30 +0000 | [diff] [blame] | 206 | Q_SET_LENGTH(q_ptr, len); |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 207 | memcpy(q_ptr + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN, str, len); |
| 208 | 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] | 209 | q = qstr_add(q_ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 210 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 211 | return q; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 214 | byte *qstr_build_start(mp_uint_t len, byte **q_ptr) { |
Damien George | 95836f8 | 2015-01-11 22:27:30 +0000 | [diff] [blame] | 215 | assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))); |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 216 | *q_ptr = m_new(byte, MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1); |
Damien George | 95836f8 | 2015-01-11 22:27:30 +0000 | [diff] [blame] | 217 | Q_SET_LENGTH(*q_ptr, len); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 218 | return Q_GET_DATA(*q_ptr); |
| 219 | } |
| 220 | |
| 221 | qstr qstr_build_end(byte *q_ptr) { |
Damien George | 2617eeb | 2014-05-25 22:27:57 +0100 | [diff] [blame] | 222 | qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr)); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 223 | if (q == 0) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 224 | mp_uint_t len = Q_GET_LENGTH(q_ptr); |
| 225 | mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len); |
Damien George | c3bd941 | 2015-07-20 11:03:13 +0000 | [diff] [blame^] | 226 | Q_SET_HASH(q_ptr, hash); |
| 227 | 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] | 228 | q = qstr_add(q_ptr); |
| 229 | } else { |
| 230 | m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr)); |
| 231 | } |
| 232 | return q; |
| 233 | } |
| 234 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 235 | mp_uint_t qstr_hash(qstr q) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 236 | return Q_GET_HASH(find_qstr(q)); |
| 237 | } |
| 238 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 239 | mp_uint_t qstr_len(qstr q) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 240 | const byte *qd = find_qstr(q); |
| 241 | return Q_GET_LENGTH(qd); |
| 242 | } |
| 243 | |
| 244 | // XXX to remove! |
| 245 | const char *qstr_str(qstr q) { |
| 246 | const byte *qd = find_qstr(q); |
| 247 | return (const char*)Q_GET_DATA(qd); |
| 248 | } |
| 249 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 250 | const byte *qstr_data(qstr q, mp_uint_t *len) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 251 | const byte *qd = find_qstr(q); |
| 252 | *len = Q_GET_LENGTH(qd); |
| 253 | return Q_GET_DATA(qd); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 254 | } |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 255 | |
Damien George | 39dc145 | 2014-10-03 19:52:22 +0100 | [diff] [blame] | 256 | void qstr_pool_info(mp_uint_t *n_pool, mp_uint_t *n_qstr, mp_uint_t *n_str_data_bytes, mp_uint_t *n_total_bytes) { |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 257 | *n_pool = 0; |
| 258 | *n_qstr = 0; |
| 259 | *n_str_data_bytes = 0; |
| 260 | *n_total_bytes = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 261 | 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] | 262 | *n_pool += 1; |
| 263 | *n_qstr += pool->len; |
| 264 | for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 265 | #if MICROPY_ENABLE_GC |
| 266 | *n_str_data_bytes += gc_nbytes(*q); // this counts actual bytes used in heap |
| 267 | #else |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 268 | *n_str_data_bytes += Q_GET_ALLOC(*q); |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 269 | #endif |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 270 | } |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 271 | #if MICROPY_ENABLE_GC |
| 272 | *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap |
| 273 | #else |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 274 | *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc; |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 275 | #endif |
Damien George | 4d5b28c | 2014-01-29 18:56:46 +0000 | [diff] [blame] | 276 | } |
| 277 | *n_total_bytes += *n_str_data_bytes; |
| 278 | } |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 279 | |
| 280 | #if MICROPY_PY_MICROPYTHON_MEM_INFO |
| 281 | void qstr_dump_data(void) { |
| 282 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) { |
| 283 | 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] | 284 | mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q)); |
Damien George | ea0461d | 2015-02-10 11:02:28 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | #endif |