qstr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * This file is part of the MicroPython 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. #include <assert.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include "py/mpstate.h"
  30. #include "py/qstr.h"
  31. #include "py/gc.h"
  32. // NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
  33. // ultimately we will replace this with a static hash table of some kind
  34. // also probably need to include the length in the string data, to allow null bytes in the string
  35. #if MICROPY_DEBUG_VERBOSE // print debugging info
  36. #define DEBUG_printf DEBUG_printf
  37. #else // don't print debugging info
  38. #define DEBUG_printf(...) (void)0
  39. #endif
  40. // A qstr is an index into the qstr pool.
  41. // The data for a qstr contains (hash, length, data):
  42. // - hash (configurable number of bytes)
  43. // - length (configurable number of bytes)
  44. // - data ("length" number of bytes)
  45. // - \0 terminated (so they can be printed using printf)
  46. #if MICROPY_QSTR_BYTES_IN_HASH == 1
  47. #define Q_HASH_MASK (0xff)
  48. #define Q_GET_HASH(q) ((mp_uint_t)(q)[0])
  49. #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); } while (0)
  50. #elif MICROPY_QSTR_BYTES_IN_HASH == 2
  51. #define Q_HASH_MASK (0xffff)
  52. #define Q_GET_HASH(q) ((mp_uint_t)(q)[0] | ((mp_uint_t)(q)[1] << 8))
  53. #define Q_SET_HASH(q, hash) do { (q)[0] = (hash); (q)[1] = (hash) >> 8; } while (0)
  54. #else
  55. #error unimplemented qstr hash decoding
  56. #endif
  57. #define Q_GET_ALLOC(q) (MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
  58. #define Q_GET_DATA(q) ((q) + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN)
  59. #if MICROPY_QSTR_BYTES_IN_LEN == 1
  60. #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH])
  61. #define Q_SET_LENGTH(q, len) do { (q)[MICROPY_QSTR_BYTES_IN_HASH] = (len); } while (0)
  62. #elif MICROPY_QSTR_BYTES_IN_LEN == 2
  63. #define Q_GET_LENGTH(q) ((q)[MICROPY_QSTR_BYTES_IN_HASH] | ((q)[MICROPY_QSTR_BYTES_IN_HASH + 1] << 8))
  64. #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)
  65. #else
  66. #error unimplemented qstr length decoding
  67. #endif
  68. #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
  69. #define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
  70. #define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex))
  71. #else
  72. #define QSTR_ENTER()
  73. #define QSTR_EXIT()
  74. #endif
  75. // this must match the equivalent function in makeqstrdata.py
  76. mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
  77. // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
  78. mp_uint_t hash = 5381;
  79. for (const byte *top = data + len; data < top; data++) {
  80. hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
  81. }
  82. hash &= Q_HASH_MASK;
  83. // Make sure that valid hash is never zero, zero means "hash not computed"
  84. if (hash == 0) {
  85. hash++;
  86. }
  87. return hash;
  88. }
  89. const qstr_pool_t mp_qstr_const_pool = {
  90. NULL, // no previous pool
  91. 0, // no previous pool
  92. 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
  93. MP_QSTRnumber_of, // corresponds to number of strings in array just below
  94. {
  95. #ifndef NO_QSTR
  96. #define QDEF(id, str) str,
  97. #include "genhdr/qstrdefs.generated.h"
  98. #undef QDEF
  99. #endif
  100. },
  101. };
  102. #ifdef MICROPY_QSTR_EXTRA_POOL
  103. extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL;
  104. #define CONST_POOL MICROPY_QSTR_EXTRA_POOL
  105. #else
  106. #define CONST_POOL mp_qstr_const_pool
  107. #endif
  108. void qstr_init(void) {
  109. MP_STATE_VM(last_pool) = (qstr_pool_t*)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
  110. MP_STATE_VM(qstr_last_chunk) = NULL;
  111. #if MICROPY_PY_THREAD
  112. mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
  113. #endif
  114. }
  115. STATIC const byte *find_qstr(qstr q) {
  116. // search pool for this qstr
  117. for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
  118. if (q >= pool->total_prev_len) {
  119. return pool->qstrs[q - pool->total_prev_len];
  120. }
  121. }
  122. // not found
  123. return 0;
  124. }
  125. // qstr_mutex must be taken while in this function
  126. STATIC qstr qstr_add(const byte *q_ptr) {
  127. 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));
  128. // make sure we have room in the pool for a new qstr
  129. if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
  130. qstr_pool_t *pool = m_new_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
  131. if (pool == NULL) {
  132. QSTR_EXIT();
  133. m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2);
  134. }
  135. pool->prev = MP_STATE_VM(last_pool);
  136. pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
  137. pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
  138. pool->len = 0;
  139. MP_STATE_VM(last_pool) = pool;
  140. DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
  141. }
  142. // add the new qstr
  143. MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr;
  144. // return id for the newly-added qstr
  145. return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1;
  146. }
  147. qstr qstr_find_strn(const char *str, size_t str_len) {
  148. // work out hash of str
  149. mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
  150. // search pools for the data
  151. for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
  152. for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
  153. if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
  154. return pool->total_prev_len + (q - pool->qstrs);
  155. }
  156. }
  157. }
  158. // not found; return null qstr
  159. return 0;
  160. }
  161. qstr qstr_from_str(const char *str) {
  162. return qstr_from_strn(str, strlen(str));
  163. }
  164. qstr qstr_from_strn(const char *str, size_t len) {
  165. assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
  166. QSTR_ENTER();
  167. qstr q = qstr_find_strn(str, len);
  168. if (q == 0) {
  169. // qstr does not exist in interned pool so need to add it
  170. // compute number of bytes needed to intern this string
  171. size_t n_bytes = MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1;
  172. if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
  173. // not enough room at end of previously interned string so try to grow
  174. 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);
  175. if (new_p == NULL) {
  176. // could not grow existing memory; shrink it to fit previous
  177. (void)m_renew_maybe(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
  178. MP_STATE_VM(qstr_last_chunk) = NULL;
  179. } else {
  180. // could grow existing memory
  181. MP_STATE_VM(qstr_last_alloc) += n_bytes;
  182. }
  183. }
  184. if (MP_STATE_VM(qstr_last_chunk) == NULL) {
  185. // no existing memory for the interned string so allocate a new chunk
  186. size_t al = n_bytes;
  187. if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
  188. al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
  189. }
  190. MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, al);
  191. if (MP_STATE_VM(qstr_last_chunk) == NULL) {
  192. // failed to allocate a large chunk so try with exact size
  193. MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, n_bytes);
  194. if (MP_STATE_VM(qstr_last_chunk) == NULL) {
  195. QSTR_EXIT();
  196. m_malloc_fail(n_bytes);
  197. }
  198. al = n_bytes;
  199. }
  200. MP_STATE_VM(qstr_last_alloc) = al;
  201. MP_STATE_VM(qstr_last_used) = 0;
  202. }
  203. // allocate memory from the chunk for this new interned string's data
  204. byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
  205. MP_STATE_VM(qstr_last_used) += n_bytes;
  206. // store the interned strings' data
  207. mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
  208. Q_SET_HASH(q_ptr, hash);
  209. Q_SET_LENGTH(q_ptr, len);
  210. memcpy(q_ptr + MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN, str, len);
  211. q_ptr[MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
  212. q = qstr_add(q_ptr);
  213. }
  214. QSTR_EXIT();
  215. return q;
  216. }
  217. mp_uint_t qstr_hash(qstr q) {
  218. return Q_GET_HASH(find_qstr(q));
  219. }
  220. size_t qstr_len(qstr q) {
  221. const byte *qd = find_qstr(q);
  222. return Q_GET_LENGTH(qd);
  223. }
  224. const char *qstr_str(qstr q) {
  225. const byte *qd = find_qstr(q);
  226. return (const char*)Q_GET_DATA(qd);
  227. }
  228. const byte *qstr_data(qstr q, size_t *len) {
  229. const byte *qd = find_qstr(q);
  230. *len = Q_GET_LENGTH(qd);
  231. return Q_GET_DATA(qd);
  232. }
  233. void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
  234. QSTR_ENTER();
  235. *n_pool = 0;
  236. *n_qstr = 0;
  237. *n_str_data_bytes = 0;
  238. *n_total_bytes = 0;
  239. for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
  240. *n_pool += 1;
  241. *n_qstr += pool->len;
  242. for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
  243. *n_str_data_bytes += Q_GET_ALLOC(*q);
  244. }
  245. #if MICROPY_ENABLE_GC
  246. *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
  247. #else
  248. *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
  249. #endif
  250. }
  251. *n_total_bytes += *n_str_data_bytes;
  252. QSTR_EXIT();
  253. }
  254. #if MICROPY_PY_MICROPYTHON_MEM_INFO
  255. void qstr_dump_data(void) {
  256. QSTR_ENTER();
  257. for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
  258. for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
  259. mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q));
  260. }
  261. }
  262. QSTR_EXIT();
  263. }
  264. #endif