Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
test.c: get it to work with valgrind by using realloc less
Browse files Browse the repository at this point in the history
For some reason valgrind would rapidly run out of memory on my machine
without this.
  • Loading branch information
Cliff Frey authored and ry committed Jul 6, 2010
1 parent c46b3e3 commit 5dd7403
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions test.c
Expand Up @@ -1448,39 +1448,27 @@ char *
create_large_chunked_message (int body_size_in_kb, const char* headers) create_large_chunked_message (int body_size_in_kb, const char* headers)
{ {
int i; int i;
size_t needed, wrote = 0; size_t wrote = 0;
size_t headers_len = strlen(headers); size_t headers_len = strlen(headers);
size_t bufsize = headers_len + 10; size_t bufsize = headers_len + (5+1024+2)*body_size_in_kb + 6;
char * buf = malloc(bufsize); char * buf = malloc(bufsize);


strncpy(buf, headers, headers_len); memcpy(buf, headers, headers_len);
wrote += headers_len; wrote += headers_len;


for (i = 0; i < body_size_in_kb; i++) { for (i = 0; i < body_size_in_kb; i++) {
// write 1kb chunk into the body. // write 1kb chunk into the body.
needed = 5 + 1024 + 2; // "400\r\nCCCC...CCCC\r\n" memcpy(buf + wrote, "400\r\n", 5);
if (bufsize - wrote < needed) {
buf = realloc(buf, bufsize + needed);
bufsize += needed;
}

strcpy(buf + wrote, "400\r\n");
wrote += 5; wrote += 5;
memset(buf + wrote, 'C', 1024); memset(buf + wrote, 'C', 1024);
wrote += 1024; wrote += 1024;
strcpy(buf + wrote, "\r\n"); strcpy(buf + wrote, "\r\n");
wrote += 2; wrote += 2;
} }


needed = 5; // "0\r\n\r\n" memcpy(buf + wrote, "0\r\n\r\n", 6);
if (bufsize - wrote < needed) { wrote += 6;
buf = realloc(buf, bufsize + needed); assert(wrote == bufsize);
bufsize += needed;
}
strcpy(buf + wrote, "0\r\n\r\n");
wrote += 5;

assert(buf[wrote] == 0);


return buf; return buf;
} }
Expand Down

0 comments on commit 5dd7403

Please sign in to comment.