blob: e4b5c111b5fb4a09cc7767b48ec71419cba5001c [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 }
38 return hash & 0xffff;
39}
40
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000041typedef struct _qstr_pool_t {
42 struct _qstr_pool_t *prev;
43 uint total_prev_len;
44 uint alloc;
45 uint len;
Damien George55baff42014-01-21 21:40:13 +000046 const byte *qstrs[];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000047} qstr_pool_t;
48
49const static qstr_pool_t const_pool = {
50 NULL, // no previous pool
51 0, // no previous pool
52 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
53 MP_QSTR_number_of, // corresponds to number of strings in array just below
54 {
Damien George55baff42014-01-21 21:40:13 +000055 (const byte*) "\0\0\0\0", // invalid/no qstr has empty data
56 (const byte*) "\0\0\0\0", // empty qstr
57#define Q(id, str) str,
58// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
Dave Hylandsc89c6812014-01-24 01:05:30 -080059#include "build/py/qstrdefs.generated.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000060#undef Q
61 },
62};
63
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020064STATIC qstr_pool_t *last_pool;
Damien429d7192013-10-04 19:53:11 +010065
Damien8b3a7c22013-10-23 20:20:17 +010066void qstr_init(void) {
Dave Hylandsd986b582014-01-05 11:55:23 -080067 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 +010068}
69
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020070STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000071 // search pool for this qstr
72 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
73 if (q >= pool->total_prev_len) {
74 return pool->qstrs[q - pool->total_prev_len];
75 }
76 }
77
78 // not found
79 return 0;
80}
81
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020082STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +000083 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 +000084
85 // make sure we have room in the pool for a new qstr
86 if (last_pool->len >= last_pool->alloc) {
87 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2);
88 pool->prev = last_pool;
89 pool->total_prev_len = last_pool->total_prev_len + last_pool->len;
90 pool->alloc = last_pool->alloc * 2;
91 pool->len = 0;
92 last_pool = pool;
93 DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc);
Damien429d7192013-10-04 19:53:11 +010094 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000095
96 // add the new qstr
Damien George55baff42014-01-21 21:40:13 +000097 last_pool->qstrs[last_pool->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000098
99 // return id for the newly-added qstr
100 return last_pool->total_prev_len + last_pool->len - 1;
Damien429d7192013-10-04 19:53:11 +0100101}
102
Damien George5fa93b62014-01-22 14:35:10 +0000103qstr qstr_find_strn(const byte *str, uint str_len) {
Damien George55baff42014-01-21 21:40:13 +0000104 // work out hash of str
Damien George5fa93b62014-01-22 14:35:10 +0000105 machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000106
107 // search pools for the data
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000108 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000109 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000110 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 +0000111 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000112 }
Damien429d7192013-10-04 19:53:11 +0100113 }
114 }
Damien George55baff42014-01-21 21:40:13 +0000115
116 // not found; return null qstr
117 return 0;
Damien429d7192013-10-04 19:53:11 +0100118}
119
Damien George55baff42014-01-21 21:40:13 +0000120qstr qstr_from_str(const char *str) {
121 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100122}
123
Damien George55baff42014-01-21 21:40:13 +0000124qstr qstr_from_strn(const char *str, uint len) {
125 qstr q = qstr_find_strn((const byte*)str, len);
126 if (q == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000127 machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000128 byte *q_ptr = m_new(byte, 4 + len + 1);
129 q_ptr[0] = hash;
130 q_ptr[1] = hash >> 8;
131 q_ptr[2] = len;
132 q_ptr[3] = len >> 8;
133 memcpy(q_ptr + 4, str, len);
134 q_ptr[4 + len] = '\0';
135 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100136 }
Damien George55baff42014-01-21 21:40:13 +0000137 return q;
Damien429d7192013-10-04 19:53:11 +0100138}
139
Damien George55baff42014-01-21 21:40:13 +0000140qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
141 qstr q = qstr_from_strn(str, len);
142 m_del(char, str, alloc_len);
143 return q;
144}
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000145
Damien George55baff42014-01-21 21:40:13 +0000146byte *qstr_build_start(uint len, byte **q_ptr) {
147 assert(len <= 65535);
148 *q_ptr = m_new(byte, 4 + len + 1);
149 (*q_ptr)[2] = len;
150 (*q_ptr)[3] = len >> 8;
151 return Q_GET_DATA(*q_ptr);
152}
153
154qstr qstr_build_end(byte *q_ptr) {
155 qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
156 if (q == 0) {
157 machine_uint_t len = Q_GET_LENGTH(q_ptr);
Damien George5fa93b62014-01-22 14:35:10 +0000158 machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000159 q_ptr[0] = hash;
160 q_ptr[1] = hash >> 8;
161 q_ptr[4 + len] = '\0';
162 q = qstr_add(q_ptr);
163 } else {
164 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
165 }
166 return q;
167}
168
169machine_uint_t qstr_hash(qstr q) {
170 return Q_GET_HASH(find_qstr(q));
171}
172
173uint qstr_len(qstr q) {
174 const byte *qd = find_qstr(q);
175 return Q_GET_LENGTH(qd);
176}
177
178// XXX to remove!
179const char *qstr_str(qstr q) {
180 const byte *qd = find_qstr(q);
181 return (const char*)Q_GET_DATA(qd);
182}
183
184const byte *qstr_data(qstr q, uint *len) {
185 const byte *qd = find_qstr(q);
186 *len = Q_GET_LENGTH(qd);
187 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100188}
Damien George4d5b28c2014-01-29 18:56:46 +0000189
190void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) {
191 *n_pool = 0;
192 *n_qstr = 0;
193 *n_str_data_bytes = 0;
194 *n_total_bytes = 0;
195 for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) {
196 *n_pool += 1;
197 *n_qstr += pool->len;
198 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
199 *n_str_data_bytes += Q_GET_ALLOC(*q);
200 }
201 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
202 }
203 *n_total_bytes += *n_str_data_bytes;
204}