Skip to content

Commit 28869ea

Browse files
committed
Use free followed by malloc instead of realloc when increasing raw buffer
At this point we don't need to preserve the data inside the buffer. Using free followed by malloc reduces fragmentation for some heap implementations.
1 parent 43a90bc commit 28869ea

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

ssl/tls1.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -1402,15 +1402,19 @@ int basic_read(SSL *ssl, uint8_t **in_data)
14021402

14031403
void increase_bm_data_size(SSL *ssl)
14041404
{
1405-
uint8_t* pr = (uint8_t*) realloc(ssl->bm_all_data, RT_MAX_PLAIN_LENGTH + RT_EXTRA);
1406-
if (pr) {
1407-
ssl->max_plain_length = RT_MAX_PLAIN_LENGTH;
1408-
ssl->bm_all_data = pr;
1409-
ssl->bm_data = pr + BM_RECORD_OFFSET;
1405+
if (ssl->max_plain_length == RT_MAX_PLAIN_LENGTH) {
1406+
return;
14101407
}
1411-
else {
1408+
1409+
free(ssl->bm_all_data);
1410+
ssl->bm_data = 0;
1411+
ssl->bm_all_data = malloc(RT_MAX_PLAIN_LENGTH + RT_EXTRA);
1412+
if (!ssl->bm_all_data) {
14121413
printf("failed to grow plain buffer\r\n");
1414+
return;
14131415
}
1416+
ssl->max_plain_length = RT_MAX_PLAIN_LENGTH;
1417+
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET;
14141418
}
14151419

14161420
/**

0 commit comments

Comments
 (0)