equal
deleted
inserted
replaced
24 } |
24 } |
25 |
25 |
26 void NetworkBufferBase::resize (size_t item_size) { |
26 void NetworkBufferBase::resize (size_t item_size) { |
27 size_t new_size = size; |
27 size_t new_size = size; |
28 |
28 |
29 // make sure that new_size isn't zero, because zero times two is zero, even if you do that in an infinite loop :) |
29 // make sure that new_size isn't zero, because zero times two is zero, even if you try and calculate that in an |
|
30 // infinite loop :) |
30 if (new_size == 0) |
31 if (new_size == 0) |
31 new_size = 1; |
32 new_size = 1; |
32 |
33 |
33 // grow new_size until item_size fits |
34 // grow new_size until item_size fits |
34 while (offset + item_size > new_size) |
35 while (offset + item_size > new_size) |
35 new_size *= 2; |
36 new_size *= 2; |
36 |
37 |
37 // grow if needed |
38 // grow if needed |
38 if (new_size != size) { |
39 if (new_size != size) { |
|
40 char *temp = buf; |
|
41 |
39 // realloc buffer |
42 // realloc buffer |
40 if ((buf = (char *) realloc((void *) buf, new_size)) == NULL) |
43 if ((buf = (char *) realloc((void *) buf, new_size)) == NULL) { |
|
44 // restore temp, but the buffer may be corrupted now |
|
45 buf = temp; |
|
46 |
|
47 // raise error |
41 throw NetworkBufferError("realloc failed"); |
48 throw NetworkBufferError("realloc failed"); |
|
49 } |
42 |
50 |
43 // update size |
51 // update size |
44 size = new_size; |
52 size = new_size; |
45 |
53 |
46 } else if (new_size > (offset + item_size) * 4) { |
54 } else if (size > (offset + item_size) * 4) { |
47 // XXX: shrink? |
55 // XXX: shrink? |
48 } |
56 } |
49 } |
57 } |
50 |
58 |
51 void NetworkBufferBase::trim (size_t prefix_size) { |
59 void NetworkBufferBase::trim (size_t prefix_size) { |
174 return (offset > 0); |
182 return (offset > 0); |
175 } |
183 } |
176 |
184 |
177 bool NetworkBufferOutput::write_prefix (char *buf, uint16_t prefix) { |
185 bool NetworkBufferOutput::write_prefix (char *buf, uint16_t prefix) { |
178 uint16_t nval = htons(prefix); |
186 uint16_t nval = htons(prefix); |
179 |
187 |
|
188 // XXX: not exception-safe |
180 push_write((char*) &nval, sizeof(uint16_t)); |
189 push_write((char*) &nval, sizeof(uint16_t)); |
181 push_write(buf, prefix); |
190 push_write(buf, prefix); |
182 |
191 |
183 // anything in the buffer? |
192 // anything in the buffer? |
184 return (offset > 0); |
193 return (offset > 0); |
185 } |
194 } |
186 |
195 |
187 bool NetworkBufferOutput::write_prefix (char *buf, uint32_t prefix) { |
196 bool NetworkBufferOutput::write_prefix (char *buf, uint32_t prefix) { |
188 uint32_t nval = htonl(prefix); |
197 uint32_t nval = htonl(prefix); |
189 |
198 |
|
199 // XXX: not exception-safe |
190 push_write((char*) &nval, sizeof(uint32_t)); |
200 push_write((char*) &nval, sizeof(uint32_t)); |
191 push_write(buf, prefix); |
201 push_write(buf, prefix); |
192 |
202 |
193 // anything in the buffer? |
203 // anything in the buffer? |
194 return (offset > 0); |
204 return (offset > 0); |