1 |
2 |
tarookumic |
/* 2003: Konrad Eisele <eiselekd@web.de> */
|
2 |
|
|
#include <stdlib.h>
|
3 |
|
|
#include "tmki.h"
|
4 |
|
|
|
5 |
|
|
/* ----------------------------- general mem alloc --------------------------------------*/
|
6 |
|
|
|
7 |
|
|
void ti_free(void *m) {
|
8 |
|
|
if (m != NULL)
|
9 |
|
|
free(m);
|
10 |
|
|
}
|
11 |
|
|
|
12 |
|
|
void *ti_alloc(size_t c) {
|
13 |
|
|
void *r;
|
14 |
|
|
if (c < 1) c = 1;
|
15 |
|
|
r = (void *)malloc(c);
|
16 |
|
|
if (r) memset(((char *)r) , 0, c);
|
17 |
|
|
if (!r) ti_print_err("tmki: ti_alloc(%i) failed -- out of memory",c);
|
18 |
|
|
return r;
|
19 |
|
|
}
|
20 |
|
|
|
21 |
|
|
void *ti_realloc(void *o,size_t oc,size_t nc) {
|
22 |
|
|
void *r;
|
23 |
|
|
if (nc < 1) nc = 1;
|
24 |
|
|
if (oc < 1) oc = 1;
|
25 |
|
|
r = (void *)realloc((char *)o, nc);
|
26 |
|
|
if (nc > oc && r) memset(((char *)r) + oc, 0, nc - oc);
|
27 |
|
|
if (!r) ti_print_err("tmki: ti_realloc(%x,%i,%i) failed -- out of memory",o,oc,nc);
|
28 |
|
|
return (r);
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
/* ------------------------- fixed size bitmap memory allocator ---------------------------*/
|
32 |
|
|
|
33 |
|
|
static ti_memf_chnk *ti_memf_addchunk (ti_memf_ctrl *ctrl) {
|
34 |
|
|
|
35 |
|
|
ti_memf_chnk *c;
|
36 |
|
|
unsigned int oe = TI_MEMALIGN(sizeof(ti_memf_chnk) + (TI_ALIGN(ctrl ->cc,8)/8));
|
37 |
|
|
unsigned int cs = oe + (ctrl ->cc * ctrl ->es);
|
38 |
|
|
|
39 |
|
|
if ((c = (ti_memf_chnk *) ti_alloc(cs)) == NULL)
|
40 |
|
|
return NULL;
|
41 |
|
|
|
42 |
|
|
if (ctrl ->c == NULL) {
|
43 |
|
|
ctrl ->c = c;
|
44 |
|
|
c ->n = c;
|
45 |
|
|
} else {
|
46 |
|
|
c ->n = ctrl ->c ->n;
|
47 |
|
|
ctrl ->c ->n = c;
|
48 |
|
|
}
|
49 |
|
|
c ->cs = cs;
|
50 |
|
|
c ->oe = oe;
|
51 |
|
|
c ->cc = ctrl ->cc;
|
52 |
|
|
c ->f = ctrl ->cc;
|
53 |
|
|
return c;
|
54 |
|
|
}
|
55 |
|
|
|
56 |
|
|
ti_memf_ctrl *ti_memf_init (unsigned int es, unsigned int cc) {
|
57 |
|
|
|
58 |
|
|
ti_memf_ctrl *ctrl;
|
59 |
|
|
if ((ctrl = ti_alloc(sizeof(ti_memf_ctrl))) == NULL)
|
60 |
|
|
return NULL;
|
61 |
|
|
|
62 |
|
|
ctrl ->cc = cc;
|
63 |
|
|
ctrl ->es = TI_MEMALIGN(es);
|
64 |
|
|
ti_memf_addchunk(ctrl);
|
65 |
|
|
return ctrl;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
void ti_memf_free(ti_memf_ctrl *ctrl) {
|
69 |
|
|
|
70 |
|
|
ti_memf_chnk *p,*f;
|
71 |
|
|
if ((p = ctrl ->c) == NULL)
|
72 |
|
|
return;
|
73 |
|
|
|
74 |
|
|
do {
|
75 |
|
|
f = p;
|
76 |
|
|
p = p ->n;
|
77 |
|
|
ti_free(f);
|
78 |
|
|
} while (ctrl ->c != p);
|
79 |
|
|
|
80 |
|
|
ti_free (ctrl);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
void *ti_memf_get (ti_memf_ctrl *ctrl) {
|
84 |
|
|
ti_memf_chnk *p;
|
85 |
|
|
unsigned char m;
|
86 |
|
|
unsigned long b;
|
87 |
|
|
unsigned int i;
|
88 |
|
|
|
89 |
|
|
if (ctrl ->f == 0) {
|
90 |
|
|
if (ti_memf_addchunk(ctrl) == NULL)
|
91 |
|
|
return NULL;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
p = ctrl ->c;
|
95 |
|
|
while (1) {
|
96 |
|
|
if (p ->f) {
|
97 |
|
|
ctrl ->c = p;
|
98 |
|
|
i = p ->c;
|
99 |
|
|
b = TI_PTRADD(p,sizeof(ti_memf_chnk));
|
100 |
|
|
while (1) {
|
101 |
|
|
if ((++i) >= p ->cc)
|
102 |
|
|
i = 0;
|
103 |
|
|
m = ((unsigned char)0x80)>>(i & 7);
|
104 |
|
|
if ((((unsigned char*)b)[(i)/8] & m) == 0) {
|
105 |
|
|
p ->c = i;
|
106 |
|
|
p ->f--;
|
107 |
|
|
ctrl ->f--;
|
108 |
|
|
((unsigned char*)b)[(i)/8] |= m;
|
109 |
|
|
b = TI_PTRADD(p,p->oe+(ctrl->es * i));
|
110 |
|
|
return (void *)b;
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
p = p ->n;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/* ------------------------- stringbuffer ---------------------------*/
|
119 |
|
|
|
120 |
|
|
static int rand_tabel[] = { // map characters to random values
|
121 |
|
|
2078917053, 143302914, 1027100827, 1953210302, 755253631,
|
122 |
|
|
2002600785, 1405390230, 45248011, 1099951567, 433832350,
|
123 |
|
|
2018585307, 438263339, 813528929, 1703199216, 618906479,
|
124 |
|
|
573714703, 766270699, 275680090, 1510320440, 1583583926,
|
125 |
|
|
1723401032, 1965443329, 1098183682, 1636505764, 980071615,
|
126 |
|
|
1011597961, 643279273, 1315461275, 157584038, 1069844923,
|
127 |
|
|
471560540, 89017443, 1213147837, 1498661368, 2042227746,
|
128 |
|
|
1968401469, 1353778505, 1300134328, 2013649480, 306246424,
|
129 |
|
|
1733966678, 1884751139, 744509763, 400011959, 1440466707,
|
130 |
|
|
1363416242, 973726663, 59253759, 1639096332, 336563455,
|
131 |
|
|
1642837685, 1215013716, 154523136, 593537720, 704035832,
|
132 |
|
|
1134594751, 1605135681, 1347315106, 302572379, 1762719719,
|
133 |
|
|
269676381, 774132919, 1851737163, 1482824219, 125310639,
|
134 |
|
|
1746481261, 1303742040, 1479089144, 899131941, 1169907872,
|
135 |
|
|
1785335569, 485614972, 907175364, 382361684, 885626931,
|
136 |
|
|
200158423, 1745777927, 1859353594, 259412182, 1237390611,
|
137 |
|
|
48433401, 1902249868, 304920680, 202956538, 348303940,
|
138 |
|
|
1008956512, 1337551289, 1953439621, 208787970, 1640123668,
|
139 |
|
|
1568675693, 478464352, 266772940, 1272929208, 1961288571,
|
140 |
|
|
392083579, 871926821, 1117546963, 1871172724, 1771058762,
|
141 |
|
|
139971187, 1509024645, 109190086, 1047146551, 1891386329,
|
142 |
|
|
994817018, 1247304975, 1489680608, 706686964, 1506717157,
|
143 |
|
|
579587572, 755120366, 1261483377, 884508252, 958076904,
|
144 |
|
|
1609787317, 1893464764, 148144545, 1415743291, 2102252735,
|
145 |
|
|
1788268214, 836935336, 433233439, 2055041154, 2109864544,
|
146 |
|
|
247038362, 299641085, 834307717, 1364585325, 23330161,
|
147 |
|
|
457882831, 1504556512, 1532354806, 567072918, 404219416,
|
148 |
|
|
1276257488, 1561889936, 1651524391, 618454448, 121093252,
|
149 |
|
|
1010757900, 1198042020, 876213618, 124757630, 2082550272,
|
150 |
|
|
1834290522, 1734544947, 1828531389, 1982435068, 1002804590,
|
151 |
|
|
1783300476, 1623219634, 1839739926, 69050267, 1530777140,
|
152 |
|
|
1802120822, 316088629, 1830418225, 488944891, 1680673954,
|
153 |
|
|
1853748387, 946827723, 1037746818, 1238619545, 1513900641,
|
154 |
|
|
1441966234, 367393385, 928306929, 946006977, 985847834,
|
155 |
|
|
1049400181, 1956764878, 36406206, 1925613800, 2081522508,
|
156 |
|
|
2118956479, 1612420674, 1668583807, 1800004220, 1447372094,
|
157 |
|
|
523904750, 1435821048, 923108080, 216161028, 1504871315,
|
158 |
|
|
306401572, 2018281851, 1820959944, 2136819798, 359743094,
|
159 |
|
|
1354150250, 1843084537, 1306570817, 244413420, 934220434,
|
160 |
|
|
672987810, 1686379655, 1301613820, 1601294739, 484902984,
|
161 |
|
|
139978006, 503211273, 294184214, 176384212, 281341425,
|
162 |
|
|
228223074, 147857043, 1893762099, 1896806882, 1947861263,
|
163 |
|
|
1193650546, 273227984, 1236198663, 2116758626, 489389012,
|
164 |
|
|
593586330, 275676551, 360187215, 267062626, 265012701,
|
165 |
|
|
719930310, 1621212876, 2108097238, 2026501127, 1865626297,
|
166 |
|
|
894834024, 552005290, 1404522304, 48964196, 5816381,
|
167 |
|
|
1889425288, 188942202, 509027654, 36125855, 365326415,
|
168 |
|
|
790369079, 264348929, 513183458, 536647531, 13672163,
|
169 |
|
|
313561074, 1730298077, 286900147, 1549759737, 1699573055,
|
170 |
|
|
776289160, 2143346068, 1975249606, 1136476375, 262925046,
|
171 |
|
|
92778659, 1856406685, 1884137923, 53392249, 1735424165,
|
172 |
|
|
1602280572
|
173 |
|
|
};
|
174 |
|
|
|
175 |
|
|
static unsigned int
|
176 |
|
|
hash_func(const char *s,int c,int m) {
|
177 |
|
|
|
178 |
|
|
unsigned int v = 0;
|
179 |
|
|
while (c--)
|
180 |
|
|
v += (v << 1) + rand_tabel[((unsigned char*)(s))[c]];
|
181 |
|
|
return v & (m-1);
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
static unsigned int
|
185 |
|
|
hash_func2(const char *arKey, unsigned int nKeyLength, int m) {
|
186 |
|
|
|
187 |
|
|
int h = 0, g;
|
188 |
|
|
const char *arEnd=arKey+nKeyLength;
|
189 |
|
|
|
190 |
|
|
while (arKey < arEnd) {
|
191 |
|
|
h = (h << 4) + *arKey++;
|
192 |
|
|
if ((g = (h & 0xF0000000))) {
|
193 |
|
|
h = h ^ (g >> 24);
|
194 |
|
|
h = h ^ g;
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
return h & (m-1);
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
ti_strbuf_ctrl *ti_strbuf_init(int hs) {
|
201 |
|
|
unsigned int i = 1;
|
202 |
|
|
ti_strbuf_ctrl *ctrl;
|
203 |
|
|
while (i < hs)
|
204 |
|
|
i = i << 1;
|
205 |
|
|
if ((ctrl = (ti_strbuf_ctrl *)ti_alloc(sizeof(ti_strbuf_ctrl))) == NULL)
|
206 |
|
|
return NULL;
|
207 |
|
|
|
208 |
|
|
ctrl ->hs = i;
|
209 |
|
|
i *= sizeof(ti_strbuf_he *);
|
210 |
|
|
if (((ctrl ->h = (ti_strbuf_he **) ti_alloc(i)) == NULL) ||
|
211 |
|
|
((ctrl ->m = ti_memf_init(sizeof(ti_strbuf_he),256)) == NULL)) {
|
212 |
|
|
ti_free(ctrl ->h);
|
213 |
|
|
ti_free(ctrl);
|
214 |
|
|
return NULL;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
return ctrl;
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
void ti_strbuf_free (ti_strbuf_ctrl *ctrl) {
|
221 |
|
|
if (ctrl == NULL) return;
|
222 |
|
|
ti_memf_free(ctrl ->m);
|
223 |
|
|
ti_free (ctrl ->h);
|
224 |
|
|
ti_free (ctrl);
|
225 |
|
|
}
|