|
Lines 18-23
Link Here
|
| 18 |
#include "buffer.h" |
18 |
#include "buffer.h" |
| 19 |
#include "log.h" |
19 |
#include "log.h" |
| 20 |
|
20 |
|
|
|
21 |
#define BUFFER_MAX_CHUNK 0x100000 |
| 22 |
#define BUFFER_MAX_LEN 0xa00000 |
| 23 |
#define BUFFER_ALLOC_INCREMENT 0x008000 |
| 24 |
|
| 21 |
/* Initializes the buffer structure. */ |
25 |
/* Initializes the buffer structure. */ |
| 22 |
|
26 |
|
| 23 |
void |
27 |
void |
|
Lines 66-71
buffer_append(Buffer *buffer, const void
Link Here
|
| 66 |
memcpy(p, data, len); |
70 |
memcpy(p, data, len); |
| 67 |
} |
71 |
} |
| 68 |
|
72 |
|
|
|
73 |
static void |
| 74 |
buffer_compact(Buffer *buffer) |
| 75 |
{ |
| 76 |
/* |
| 77 |
* If the buffer is quite empty, but all data is at the end, move the |
| 78 |
* data to the beginning. |
| 79 |
*/ |
| 80 |
if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
| 81 |
memmove(buffer->buf, buffer->buf + buffer->offset, |
| 82 |
buffer->end - buffer->offset); |
| 83 |
buffer->end -= buffer->offset; |
| 84 |
buffer->offset = 0; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 69 |
/* |
88 |
/* |
| 70 |
* Appends space to the buffer, expanding the buffer if necessary. This does |
89 |
* Appends space to the buffer, expanding the buffer if necessary. This does |
| 71 |
* not actually copy the data into the buffer, but instead returns a pointer |
90 |
* not actually copy the data into the buffer, but instead returns a pointer |
|
Lines 93-112
restart:
Link Here
|
| 93 |
buffer->end += len; |
112 |
buffer->end += len; |
| 94 |
return p; |
113 |
return p; |
| 95 |
} |
114 |
} |
| 96 |
/* |
|
|
| 97 |
* If the buffer is quite empty, but all data is at the end, move the |
| 98 |
* data to the beginning and retry. |
| 99 |
*/ |
| 100 |
if (buffer->offset > MIN(buffer->alloc, BUFFER_MAX_CHUNK)) { |
| 101 |
memmove(buffer->buf, buffer->buf + buffer->offset, |
| 102 |
buffer->end - buffer->offset); |
| 103 |
buffer->end -= buffer->offset; |
| 104 |
buffer->offset = 0; |
| 105 |
goto restart; |
| 106 |
} |
| 107 |
/* Increase the size of the buffer and retry. */ |
| 108 |
|
115 |
|
| 109 |
newlen = buffer->alloc + len + 32768; |
116 |
/* Compact data back to the start of the buffer if necessary */ |
|
|
117 |
buffer_compact(buffer); |
| 118 |
|
| 119 |
/* Increase the size of the buffer and retry. */ |
| 120 |
newlen = roundup(buffer->alloc + len, BUFFER_ALLOC_INCREMENT); |
| 110 |
if (newlen > BUFFER_MAX_LEN) |
121 |
if (newlen > BUFFER_MAX_LEN) |
| 111 |
fatal("buffer_append_space: alloc %u not supported", |
122 |
fatal("buffer_append_space: alloc %u not supported", |
| 112 |
newlen); |
123 |
newlen); |
|
Lines 114-119
restart:
Link Here
|
| 114 |
buffer->alloc = newlen; |
125 |
buffer->alloc = newlen; |
| 115 |
goto restart; |
126 |
goto restart; |
| 116 |
/* NOTREACHED */ |
127 |
/* NOTREACHED */ |
|
|
128 |
} |
| 129 |
|
| 130 |
/* Check whether an allocation of 'len' will fit in the buffer */ |
| 131 |
int |
| 132 |
buffer_check_alloc(Buffer *buffer, u_int len) |
| 133 |
{ |
| 134 |
buffer_compact(buffer); |
| 135 |
return (roundup(buffer->alloc + len, BUFFER_ALLOC_INCREMENT) <= |
| 136 |
BUFFER_MAX_LEN); |
| 117 |
} |
137 |
} |
| 118 |
|
138 |
|
| 119 |
/* Returns the number of bytes of data in the buffer. */ |
139 |
/* Returns the number of bytes of data in the buffer. */ |