From: Florian Forster Date: Tue, 1 Apr 2008 08:40:39 +0000 (+0200) Subject: apache plugin: Increase the size of the read-buffer dynamically. X-Git-Tag: collectd-4.2.7~12 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=5c3efb12ed19f99017b8b13943d4a983de040050;p=collectd.git apache plugin: Increase the size of the read-buffer dynamically. Why guess the size if you can just adapt to whatever comes in? The memory isn't freed ever, though. --- diff --git a/src/apache.c b/src/apache.c index 280c687f..2a7e0b80 100644 --- a/src/apache.c +++ b/src/apache.c @@ -36,10 +36,10 @@ static char *cacert = NULL; static CURL *curl = NULL; -#define ABUFFER_SIZE 16384 -static char apache_buffer[ABUFFER_SIZE]; -static int apache_buffer_len = 0; -static char apache_curl_error[CURL_ERROR_SIZE]; +static char *apache_buffer = NULL; +static size_t apache_buffer_size = 0; +static size_t apache_buffer_fill = 0; +static char apache_curl_error[CURL_ERROR_SIZE]; static const char *config_keys[] = { @@ -50,21 +50,32 @@ static const char *config_keys[] = }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); -static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, void *stream) +static size_t apache_curl_callback (void *buf, size_t size, size_t nmemb, + void *stream) { size_t len = size * nmemb; - if ((apache_buffer_len + len) >= ABUFFER_SIZE) - { - len = (ABUFFER_SIZE - 1) - apache_buffer_len; - } - if (len <= 0) return (len); - memcpy (apache_buffer + apache_buffer_len, (char *) buf, len); - apache_buffer_len += len; - apache_buffer[apache_buffer_len] = '\0'; + if ((apache_buffer_fill + len) >= apache_buffer_size) + { + char *temp; + + temp = (char *) realloc (apache_buffer, + apache_buffer_fill + len + 1); + if (temp == NULL) + { + ERROR ("apache plugin: realloc failed."); + return (0); + } + apache_buffer = temp; + apache_buffer_size = apache_buffer_fill + len + 1; + } + + memcpy (apache_buffer + apache_buffer_fill, (char *) buf, len); + apache_buffer_fill += len; + apache_buffer[apache_buffer_fill] = 0; return (len); } @@ -269,7 +280,7 @@ static int apache_read (void) if (url == NULL) return (-1); - apache_buffer_len = 0; + apache_buffer_fill = 0; if (curl_easy_perform (curl) != 0) { ERROR ("apache: curl_easy_perform failed: %s", @@ -312,7 +323,7 @@ static int apache_read (void) } } - apache_buffer_len = 0; + apache_buffer_fill = 0; return (0); } /* int apache_read */