blob: f9f927f583380d7925d9f18fdc9d713533236c3f [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
13#include <stdio.h>
14#define DEBUG_printf(args...) printf(args)
15#else // don't print debugging info
16#define DEBUG_printf(args...) (void)0
17#endif
18
Damien George55baff42014-01-21 21:40:13 +000019// A qstr is an index into the qstr pool.
20// The data for a qstr contains (hash, length, data).
21// For now we use very simple encoding, just to get the framework correct:
22// - hash is 2 bytes (simply the sum of data bytes)
23// - length is 2 bytes
24// - data follows
25// - \0 terminated (for now, so they can be printed using printf)
26
27#define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8))
28#define Q_GET_ALLOC(q) (4 + Q_GET_LENGTH(q) + 1)
29#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
30#define Q_GET_DATA(q) ((q) + 4)
31
Damien George5fa93b62014-01-22 14:35:10 +000032machine_uint_t qstr_compute_hash(const byte *data, uint len) {
Damien George55baff42014-01-21 21:40:13 +000033 machine_uint_t hash = 0;
34 for (const byte *top = data + len; data < top; data++) {
35 hash += *data;
36 }
37 return hash & 0xffff;
38}
39
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000040typedef struct _qstr_pool_t {
41 struct _qstr_pool_t *prev;
42 uint total_prev_len;
43 uint alloc;
44 uint len;
Damien George55baff42014-01-21 21:40:13 +000045 const byte *qstrs[];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000046} qstr_pool_t;
47
48const static qstr_pool_t const_pool = {
49 NULL, // no previous pool
50 0, // no previous pool
51 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
52 MP_QSTR_number_of, // corresponds to number of strings in array just below
53 {
Damien George55baff42014-01-21 21:40:13 +000054 (const byte*) "\0\0\0\0", // invalid/no qstr has empty data
55 (const byte*) "\0\0\0\0", // empty qstr
56#define Q(id, str) str,
57// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
Dave Hylandsc89c6812014-01-24 01:05:30 -080058#include "build/py/qstrdefs.generated.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000059#undef Q
60 },
61};
62
Dave Hylandsd986b582014-01-05 11:55:23 -080063static qstr_pool_t *last_pool;
Damien429d7192013-10-04 19:53:11 +010064
Damien8b3a7c22013-10-23 20:20:17 +010065void qstr_init(void) {
Dave Hylandsd986b582014-01-05 11:55:23 -080066 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 +010067}
68
Damien George55baff42014-01-21 21:40:13 +000069static const byte *find_qstr(qstr q) {
70 // search pool for this qstr
71 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
72 if (q >= pool->total_prev_len) {
73 return pool->qstrs[q - pool->total_prev_len];
74 }
75 }
76
77 // not found
78 return 0;
79}
80
81static qstr qstr_add(const byte *q_ptr) {
82 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 +000083
84 // make sure we have room in the pool for a new qstr
85 if (last_pool->len >= last_pool->alloc) {
86 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, last_pool->alloc * 2);
87 pool->prev = last_pool;
88 pool->total_prev_len = last_pool->total_prev_len + last_pool->len;
89 pool->alloc = last_pool->alloc * 2;
90 pool->len = 0;
91 last_pool = pool;
92 DEBUG_printf("QSTR: allocate new pool of size %d\n", last_pool->alloc);
Damien429d7192013-10-04 19:53:11 +010093 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000094
95 // add the new qstr
Damien George55baff42014-01-21 21:40:13 +000096 last_pool->qstrs[last_pool->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000097
98 // return id for the newly-added qstr
99 return last_pool->total_prev_len + last_pool->len - 1;
Damien429d7192013-10-04 19:53:11 +0100100}
101
Damien George5fa93b62014-01-22 14:35:10 +0000102qstr qstr_find_strn(const byte *str, uint str_len) {
Damien George55baff42014-01-21 21:40:13 +0000103 // work out hash of str
Damien George5fa93b62014-01-22 14:35:10 +0000104 machine_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000105
106 // search pools for the data
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000107 for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000108 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000109 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 +0000110 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000111 }
Damien429d7192013-10-04 19:53:11 +0100112 }
113 }
Damien George55baff42014-01-21 21:40:13 +0000114
115 // not found; return null qstr
116 return 0;
Damien429d7192013-10-04 19:53:11 +0100117}
118
Damien George55baff42014-01-21 21:40:13 +0000119qstr qstr_from_str(const char *str) {
120 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100121}
122
Damien George55baff42014-01-21 21:40:13 +0000123qstr qstr_from_strn(const char *str, uint len) {
124 qstr q = qstr_find_strn((const byte*)str, len);
125 if (q == 0) {
Damien George5fa93b62014-01-22 14:35:10 +0000126 machine_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George55baff42014-01-21 21:40:13 +0000127 byte *q_ptr = m_new(byte, 4 + len + 1);
128 q_ptr[0] = hash;
129 q_ptr[1] = hash >> 8;
130 q_ptr[2] = len;
131 q_ptr[3] = len >> 8;
132 memcpy(q_ptr + 4, str, len);
133 q_ptr[4 + len] = '\0';
134 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100135 }
Damien George55baff42014-01-21 21:40:13 +0000136 return q;
Damien429d7192013-10-04 19:53:11 +0100137}
138
Damien George55baff42014-01-21 21:40:13 +0000139qstr qstr_from_strn_take(char *str, uint alloc_len, uint len) {
140 qstr q = qstr_from_strn(str, len);
141 m_del(char, str, alloc_len);
142 return q;
143}
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000144
Damien George55baff42014-01-21 21:40:13 +0000145byte *qstr_build_start(uint len, byte **q_ptr) {
146 assert(len <= 65535);
147 *q_ptr = m_new(byte, 4 + len + 1);
148 (*q_ptr)[2] = len;
149 (*q_ptr)[3] = len >> 8;
150 return Q_GET_DATA(*q_ptr);
151}
152
153qstr qstr_build_end(byte *q_ptr) {
154 qstr q = qstr_find_strn(Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
155 if (q == 0) {
156 machine_uint_t len = Q_GET_LENGTH(q_ptr);
Damien George5fa93b62014-01-22 14:35:10 +0000157 machine_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000158 q_ptr[0] = hash;
159 q_ptr[1] = hash >> 8;
160 q_ptr[4 + len] = '\0';
161 q = qstr_add(q_ptr);
162 } else {
163 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
164 }
165 return q;
166}
167
168machine_uint_t qstr_hash(qstr q) {
169 return Q_GET_HASH(find_qstr(q));
170}
171
172uint qstr_len(qstr q) {
173 const byte *qd = find_qstr(q);
174 return Q_GET_LENGTH(qd);
175}
176
177// XXX to remove!
178const char *qstr_str(qstr q) {
179 const byte *qd = find_qstr(q);
180 return (const char*)Q_GET_DATA(qd);
181}
182
183const byte *qstr_data(qstr q, uint *len) {
184 const byte *qd = find_qstr(q);
185 *len = Q_GET_LENGTH(qd);
186 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100187}
Damien George4d5b28c2014-01-29 18:56:46 +0000188
189void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) {
190 *n_pool = 0;
191 *n_qstr = 0;
192 *n_str_data_bytes = 0;
193 *n_total_bytes = 0;
194 for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) {
195 *n_pool += 1;
196 *n_qstr += pool->len;
197 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
198 *n_str_data_bytes += Q_GET_ALLOC(*q);
199 }
200 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
201 }
202 *n_total_bytes += *n_str_data_bytes;
203}