Skip to content

Commit

Permalink
Tweak buffer allocation to alloc double the previous memory as before…
Browse files Browse the repository at this point in the history
… when encoding requests to avoid unnecessary allocs
  • Loading branch information
arr2036 committed Mar 18, 2014
1 parent 1587615 commit 6417918
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/modules/rlm_rest/rest.c
Expand Up @@ -761,11 +761,11 @@ static size_t rest_encode_json(void *out, size_t size, size_t nmemb, void *userd
/** Emulates successive libcurl calls to an encoding function
*
* This function is used when the request will be sent to the HTTP server as one
* contiguous entity. A buffer of REST_BODY_INCR bytes is allocated and passed
* contiguous entity. A buffer of REST_BODY_INIT bytes is allocated and passed
* to the stream encoding function.
*
* If the stream function does not return 0, a new buffer is allocated which is
* the size of the previous buffer + REST_BODY_INCR bytes, the data from the
* the size of the previous buffer + REST_BODY_INIT bytes, the data from the
* previous buffer is copied, and freed, and another call is made to the stream
* function, passing a pointer into the new buffer at the end of the previously
* written data.
Expand All @@ -787,26 +787,26 @@ static ssize_t rest_read_wrapper(char **buffer, rest_read_t func, size_t limit,
char *previous = NULL;
char *current;

size_t alloc = REST_BODY_INCR; /* Size of buffer to alloc */
size_t alloc = REST_BODY_INIT; /* Size of buffer to alloc */
size_t used = 0; /* Size of data written */
size_t len = 0;

while (alloc < limit) {
while (alloc <= limit) {
current = rad_malloc(alloc);

if (previous) {
strlcpy(current, previous, used + 1);
free(previous);
}

len = func(current + used, REST_BODY_INCR, 1, userdata);
len = func(current + used, alloc - used, 1, userdata);
used += len;
if (!len) {
*buffer = current;
return used;
}

alloc += REST_BODY_INCR;
alloc = alloc * 2;
previous = current;
};

Expand Down Expand Up @@ -1541,7 +1541,7 @@ static size_t rest_write_body(void *ptr, size_t size, size_t nmemb, void *userda

default:
if (t > (ctx->alloc - ctx->used)) {
ctx->alloc += ((t + 1) > REST_BODY_INCR) ? t + 1 : REST_BODY_INCR;
ctx->alloc += ((t + 1) > REST_BODY_INIT) ? t + 1 : REST_BODY_INIT;

tmp = ctx->buffer;

Expand Down
2 changes: 1 addition & 1 deletion src/modules/rlm_rest/rest.h
Expand Up @@ -41,7 +41,7 @@ RCSIDH(other_h, "$Id$")

#define REST_URI_MAX_LEN 2048
#define REST_BODY_MAX_LEN 8192
#define REST_BODY_INCR 512
#define REST_BODY_INIT 1024
#define REST_BODY_MAX_ATTRS 256

typedef enum {
Expand Down

0 comments on commit 6417918

Please sign in to comment.