summaryrefslogtreecommitdiff
path: root/accel-pppd
diff options
context:
space:
mode:
authorDmitry Kozlov <xeb@mail.ru>2014-05-18 01:07:04 +0000
committerDmitry Kozlov <xeb@mail.ru>2014-05-18 02:15:30 +0000
commita833b45f18f8213bc88ff6d1c72154ec9c13ad4d (patch)
tree33fcd30f0e43cf39fb2453e53c77df1bb2bbd177 /accel-pppd
parentd5c3630d92b329735048b228878bb3ae137799cc (diff)
downloadaccel-ppp-a833b45f18f8213bc88ff6d1c72154ec9c13ad4d.tar.gz
accel-ppp-a833b45f18f8213bc88ff6d1c72154ec9c13ad4d.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')
-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 06121522..dcb2b113 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;