Numworks Epsilon  1.4.1
Graphing Calculator Operating System
qstr.c
Go to the documentation of this file.
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 
27 #include <assert.h>
28 #include <string.h>
29 #include <stdio.h>
30 
31 #include "py/mpstate.h"
32 #include "py/qstr.h"
33 #include "py/gc.h"
34 
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 MICROPY_DEBUG_VERBOSE // print debugging info
40 #define DEBUG_printf DEBUG_printf
41 #else // don't print debugging info
42 #define DEBUG_printf(...) (void)0
43 #endif
44 
45 // A qstr is an index into the qstr pool.
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)
51 
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)
65 #if MICROPY_QSTR_BYTES_IN_LEN == 1
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)
68 #elif MICROPY_QSTR_BYTES_IN_LEN == 2
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)
71 #else
72  #error unimplemented qstr length decoding
73 #endif
74 
75 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
76 #define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
77 #define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex))
78 #else
79 #define QSTR_ENTER()
80 #define QSTR_EXIT()
81 #endif
82 
83 // this must match the equivalent function in makeqstrdata.py
84 mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
85  // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
86  mp_uint_t hash = 5381;
87  for (const byte *top = data + len; data < top; data++) {
88  hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
89  }
90  hash &= Q_HASH_MASK;
91  // Make sure that valid hash is never zero, zero means "hash not computed"
92  if (hash == 0) {
93  hash++;
94  }
95  return hash;
96 }
97 
99  NULL, // no previous pool
100  0, // no previous pool
101  10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
102  MP_QSTRnumber_of, // corresponds to number of strings in array just below
103  {
104 #ifndef NO_QSTR
105 #define QDEF(id, str) str,
106 #include "genhdr/qstrdefs.generated.h"
107 #undef QDEF
108 #endif
109  },
110 };
111 
112 #ifdef MICROPY_QSTR_EXTRA_POOL
113 extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL;
114 #define CONST_POOL MICROPY_QSTR_EXTRA_POOL
115 #else
116 #define CONST_POOL mp_qstr_const_pool
117 #endif
118 
119 void qstr_init(void) {
120  MP_STATE_VM(last_pool) = (qstr_pool_t*)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
121  MP_STATE_VM(qstr_last_chunk) = NULL;
122 
123  #if MICROPY_PY_THREAD
124  mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
125  #endif
126 }
127 
129  // search pool for this qstr
130  for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
131  if (q >= pool->total_prev_len) {
132  return pool->qstrs[q - pool->total_prev_len];
133  }
134  }
135 
136  // not found
137  return 0;
138 }
139 
140 // qstr_mutex must be taken while in this function
141 STATIC qstr qstr_add(const byte *q_ptr) {
142  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));
143 
144  // make sure we have room in the pool for a new qstr
145  if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
146  qstr_pool_t *pool = m_new_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
147  if (pool == NULL) {
148  QSTR_EXIT();
149  m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2);
150  }
151  pool->prev = MP_STATE_VM(last_pool);
152  pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
153  pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
154  pool->len = 0;
155  MP_STATE_VM(last_pool) = pool;
156  DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
157  }
158 
159  // add the new qstr
160  MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr;
161 
162  // return id for the newly-added qstr
163  return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1;
164 }
165 
166 qstr qstr_find_strn(const char *str, size_t str_len) {
167  // work out hash of str
168  mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
169 
170  // search pools for the data
171  for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
172  for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
173  if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
174  return pool->total_prev_len + (q - pool->qstrs);
175  }
176  }
177  }
178 
179  // not found; return null qstr
180  return 0;
181 }
182 
183 qstr qstr_from_str(const char *str) {
184  return qstr_from_strn(str, strlen(str));
185 }
186 
187 qstr qstr_from_strn(const char *str, size_t len) {
188  assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
189  QSTR_ENTER();
190  qstr q = qstr_find_strn(str, len);
191  if (q == 0) {
192  // qstr does not exist in interned pool so need to add it
193 
194  // compute number of bytes needed to intern this string
195  size_t n_bytes = MICROPY_QSTR_BYTES_IN_HASH + MICROPY_QSTR_BYTES_IN_LEN + len + 1;
196 
197  if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
198  // not enough room at end of previously interned string so try to grow
199  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);
200  if (new_p == NULL) {
201  // could not grow existing memory; shrink it to fit previous
202  (void)m_renew_maybe(byte, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
203  MP_STATE_VM(qstr_last_chunk) = NULL;
204  } else {
205  // could grow existing memory
206  MP_STATE_VM(qstr_last_alloc) += n_bytes;
207  }
208  }
209 
210  if (MP_STATE_VM(qstr_last_chunk) == NULL) {
211  // no existing memory for the interned string so allocate a new chunk
212  size_t al = n_bytes;
215  }
216  MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, al);
217  if (MP_STATE_VM(qstr_last_chunk) == NULL) {
218  // failed to allocate a large chunk so try with exact size
219  MP_STATE_VM(qstr_last_chunk) = m_new_maybe(byte, n_bytes);
220  if (MP_STATE_VM(qstr_last_chunk) == NULL) {
221  QSTR_EXIT();
222  m_malloc_fail(n_bytes);
223  }
224  al = n_bytes;
225  }
226  MP_STATE_VM(qstr_last_alloc) = al;
227  MP_STATE_VM(qstr_last_used) = 0;
228  }
229 
230  // allocate memory from the chunk for this new interned string's data
231  byte *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
232  MP_STATE_VM(qstr_last_used) += n_bytes;
233 
234  // store the interned strings' data
235  mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
236  Q_SET_HASH(q_ptr, hash);
237  Q_SET_LENGTH(q_ptr, len);
240  q = qstr_add(q_ptr);
241  }
242  QSTR_EXIT();
243  return q;
244 }
245 
246 byte *qstr_build_start(size_t len, byte **q_ptr) {
247  assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
249  Q_SET_LENGTH(*q_ptr, len);
250  return Q_GET_DATA(*q_ptr);
251 }
252 
254  QSTR_ENTER();
255  qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
256  if (q == 0) {
257  size_t len = Q_GET_LENGTH(q_ptr);
258  mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
259  Q_SET_HASH(q_ptr, hash);
261  q = qstr_add(q_ptr);
262  } else {
263  m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
264  }
265  QSTR_EXIT();
266  return q;
267 }
268 
270  return Q_GET_HASH(find_qstr(q));
271 }
272 
273 size_t qstr_len(qstr q) {
274  const byte *qd = find_qstr(q);
275  return Q_GET_LENGTH(qd);
276 }
277 
278 const char *qstr_str(qstr q) {
279  const byte *qd = find_qstr(q);
280  return (const char*)Q_GET_DATA(qd);
281 }
282 
283 const byte *qstr_data(qstr q, size_t *len) {
284  const byte *qd = find_qstr(q);
285  *len = Q_GET_LENGTH(qd);
286  return Q_GET_DATA(qd);
287 }
288 
289 void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
290  QSTR_ENTER();
291  *n_pool = 0;
292  *n_qstr = 0;
293  *n_str_data_bytes = 0;
294  *n_total_bytes = 0;
295  for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
296  *n_pool += 1;
297  *n_qstr += pool->len;
298  for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
299  *n_str_data_bytes += Q_GET_ALLOC(*q);
300  }
301  #if MICROPY_ENABLE_GC
302  *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
303  #else
304  *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
305  #endif
306  }
307  *n_total_bytes += *n_str_data_bytes;
308  QSTR_EXIT();
309 }
310 
311 #if MICROPY_PY_MICROPYTHON_MEM_INFO
312 void qstr_dump_data(void) {
313  QSTR_ENTER();
314  for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
315  for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
316  mp_printf(&mp_plat_print, "Q(%s)\n", Q_GET_DATA(*q));
317  }
318  }
319  QSTR_EXIT();
320 }
321 #endif
const mp_print_t mp_plat_print
Definition: mpprint.c:51
#define m_renew_maybe(type, ptr, old_num, new_num, allow_move)
Definition: misc.h:76
struct _qstr_pool_t * prev
Definition: qstr.h:51
qstr qstr_from_strn(const char *str, size_t len)
Definition: qstr.c:187
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
STATIC qstr qstr_add(const byte *q_ptr)
Definition: qstr.c:141
const char * qstr_str(qstr q)
Definition: qstr.c:278
#define QSTR_ENTER()
Definition: qstr.c:79
#define assert(e)
Definition: assert.h:9
def data
Definition: i18n.py:176
#define m_del(type, ptr, num)
Definition: misc.h:77
#define Q_GET_DATA(q)
Definition: qstr.c:64
STATIC const byte * find_qstr(qstr q)
Definition: qstr.c:128
qstr qstr_from_str(const char *str)
Definition: qstr.c:183
#define MP_STATE_VM(x)
Definition: mpstate.h:241
void qstr_init(void)
Definition: qstr.c:119
#define STATIC
Definition: mpconfig.h:1178
qstr qstr_find_strn(const char *str, size_t str_len)
Definition: qstr.c:166
struct _qstr_pool_t qstr_pool_t
#define Q_GET_ALLOC(q)
Definition: qstr.c:63
mp_uint_t qstr_hash(qstr q)
Definition: qstr.c:269
#define MICROPY_ALLOC_QSTR_CHUNK_INIT
Definition: mpconfig.h:130
size_t len
Definition: qstr.h:54
size_t strlen(const char *s)
Definition: strlen.c:3
#define m_new_maybe(type, num)
Definition: misc.h:58
#define NULL
Definition: stddef.h:4
#define MICROPY_QSTR_BYTES_IN_LEN
Definition: mpconfig.h:205
size_t alloc
Definition: qstr.h:53
size_t qstr
Definition: qstr.h:48
const qstr_pool_t mp_qstr_const_pool
Definition: qstr.c:98
size_t total_prev_len
Definition: qstr.h:52
unsigned char byte
Definition: misc.h:37
#define MICROPY_QSTR_BYTES_IN_HASH
Definition: mpconfigport.h:11
#define DEBUG_printf(...)
Definition: qstr.c:42
const byte * qstr_data(qstr q, size_t *len)
Definition: qstr.c:283
NORETURN void m_malloc_fail(size_t num_bytes)
Definition: runtime.c:1437
mp_uint_t qstr_compute_hash(const byte *data, size_t len)
Definition: qstr.c:84
byte * qstr_build_start(size_t len, byte **q_ptr)
Definition: qstr.c:246
qstr qstr_build_end(byte *q_ptr)
Definition: qstr.c:253
void qstr_dump_data(void)
#define CONST_POOL
Definition: qstr.c:116
LIBA_BEGIN_DECLS int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
#define QSTR_EXIT()
Definition: qstr.c:80
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
size_t qstr_len(qstr q)
Definition: qstr.c:273
size_t gc_nbytes(const void *ptr)
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3
#define m_new_obj_var_maybe(obj_type, var_type, var_num)
Definition: misc.h:63
#define m_new(type, num)
Definition: misc.h:57
void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes)
Definition: qstr.c:289