Browse Source

Only ever increase buffer size in add_buflen

master
Con Kolivas 9 years ago
parent
commit
f78fe91cef
  1. 13
      src/ckpool.c
  2. 3
      src/ckpool.h

13
src/ckpool.c

@ -518,9 +518,10 @@ void empty_buffer(connsock_t *cs)
* only unprocessed data of bufofs length. */ * only unprocessed data of bufofs length. */
static void clear_bufline(connsock_t *cs) static void clear_bufline(connsock_t *cs)
{ {
if (unlikely(!cs->buf)) if (unlikely(!cs->buf)) {
cs->buf = ckzalloc(PAGESIZE); cs->buf = ckzalloc(PAGESIZE);
else if (cs->buflen) { cs->bufsize = PAGESIZE;
} else if (cs->buflen) {
memmove(cs->buf, cs->buf + cs->bufofs, cs->buflen); memmove(cs->buf, cs->buf + cs->bufofs, cs->buflen);
memset(cs->buf + cs->buflen, 0, cs->bufofs); memset(cs->buf + cs->buflen, 0, cs->bufofs);
cs->bufofs = cs->buflen; cs->bufofs = cs->buflen;
@ -532,14 +533,16 @@ static void clear_bufline(connsock_t *cs)
static void add_buflen(connsock_t *cs, const char *readbuf, const int len) static void add_buflen(connsock_t *cs, const char *readbuf, const int len)
{ {
int backoff = 1; int backoff = 1;
size_t buflen; int buflen;
char *newbuf; char *newbuf;
buflen = round_up_page(cs->bufofs + len + 1); buflen = round_up_page(cs->bufofs + len + 1);
while (42) { while (cs->bufsize < buflen) {
newbuf = realloc(cs->buf, buflen); newbuf = realloc(cs->buf, buflen);
if (likely(newbuf)) if (likely(newbuf)) {
cs->bufsize = buflen;
break; break;
}
if (backoff == 1) if (backoff == 1)
fprintf(stderr, "Failed to realloc %d in read_socket_line, retrying\n", (int)buflen); fprintf(stderr, "Failed to realloc %d in read_socket_line, retrying\n", (int)buflen);
cksleep_ms(backoff); cksleep_ms(backoff);

3
src/ckpool.h

@ -76,9 +76,12 @@ struct connsock {
char *url; char *url;
char *port; char *port;
char *auth; char *auth;
char *buf; char *buf;
int bufofs; int bufofs;
int buflen; int buflen;
int bufsize;
ckpool_t *ckp; ckpool_t *ckp;
/* Semaphore used to serialise request/responses */ /* Semaphore used to serialise request/responses */
sem_t sem; sem_t sem;

Loading…
Cancel
Save