summaryrefslogtreecommitdiff
path: root/accel-pppd/log.c
diff options
context:
space:
mode:
authorDmitry Kozlov <xeb@mail.ru>2014-05-18 01:07:04 +0000
committerDmitry Kozlov <xeb@mail.ru>2014-07-11 14:57:17 +0400
commit34751881be05d546f04dea2515e5b0d2617bff7c (patch)
tree2f72a683040d229bc6bfee2239009941ed28ab67 /accel-pppd/log.c
parent52e451250c9d445e73ed9df27b07e59a8d57664a (diff)
downloadaccel-ppp-34751881be05d546f04dea2515e5b0d2617bff7c.tar.gz
accel-ppp-34751881be05d546f04dea2515e5b0d2617bff7c.zip
log: append data to previous chunk
Check if there is space in previous chunk and append log data there. This reduces memory usage and message fragmentation
Diffstat (limited to 'accel-pppd/log.c')
-rw-r--r--accel-pppd/log.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/accel-pppd/log.c b/accel-pppd/log.c
index 43e98a97..03bfc0d0 100644
--- a/accel-pppd/log.c
+++ b/accel-pppd/log.c
@@ -16,6 +16,10 @@
#include "memdebug.h"
+#ifndef min
+#define min(x,y) ((x)<(y)?(x):(y))
+#endif
+
#define LOG_MSG 0
#define LOG_ERROR 1
#define LOG_WARN 2
@@ -306,9 +310,22 @@ static struct log_msg_t *clone_msg(struct _log_msg_t *msg)
static int add_msg(struct _log_msg_t *msg, const char *buf)
{
struct log_chunk_t *chunk;
- int i, len, chunk_cnt;
+ int i, chunk_cnt, len = strlen(buf);
- len = strlen(buf);
+ if (!list_empty(&msg->chunks)) {
+ chunk = list_entry(msg->chunks.prev, typeof(*chunk), entry);
+ i = min(len, LOG_CHUNK_SIZE - chunk->len);
+ memcpy(chunk->msg + chunk->len, buf, i);
+ chunk->len += i;
+ chunk->msg[chunk->len] = 0;
+
+ if (i == len)
+ return 0;
+
+ buf += i;
+ len -= i;
+ }
+
chunk_cnt = (len - 1)/LOG_CHUNK_SIZE + 1;
for (i = 0; i < chunk_cnt; i++) {
@@ -316,7 +333,7 @@ static int add_msg(struct _log_msg_t *msg, const char *buf)
if (!chunk)
return -1;
- chunk->len = i == chunk_cnt -1 ? len - i * LOG_CHUNK_SIZE : LOG_CHUNK_SIZE;
+ chunk->len = i == chunk_cnt - 1 ? len - i * LOG_CHUNK_SIZE : LOG_CHUNK_SIZE;
memcpy(chunk->msg, buf + i * LOG_CHUNK_SIZE, chunk->len);
chunk->msg[chunk->len] = 0;