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.16 by root, Sun Jul 1 05:00:20 2007 UTC vs.
Revision 1.35 by root, Tue Jan 3 11:25:36 2012 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Crossfire TRT is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
8 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * (at your option) any later version. 9 * option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
18 * 19 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de> 20 * The authors can be reached via e-mail to <support@deliantra.net>
20 */ 21 */
21 22
22#include "global.h" 23#include "global.h"
23 24
24#include <cstdio> 25#include <cstdio>
25 26
26dynbuf::dynbuf (int initial, int extend) 27void
28dynbuf::init (int initial)
27{ 29{
28 ext = extend; 30 cextend = extend;
29 _size = 0; 31 _size = 0;
30 32
31 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial); 33 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
32 first->alloc = sizeof (chunk) + initial; 34 first->alloc = sizeof (chunk) + initial;
33 first->next = 0; 35 first->next = 0;
34 36
35 ptr = first->data; 37 ptr = first->data;
36 end = ptr + initial; 38 end = ptr + initial;
37} 39}
38 40
39dynbuf::~dynbuf () 41// frees a full chain and sets the pointer to zero
40{
41 _clear ();
42}
43
44void 42void
45dynbuf::_clear () 43dynbuf::free (chunk *&chain)
46{ 44{
47 while (first) 45 while (chain)
48 { 46 {
49 chunk *next = first->next; 47 chunk *next = chain->next;
50 48
51 sfree<char> ((char *)first, first->alloc); 49 sfree<char> ((char *)chain, chain->alloc);
52 first = next; 50 chain = next;
53 } 51 }
54} 52}
55 53
56void 54void
57dynbuf::clear () 55dynbuf::clear ()
58{ 56{
59 _clear (); 57 cextend = extend;
58 free (first->next);
59
60 _size = 0; 60 _size = 0;
61
62 first = last = (chunk *)salloc<char> (sizeof (chunk) + ext);
63 first->alloc = sizeof (chunk) + ext;
64 first->next = 0;
65
66 ptr = first->data; 61 ptr = first->data;
67 end = ptr + ext; 62 end = ptr + first->alloc - sizeof (chunk);
63 last = first;
68} 64}
69 65
70void 66void
71dynbuf::finish () 67dynbuf::finalise ()
72{ 68{
73 // finalise current chunk 69 // finalise current chunk
74 _size += last->size = ptr - last->data; 70 _size += last->size = ptr - last->data;
75} 71}
76 72
77void 73void
78dynbuf::_reserve (int size) 74dynbuf::reserve (int size)
79{ 75{
80 finish (); 76 finalise ();
81 77
82 do 78 do
83 { 79 {
84 ext += ext >> 1; 80 cextend += cextend >> 1;
85 ext = (ext + 15) & ~15; 81 cextend = (cextend + 15) & ~15;
86 } 82 }
87 while (ext < size); 83 while (cextend < size);
88 84
89 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext); 85 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + cextend);
90 add->alloc = sizeof (chunk) + ext; 86 add->alloc = sizeof (chunk) + cextend;
91 add->next = 0; 87 add->next = 0;
92 88
93 last->next = add; 89 last->next = add;
94 last = add; 90 last = add;
95 91
96 ptr = last->data; 92 ptr = last->data;
97 end = ptr + ext; 93 end = ptr + cextend;
98} 94}
99 95
100void 96void
101dynbuf::linearise (void *data) 97dynbuf::linearise (void *data)
102{ 98{
108 data = (void *)(((char *)data) + c->size); 104 data = (void *)(((char *)data) + c->size);
109 } 105 }
110} 106}
111 107
112char * 108char *
113dynbuf::linearise () 109dynbuf::_linearise (int extra)
114{ 110{
115 if (first->next) 111 finalise ();
116 {
117 finish ();
118 112
119 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); 113 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size + extra);
120 add->alloc = sizeof (chunk) + _size; 114 add->alloc = sizeof (chunk) + _size;
121 add->next = 0; 115 add->next = 0;
122 116
123 linearise ((void *)add->data); 117 linearise ((void *)add->data);
124 _clear (); 118 free (first);
125 119
126 first = last = add; 120 first = last = add;
127 ptr = last->data + _size; 121 ptr = last->data + _size;
128 end = ptr; 122 end = ptr + extra;
129 _size = 0; 123 _size = 0;
130 }
131 124
132 return first->data; 125 return first->data;
133} 126}
134 127
135dynbuf::operator std::string () 128dynbuf::operator std::string ()
137 // could optimise 130 // could optimise
138 return std::string (linearise (), size ()); 131 return std::string (linearise (), size ());
139} 132}
140 133
141void 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
142dynbuf_text::printf (const char *format, ...) 160dynbuf_text::vprintf (const char *format, va_list ap)
143{ 161{
144 int len; 162 int len;
145 163
146 { 164 {
147 force (128); 165 force (128);
148 166
149 va_list ap; 167 va_list apc;
150 va_start (ap, format); 168 va_copy (apc, ap);
151 len = vsnprintf (ptr, end - ptr, format, ap); 169 len = vsnprintf (ptr, end - ptr, format, apc);
152 va_end (ap); 170 va_end (apc);
153 171
154 assert (len >= 0); // shield against broken vsnprintf's 172 assert (len >= 0); // shield against broken vsnprintf's
155 173
156 // was enough room available 174 // was enough room available
157 if (ptr + len < end) 175 if (ptr + len < end)
160 return; 178 return;
161 } 179 }
162 } 180 }
163 181
164 // longer, try harder 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{
165 va_list ap; 191 va_list ap;
166 va_start (ap, format); 192 va_start (ap, format);
167 vsnprintf (force (len + 1), len + 1, format, ap); 193 vprintf (format, ap);
168 va_end (ap); 194 va_end (ap);
169
170 ptr += len;
171} 195}
172 196
173// simply return a mask with "bits" bits set 197// simply return a mask with "bits" bits set
174inline uint64 198static inline uint64
175m (int b) 199m (int b)
176{ 200{
177 return (uint64 (1) << b) - 1; 201 return (uint64 (1) << b) - 1;
178} 202}
179 203
180// convert 9 digits to ascii, using only a single multiplication 204// convert 9 digits to ascii, using only a single multiplication
181// (depending on cpu and compiler). 205// (depending on cpu and compiler).
182// will generate a single 0 as output when v=lz=0 206// will generate a single 0 as output when v=lz=0
183inline char * 207static inline char *
184i2a_9 (char *ptr, uint32 v, bool lz) 208i2a_9 (char *ptr, uint32 v, bool lz)
185{ 209{
186 // convert to 4.56 fixed-point representation 210 // convert to 4.56 fixed-point representation
187 // this should be optimal on 64 bit cpus, and rather 211 // this should be optimal on 64 bit cpus, and rather
188 // slow on 32 bit cpus. go figure :) 212 // slow on 32 bit cpus. go figure :)
231 255
232 *ptr = '-'; ptr += i < 0 ? 1 : 0; 256 *ptr = '-'; ptr += i < 0 ? 1 : 0;
233 uint32 u = i < 0 ? -i : i; 257 uint32 u = i < 0 ? -i : i;
234 258
235 if (expect_true (u < 10)) // we have a lot of single-digit numbers, so optimise 259 if (expect_true (u < 10)) // we have a lot of single-digit numbers, so optimise
236 fadd (char (u + '0')); 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 }
237 else if (expect_true (u < 1000000000)) // 9 0's 267 else if (expect_true (u < 1000000000)) // 9 0's
238 ptr = i2a_9 (ptr, u, false); 268 ptr = i2a_9 (ptr, u, false);
239 else 269 else
240 { 270 {
241 sint32 div = u / 1000000000; 271 uint32 div = u / 1000000000;
242 uint32 rem = u % 1000000000; 272 uint32 rem = u % 1000000000;
243 273
244 ptr = i2a_9 (ptr, div, false); 274 ptr = i2a_9 (ptr, div, false);
245 ptr = i2a_9 (ptr, rem, true); 275 ptr = i2a_9 (ptr, rem, true);
246 } 276 }
258 // (#19) and two 9 digit parts (9..18 and 0..8) 288 // (#19) and two 9 digit parts (9..18 and 0..8)
259 289
260 // good compilers will only use multiplications here 290 // good compilers will only use multiplications here
261 291
262 if (u < 10) // we have a lot of single-digit numbers, so optimise 292 if (u < 10) // we have a lot of single-digit numbers, so optimise
263 fadd (char (u + '0')); 293 *ptr++ = u + '0';
264 else if (expect_true (u < 1000000000)) // 9 0's 294 else if (expect_true (u < 1000000000)) // 9 0's
265 ptr = i2a_9 (ptr, u, false); 295 ptr = i2a_9 (ptr, u, false);
266 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's 296 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's
267 { 297 {
268 sint32 div = u / 1000000000; 298 uint32 div = u / 1000000000;
269 uint32 rem = u % 1000000000; 299 uint32 rem = u % 1000000000;
270 300
271 ptr = i2a_9 (ptr, div, false); 301 ptr = i2a_9 (ptr, div, false);
272 ptr = i2a_9 (ptr, rem, true); 302 ptr = i2a_9 (ptr, rem, true);
273 } 303 }
274 else 304 else
275 { 305 {
276 // a biggy 306 // a biggy, split off the topmost digit
277 sint32 div = u / UINT64_C (1000000000000000000); 307 uint32 div = u / UINT64_C (1000000000000000000);
278 uint64 rem = u % UINT64_C (1000000000000000000); 308 uint64 rem = u % UINT64_C (1000000000000000000);
279 309
280 fadd (char (div + '0')); 310 *ptr++ = div + '0';
311
281 u = rem; 312 u = rem;
282 313
283 { 314 {
284 sint32 div = u / 1000000000; 315 uint32 div = u / 1000000000;
285 uint32 rem = u % 1000000000; 316 uint32 rem = u % 1000000000;
286 317
287 ptr = i2a_9 (ptr, div, true); 318 ptr = i2a_9 (ptr, div, true);
288 ptr = i2a_9 (ptr, rem, true); 319 ptr = i2a_9 (ptr, rem, true);
289 } 320 }
290 } 321 }
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)
336 return;
337
338 *this << '(' << name;
339
340 const char *sep = ": ";
341 for_all_bits_sparse_32 (abilities, i)
342 {
343 *this << sep; sep = ", ";
344 *this << attacks [i];
345 }
346
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))
361 {
362 *this << sep; sep = ", ";
363 *this << spellpathnames [i];
364 }
365
366 *this << ')';
291} 367}
292 368
293#if 0 369#if 0
294struct dynbuf_test_class { 370struct dynbuf_test_class {
295 dynbuf_test_class () 371 dynbuf_test_class ()
317 393
318 exit (0); 394 exit (0);
319 } 395 }
320} dynbuf_test; 396} dynbuf_test;
321#endif 397#endif
398

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines