blob: dd04ae8f7578ed1a41618ca604d933949cae651f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28#include <string.h>
Damien Georgeea0461d2015-02-10 11:02:28 +000029#include <stdio.h>
Damien429d7192013-10-04 19:53:11 +010030
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/qstr.h"
33#include "py/gc.h"
Damien429d7192013-10-04 19:53:11 +010034
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000035// 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 Sokolovsky44739e22014-02-16 18:11:42 +020040#define DEBUG_printf DEBUG_printf
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000041#else // don't print debugging info
Damien George1dc76af2014-02-26 16:57:08 +000042#define DEBUG_printf(...) (void)0
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000043#endif
44
Damien George55baff42014-01-21 21:40:13 +000045// A qstr is an index into the qstr pool.
46// The data for a qstr contains (hash, length, data).
47// For now we use very simple encoding, just to get the framework correct:
Damien George6e628c42014-03-25 15:27:15 +000048// - hash is 2 bytes (see function below)
Damien George55baff42014-01-21 21:40:13 +000049// - length is 2 bytes
50// - data follows
51// - \0 terminated (for now, so they can be printed using printf)
52
Damien George963a5a32015-01-16 17:47:07 +000053#define Q_GET_HASH(q) ((mp_uint_t)(q)[0] | ((mp_uint_t)(q)[1] << 8))
Damien George95836f82015-01-11 22:27:30 +000054#define Q_GET_ALLOC(q) (2 + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
55#define Q_GET_DATA(q) ((q) + 2 + MICROPY_QSTR_BYTES_IN_LEN)
56#if MICROPY_QSTR_BYTES_IN_LEN == 1
57 #define Q_GET_LENGTH(q) ((q)[2])
58 #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); } while (0)
59#elif MICROPY_QSTR_BYTES_IN_LEN == 2
60 #define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
61 #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); (q)[3] = (len) >> 8; } while (0)
62#else
63 #error unimplemented qstr length decoding
64#endif
Damien George55baff42014-01-21 21:40:13 +000065
Damien George6e628c42014-03-25 15:27:15 +000066// this must match the equivalent function in makeqstrdata.py
Damien George39dc1452014-10-03 19:52:22 +010067mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) {
Damien George6e628c42014-03-25 15:27:15 +000068 // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
Damien George40f3c022014-07-03 13:25:24 +010069 mp_uint_t hash = 5381;
Damien George55baff42014-01-21 21:40:13 +000070 for (const byte *top = data + len; data < top; data++) {
Damien George6e628c42014-03-25 15:27:15 +000071 hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
Damien George55baff42014-01-21 21:40:13 +000072 }
Paul Sokolovsky59e269c2014-04-14 01:43:01 +030073 hash &= 0xffff;
74 // Make sure that valid hash is never zero, zero means "hash not computed"
75 if (hash == 0) {
76 hash++;
77 }
78 return hash;
Damien George55baff42014-01-21 21:40:13 +000079}
80
Damien George73c98d82014-06-11 19:18:03 +010081STATIC const qstr_pool_t const_pool = {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000082 NULL, // no previous pool
83 0, // no previous pool
84 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
85 MP_QSTR_number_of, // corresponds to number of strings in array just below
86 {
Damien George6942f802015-01-11 17:52:45 +000087#define QDEF(id, str) str,
Damien Georged553be52014-04-17 18:03:27 +010088#include "genhdr/qstrdefs.generated.h"
Damien George6942f802015-01-11 17:52:45 +000089#undef QDEF
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000090 },
91};
92
Damien8b3a7c22013-10-23 20:20:17 +010093void qstr_init(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +000094 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 Georgeade9a052015-06-13 21:53:22 +010095 MP_STATE_VM(qstr_last_chunk) = NULL;
Damien429d7192013-10-04 19:53:11 +010096}
97
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020098STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000099 // search pool for this qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000100 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000101 if (q >= pool->total_prev_len) {
102 return pool->qstrs[q - pool->total_prev_len];
103 }
104 }
105
106 // not found
107 return 0;
108}
109
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200110STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +0000111 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 Georgeeb7bfcb2014-01-04 15:57:35 +0000112
113 // make sure we have room in the pool for a new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000114 if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
115 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
116 pool->prev = MP_STATE_VM(last_pool);
117 pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
118 pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000119 pool->len = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000120 MP_STATE_VM(last_pool) = pool;
121 DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
Damien429d7192013-10-04 19:53:11 +0100122 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000123
124 // add the new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000125 MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000126
127 // return id for the newly-added qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000128 return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1;
Damien429d7192013-10-04 19:53:11 +0100129}
130
Damien George39dc1452014-10-03 19:52:22 +0100131qstr qstr_find_strn(const char *str, mp_uint_t str_len) {
Damien George55baff42014-01-21 21:40:13 +0000132 // work out hash of str
Damien George40f3c022014-07-03 13:25:24 +0100133 mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000134
135 // search pools for the data
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000136 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000137 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000138 if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
Damien George55baff42014-01-21 21:40:13 +0000139 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000140 }
Damien429d7192013-10-04 19:53:11 +0100141 }
142 }
Damien George55baff42014-01-21 21:40:13 +0000143
144 // not found; return null qstr
145 return 0;
Damien429d7192013-10-04 19:53:11 +0100146}
147
Damien George55baff42014-01-21 21:40:13 +0000148qstr qstr_from_str(const char *str) {
149 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100150}
151
Damien George39dc1452014-10-03 19:52:22 +0100152qstr qstr_from_strn(const char *str, mp_uint_t len) {
Damien George4c81ba82015-01-13 16:21:23 +0000153 assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
Damien George2617eeb2014-05-25 22:27:57 +0100154 qstr q = qstr_find_strn(str, len);
Damien George55baff42014-01-21 21:40:13 +0000155 if (q == 0) {
Damien Georgeade9a052015-06-13 21:53:22 +0100156 // qstr does not exist in interned pool so need to add it
157
158 // compute number of bytes needed to intern this string
159 mp_uint_t n_bytes = 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1;
160
161 if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
162 // not enough room at end of previously interned string so try to grow
163 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);
164 if (new_p == NULL) {
165 // could not grow existing memory; shrink it to fit previous
166 (void)m_renew(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used));
167 MP_STATE_VM(qstr_last_chunk) = NULL;
168 } else {
169 // could grow existing memory
170 MP_STATE_VM(qstr_last_alloc) += n_bytes;
171 }
172 }
173
174 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
175 // no existing memory for the interned string so allocate a new chunk
176 mp_uint_t al = n_bytes;
177 if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
178 al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
179 }
180 MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, al);
181 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
182 // failed to allocate a large chunk so try with exact size
183 MP_STATE_VM(qstr_last_chunk) = m_new(byte, n_bytes);
184 al = n_bytes;
185 }
186 MP_STATE_VM(qstr_last_alloc) = al;
187 MP_STATE_VM(qstr_last_used) = 0;
188 }
189
190 // allocate memory from the chunk for this new interned string's data
191 byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
192 MP_STATE_VM(qstr_last_used) += n_bytes;
193
194 // store the interned strings' data
Damien George40f3c022014-07-03 13:25:24 +0100195 mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000196 q_ptr[0] = hash;
197 q_ptr[1] = hash >> 8;
Damien George95836f82015-01-11 22:27:30 +0000198 Q_SET_LENGTH(q_ptr, len);
199 memcpy(q_ptr + 2 + MICROPY_QSTR_BYTES_IN_LEN, str, len);
200 q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
Damien George55baff42014-01-21 21:40:13 +0000201 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100202 }
Damien George55baff42014-01-21 21:40:13 +0000203 return q;
Damien429d7192013-10-04 19:53:11 +0100204}
205
Damien George39dc1452014-10-03 19:52:22 +0100206byte *qstr_build_start(mp_uint_t len, byte **q_ptr) {
Damien George95836f82015-01-11 22:27:30 +0000207 assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
208 *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
209 Q_SET_LENGTH(*q_ptr, len);
Damien George55baff42014-01-21 21:40:13 +0000210 return Q_GET_DATA(*q_ptr);
211}
212
213qstr qstr_build_end(byte *q_ptr) {
Damien George2617eeb2014-05-25 22:27:57 +0100214 qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
Damien George55baff42014-01-21 21:40:13 +0000215 if (q == 0) {
Damien George40f3c022014-07-03 13:25:24 +0100216 mp_uint_t len = Q_GET_LENGTH(q_ptr);
217 mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000218 q_ptr[0] = hash;
219 q_ptr[1] = hash >> 8;
Damien George95836f82015-01-11 22:27:30 +0000220 q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
Damien George55baff42014-01-21 21:40:13 +0000221 q = qstr_add(q_ptr);
222 } else {
223 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
224 }
225 return q;
226}
227
Damien George40f3c022014-07-03 13:25:24 +0100228mp_uint_t qstr_hash(qstr q) {
Damien George55baff42014-01-21 21:40:13 +0000229 return Q_GET_HASH(find_qstr(q));
230}
231
Damien George39dc1452014-10-03 19:52:22 +0100232mp_uint_t qstr_len(qstr q) {
Damien George55baff42014-01-21 21:40:13 +0000233 const byte *qd = find_qstr(q);
234 return Q_GET_LENGTH(qd);
235}
236
237// XXX to remove!
238const char *qstr_str(qstr q) {
239 const byte *qd = find_qstr(q);
240 return (const char*)Q_GET_DATA(qd);
241}
242
Damien George39dc1452014-10-03 19:52:22 +0100243const byte *qstr_data(qstr q, mp_uint_t *len) {
Damien George55baff42014-01-21 21:40:13 +0000244 const byte *qd = find_qstr(q);
245 *len = Q_GET_LENGTH(qd);
246 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100247}
Damien George4d5b28c2014-01-29 18:56:46 +0000248
Damien George39dc1452014-10-03 19:52:22 +0100249void 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 George4d5b28c2014-01-29 18:56:46 +0000250 *n_pool = 0;
251 *n_qstr = 0;
252 *n_str_data_bytes = 0;
253 *n_total_bytes = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000254 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) {
Damien George4d5b28c2014-01-29 18:56:46 +0000255 *n_pool += 1;
256 *n_qstr += pool->len;
257 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George0b13f3e2014-10-24 23:12:25 +0100258 #if MICROPY_ENABLE_GC
259 *n_str_data_bytes += gc_nbytes(*q); // this counts actual bytes used in heap
260 #else
Damien George4d5b28c2014-01-29 18:56:46 +0000261 *n_str_data_bytes += Q_GET_ALLOC(*q);
Damien George0b13f3e2014-10-24 23:12:25 +0100262 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000263 }
Damien George0b13f3e2014-10-24 23:12:25 +0100264 #if MICROPY_ENABLE_GC
265 *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
266 #else
Damien George4d5b28c2014-01-29 18:56:46 +0000267 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
Damien George0b13f3e2014-10-24 23:12:25 +0100268 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000269 }
270 *n_total_bytes += *n_str_data_bytes;
271}
Damien Georgeea0461d2015-02-10 11:02:28 +0000272
273#if MICROPY_PY_MICROPYTHON_MEM_INFO
274void qstr_dump_data(void) {
275 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) {
276 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien Georgee72cda92015-04-11 12:15:47 +0100277 mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q));
Damien Georgeea0461d2015-02-10 11:02:28 +0000278 }
279 }
280}
281#endif