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.25 by root, Thu Oct 15 21:09:32 2009 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,2008 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 it under 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 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 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version. 9 * option) any later version.
104 data = (void *)(((char *)data) + c->size); 104 data = (void *)(((char *)data) + c->size);
105 } 105 }
106} 106}
107 107
108char * 108char *
109dynbuf::_linearise () 109dynbuf::_linearise (int extra)
110{ 110{
111 finalise (); 111 finalise ();
112 112
113 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); 113 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size + extra);
114 add->alloc = sizeof (chunk) + _size; 114 add->alloc = sizeof (chunk) + _size;
115 add->next = 0; 115 add->next = 0;
116 116
117 linearise ((void *)add->data); 117 linearise ((void *)add->data);
118 free (first); 118 free (first);
119 119
120 first = last = add; 120 first = last = add;
121 ptr = last->data + _size; 121 ptr = last->data + _size;
122 end = ptr; 122 end = ptr + extra;
123 _size = 0; 123 _size = 0;
124 124
125 return first->data; 125 return first->data;
126} 126}
127 127
128dynbuf::operator std::string () 128dynbuf::operator std::string ()
129{ 129{
130 // could optimise 130 // could optimise
131 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;
132} 157}
133 158
134void 159void
135dynbuf_text::vprintf (const char *format, va_list ap) 160dynbuf_text::vprintf (const char *format, va_list ap)
136{ 161{
168 vprintf (format, ap); 193 vprintf (format, ap);
169 va_end (ap); 194 va_end (ap);
170} 195}
171 196
172// simply return a mask with "bits" bits set 197// simply return a mask with "bits" bits set
173inline uint64 198static inline uint64
174m (int b) 199m (int b)
175{ 200{
176 return (uint64 (1) << b) - 1; 201 return (uint64 (1) << b) - 1;
177} 202}
178 203
179// convert 9 digits to ascii, using only a single multiplication 204// convert 9 digits to ascii, using only a single multiplication
180// (depending on cpu and compiler). 205// (depending on cpu and compiler).
181// will generate a single 0 as output when v=lz=0 206// will generate a single 0 as output when v=lz=0
182inline char * 207static inline char *
183i2a_9 (char *ptr, uint32 v, bool lz) 208i2a_9 (char *ptr, uint32 v, bool lz)
184{ 209{
185 // convert to 4.56 fixed-point representation 210 // convert to 4.56 fixed-point representation
186 // this should be optimal on 64 bit cpus, and rather 211 // this should be optimal on 64 bit cpus, and rather
187 // slow on 32 bit cpus. go figure :) 212 // slow on 32 bit cpus. go figure :)
230 255
231 *ptr = '-'; ptr += i < 0 ? 1 : 0; 256 *ptr = '-'; ptr += i < 0 ? 1 : 0;
232 uint32 u = i < 0 ? -i : i; 257 uint32 u = i < 0 ? -i : i;
233 258
234 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
235 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 }
236 else if (expect_true (u < 1000000000)) // 9 0's 267 else if (expect_true (u < 1000000000)) // 9 0's
237 ptr = i2a_9 (ptr, u, false); 268 ptr = i2a_9 (ptr, u, false);
238 else 269 else
239 { 270 {
240 sint32 div = u / 1000000000; 271 uint32 div = u / 1000000000;
241 uint32 rem = u % 1000000000; 272 uint32 rem = u % 1000000000;
242 273
243 ptr = i2a_9 (ptr, div, false); 274 ptr = i2a_9 (ptr, div, false);
244 ptr = i2a_9 (ptr, rem, true); 275 ptr = i2a_9 (ptr, rem, true);
245 } 276 }
257 // (#19) and two 9 digit parts (9..18 and 0..8) 288 // (#19) and two 9 digit parts (9..18 and 0..8)
258 289
259 // good compilers will only use multiplications here 290 // good compilers will only use multiplications here
260 291
261 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
262 fadd (char (u + '0')); 293 *ptr++ = u + '0';
263 else if (expect_true (u < 1000000000)) // 9 0's 294 else if (expect_true (u < 1000000000)) // 9 0's
264 ptr = i2a_9 (ptr, u, false); 295 ptr = i2a_9 (ptr, u, false);
265 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's 296 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's
266 { 297 {
267 sint32 div = u / 1000000000; 298 uint32 div = u / 1000000000;
268 uint32 rem = u % 1000000000; 299 uint32 rem = u % 1000000000;
269 300
270 ptr = i2a_9 (ptr, div, false); 301 ptr = i2a_9 (ptr, div, false);
271 ptr = i2a_9 (ptr, rem, true); 302 ptr = i2a_9 (ptr, rem, true);
272 } 303 }
273 else 304 else
274 { 305 {
275 // a biggy 306 // a biggy, split off the topmost digit
276 sint32 div = u / UINT64_C (1000000000000000000); 307 uint32 div = u / UINT64_C (1000000000000000000);
277 uint64 rem = u % UINT64_C (1000000000000000000); 308 uint64 rem = u % UINT64_C (1000000000000000000);
278 309
279 fadd (char (div + '0')); 310 *ptr++ = div + '0';
311
280 u = rem; 312 u = rem;
281 313
282 { 314 {
283 sint32 div = u / 1000000000; 315 uint32 div = u / 1000000000;
284 uint32 rem = u % 1000000000; 316 uint32 rem = u % 1000000000;
285 317
286 ptr = i2a_9 (ptr, div, true); 318 ptr = i2a_9 (ptr, div, true);
287 ptr = i2a_9 (ptr, rem, true); 319 ptr = i2a_9 (ptr, rem, true);
288 } 320 }
304 return; 336 return;
305 337
306 *this << '(' << name; 338 *this << '(' << name;
307 339
308 const char *sep = ": "; 340 const char *sep = ": ";
309 for (int i = 0; i < NROFATTACKS; ++i) 341 for_all_bits_sparse_32 (abilities, i)
310 if (abilities & (1 << i))
311 { 342 {
312 *this << sep; sep = ", "; 343 *this << sep; sep = ", ";
313 *this << attacks [i]; 344 *this << attacks [i];
314 } 345 }
315 346
316 *this << ')'; 347 *this << ')';
317} 348}
318 349
319void 350void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines