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.6 by root, Tue Dec 26 08:54:59 2006 UTC vs.
Revision 1.8 by root, Mon Apr 23 18:21:54 2007 UTC

4 4
5dynbuf::dynbuf (int initial, int extend) 5dynbuf::dynbuf (int initial, int extend)
6{ 6{
7 _size = 0; 7 _size = 0;
8 ext = extend; 8 ext = extend;
9
9 first = last = (chunk *) new char[sizeof (chunk) + initial]; 10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
10 11 first->alloc = sizeof (chunk) + initial;
11 first->next = 0; 12 first->next = 0;
13
12 room = initial; 14 room = initial;
13 ptr = first->data; 15 ptr = first->data;
14} 16}
15 17
16dynbuf::~dynbuf () 18dynbuf::~dynbuf ()
23{ 25{
24 while (first) 26 while (first)
25 { 27 {
26 chunk *next = first->next; 28 chunk *next = first->next;
27 29
28 delete[](char *) first; 30 sfree<char> ((char *)first, first->alloc);
29 first = next; 31 first = next;
30 } 32 }
31} 33}
32 34
33void 35void
47 ext += ext >> 1; 49 ext += ext >> 1;
48 ext = (ext + 15) & ~15; 50 ext = (ext + 15) & ~15;
49 } 51 }
50 while (ext < size); 52 while (ext < size);
51 53
52 chunk *add = (chunk *) new char[sizeof (chunk) + ext]; 54 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + ext);
53 55 add->alloc = sizeof (chunk) + ext;
54 add->next = 0; 56 add->next = 0;
55 57
56 last->next = add; 58 last->next = add;
57 last = add; 59 last = add;
58 60
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;
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
99void 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
100dynbuf::add (sint32 i) 141dynbuf_text::add (sint32 i)
101{ 142{
102 char buf[max_sint32_size]; 143 char buf[max_sint32_size];
103 char *p = buf + sizeof (buf); 144 char *p = buf + sizeof (buf);
104 char neg; 145 char neg;
105 146
130 171
131 add ((void *) p, buf + sizeof (buf) - p); 172 add ((void *) p, buf + sizeof (buf) - p);
132} 173}
133 174
134void 175void
135dynbuf::add (sint64 i) 176dynbuf_text::add (sint64 i)
136{ 177{
137 if (i > -10000000 && i < 10000000) 178 if (i > -10000000 && i < 10000000)
138 { 179 {
139 add (sint32 (i)); 180 add (sint32 (i));
140 return; 181 return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines