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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines