blob: 990ff37727e39aaa7923c7d137674d8000aa7e6b [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <assert.h>
2#include <string.h>
3
4#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +00005#include "mpconfig.h"
6#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +01007
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00008// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
9// ultimately we will replace this with a static hash table of some kind
10// also probably need to include the length in the string data, to allow null bytes in the string
11
12#if 0 // print debugging info
Paul Sokolovsky44739e22014-02-16 18:11:42 +020013#define DEBUG_printf DEBUG_printf
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000014#else // don't print debugging info
Damien George1dc76af2014-02-26 16:57:08 +000015#define DEBUG_printf(...) (void)0
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000016#endif
17
Damien George55baff42014-01-21 21:40:13 +000018// A qstr is an index into the qstr pool.
19// The data for a qstr contains (hash, length, data).
20// For now we use very simple encoding, just to get the framework correct:
Damien George6e628c42014-03-25 15:27:15 +000021// - hash is 2 bytes (see function below)
Damien George55baff42014-01-21 21:40:13 +000022// - length is 2 bytes
23// - data follows
24// - \0 terminated (for now, so they can be printed using printf)
25
26#define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8))
27#define Q_GET_ALLOC(q) (4 + Q_GET_LENGTH(q) + 1)
28#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
29#define Q_GET_DATA(q) ((q) + 4)
30
Damien George6e628c42014-03-25 15:27:15 +000031// this must match the equivalent function in makeqstrdata.py
Damien George5fa93b62014-01-22 14:35:10 +000032machine_uint_t qstr_compute_hash(const byte *data, uint len) {
Damien George6e628c42014-03-25 15:27:15 +000033 // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
34 machine_uint_t hash = 5381;
Damien George55baff42014-01-21 21:40:13 +000035 for (const byte *top = data + len; data < top; data++) {
Damien George6e628c42014-03-25 15:27:15 +000036 hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
Damien George55baff42014-01-21 21:40:13 +000037 }
Paul Sokolovsky59e269c2014-04-14 01:43:01 +030038 hash &= 0xffff;
39 // Make sure that valid hash is never zero, zero means "hash not computed"
40 if (hash == 0) {
41 hash++;
42 }
43 return hash;
Damien George55baff42014-01-21 21:40:13 +000044}
45
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000046typedef struct _qstr_pool_t {
47 struct _qstr_pool_t *prev;
48 uint total_prev_len;
49 uint alloc;
50 uint len;
Damien George55baff42014-01-21 21:40:13 +000051 const byte *qstrs[];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000052} qstr_pool_t;
53
54const static qstr_pool_t const_pool = {
55 NULL, // no previous pool
56 0, // no previous pool
57 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
58 MP_QSTR_number_of, // corresponds to number of strings in array just below
59 {
Damien George55baff42014-01-21 21:40:13 +000060 (const byte*) "\0\0\0\0", // invalid/no qstr has empty data
61 (const byte*) "\0\0\0\0", // empty qstr
62#define Q(id, str) str,
63// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
Dave Hylandsc89c6812014-01-24 01:05:30 -080064#include "build/py/qstrdefs.generated.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000065#undef Q
66 },
67};
68
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020069STATIC qstr_pool_t *last_pool;
Damien429d7192013-10-04 19:53:11 +010070
Damien8b3a7c22013-10-23 20:20:17 +010071void qstr_init(void) {
Dave Hylandsd986b582014-01-05 11:55:23 -080072 last_pool = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left
Damien429d7192013-10-04 19:53:11 +010073}
74
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020075STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000076 // search pool for this qstr
77 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
78 if (q >= pool->total_prev_len) {
79 return pool->qstrs[q - pool->total_prev_len];
80 }
81 }
82
83 // not found
84 return 0;
85}
86
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020087STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +000088 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 +000089
90 // make sure we have room in the pool for a new qstr
91 if (last_pool->len >= last_pool->alloc) {
92 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2);
93 pool->prev = last_pool;
94 pool->total_prev_len = last_pool->total_prev_len + last_pool->len;
95 pool->alloc = last_pool->alloc * 2;
96 pool->len = 0;
97 last_pool = pool;
98 DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc);
Damien429d7192013-10-04 19:53:11 +010099 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000100
101 // add the new qstr
Damien George55baff42014-01-21 21:40:13 +0000102 last_pool->qstrs[last_pool->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000103
104 // return id for the newly-added qstr
105 return last_pool->total_prev_len + last_pool->len - 1;
Damien429d7192013-10-04 19:53:11 +0100106}
107
Damien George5fa93b62014-01-22 14:35:10 +0000108qstr qstr_find_strn(const byte *str, uint str_len) {
Damien George55baff42014-01-21 21:40:13 +0000109 // work out hash of str
Damien George5fa93b62014-01-22 14:35:10 +0000110 machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000111
112 // search pools for the data
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000113 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000114 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000115 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 +0000116 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000117 }
Damien429d7192013-10-04 19:53:11 +0100118 }
119 }
Damien George55baff42014-01-21 21:40:13 +0000120
121 // not found; return null qstr
122 return 0;
Damien429d7192013-10-04 19:53:11 +0100123}
124
Damien George55baff42014-01-21 21:40:13 +0000125qstr qstr_from_str(const char *str) {
126 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100127}
128
Damien George55baff42014-01-21 21:40:13 +0000129qstr qstr_from_strn(const char *str, uint len) {
130 qstr q = qstr_find_strn((const byte*)str, len);
131 if (q == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000132 machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000133 byte *q_ptr = m_new(byte, 4 + len + 1);
134 q_ptr[0] = hash;
135 q_ptr[1] = hash >> 8;
136 q_ptr[2] = len;
137 q_ptr[3] = len >> 8;
138 memcpy(q_ptr + 4, str, len);
139 q_ptr[4 + len] = '\0';
140 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100141 }
Damien George55baff42014-01-21 21:40:13 +0000142 return q;
Damien429d7192013-10-04 19:53:11 +0100143}
144
Damien George55baff42014-01-21 21:40:13 +0000145qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
146 qstr q = qstr_from_strn(str, len);
147 m_del(char, str, alloc_len);
148 return q;
149}
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000150
Damien George55baff42014-01-21 21:40:13 +0000151byte *qstr_build_start(uint len, byte **q_ptr) {
152 assert(len <= 65535);
153 *q_ptr = m_new(byte, 4 + len + 1);
154 (*q_ptr)[2] = len;
155 (*q_ptr)[3] = len >> 8;
156 return Q_GET_DATA(*q_ptr);
157}
158
159qstr qstr_build_end(byte *q_ptr) {
160 qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
161 if (q == 0) {
162 machine_uint_t len = Q_GET_LENGTH(q_ptr);
Damien George5fa93b62014-01-22 14:35:10 +0000163 machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000164 q_ptr[0] = hash;
165 q_ptr[1] = hash >> 8;
166 q_ptr[4 + len] = '\0';
167 q = qstr_add(q_ptr);
168 } else {
169 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
170 }
171 return q;
172}
173
174machine_uint_t qstr_hash(qstr q) {
175 return Q_GET_HASH(find_qstr(q));
176}
177
178uint qstr_len(qstr q) {
179 const byte *qd = find_qstr(q);
180 return Q_GET_LENGTH(qd);
181}
182
183// XXX to remove!
184const char *qstr_str(qstr q) {
185 const byte *qd = find_qstr(q);
186 return (const char*)Q_GET_DATA(qd);
187}
188
189const byte *qstr_data(qstr q, uint *len) {
190 const byte *qd = find_qstr(q);
191 *len = Q_GET_LENGTH(qd);
192 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100193}
Damien George4d5b28c2014-01-29 18:56:46 +0000194
195void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) {
196 *n_pool = 0;
197 *n_qstr = 0;
198 *n_str_data_bytes = 0;
199 *n_total_bytes = 0;
200 for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) {
201 *n_pool += 1;
202 *n_qstr += pool->len;
203 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
204 *n_str_data_bytes += Q_GET_ALLOC(*q);
205 }
206 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
207 }
208 *n_total_bytes += *n_str_data_bytes;
209}