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.4 by root, Sat Sep 2 16:37:58 2006 UTC vs.
Revision 1.8 by root, Mon Apr 23 18:21:54 2007 UTC

1#define __STDC_CONSTANT_MACROS
2#include <stdint.h>
3
4#ifndef UINT64_C
5# error missing c99 support, define UINT64_C yourself
6# define UINT64_C(c) c ## LL
7#endif
8
9#include "global.h" 1#include "global.h"
10 2
11#include <cstdio> 3#include <cstdio>
12 4
13dynbuf::dynbuf (int initial, int extend) 5dynbuf::dynbuf (int initial, int extend)
14{ 6{
15 _size = 0; 7 _size = 0;
16 ext = extend; 8 ext = extend;
9
17 first = last = (chunk *)new char [sizeof (chunk) + initial]; 10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
11 first->alloc = sizeof (chunk) + initial;
18 first->next = 0; 12 first->next = 0;
13
19 room = initial; 14 room = initial;
20 ptr = first->data; 15 ptr = first->data;
21} 16}
22 17
23dynbuf::~dynbuf () 18dynbuf::~dynbuf ()
24{ 19{
25 clear (); 20 clear ();
26} 21}
27 22
23void
28void dynbuf::clear () 24dynbuf::clear ()
29{ 25{
30 while (first) 26 while (first)
31 { 27 {
32 chunk *next = first->next; 28 chunk *next = first->next;
33 delete [] (char *)first; 29
30 sfree<char> ((char *)first, first->alloc);
34 first = next; 31 first = next;
35 } 32 }
36} 33}
37 34
35void
38void dynbuf::finish () 36dynbuf::finish ()
39{ 37{
40 // finalise current chunk 38 // finalise current chunk
41 _size += last->size = ptr - last->data; 39 _size += last->size = ptr - last->data;
42} 40}
43 41
42void
44void dynbuf::_reserve (int size) 43dynbuf::_reserve (int size)
45{ 44{
46 finish (); 45 finish ();
47 46
48 do 47 do
49 { 48 {
50 ext += ext >> 1; 49 ext += ext >> 1;
51 ext = (ext + 15) & ~15; 50 ext = (ext + 15) & ~15;
52 } 51 }
53 while (ext < size); 52 while (ext < size);
54 53
55 chunk *add = (chunk *)new char [sizeof (chunk) + ext]; 54 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext);
55 add->alloc = sizeof (chunk) + ext;
56 add->next = 0; 56 add->next = 0;
57 57
58 last->next = add; 58 last->next = add;
59 last = add; 59 last = add;
60 60
61 room = ext; 61 room = ext;
62 ptr = last->data; 62 ptr = last->data;
63} 63}
64 64
65void
65void dynbuf::linearise (void *data) 66dynbuf::linearise (void *data)
66{ 67{
67 char *p = (char *)data; 68 char *p = (char *) data;
68 69
69 last->size = ptr - last->data; 70 last->size = ptr - last->data;
70 71
71 for (chunk *c = first; c; c = c->next) 72 for (chunk * c = first; c; c = c->next)
72 { 73 {
73 memcpy (p, c->data, c->size); 74 memcpy (p, c->data, c->size);
74 p += c->size; 75 p += c->size;
75 } 76 }
76} 77}
77 78
79char *
78char *dynbuf::linearise () 80dynbuf::linearise ()
79{ 81{
80 if (first->next) 82 if (first->next)
81 { 83 {
82 finish (); 84 finish ();
83 85
84 chunk *add = (chunk *)new char [sizeof (chunk) + _size]; 86 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size);
85 87 add->alloc = sizeof (chunk) + _size;
86 add->next = 0; 88 add->next = 0;
89
87 linearise ((void *)add->data); 90 linearise ((void *)add->data);
88 clear (); 91 clear ();
89 92
90 first = last = add; 93 first = last = add;
91 ptr = last->data + _size; 94 ptr = last->data + _size;
92 _size = 0; 95 _size = 0;
93 room = 0; 96 room = 0;
94 } 97 }
95 98
96 return first->data; 99 return first->data;
97} 100}
98 101
102dynbuf::operator std::string ()
103{
104 // could optimise
105 return std::string (linearise (), size ());
106}
107
108void
109dynbuf_text::printf (const char *format, ...)
110{
111 int len;
112
113 {
114 force (128);
115
116 va_list ap;
117 va_start (ap, format);
118 len = vsnprintf (ptr, room, format, ap);
119 va_end (ap);
120
121 assert (len >= 0); // shield against broken vsnprintf's
122
123 // was enough room available
124 if (len < room)
125 {
126 alloc (len);
127 return;
128 }
129 }
130
131 // longer, try harder
132 va_list ap;
133 va_start (ap, format);
134 vsnprintf (force (len + 1), len + 1, format, ap);
135 va_end (ap);
136
137 alloc (len);
138}
139
140void
99void dynbuf::add (sint32 i) 141dynbuf_text::add (sint32 i)
100{ 142{
101 char buf [max_sint32_size]; 143 char buf[max_sint32_size];
102 char *p = buf + sizeof (buf); 144 char *p = buf + sizeof (buf);
103 char neg; 145 char neg;
104 146
105 uint32 val; 147 uint32 val;
106 148
117 159
118 do 160 do
119 { 161 {
120 uint32 div = val / 10; 162 uint32 div = val / 10;
121 *--p = '0' + char (val - div * 10); 163 *--p = '0' + char (val - div * 10);
164
122 val = div; 165 val = div;
123 } 166 }
124 while (val); 167 while (val);
125 168
126 if (neg) 169 if (neg)
127 *--p = neg; 170 *--p = neg;
128 171
129 add ((void *)p, buf + sizeof (buf) - p); 172 add ((void *) p, buf + sizeof (buf) - p);
130} 173}
131 174
175void
132void dynbuf::add (sint64 i) 176dynbuf_text::add (sint64 i)
133{ 177{
134 if (i > -10000000 && i < 10000000) 178 if (i > -10000000 && i < 10000000)
135 { 179 {
136 add (sint32 (i)); 180 add (sint32 (i));
137 return; 181 return;
138 } 182 }
139 183
140 char buf [max_sint64_size]; 184 char buf[max_sint64_size];
141 char *p = buf + sizeof (buf); 185 char *p = buf + sizeof (buf);
142 char neg; 186 char neg;
143 187
144 uint64 val; 188 uint64 val;
145 189
156 200
157 do 201 do
158 { 202 {
159 uint64 div = val / 10; 203 uint64 div = val / 10;
160 *--p = '0' + char (val - div * 10); 204 *--p = '0' + char (val - div * 10);
205
161 val = div; 206 val = div;
162 } 207 }
163 while (val); 208 while (val);
164 209
165 if (neg) 210 if (neg)
166 *--p = neg; 211 *--p = neg;
167 212
168 add ((void *)p, buf + sizeof (buf) - p); 213 add ((void *) p, buf + sizeof (buf) - p);
169} 214}
170

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines