ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/dynbuf.C
(Generate patch)

Comparing deliantra/server/server/dynbuf.C (file contents):
Revision 1.1 by root, Thu Aug 31 18:05:03 2006 UTC vs.
Revision 1.31 by root, Fri Mar 26 01:04:45 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines