|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * |
|
|
6 | * Deliantra is free software: you can redistribute it and/or modify |
|
|
7 | * it under the terms of the GNU General Public License as published by |
|
|
8 | * the Free Software Foundation, either version 3 of the License, or |
|
|
9 | * (at your option) any later version. |
|
|
10 | * |
|
|
11 | * This program is distributed in the hope that it will be useful, |
|
|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | * GNU General Public License for more details. |
|
|
15 | * |
|
|
16 | * You should have received a copy of the GNU General Public License |
|
|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
18 | * |
|
|
19 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
20 | */ |
|
|
21 | |
1 | #include "global.h" |
22 | #include "global.h" |
2 | |
23 | |
3 | #include <cstdio> |
24 | #include <cstdio> |
4 | |
25 | |
5 | dynbuf::dynbuf (int initial, int extend) |
26 | void |
|
|
27 | dynbuf::init (int initial) |
6 | { |
28 | { |
7 | _size = 0; |
29 | _size = 0; |
8 | ext = extend; |
|
|
9 | |
30 | |
10 | first = last = (chunk *)salloc<char> (sizeof (chunk) + initial); |
31 | first = last = (chunk *)salloc<char> (sizeof (chunk) + initial); |
11 | first->alloc = sizeof (chunk) + initial; |
32 | first->alloc = sizeof (chunk) + initial; |
12 | first->next = 0; |
33 | first->next = 0; |
13 | |
34 | |
14 | room = initial; |
|
|
15 | ptr = first->data; |
35 | ptr = first->data; |
|
|
36 | end = ptr + initial; |
16 | } |
37 | } |
17 | |
38 | |
18 | dynbuf::~dynbuf () |
39 | void |
|
|
40 | dynbuf::free (chunk *&chain) |
19 | { |
41 | { |
20 | clear (); |
42 | while (chain) |
|
|
43 | { |
|
|
44 | chunk *next = chain->next; |
|
|
45 | |
|
|
46 | sfree<char> ((char *)chain, chain->alloc); |
|
|
47 | chain = next; |
|
|
48 | } |
21 | } |
49 | } |
22 | |
50 | |
23 | void |
51 | void |
24 | dynbuf::clear () |
52 | dynbuf::clear () |
25 | { |
53 | { |
26 | while (first) |
54 | free (first->next); |
27 | { |
|
|
28 | chunk *next = first->next; |
|
|
29 | |
55 | |
30 | sfree<char> ((char *)first, first->alloc); |
56 | _size = 0; |
31 | first = next; |
57 | ptr = first->data; |
32 | } |
58 | end = ptr + first->alloc - sizeof (chunk); |
33 | } |
59 | } |
34 | |
60 | |
35 | void |
61 | void |
36 | dynbuf::finish () |
62 | dynbuf::finalise () |
37 | { |
63 | { |
38 | // finalise current chunk |
64 | // finalise current chunk |
39 | _size += last->size = ptr - last->data; |
65 | _size += last->size = ptr - last->data; |
40 | } |
66 | } |
41 | |
67 | |
42 | void |
68 | void |
43 | dynbuf::_reserve (int size) |
69 | dynbuf::reserve (int size) |
44 | { |
70 | { |
45 | finish (); |
71 | finalise (); |
46 | |
72 | |
47 | do |
73 | do |
48 | { |
74 | { |
49 | ext += ext >> 1; |
75 | extend += extend >> 1; |
50 | ext = (ext + 15) & ~15; |
76 | extend = (extend + 15) & ~15; |
51 | } |
77 | } |
52 | while (ext < size); |
78 | while (extend < size); |
53 | |
79 | |
54 | chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext); |
80 | chunk *add = (chunk *) salloc<char> (sizeof (chunk) + extend); |
55 | add->alloc = sizeof (chunk) + ext; |
81 | add->alloc = sizeof (chunk) + extend; |
56 | add->next = 0; |
82 | add->next = 0; |
57 | |
83 | |
58 | last->next = add; |
84 | last->next = add; |
59 | last = add; |
85 | last = add; |
60 | |
86 | |
61 | room = ext; |
|
|
62 | ptr = last->data; |
87 | ptr = last->data; |
|
|
88 | end = ptr + extend; |
63 | } |
89 | } |
64 | |
90 | |
65 | void |
91 | void |
66 | dynbuf::linearise (void *data) |
92 | dynbuf::linearise (void *data) |
67 | { |
93 | { |
68 | char *p = (char *) data; |
|
|
69 | |
|
|
70 | last->size = ptr - last->data; |
94 | last->size = ptr - last->data; |
71 | |
95 | |
72 | for (chunk * c = first; c; c = c->next) |
96 | for (chunk *c = first; c; c = c->next) |
73 | { |
97 | { |
74 | memcpy (p, c->data, c->size); |
98 | memcpy (data, c->data, c->size); |
75 | p += c->size; |
99 | data = (void *)(((char *)data) + c->size); |
76 | } |
100 | } |
77 | } |
101 | } |
78 | |
102 | |
79 | char * |
103 | char * |
80 | dynbuf::linearise () |
104 | dynbuf::_linearise () |
81 | { |
105 | { |
82 | if (first->next) |
106 | finalise (); |
83 | { |
|
|
84 | finish (); |
|
|
85 | |
107 | |
86 | chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); |
108 | chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); |
87 | add->alloc = sizeof (chunk) + _size; |
109 | add->alloc = sizeof (chunk) + _size; |
88 | add->next = 0; |
110 | add->next = 0; |
89 | |
111 | |
90 | linearise ((void *)add->data); |
112 | linearise ((void *)add->data); |
91 | clear (); |
113 | free (first); |
92 | |
114 | |
93 | first = last = add; |
115 | first = last = add; |
94 | ptr = last->data + _size; |
116 | ptr = last->data + _size; |
|
|
117 | end = ptr; |
95 | _size = 0; |
118 | _size = 0; |
96 | room = 0; |
|
|
97 | } |
|
|
98 | |
119 | |
99 | return first->data; |
120 | return first->data; |
100 | } |
121 | } |
101 | |
122 | |
102 | dynbuf::operator std::string () |
123 | dynbuf::operator std::string () |
… | |
… | |
104 | // could optimise |
125 | // could optimise |
105 | return std::string (linearise (), size ()); |
126 | return std::string (linearise (), size ()); |
106 | } |
127 | } |
107 | |
128 | |
108 | void |
129 | void |
109 | dynbuf_text::printf (const char *format, ...) |
130 | dynbuf_text::vprintf (const char *format, va_list ap) |
110 | { |
131 | { |
111 | int len; |
132 | int len; |
112 | |
133 | |
113 | { |
134 | { |
114 | force (128); |
135 | force (128); |
115 | |
136 | |
116 | va_list ap; |
137 | va_list apc; |
117 | va_start (ap, format); |
138 | va_copy (apc, ap); |
118 | len = vsnprintf (ptr, room, format, ap); |
139 | len = vsnprintf (ptr, end - ptr, format, apc); |
119 | va_end (ap); |
140 | va_end (apc); |
120 | |
141 | |
121 | assert (len >= 0); // shield against broken vsnprintf's |
142 | assert (len >= 0); // shield against broken vsnprintf's |
122 | |
143 | |
123 | // was enough room available |
144 | // was enough room available |
124 | if (len < room) |
145 | if (ptr + len < end) |
125 | { |
146 | { |
126 | alloc (len); |
147 | ptr += len; |
127 | return; |
148 | return; |
128 | } |
149 | } |
129 | } |
150 | } |
130 | |
151 | |
131 | // longer, try harder |
152 | // longer, try harder |
|
|
153 | vsnprintf (force (len + 1), len + 1, format, ap); |
|
|
154 | |
|
|
155 | ptr += len; |
|
|
156 | } |
|
|
157 | |
|
|
158 | void |
|
|
159 | dynbuf_text::printf (const char *format, ...) |
|
|
160 | { |
132 | va_list ap; |
161 | va_list ap; |
133 | va_start (ap, format); |
162 | va_start (ap, format); |
134 | vsnprintf (force (len + 1), len + 1, format, ap); |
163 | vprintf (format, ap); |
135 | va_end (ap); |
164 | va_end (ap); |
|
|
165 | } |
136 | |
166 | |
137 | alloc (len); |
167 | // simply return a mask with "bits" bits set |
|
|
168 | inline uint64 |
|
|
169 | m (int b) |
|
|
170 | { |
|
|
171 | return (uint64 (1) << b) - 1; |
|
|
172 | } |
|
|
173 | |
|
|
174 | // convert 9 digits to ascii, using only a single multiplication |
|
|
175 | // (depending on cpu and compiler). |
|
|
176 | // will generate a single 0 as output when v=lz=0 |
|
|
177 | inline char * |
|
|
178 | i2a_9 (char *ptr, uint32 v, bool lz) |
|
|
179 | { |
|
|
180 | // convert to 4.56 fixed-point representation |
|
|
181 | // this should be optimal on 64 bit cpus, and rather |
|
|
182 | // slow on 32 bit cpus. go figure :) |
|
|
183 | const int bits = 7*8; // 7 bits per post-comma digit |
|
|
184 | |
|
|
185 | uint64 u = v * ((m (bits) + 100000000) / 100000000); // 10**8 |
|
|
186 | |
|
|
187 | if (lz) |
|
|
188 | { |
|
|
189 | // output leading zeros |
|
|
190 | // good compilers will compile this into only shifts, masks and adds |
|
|
191 | *ptr++ = char (u >> (bits - 0)) + '0'; u = (u & m (bits - 0)) * 5; |
|
|
192 | *ptr++ = char (u >> (bits - 1)) + '0'; u = (u & m (bits - 1)) * 5; |
|
|
193 | *ptr++ = char (u >> (bits - 2)) + '0'; u = (u & m (bits - 2)) * 5; |
|
|
194 | *ptr++ = char (u >> (bits - 3)) + '0'; u = (u & m (bits - 3)) * 5; |
|
|
195 | *ptr++ = char (u >> (bits - 4)) + '0'; u = (u & m (bits - 4)) * 5; |
|
|
196 | *ptr++ = char (u >> (bits - 5)) + '0'; u = (u & m (bits - 5)) * 5; |
|
|
197 | *ptr++ = char (u >> (bits - 6)) + '0'; u = (u & m (bits - 6)) * 5; |
|
|
198 | *ptr++ = char (u >> (bits - 7)) + '0'; u = (u & m (bits - 7)) * 5; |
|
|
199 | *ptr++ = char (u >> (bits - 8)) + '0'; |
|
|
200 | } |
|
|
201 | else |
|
|
202 | { |
|
|
203 | // do not output leading zeroes (except if v == 0) |
|
|
204 | // good compilers will compile this into completely branchless code |
|
|
205 | char digit, nz = 0; |
|
|
206 | |
|
|
207 | digit = (u >> (bits - 0)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 0)) * 5; |
|
|
208 | digit = (u >> (bits - 1)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 1)) * 5; |
|
|
209 | digit = (u >> (bits - 2)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 2)) * 5; |
|
|
210 | digit = (u >> (bits - 3)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 3)) * 5; |
|
|
211 | digit = (u >> (bits - 4)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 4)) * 5; |
|
|
212 | digit = (u >> (bits - 5)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 5)) * 5; |
|
|
213 | digit = (u >> (bits - 6)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 6)) * 5; |
|
|
214 | digit = (u >> (bits - 7)); *ptr = digit + '0'; nz |= digit; ptr += nz ? 1 : 0; u = (u & m (bits - 7)) * 5; |
|
|
215 | digit = (u >> (bits - 8)); *ptr = digit + '0'; nz |= digit; ptr += 1; |
|
|
216 | } |
|
|
217 | |
|
|
218 | return ptr; |
138 | } |
219 | } |
139 | |
220 | |
140 | void |
221 | void |
141 | dynbuf_text::add (sint32 i) |
222 | dynbuf_text::add (sint32 i) |
142 | { |
223 | { |
143 | char buf[max_sint32_size]; |
224 | force (sint32_digits); |
144 | char *p = buf + sizeof (buf); |
|
|
145 | char neg; |
|
|
146 | |
225 | |
147 | uint32 val; |
226 | *ptr = '-'; ptr += i < 0 ? 1 : 0; |
|
|
227 | uint32 u = i < 0 ? -i : i; |
148 | |
228 | |
149 | if (i < 0) |
229 | if (expect_true (u < 10)) // we have a lot of single-digit numbers, so optimise |
150 | { |
230 | fadd (char (u + '0')); |
151 | neg = '-'; |
231 | else if (expect_true (u < 1000000000)) // 9 0's |
152 | val = -i; |
232 | ptr = i2a_9 (ptr, u, false); |
153 | } |
|
|
154 | else |
233 | else |
155 | { |
234 | { |
156 | neg = 0; |
235 | sint32 div = u / 1000000000; |
157 | val = i; |
236 | uint32 rem = u % 1000000000; |
158 | } |
|
|
159 | |
237 | |
160 | do |
238 | ptr = i2a_9 (ptr, div, false); |
|
|
239 | ptr = i2a_9 (ptr, rem, true); |
161 | { |
240 | } |
162 | uint32 div = val / 10; |
|
|
163 | *--p = '0' + char (val - div * 10); |
|
|
164 | |
|
|
165 | val = div; |
|
|
166 | } |
|
|
167 | while (val); |
|
|
168 | |
|
|
169 | if (neg) |
|
|
170 | *--p = neg; |
|
|
171 | |
|
|
172 | add ((void *) p, buf + sizeof (buf) - p); |
|
|
173 | } |
241 | } |
174 | |
242 | |
175 | void |
243 | void |
176 | dynbuf_text::add (sint64 i) |
244 | dynbuf_text::add (sint64 i) |
177 | { |
245 | { |
178 | if (i > -10000000 && i < 10000000) |
246 | force (sint64_digits); |
179 | { |
247 | |
180 | add (sint32 (i)); |
248 | *ptr = '-'; ptr += i < 0 ? 1 : 0; |
181 | return; |
249 | uint64 u = i < 0 ? -i : i; |
|
|
250 | |
|
|
251 | // split the number into a 1-digit part |
|
|
252 | // (#19) and two 9 digit parts (9..18 and 0..8) |
|
|
253 | |
|
|
254 | // good compilers will only use multiplications here |
|
|
255 | |
|
|
256 | if (u < 10) // we have a lot of single-digit numbers, so optimise |
|
|
257 | fadd (char (u + '0')); |
|
|
258 | else if (expect_true (u < 1000000000)) // 9 0's |
|
|
259 | ptr = i2a_9 (ptr, u, false); |
|
|
260 | else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's |
182 | } |
261 | { |
|
|
262 | sint32 div = u / 1000000000; |
|
|
263 | uint32 rem = u % 1000000000; |
183 | |
264 | |
184 | char buf[max_sint64_size]; |
265 | ptr = i2a_9 (ptr, div, false); |
185 | char *p = buf + sizeof (buf); |
266 | ptr = i2a_9 (ptr, rem, true); |
186 | char neg; |
|
|
187 | |
|
|
188 | uint64 val; |
|
|
189 | |
|
|
190 | if (i < 0) |
|
|
191 | { |
|
|
192 | neg = '-'; |
|
|
193 | val = -i; |
|
|
194 | } |
267 | } |
195 | else |
268 | else |
196 | { |
269 | { |
197 | neg = 0; |
270 | // a biggy |
198 | val = i; |
271 | sint32 div = u / UINT64_C (1000000000000000000); |
199 | } |
272 | uint64 rem = u % UINT64_C (1000000000000000000); |
200 | |
273 | |
201 | do |
274 | fadd (char (div + '0')); |
|
|
275 | u = rem; |
|
|
276 | |
202 | { |
277 | { |
203 | uint64 div = val / 10; |
278 | sint32 div = u / 1000000000; |
204 | *--p = '0' + char (val - div * 10); |
279 | uint32 rem = u % 1000000000; |
205 | |
280 | |
206 | val = div; |
281 | ptr = i2a_9 (ptr, div, true); |
|
|
282 | ptr = i2a_9 (ptr, rem, true); |
207 | } |
283 | } |
208 | while (val); |
284 | } |
209 | |
|
|
210 | if (neg) |
|
|
211 | *--p = neg; |
|
|
212 | |
|
|
213 | add ((void *) p, buf + sizeof (buf) - p); |
|
|
214 | } |
285 | } |
|
|
286 | |
|
|
287 | dynbuf_text::operator const char *() |
|
|
288 | { |
|
|
289 | *this << '\0'; |
|
|
290 | linearise (); |
|
|
291 | --ptr; |
|
|
292 | return first->data; |
|
|
293 | } |
|
|
294 | |
|
|
295 | void |
|
|
296 | dynbuf_text::add_abilities (const char *name, uint32 abilities) |
|
|
297 | { |
|
|
298 | if (!abilities) |
|
|
299 | return; |
|
|
300 | |
|
|
301 | *this << '(' << name; |
|
|
302 | |
|
|
303 | const char *sep = ": "; |
|
|
304 | for (int i = 0; i < NROFATTACKS; ++i) |
|
|
305 | if (abilities & (1 << i)) |
|
|
306 | { |
|
|
307 | *this << sep; sep = ", "; |
|
|
308 | *this << attacks [i]; |
|
|
309 | } |
|
|
310 | |
|
|
311 | *this << ')'; |
|
|
312 | } |
|
|
313 | |
|
|
314 | void |
|
|
315 | dynbuf_text::add_paths (const char *name, uint32 paths) |
|
|
316 | { |
|
|
317 | if (!paths) |
|
|
318 | return; |
|
|
319 | |
|
|
320 | *this << '(' << name; |
|
|
321 | |
|
|
322 | const char *sep = ": "; |
|
|
323 | for (int i = 0; i < NRSPELLPATHS; ++i) |
|
|
324 | if (paths & (1 << i)) |
|
|
325 | { |
|
|
326 | *this << sep; sep = ", "; |
|
|
327 | *this << spellpathnames [i]; |
|
|
328 | } |
|
|
329 | |
|
|
330 | *this << ')'; |
|
|
331 | } |
|
|
332 | |
|
|
333 | #if 0 |
|
|
334 | struct dynbuf_test_class { |
|
|
335 | dynbuf_test_class () |
|
|
336 | { |
|
|
337 | sint64 s = 0; |
|
|
338 | for (int i = 0; i < 10000000; ++i) |
|
|
339 | { |
|
|
340 | char b1[256], b2[256]; |
|
|
341 | |
|
|
342 | dynbuf_text db; |
|
|
343 | db.add (s); |
|
|
344 | db.add (char (0)); |
|
|
345 | |
|
|
346 | db.linearise (b1); |
|
|
347 | sprintf (b2, "%ld", s); |
|
|
348 | |
|
|
349 | if (strcmp (b1, b2)) |
|
|
350 | printf ("<%s,%s>\n", b1, b2); |
|
|
351 | |
|
|
352 | if (i < 20) |
|
|
353 | s = (sint64) pow (10., i); |
|
|
354 | else |
|
|
355 | s = (sint64) exp (random () * (43.6682723752766 / RAND_MAX)); |
|
|
356 | } |
|
|
357 | |
|
|
358 | exit (0); |
|
|
359 | } |
|
|
360 | } dynbuf_test; |
|
|
361 | #endif |
|
|
362 | |