diff options
| author | Yoshiyuki Asaba | 2007-12-05 08:19:41 +0000 |
|---|---|---|
| committer | Yoshiyuki Asaba | 2007-12-05 08:19:41 +0000 |
| commit | b6b67b83658b5cd2fe09c34646bf6d598d5d09b9 (patch) | |
| tree | 9c61d99424d11ae282e63792b109619a1c20cf47 | |
| parent | 29aa2803866a57f4a9c353d0a1940c77a9663c0c (diff) | |
Improve buffering algorithm in pool_write().V1_STABLE
| -rw-r--r-- | pool_stream.c | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/pool_stream.c b/pool_stream.c index 92e236e82..dadcd20c1 100644 --- a/pool_stream.c +++ b/pool_stream.c @@ -309,8 +309,6 @@ char *pool_read2(POOL_CONNECTION *cp, int len) */ int pool_write(POOL_CONNECTION *cp, void *buf, int len) { - int reqlen; - if (len < 0) { pool_error("pool_write: invalid request size: %d", len); @@ -320,26 +318,31 @@ int pool_write(POOL_CONNECTION *cp, void *buf, int len) if (cp->no_forward) return 0; - /* check buffer size */ - reqlen = cp->wbufpo + len; - - if (reqlen > cp->wbufsz) + while (len > 0) { - char *p; + int remainder = cp->wbufsz - cp->wbufpo; - reqlen = (reqlen/WRITEBUFSZ+1)*WRITEBUFSZ; - p = realloc(cp->wbuf, reqlen); - if (p == NULL) + if (remainder <= 0) { - pool_error("pool_write: realloc failed"); - return -1; + /* + * Write buffer is full. so flush buffer. + * wbufpo is reset in pool_flush_it(). + */ + pool_flush_it(cp); + remainder = WRITEBUFSZ; } - cp->wbuf = p; - cp->wbufsz = reqlen; - } - memcpy(cp->wbuf+cp->wbufpo, buf, len); - cp->wbufpo += len; + /* check buffer size */ + if (remainder >= len) + { + /* OK, buffer size is enough. */ + remainder = len; + } + memcpy(cp->wbuf+cp->wbufpo, buf, remainder); + cp->wbufpo += remainder; + buf += remainder; + len -= remainder; + } return 0; } |
