blob: f9f63da3914094aeaa8a1fe26f2224ea2448fe72 [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,
Damien Georged553be52014-04-17 18:03:27 +010063#include "genhdr/qstrdefs.generated.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000064#undef Q
65 },
66};
67
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020068STATIC qstr_pool_t *last_pool;
Damien429d7192013-10-04 19:53:11 +010069
Damien8b3a7c22013-10-23 20:20:17 +010070void qstr_init(void) {
Dave Hylandsd986b582014-01-05 11:55:23 -080071 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 +010072}
73
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020074STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000075 // search pool for this qstr
76 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
77 if (q >= pool->total_prev_len) {
78 return pool->qstrs[q - pool->total_prev_len];
79 }
80 }
81
82 // not found
83 return 0;
84}
85
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020086STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +000087 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 +000088
89 // make sure we have room in the pool for a new qstr
90 if (last_pool->len >= last_pool->alloc) {
91 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2);
92 pool->prev = last_pool;
93 pool->total_prev_len = last_pool->total_prev_len + last_pool->len;
94 pool->alloc = last_pool->alloc * 2;
95 pool->len = 0;
96 last_pool = pool;
97 DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc);
Damien429d7192013-10-04 19:53:11 +010098 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000099
100 // add the new qstr
Damien George55baff42014-01-21 21:40:13 +0000101 last_pool->qstrs[last_pool->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000102
103 // return id for the newly-added qstr
104 return last_pool->total_prev_len + last_pool->len - 1;
Damien429d7192013-10-04 19:53:11 +0100105}
106
Damien George5fa93b62014-01-22 14:35:10 +0000107qstr qstr_find_strn(const byte *str, uint str_len) {
Damien George55baff42014-01-21 21:40:13 +0000108 // work out hash of str
Damien George5fa93b62014-01-22 14:35:10 +0000109 machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000110
111 // search pools for the data
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000112 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000113 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000114 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 +0000115 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000116 }
Damien429d7192013-10-04 19:53:11 +0100117 }
118 }
Damien George55baff42014-01-21 21:40:13 +0000119
120 // not found; return null qstr
121 return 0;
Damien429d7192013-10-04 19:53:11 +0100122}
123
Damien George55baff42014-01-21 21:40:13 +0000124qstr qstr_from_str(const char *str) {
125 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100126}
127
Damien George55baff42014-01-21 21:40:13 +0000128qstr qstr_from_strn(const char *str, uint len) {
129 qstr q = qstr_find_strn((const byte*)str, len);
130 if (q == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000131 machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000132 byte *q_ptr = m_new(byte, 4 + len + 1);
133 q_ptr[0] = hash;
134 q_ptr[1] = hash >> 8;
135 q_ptr[2] = len;
136 q_ptr[3] = len >> 8;
137 memcpy(q_ptr + 4, str, len);
138 q_ptr[4 + len] = '\0';
139 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100140 }
Damien George55baff42014-01-21 21:40:13 +0000141 return q;
Damien429d7192013-10-04 19:53:11 +0100142}
143
Damien George55baff42014-01-21 21:40:13 +0000144qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
145 qstr q = qstr_from_strn(str, len);
146 m_del(char, str, alloc_len);
147 return q;
148}
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000149
Damien George55baff42014-01-21 21:40:13 +0000150byte *qstr_build_start(uint len, byte **q_ptr) {
151 assert(len <= 65535);
152 *q_ptr = m_new(byte, 4 + len + 1);
153 (*q_ptr)[2] = len;
154 (*q_ptr)[3] = len >> 8;
155 return Q_GET_DATA(*q_ptr);
156}
157
158qstr qstr_build_end(byte *q_ptr) {
159 qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
160 if (q == 0) {
161 machine_uint_t len = Q_GET_LENGTH(q_ptr);
Damien George5fa93b62014-01-22 14:35:10 +0000162 machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000163 q_ptr[0] = hash;
164 q_ptr[1] = hash >> 8;
165 q_ptr[4 + len] = '\0';
166 q = qstr_add(q_ptr);
167 } else {
168 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
169 }
170 return q;
171}
172
173machine_uint_t qstr_hash(qstr q) {
174 return Q_GET_HASH(find_qstr(q));
175}
176
177uint qstr_len(qstr q) {
178 const byte *qd = find_qstr(q);
179 return Q_GET_LENGTH(qd);
180}
181
182// XXX to remove!
183const char *qstr_str(qstr q) {
184 const byte *qd = find_qstr(q);
185 return (const char*)Q_GET_DATA(qd);
186}
187
188const byte *qstr_data(qstr q, uint *len) {
189 const byte *qd = find_qstr(q);
190 *len = Q_GET_LENGTH(qd);
191 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100192}
Damien George4d5b28c2014-01-29 18:56:46 +0000193
194void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) {
195 *n_pool = 0;
196 *n_qstr = 0;
197 *n_str_data_bytes = 0;
198 *n_total_bytes = 0;
199 for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) {
200 *n_pool += 1;
201 *n_qstr += pool->len;
202 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
203 *n_str_data_bytes += Q_GET_ALLOC(*q);
204 }
205 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
206 }
207 *n_total_bytes += *n_str_data_bytes;
208}