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.8 by root, Mon Apr 23 18:21:54 2007 UTC vs.
Revision 1.10 by root, Thu May 3 09:26:45 2007 UTC

2 2
3#include <cstdio> 3#include <cstdio>
4 4
5dynbuf::dynbuf (int initial, int extend) 5dynbuf::dynbuf (int initial, int extend)
6{ 6{
7 ext = extend;
7 _size = 0; 8 _size = 0;
8 ext = extend;
9 9
10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial); 10 first = last = (chunk *)salloc<char> (sizeof (chunk) + initial);
11 first->alloc = sizeof (chunk) + initial; 11 first->alloc = sizeof (chunk) + initial;
12 first->next = 0; 12 first->next = 0;
13 13
14 room = initial;
15 ptr = first->data; 14 ptr = first->data;
15 end = ptr + initial;
16} 16}
17 17
18dynbuf::~dynbuf () 18dynbuf::~dynbuf ()
19{ 19{
20 clear (); 20 _clear ();
21} 21}
22 22
23void 23void
24dynbuf::clear () 24dynbuf::_clear ()
25{ 25{
26 while (first) 26 while (first)
27 { 27 {
28 chunk *next = first->next; 28 chunk *next = first->next;
29 29
30 sfree<char> ((char *)first, first->alloc); 30 sfree<char> ((char *)first, first->alloc);
31 first = next; 31 first = next;
32 } 32 }
33}
34
35void
36dynbuf::clear ()
37{
38 _clear ();
39 _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;
46 end = ptr + ext;
33} 47}
34 48
35void 49void
36dynbuf::finish () 50dynbuf::finish ()
37{ 51{
56 add->next = 0; 70 add->next = 0;
57 71
58 last->next = add; 72 last->next = add;
59 last = add; 73 last = add;
60 74
61 room = ext;
62 ptr = last->data; 75 ptr = last->data;
76 end = ptr + ext;
63} 77}
64 78
65void 79void
66dynbuf::linearise (void *data) 80dynbuf::linearise (void *data)
67{ 81{
68 char *p = (char *) data;
69
70 last->size = ptr - last->data; 82 last->size = ptr - last->data;
71 83
72 for (chunk * c = first; c; c = c->next) 84 for (chunk *c = first; c; c = c->next)
73 { 85 {
74 memcpy (p, c->data, c->size); 86 memcpy (data, c->data, c->size);
75 p += c->size; 87 data = (void *)(((char *)data) + c->size);
76 } 88 }
77} 89}
78 90
79char * 91char *
80dynbuf::linearise () 92dynbuf::linearise ()
86 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size); 98 chunk *add = (chunk *) salloc<char> (sizeof (chunk) + _size);
87 add->alloc = sizeof (chunk) + _size; 99 add->alloc = sizeof (chunk) + _size;
88 add->next = 0; 100 add->next = 0;
89 101
90 linearise ((void *)add->data); 102 linearise ((void *)add->data);
91 clear (); 103 _clear ();
92 104
93 first = last = add; 105 first = last = add;
94 ptr = last->data + _size; 106 ptr = last->data + _size;
107 end = ptr;
95 _size = 0; 108 _size = 0;
96 room = 0;
97 } 109 }
98 110
99 return first->data; 111 return first->data;
100} 112}
101 113
113 { 125 {
114 force (128); 126 force (128);
115 127
116 va_list ap; 128 va_list ap;
117 va_start (ap, format); 129 va_start (ap, format);
118 len = vsnprintf (ptr, room, format, ap); 130 len = vsnprintf (ptr, end - ptr, format, ap);
119 va_end (ap); 131 va_end (ap);
120 132
121 assert (len >= 0); // shield against broken vsnprintf's 133 assert (len >= 0); // shield against broken vsnprintf's
122 134
123 // was enough room available 135 // was enough room available
124 if (len < room) 136 if (ptr + len < end)
125 { 137 {
126 alloc (len); 138 alloc (len);
127 return; 139 return;
128 } 140 }
129 } 141 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines