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.20 by root, Thu Nov 8 19:43:27 2007 UTC vs.
Revision 1.31 by root, Fri Mar 26 01:04:45 2010 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Deliantra 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 <support@deliantra.net> 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"
24#include <cstdio> 25#include <cstdio>
25 26
26void 27void
27dynbuf::init (int initial) 28dynbuf::init (int initial)
28{ 29{
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
41// frees a full chain and sets the pointer to zero
39void 42void
40dynbuf::free (chunk *&chain) 43dynbuf::free (chunk *&chain)
41{ 44{
42 while (chain) 45 while (chain)
43 { 46 {
49} 52}
50 53
51void 54void
52dynbuf::clear () 55dynbuf::clear ()
53{ 56{
57 cextend = extend;
54 free (first->next); 58 free (first->next);
55 59
56 _size = 0; 60 _size = 0;
57 ptr = first->data; 61 ptr = first->data;
58 end = ptr + first->alloc - sizeof (chunk); 62 end = ptr + first->alloc - sizeof (chunk);
63 last = first;
59} 64}
60 65
61void 66void
62dynbuf::finalise () 67dynbuf::finalise ()
63{ 68{
70{ 75{
71 finalise (); 76 finalise ();
72 77
73 do 78 do
74 { 79 {
75 extend += extend >> 1; 80 cextend += cextend >> 1;
76 extend = (extend + 15) & ~15; 81 cextend = (cextend + 15) & ~15;
77 } 82 }
78 while (extend < size); 83 while (cextend < size);
79 84
80 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + extend); 85 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + cextend);
81 add->alloc = sizeof (chunk) + extend; 86 add->alloc = sizeof (chunk) + cextend;
82 add->next = 0; 87 add->next = 0;
83 88
84 last->next = add; 89 last->next = add;
85 last = add; 90 last = add;
86 91
87 ptr = last->data; 92 ptr = last->data;
88 end = ptr + extend; 93 end = ptr + cextend;
89} 94}
90 95
91void 96void
92dynbuf::linearise (void *data) 97dynbuf::linearise (void *data)
93{ 98{
99 data = (void *)(((char *)data) + c->size); 104 data = (void *)(((char *)data) + c->size);
100 } 105 }
101} 106}
102 107
103char * 108char *
104dynbuf::_linearise () 109dynbuf::_linearise (int extra)
105{ 110{
106 finalise (); 111 finalise ();
107 112
108 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); 113 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size + extra);
109 add->alloc = sizeof (chunk) + _size; 114 add->alloc = sizeof (chunk) + _size;
110 add->next = 0; 115 add->next = 0;
111 116
112 linearise ((void *)add->data); 117 linearise ((void *)add->data);
113 free (first); 118 free (first);
114 119
115 first = last = add; 120 first = last = add;
116 ptr = last->data + _size; 121 ptr = last->data + _size;
117 end = ptr; 122 end = ptr + extra;
118 _size = 0; 123 _size = 0;
119 124
120 return first->data; 125 return first->data;
121} 126}
122 127
123dynbuf::operator std::string () 128dynbuf::operator std::string ()
124{ 129{
125 // could optimise 130 // could optimise
126 return std::string (linearise (), size ()); 131 return std::string (linearise (), size ());
132}
133
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;
127} 157}
128 158
129void 159void
130dynbuf_text::vprintf (const char *format, va_list ap) 160dynbuf_text::vprintf (const char *format, va_list ap)
131{ 161{
163 vprintf (format, ap); 193 vprintf (format, ap);
164 va_end (ap); 194 va_end (ap);
165} 195}
166 196
167// simply return a mask with "bits" bits set 197// simply return a mask with "bits" bits set
168inline uint64 198static inline uint64
169m (int b) 199m (int b)
170{ 200{
171 return (uint64 (1) << b) - 1; 201 return (uint64 (1) << b) - 1;
172} 202}
173 203
174// convert 9 digits to ascii, using only a single multiplication 204// convert 9 digits to ascii, using only a single multiplication
175// (depending on cpu and compiler). 205// (depending on cpu and compiler).
176// will generate a single 0 as output when v=lz=0 206// will generate a single 0 as output when v=lz=0
177inline char * 207static inline char *
178i2a_9 (char *ptr, uint32 v, bool lz) 208i2a_9 (char *ptr, uint32 v, bool lz)
179{ 209{
180 // convert to 4.56 fixed-point representation 210 // convert to 4.56 fixed-point representation
181 // this should be optimal on 64 bit cpus, and rather 211 // this should be optimal on 64 bit cpus, and rather
182 // slow on 32 bit cpus. go figure :) 212 // slow on 32 bit cpus. go figure :)
225 255
226 *ptr = '-'; ptr += i < 0 ? 1 : 0; 256 *ptr = '-'; ptr += i < 0 ? 1 : 0;
227 uint32 u = i < 0 ? -i : i; 257 uint32 u = i < 0 ? -i : i;
228 258
229 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
230 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 }
231 else if (expect_true (u < 1000000000)) // 9 0's 267 else if (expect_true (u < 1000000000)) // 9 0's
232 ptr = i2a_9 (ptr, u, false); 268 ptr = i2a_9 (ptr, u, false);
233 else 269 else
234 { 270 {
235 sint32 div = u / 1000000000; 271 uint32 div = u / 1000000000;
236 uint32 rem = u % 1000000000; 272 uint32 rem = u % 1000000000;
237 273
238 ptr = i2a_9 (ptr, div, false); 274 ptr = i2a_9 (ptr, div, false);
239 ptr = i2a_9 (ptr, rem, true); 275 ptr = i2a_9 (ptr, rem, true);
240 } 276 }
252 // (#19) and two 9 digit parts (9..18 and 0..8) 288 // (#19) and two 9 digit parts (9..18 and 0..8)
253 289
254 // good compilers will only use multiplications here 290 // good compilers will only use multiplications here
255 291
256 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
257 fadd (char (u + '0')); 293 *ptr++ = u + '0';
258 else if (expect_true (u < 1000000000)) // 9 0's 294 else if (expect_true (u < 1000000000)) // 9 0's
259 ptr = i2a_9 (ptr, u, false); 295 ptr = i2a_9 (ptr, u, false);
260 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's 296 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's
261 { 297 {
262 sint32 div = u / 1000000000; 298 uint32 div = u / 1000000000;
263 uint32 rem = u % 1000000000; 299 uint32 rem = u % 1000000000;
264 300
265 ptr = i2a_9 (ptr, div, false); 301 ptr = i2a_9 (ptr, div, false);
266 ptr = i2a_9 (ptr, rem, true); 302 ptr = i2a_9 (ptr, rem, true);
267 } 303 }
268 else 304 else
269 { 305 {
270 // a biggy 306 // a biggy, split off the topmost digit
271 sint32 div = u / UINT64_C (1000000000000000000); 307 uint32 div = u / UINT64_C (1000000000000000000);
272 uint64 rem = u % UINT64_C (1000000000000000000); 308 uint64 rem = u % UINT64_C (1000000000000000000);
273 309
274 fadd (char (div + '0')); 310 *ptr++ = div + '0';
311
275 u = rem; 312 u = rem;
276 313
277 { 314 {
278 sint32 div = u / 1000000000; 315 uint32 div = u / 1000000000;
279 uint32 rem = u % 1000000000; 316 uint32 rem = u % 1000000000;
280 317
281 ptr = i2a_9 (ptr, div, true); 318 ptr = i2a_9 (ptr, div, true);
282 ptr = i2a_9 (ptr, rem, true); 319 ptr = i2a_9 (ptr, rem, true);
283 } 320 }
284 } 321 }
285} 322}
286 323
287dynbuf_text::operator const char *() 324dynbuf_text::operator char *()
288{ 325{
289 *this << '\0'; 326 *this << '\0';
290 linearise (); 327 linearise ();
291 --ptr; 328 --ptr;
292 return first->data; 329 return first->data;
299 return; 336 return;
300 337
301 *this << '(' << name; 338 *this << '(' << name;
302 339
303 const char *sep = ": "; 340 const char *sep = ": ";
304 for (int i = 0; i < NROFATTACKS; ++i) 341 for_all_bits_sparse_32 (abilities, i)
305 if (abilities & (1 << i))
306 { 342 {
307 *this << sep; sep = ", "; 343 *this << sep; sep = ", ";
308 *this << attacks [i]; 344 *this << attacks [i];
309 } 345 }
310 346
311 *this << ')'; 347 *this << ')';
312} 348}
313 349
314void 350void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines