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.12 by root, Sun May 27 22:57:43 2007 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{
7 ext = extend; 30 cextend = extend;
8 _size = 0; 31 _size = 0;
9 32
10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial); 33 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
11 first->alloc = sizeof (chunk) + initial; 34 first->alloc = sizeof (chunk) + initial;
12 first->next = 0; 35 first->next = 0;
13 36
14 ptr = first->data; 37 ptr = first->data;
15 end = ptr + initial; 38 end = ptr + initial;
16} 39}
17 40
18dynbuf::~dynbuf () 41// frees a full chain and sets the pointer to zero
19{
20 _clear ();
21}
22
23void 42void
24dynbuf::_clear () 43dynbuf::free (chunk *&chain)
25{ 44{
26 while (first) 45 while (chain)
27 { 46 {
28 chunk *next = first->next; 47 chunk *next = chain->next;
29 48
30 sfree<char> ((char *)first, first->alloc); 49 sfree<char> ((char *)chain, chain->alloc);
31 first = next; 50 chain = next;
32 } 51 }
33} 52}
34 53
35void 54void
36dynbuf::clear () 55dynbuf::clear ()
37{ 56{
38 _clear (); 57 cextend = extend;
58 free (first->next);
59
39 _size = 0; 60 _size = 0;
40
41 first = last = (chunk *)salloc<char> (sizeof (chunk) + ext);
42 first->alloc = sizeof (chunk) + ext;
43 first->next = 0;
44
45 ptr = first->data; 61 ptr = first->data;
46 end = ptr + ext; 62 end = ptr + first->alloc - sizeof (chunk);
63 last = first;
47} 64}
48 65
49void 66void
50dynbuf::finish () 67dynbuf::finalise ()
51{ 68{
52 // finalise current chunk 69 // finalise current chunk
53 _size += last->size = ptr - last->data; 70 _size += last->size = ptr - last->data;
54} 71}
55 72
56void 73void
57dynbuf::_reserve (int size) 74dynbuf::reserve (int size)
58{ 75{
59 finish (); 76 finalise ();
60 77
61 do 78 do
62 { 79 {
63 ext += ext >> 1; 80 cextend += cextend >> 1;
64 ext = (ext + 15) & ~15; 81 cextend = (cextend + 15) & ~15;
65 } 82 }
66 while (ext < size); 83 while (cextend < size);
67 84
68 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext); 85 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + cextend);
69 add->alloc = sizeof (chunk) + ext; 86 add->alloc = sizeof (chunk) + cextend;
70 add->next = 0; 87 add->next = 0;
71 88
72 last->next = add; 89 last->next = add;
73 last = add; 90 last = add;
74 91
75 ptr = last->data; 92 ptr = last->data;
76 end = ptr + ext; 93 end = ptr + cextend;
77} 94}
78 95
79void 96void
80dynbuf::linearise (void *data) 97dynbuf::linearise (void *data)
81{ 98{
87 data = (void *)(((char *)data) + c->size); 104 data = (void *)(((char *)data) + c->size);
88 } 105 }
89} 106}
90 107
91char * 108char *
92dynbuf::linearise () 109dynbuf::_linearise (int extra)
93{ 110{
94 if (first->next) 111 finalise ();
95 {
96 finish ();
97 112
98 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); 113 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size + extra);
99 add->alloc = sizeof (chunk) + _size; 114 add->alloc = sizeof (chunk) + _size;
100 add->next = 0; 115 add->next = 0;
101 116
102 linearise ((void *)add->data); 117 linearise ((void *)add->data);
103 _clear (); 118 free (first);
104 119
105 first = last = add; 120 first = last = add;
106 ptr = last->data + _size; 121 ptr = last->data + _size;
107 end = ptr; 122 end = ptr + extra;
108 _size = 0; 123 _size = 0;
109 }
110 124
111 return first->data; 125 return first->data;
112} 126}
113 127
114dynbuf::operator std::string () 128dynbuf::operator std::string ()
116 // could optimise 130 // could optimise
117 return std::string (linearise (), size ()); 131 return std::string (linearise (), size ());
118} 132}
119 133
120void 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
121dynbuf_text::printf (const char *format, ...) 160dynbuf_text::vprintf (const char *format, va_list ap)
122{ 161{
123 int len; 162 int len;
124 163
125 { 164 {
126 force (128); 165 force (128);
127 166
128 va_list ap; 167 va_list apc;
129 va_start (ap, format); 168 va_copy (apc, ap);
130 len = vsnprintf (ptr, end - ptr, format, ap); 169 len = vsnprintf (ptr, end - ptr, format, apc);
131 va_end (ap); 170 va_end (apc);
132 171
133 assert (len >= 0); // shield against broken vsnprintf's 172 assert (len >= 0); // shield against broken vsnprintf's
134 173
135 // was enough room available 174 // was enough room available
136 if (ptr + len < end) 175 if (ptr + len < end)
137 { 176 {
138 alloc (len); 177 ptr += len;
139 return; 178 return;
140 } 179 }
141 } 180 }
142 181
143 // 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{
144 va_list ap; 191 va_list ap;
145 va_start (ap, format); 192 va_start (ap, format);
146 vsnprintf (force (len + 1), len + 1, format, ap); 193 vprintf (format, ap);
147 va_end (ap); 194 va_end (ap);
148
149 alloc (len);
150} 195}
151 196
152// simply return a mask with "bits" bits set 197// simply return a mask with "bits" bits set
153inline uint64 198static inline uint64
154m (int b) 199m (int b)
155{ 200{
156 return (uint64 (1) << b) - 1; 201 return (uint64 (1) << b) - 1;
157} 202}
158 203
159// convert 9 digits to ascii, using only a single multiplication 204// convert 9 digits to ascii, using only a single multiplication
160// (depending on cpu and compiler). 205// (depending on cpu and compiler).
161// will generate a single 0 as output when v=lz=0 206// will generate a single 0 as output when v=lz=0
162inline char * 207static inline char *
163i2a_9 (char *ptr, uint32 v, bool lz) 208i2a_9 (char *ptr, uint32 v, bool lz)
164{ 209{
165 // convert to 4.56 fixed-point representation 210 // convert to 4.56 fixed-point representation
166 // this should be optimal on 64 bit cpus, and rather 211 // this should be optimal on 64 bit cpus, and rather
167 // slow on 32 bit cpus. go figure :) 212 // slow on 32 bit cpus. go figure :)
204} 249}
205 250
206void 251void
207dynbuf_text::add (sint32 i) 252dynbuf_text::add (sint32 i)
208{ 253{
209 force (11); // 10 digits + '-' 254 force (sint32_digits);
210 255
211 *ptr = '-'; ptr += i < 0 ? 1 : 0; 256 *ptr = '-'; ptr += i < 0 ? 1 : 0;
212 uint32 u = i < 0 ? -i : i; 257 uint32 u = i < 0 ? -i : i;
213 258
214 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
215 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 }
216 else if (expect_true (u < 1000000000)) // 9 0's 267 else if (expect_true (u < 1000000000)) // 9 0's
217 ptr = i2a_9 (ptr, u, false); 268 ptr = i2a_9 (ptr, u, false);
218 else 269 else
219 { 270 {
220 sint32 div = u / 1000000000; 271 uint32 div = u / 1000000000;
221 uint32 rem = u % 1000000000; 272 uint32 rem = u % 1000000000;
222 273
223 ptr = i2a_9 (ptr, div, false); 274 ptr = i2a_9 (ptr, div, false);
224 ptr = i2a_9 (ptr, rem, true); 275 ptr = i2a_9 (ptr, rem, true);
225 } 276 }
226} 277}
227 278
228void 279void
229dynbuf_text::add (sint64 i) 280dynbuf_text::add (sint64 i)
230{ 281{
231 force (20); // 19 digits + '-' 282 force (sint64_digits);
232 283
233 *ptr = '-'; ptr += i < 0 ? 1 : 0; 284 *ptr = '-'; ptr += i < 0 ? 1 : 0;
234 uint64 u = i < 0 ? -i : i; 285 uint64 u = i < 0 ? -i : i;
235 286
236 // split the number into a 1-digit part 287 // split the number into a 1-digit part
237 // (#19) and two 9 digit parts (9..18 and 0..8) 288 // (#19) and two 9 digit parts (9..18 and 0..8)
238 289
239 // good compilers will only use multiplications here 290 // good compilers will only use multiplications here
240 291
241 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
242 fadd (char (u + '0')); 293 *ptr++ = u + '0';
243 else if (expect_true (u < 1000000000)) // 9 0's 294 else if (expect_true (u < 1000000000)) // 9 0's
244 ptr = i2a_9 (ptr, u, false); 295 ptr = i2a_9 (ptr, u, false);
245 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's 296 else if (expect_true (u < UINT64_C (1000000000000000000))) // 18 0's
246 { 297 {
247 sint32 div = u / 1000000000; 298 uint32 div = u / 1000000000;
248 uint32 rem = u % 1000000000; 299 uint32 rem = u % 1000000000;
249 300
250 ptr = i2a_9 (ptr, div, false); 301 ptr = i2a_9 (ptr, div, false);
251 ptr = i2a_9 (ptr, rem, true); 302 ptr = i2a_9 (ptr, rem, true);
252 } 303 }
253 else 304 else
254 { 305 {
255 // a biggy 306 // a biggy, split off the topmost digit
256 sint32 div = u / UINT64_C (1000000000000000000); 307 uint32 div = u / UINT64_C (1000000000000000000);
257 uint64 rem = u % UINT64_C (1000000000000000000); 308 uint64 rem = u % UINT64_C (1000000000000000000);
258 309
259 fadd (char (div + '0')); 310 *ptr++ = div + '0';
311
260 u = rem; 312 u = rem;
261 313
262 { 314 {
263 sint32 div = u / 1000000000; 315 uint32 div = u / 1000000000;
264 uint32 rem = u % 1000000000; 316 uint32 rem = u % 1000000000;
265 317
266 ptr = i2a_9 (ptr, div, true); 318 ptr = i2a_9 (ptr, div, true);
267 ptr = i2a_9 (ptr, rem, true); 319 ptr = i2a_9 (ptr, rem, true);
268 } 320 }
269 } 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 << ')';
270} 367}
271 368
272#if 0 369#if 0
273struct dynbuf_test_class { 370struct dynbuf_test_class {
274 dynbuf_test_class () 371 dynbuf_test_class ()
296 393
297 exit (0); 394 exit (0);
298 } 395 }
299} dynbuf_test; 396} dynbuf_test;
300#endif 397#endif
398

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines