blob: a34479cbfd4f614adbfc732fe8388a427c1a4932 [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
15#define DEBUG_printf(args...) (void)0
16#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:
21// - hash is 2 bytes (simply the sum of data bytes)
22// - 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 George5fa93b62014-01-22 14:35:10 +000031machine_uint_t qstr_compute_hash(const byte *data, uint len) {
Damien George55baff42014-01-21 21:40:13 +000032 machine_uint_t hash = 0;
33 for (const byte *top = data + len; data < top; data++) {
34 hash += *data;
35 }
36 return hash & 0xffff;
37}
38
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000039typedef struct _qstr_pool_t {
40 struct _qstr_pool_t *prev;
41 uint total_prev_len;
42 uint alloc;
43 uint len;
Damien George55baff42014-01-21 21:40:13 +000044 const byte *qstrs[];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000045} qstr_pool_t;
46
47const static qstr_pool_t const_pool = {
48 NULL, // no previous pool
49 0, // no previous pool
50 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
51 MP_QSTR_number_of, // corresponds to number of strings in array just below
52 {
Damien George55baff42014-01-21 21:40:13 +000053 (const byte*) "\0\0\0\0", // invalid/no qstr has empty data
54 (const byte*) "\0\0\0\0", // empty qstr
55#define Q(id, str) str,
56// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
Dave Hylandsc89c6812014-01-24 01:05:30 -080057#include "build/py/qstrdefs.generated.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000058#undef Q
59 },
60};
61
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020062STATIC qstr_pool_t *last_pool;
Damien429d7192013-10-04 19:53:11 +010063
Damien8b3a7c22013-10-23 20:20:17 +010064void qstr_init(void) {
Dave Hylandsd986b582014-01-05 11:55:23 -080065 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 +010066}
67
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020068STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000069 // search pool for this qstr
70 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
71 if (q >= pool->total_prev_len) {
72 return pool->qstrs[q - pool->total_prev_len];
73 }
74 }
75
76 // not found
77 return 0;
78}
79
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020080STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +000081 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 +000082
83 // make sure we have room in the pool for a new qstr
84 if (last_pool->len >= last_pool->alloc) {
85 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2);
86 pool->prev = last_pool;
87 pool->total_prev_len = last_pool->total_prev_len + last_pool->len;
88 pool->alloc = last_pool->alloc * 2;
89 pool->len = 0;
90 last_pool = pool;
91 DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc);
Damien429d7192013-10-04 19:53:11 +010092 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000093
94 // add the new qstr
Damien George55baff42014-01-21 21:40:13 +000095 last_pool->qstrs[last_pool->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000096
97 // return id for the newly-added qstr
98 return last_pool->total_prev_len + last_pool->len - 1;
Damien429d7192013-10-04 19:53:11 +010099}
100
Damien George5fa93b62014-01-22 14:35:10 +0000101qstr qstr_find_strn(const byte *str, uint str_len) {
Damien George55baff42014-01-21 21:40:13 +0000102 // work out hash of str
Damien George5fa93b62014-01-22 14:35:10 +0000103 machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000104
105 // search pools for the data
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000106 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000107 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000108 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 +0000109 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000110 }
Damien429d7192013-10-04 19:53:11 +0100111 }
112 }
Damien George55baff42014-01-21 21:40:13 +0000113
114 // not found; return null qstr
115 return 0;
Damien429d7192013-10-04 19:53:11 +0100116}
117
Damien George55baff42014-01-21 21:40:13 +0000118qstr qstr_from_str(const char *str) {
119 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100120}
121
Damien George55baff42014-01-21 21:40:13 +0000122qstr qstr_from_strn(const char *str, uint len) {
123 qstr q = qstr_find_strn((const byte*)str, len);
124 if (q == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000125 machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000126 byte *q_ptr = m_new(byte, 4 + len + 1);
127 q_ptr[0] = hash;
128 q_ptr[1] = hash >> 8;
129 q_ptr[2] = len;
130 q_ptr[3] = len >> 8;
131 memcpy(q_ptr + 4, str, len);
132 q_ptr[4 + len] = '\0';
133 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100134 }
Damien George55baff42014-01-21 21:40:13 +0000135 return q;
Damien429d7192013-10-04 19:53:11 +0100136}
137
Damien George55baff42014-01-21 21:40:13 +0000138qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
139 qstr q = qstr_from_strn(str, len);
140 m_del(char, str, alloc_len);
141 return q;
142}
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000143
Damien George55baff42014-01-21 21:40:13 +0000144byte *qstr_build_start(uint len, byte **q_ptr) {
145 assert(len <= 65535);
146 *q_ptr = m_new(byte, 4 + len + 1);
147 (*q_ptr)[2] = len;
148 (*q_ptr)[3] = len >> 8;
149 return Q_GET_DATA(*q_ptr);
150}
151
152qstr qstr_build_end(byte *q_ptr) {
153 qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
154 if (q == 0) {
155 machine_uint_t len = Q_GET_LENGTH(q_ptr);
Damien George5fa93b62014-01-22 14:35:10 +0000156 machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000157 q_ptr[0] = hash;
158 q_ptr[1] = hash >> 8;
159 q_ptr[4 + len] = '\0';
160 q = qstr_add(q_ptr);
161 } else {
162 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
163 }
164 return q;
165}
166
167machine_uint_t qstr_hash(qstr q) {
168 return Q_GET_HASH(find_qstr(q));
169}
170
171uint qstr_len(qstr q) {
172 const byte *qd = find_qstr(q);
173 return Q_GET_LENGTH(qd);
174}
175
176// XXX to remove!
177const char *qstr_str(qstr q) {
178 const byte *qd = find_qstr(q);
179 return (const char*)Q_GET_DATA(qd);
180}
181
182const byte *qstr_data(qstr q, uint *len) {
183 const byte *qd = find_qstr(q);
184 *len = Q_GET_LENGTH(qd);
185 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100186}
Damien George4d5b28c2014-01-29 18:56:46 +0000187
188void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) {
189 *n_pool = 0;
190 *n_qstr = 0;
191 *n_str_data_bytes = 0;
192 *n_total_bytes = 0;
193 for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) {
194 *n_pool += 1;
195 *n_qstr += pool->len;
196 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
197 *n_str_data_bytes += Q_GET_ALLOC(*q);
198 }
199 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
200 }
201 *n_total_bytes += *n_str_data_bytes;
202}