From d69ed7ffcf5aca6921aaba10734aeff8839c3260 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 2 Jan 2016 08:18:30 +1100 Subject: [PATCH] More fixes --- src/ckpool.c | 24 ++++++++++-------------- src/connector.c | 8 +++++--- src/libckpool.c | 12 ++++++++++++ src/libckpool.h | 2 ++ 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 2f49936a..6cfbd9f9 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -555,7 +555,7 @@ static int read_cs_length(connsock_t *cs, float *timeout, int len) int ret = len; while (cs->buflen < len) { - char readbuf[PAGESIZE] = {}; + char readbuf[PAGESIZE]; ret = wait_read_select(cs->fd, *timeout); if (ret < 1) @@ -629,7 +629,7 @@ static int read_gz_line(connsock_t *cs, float *timeout) buflen = cs->buflen; cs->buf = dest; dest = NULL; - ret = cs->buflen = decompsize - 1; + ret = cs->buflen = decompsize; if (buflen) add_bufline(cs, buf, buflen); out: @@ -732,23 +732,20 @@ int write_cs(connsock_t *cs, const char *buf, int len) unsigned long compsize, decompsize = len; char *dest = NULL; uint32_t msglen; - int ret = -1; + int ret; /* Connsock doesn't expect gz compressed messages */ - if (!cs->gz || len <= 1492) { - ret = write_socket(cs->fd, buf, len); - goto out; - } - dest = ckalloc(len + 12); + if (!cs->gz || len <= 1492) + return write_socket(cs->fd, buf, len); + compsize = round_up_page(len + 12); + dest = alloca(compsize); /* Do compression here */ - compsize = len; + compsize -= 12; ret = compress((Bytef *)dest + 12, &compsize, (Bytef *)buf, len); if (ret != Z_OK) { LOGINFO("Failed to gz compress in write_cs, writing uncompressed"); - ret = write_socket(cs->fd, buf, len); - goto out; + return write_socket(cs->fd, buf, len); } - LOGDEBUG("Writing connsock message compressed %lu from %lu", compsize, decompsize); /* Copy gz magic header */ sprintf(dest, gzip_magic); /* Copy compressed message length */ @@ -758,13 +755,12 @@ int write_cs(connsock_t *cs, const char *buf, int len) msglen = htole32(decompsize); memcpy(dest + 8, &msglen, 4); len = compsize + 12; + LOGDEBUG("Writing connsock message compressed %d from %lu", len, decompsize); ret = write_socket(cs->fd, dest, len); if (ret == len) ret = decompsize; else ret = -1; -out: - free(dest); return ret; } diff --git a/src/connector.c b/src/connector.c index dd173478..0f7633fb 100644 --- a/src/connector.c +++ b/src/connector.c @@ -1005,18 +1005,19 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf) * larger than one MTU. */ if (client->gz) { unsigned long compsize, decompsize = len; - char *dest = ckalloc(len + 12); uint32_t msglen; + char *dest; int ret; - compsize = len; + compsize = round_up_page(len + 12); + dest = ckalloc(compsize); + compsize -= 12; ret = compress((Bytef *)dest + 12, &compsize, (Bytef *)buf, len); if (unlikely(ret != Z_OK)) { LOGINFO("Failed to gz compress in send_client, got %d sending uncompressed", ret); free(dest); goto out; } - LOGDEBUG("Sending client message compressed %lu from %lu", compsize, decompsize); /* Copy gz magic header */ sprintf(dest, gzip_magic); /* Copy compressed message length */ @@ -1026,6 +1027,7 @@ static void send_client(cdata_t *cdata, const int64_t id, char *buf) msglen = htole32(decompsize); memcpy(dest + 8, &msglen, 4); len = compsize + 12; + LOGDEBUG("Sending client message compressed %d from %lu", len, decompsize); free(buf); buf = dest; } diff --git a/src/libckpool.c b/src/libckpool.c index a0557a71..2e78d330 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1389,6 +1389,18 @@ void *_ckzalloc(size_t len, const char *file, const char *func, const int line) return ptr; } +/* Round up to the nearest page size for efficient malloc */ +size_t round_up_page(size_t len) +{ + int rem = len % PAGESIZE; + + if (rem) + len += PAGESIZE - rem; + return len; +} + + + /* Adequate size s==len*2 + 1 must be alloced to use this variant */ void __bin2hex(void *vs, const void *vp, size_t len) { diff --git a/src/libckpool.h b/src/libckpool.h index 9d8ba341..10c18248 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -531,6 +531,8 @@ void trail_slash(char **buf); void *_ckalloc(size_t len, const char *file, const char *func, const int line); void *json_ckalloc(size_t size); void *_ckzalloc(size_t len, const char *file, const char *func, const int line); +size_t round_up_page(size_t len); + extern const int hex2bin_tbl[]; void __bin2hex(void *vs, const void *vp, size_t len); void *bin2hex(const void *vp, size_t len);