42
42
// A qstr is an index into the qstr pool.
43
43
// The data for a qstr is \0 terminated (so they can be printed using printf)
44
44
45
+ #if MICROPY_QSTR_BYTES_IN_HASH
45
46
#define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1)
47
+ #else
48
+ #define Q_HASH_MASK (0xffff)
49
+ #endif
46
50
47
51
#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
48
52
#define QSTR_ENTER () mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
@@ -77,6 +81,7 @@ size_t qstr_compute_hash(const byte *data, size_t len) {
77
81
// future .mpy version we could re-order them and make it sorted). It also
78
82
// contains additional qstrs that must have IDs <256, see operator_qstr_list
79
83
// in makeqstrdata.py.
84
+ #if MICROPY_QSTR_BYTES_IN_HASH
80
85
const qstr_hash_t mp_qstr_const_hashes_static [] = {
81
86
#ifndef NO_QSTR
82
87
#define QDEF0 (id , hash , len , str ) hash ,
@@ -86,6 +91,7 @@ const qstr_hash_t mp_qstr_const_hashes_static[] = {
86
91
#undef QDEF1
87
92
#endif
88
93
};
94
+ #endif
89
95
90
96
const qstr_len_t mp_qstr_const_lengths_static [] = {
91
97
#ifndef NO_QSTR
@@ -103,7 +109,9 @@ const qstr_pool_t mp_qstr_const_pool_static = {
103
109
false, // is_sorted
104
110
MICROPY_ALLOC_QSTR_ENTRIES_INIT ,
105
111
MP_QSTRnumber_of_static , // corresponds to number of strings in array just below
112
+ #if MICROPY_QSTR_BYTES_IN_HASH
106
113
(qstr_hash_t * )mp_qstr_const_hashes_static ,
114
+ #endif
107
115
(qstr_len_t * )mp_qstr_const_lengths_static ,
108
116
{
109
117
#ifndef NO_QSTR
@@ -118,6 +126,7 @@ const qstr_pool_t mp_qstr_const_pool_static = {
118
126
119
127
// The next pool is the remainder of the qstrs defined in the firmware. This
120
128
// is sorted.
129
+ #if MICROPY_QSTR_BYTES_IN_HASH
121
130
const qstr_hash_t mp_qstr_const_hashes [ ] = {
122
131
#ifndef NO_QSTR
123
132
#define QDEF0 (id , hash , len , str )
@@ -127,6 +136,7 @@ const qstr_hash_t mp_qstr_const_hashes[] = {
127
136
#undef QDEF1
128
137
#endif
129
138
};
139
+ #endif
130
140
131
141
const qstr_len_t mp_qstr_const_lengths [] = {
132
142
#ifndef NO_QSTR
@@ -144,7 +154,9 @@ const qstr_pool_t mp_qstr_const_pool = {
144
154
true, // is_sorted
145
155
MICROPY_ALLOC_QSTR_ENTRIES_INIT ,
146
156
MP_QSTRnumber_of - MP_QSTRnumber_of_static , // corresponds to number of strings in array just below
157
+ #if MICROPY_QSTR_BYTES_IN_HASH
147
158
(qstr_hash_t * )mp_qstr_const_hashes ,
159
+ #endif
148
160
(qstr_len_t * )mp_qstr_const_lengths ,
149
161
{
150
162
#ifndef NO_QSTR
@@ -188,8 +200,13 @@ STATIC const qstr_pool_t *find_qstr(qstr *q) {
188
200
}
189
201
190
202
// qstr_mutex must be taken while in this function
191
- STATIC qstr qstr_add (mp_uint_t hash , mp_uint_t len , const char * q_ptr ) {
203
+ STATIC qstr qstr_add (mp_uint_t len , const char * q_ptr ) {
204
+ #if MICROPY_QSTR_BYTES_IN_HASH
205
+ mp_uint_t hash = qstr_compute_hash ((const byte * )q_ptr , len );
192
206
DEBUG_printf ("QSTR: add hash=%d len=%d data=%.*s\n" , hash , len , len , q_ptr );
207
+ #else
208
+ DEBUG_printf ("QSTR: add len=%d data=%.*s\n" , len , len , q_ptr );
209
+ #endif
193
210
194
211
// make sure we have room in the pool for a new qstr
195
212
if (MP_STATE_VM (last_pool )-> len >= MP_STATE_VM (last_pool )-> alloc ) {
@@ -199,7 +216,11 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
199
216
new_alloc = MAX (MICROPY_ALLOC_QSTR_ENTRIES_INIT , new_alloc );
200
217
#endif
201
218
mp_uint_t pool_size = sizeof (qstr_pool_t )
202
- + (sizeof (const char * ) + sizeof (qstr_hash_t ) + sizeof (qstr_len_t )) * new_alloc ;
219
+ + (sizeof (const char * )
220
+ #if MICROPY_QSTR_BYTES_IN_HASH
221
+ + sizeof (qstr_hash_t )
222
+ #endif
223
+ + sizeof (qstr_len_t )) * new_alloc ;
203
224
qstr_pool_t * pool = (qstr_pool_t * )m_malloc_maybe (pool_size );
204
225
if (pool == NULL ) {
205
226
// Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned
@@ -211,8 +232,12 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
211
232
QSTR_EXIT ();
212
233
m_malloc_fail (new_alloc );
213
234
}
235
+ #if MICROPY_QSTR_BYTES_IN_HASH
214
236
pool -> hashes = (qstr_hash_t * )(pool -> qstrs + new_alloc );
215
237
pool -> lengths = (qstr_len_t * )(pool -> hashes + new_alloc );
238
+ #else
239
+ pool -> lengths = (qstr_len_t * )(pool -> qstrs + new_alloc );
240
+ #endif
216
241
pool -> prev = MP_STATE_VM (last_pool );
217
242
pool -> total_prev_len = MP_STATE_VM (last_pool )-> total_prev_len + MP_STATE_VM (last_pool )-> len ;
218
243
pool -> alloc = new_alloc ;
@@ -223,7 +248,9 @@ STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
223
248
224
249
// add the new qstr
225
250
mp_uint_t at = MP_STATE_VM (last_pool )-> len ;
251
+ #if MICROPY_QSTR_BYTES_IN_HASH
226
252
MP_STATE_VM (last_pool )-> hashes [at ] = hash ;
253
+ #endif
227
254
MP_STATE_VM (last_pool )-> lengths [at ] = len ;
228
255
MP_STATE_VM (last_pool )-> qstrs [at ] = q_ptr ;
229
256
MP_STATE_VM (last_pool )-> len ++ ;
@@ -238,8 +265,10 @@ qstr qstr_find_strn(const char *str, size_t str_len) {
238
265
return MP_QSTR_ ;
239
266
}
240
267
268
+ #if MICROPY_QSTR_BYTES_IN_HASH
241
269
// work out hash of str
242
270
size_t str_hash = qstr_compute_hash ((const byte * )str , str_len );
271
+ #endif
243
272
244
273
// search pools for the data
245
274
for (const qstr_pool_t * pool = MP_STATE_VM (last_pool ); pool != NULL ; pool = pool -> prev ) {
@@ -261,7 +290,11 @@ qstr qstr_find_strn(const char *str, size_t str_len) {
261
290
262
291
// sequential search for the remaining strings
263
292
for (mp_uint_t at = low ; at < high + 1 ; at ++ ) {
264
- if (pool -> hashes [at ] == str_hash && pool -> lengths [at ] == str_len
293
+ if (
294
+ #if MICROPY_QSTR_BYTES_IN_HASH
295
+ pool -> hashes [at ] == str_hash &&
296
+ #endif
297
+ pool -> lengths [at ] == str_len
265
298
&& memcmp (pool -> qstrs [at ], str , str_len ) == 0 ) {
266
299
return pool -> total_prev_len + at ;
267
300
}
@@ -329,18 +362,21 @@ qstr qstr_from_strn(const char *str, size_t len) {
329
362
MP_STATE_VM (qstr_last_used ) += n_bytes ;
330
363
331
364
// store the interned strings' data
332
- size_t hash = qstr_compute_hash ((const byte * )str , len );
333
365
memcpy (q_ptr , str , len );
334
366
q_ptr [len ] = '\0' ;
335
- q = qstr_add (hash , len , q_ptr );
367
+ q = qstr_add (len , q_ptr );
336
368
}
337
369
QSTR_EXIT ();
338
370
return q ;
339
371
}
340
372
341
373
mp_uint_t qstr_hash (qstr q ) {
342
374
const qstr_pool_t * pool = find_qstr (& q );
375
+ #if MICROPY_QSTR_BYTES_IN_HASH
343
376
return pool -> hashes [q ];
377
+ #else
378
+ return qstr_compute_hash ((byte * )pool -> qstrs [q ], pool -> lengths [q ]);
379
+ #endif
344
380
}
345
381
346
382
size_t qstr_len (qstr q ) {
@@ -375,7 +411,11 @@ void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, si
375
411
* n_total_bytes += gc_nbytes (pool ); // this counts actual bytes used in heap
376
412
#else
377
413
* n_total_bytes += sizeof (qstr_pool_t )
378
- + (sizeof (const char * ) + sizeof (qstr_hash_t ) + sizeof (qstr_len_t )) * pool -> alloc ;
414
+ + (sizeof (const char * )
415
+ #if MICROPY_QSTR_BYTES_IN_HASH
416
+ + sizeof (qstr_hash_t )
417
+ #endif
418
+ + sizeof (qstr_len_t )) * pool -> alloc ;
379
419
#endif
380
420
}
381
421
* n_total_bytes += * n_str_data_bytes ;
0 commit comments