From 9f741ad2e3925cbce5a25e9d1f37f94712eaf44b Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sun, 25 Nov 2012 19:22:37 -0800 Subject: [PATCH] Remove the iscsi data alloc_size field. Avoiding to realloc data over and over should rather be handled with something similar to iovectors instead. --- include/iscsi.h | 1 - lib/iscsi-command.c | 2 +- lib/pdu.c | 31 +++++++++---------------------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/include/iscsi.h b/include/iscsi.h index 43b10cc..827b9de 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -537,7 +537,6 @@ iscsi_task_mgmt_target_cold_reset_async(struct iscsi_context *iscsi, struct iscsi_data { int size; - size_t alloc_size; unsigned char *data; }; diff --git a/lib/iscsi-command.c b/lib/iscsi-command.c index c34d901..f277a27 100644 --- a/lib/iscsi-command.c +++ b/lib/iscsi-command.c @@ -438,7 +438,7 @@ iscsi_process_scsi_data_in(struct iscsi_context *iscsi, struct iscsi_pdu *pdu, /* Dont add to reassembly buffer if we already have a user buffer */ if (scsi_task_get_data_in_buffer(task, 0, NULL) == NULL) { if (task->expxferlen > dsl && pdu->indata.data == NULL) { - pdu->indata.alloc_size = task->expxferlen; + pdu->indata.size = task->expxferlen; pdu->indata.data = iscsi_malloc(iscsi, task->expxferlen); if (pdu->indata.data == NULL) { iscsi_set_error(iscsi, "Out-of-memory: failed to allocate pdu indata buffer"); diff --git a/lib/pdu.c b/lib/pdu.c index 458190a..af0d448 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -43,9 +43,7 @@ iscsi_allocate_pdu_with_itt_flags(struct iscsi_context *iscsi, enum iscsi_opcode } pdu->outdata.size = ISCSI_HEADER_SIZE; - pdu->outdata.alloc_size = 64; - - pdu->outdata.data = iscsi_malloc(iscsi, pdu->outdata.alloc_size); + pdu->outdata.data = iscsi_malloc(iscsi, pdu->outdata.size); memset(pdu->outdata.data, 0, ISCSI_HEADER_SIZE); if (pdu->outdata.data == NULL) { @@ -117,42 +115,31 @@ iscsi_add_data(struct iscsi_context *iscsi, struct iscsi_data *data, } len = data->size + dsize; + aligned = len; if (pdualignment) { aligned = (aligned+3)&0xfffffffc; } - size_t new_alloc_size = data->alloc_size; - if (new_alloc_size < 64) new_alloc_size=64; - - while (aligned > new_alloc_size) new_alloc_size<<=1; - - if (data->data != NULL && data->alloc_size == 0) data->alloc_size=data->size; - - if (data->alloc_size == 0) { - data->data = iscsi_malloc(iscsi, new_alloc_size); + if (data->size == 0) { + data->data = iscsi_malloc(iscsi, aligned); + } else { + data->data = iscsi_realloc(iscsi, data->data, aligned); } - else - if (data->alloc_size != new_alloc_size) { - data->data = iscsi_realloc(iscsi, data->data, new_alloc_size); - } - if (data->data == NULL) { iscsi_set_error(iscsi, "failed to allocate buffer for %d " "bytes", (int) len); return -1; } - - data->alloc_size = new_alloc_size; + memcpy(data->data + data->size, dptr, dsize); + data->size += dsize; if (len != aligned) { /* zero out any padding at the end */ - memset(data->data+len, 0, aligned-len); + memset(data->data + len, 0, aligned - len); } - data->size = len; - return 0; }