MEMORY add wrappers around all mallocs and frees and trace them
This patch adds a wrapper around all memory allocations and frees. The idea is to get warned immediately if the application leaks memory. Additionally the wrapper functions make it easy to add different memory allocators or memory pools in the future.
This commit is contained in:
@@ -61,7 +61,7 @@ iscsi_testunitready_cb(struct iscsi_context *iscsi, int status,
|
||||
"failed.");
|
||||
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL,
|
||||
ct->private_data);
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
}
|
||||
scsi_free_scsi_task(task);
|
||||
return;
|
||||
@@ -80,7 +80,7 @@ iscsi_testunitready_cb(struct iscsi_context *iscsi, int status,
|
||||
ct->cb(iscsi, status?SCSI_STATUS_ERROR:SCSI_STATUS_GOOD, NULL,
|
||||
ct->private_data);
|
||||
scsi_free_scsi_task(task);
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -92,7 +92,7 @@ iscsi_login_cb(struct iscsi_context *iscsi, int status, void *command_data _U_,
|
||||
if (status == SCSI_STATUS_REDIRECT && iscsi->target_address[0]) {
|
||||
iscsi_disconnect(iscsi);
|
||||
if (iscsi_connect_async(iscsi, iscsi->target_address, iscsi_connect_cb, iscsi->connect_data) != 0) {
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
@@ -100,7 +100,7 @@ iscsi_login_cb(struct iscsi_context *iscsi, int status, void *command_data _U_,
|
||||
|
||||
if (status != 0) {
|
||||
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,14 +121,14 @@ iscsi_connect_cb(struct iscsi_context *iscsi, int status, void *command_data _U_
|
||||
iscsi_set_error(iscsi, "Failed to connect to iSCSI socket. "
|
||||
"%s", iscsi_get_error(iscsi));
|
||||
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (iscsi_login_async(iscsi, iscsi_login_cb, ct) != 0) {
|
||||
iscsi_set_error(iscsi, "iscsi_login_async failed.");
|
||||
ct->cb(iscsi, SCSI_STATUS_ERROR, NULL, ct->private_data);
|
||||
free(ct);
|
||||
iscsi_free(iscsi, ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ iscsi_full_connect_async(struct iscsi_context *iscsi, const char *portal,
|
||||
iscsi->lun = lun;
|
||||
strncpy(iscsi->portal,portal,MAX_STRING_SIZE);
|
||||
|
||||
ct = malloc(sizeof(struct connect_task));
|
||||
ct = iscsi_malloc(iscsi, sizeof(struct connect_task));
|
||||
if (ct == NULL) {
|
||||
iscsi_set_error(iscsi, "Out-of-memory. Failed to allocate "
|
||||
"connect_task structure.");
|
||||
@@ -318,18 +318,22 @@ try_again:
|
||||
}
|
||||
|
||||
if (old_iscsi->incoming != NULL) {
|
||||
iscsi_free_iscsi_in_pdu(old_iscsi->incoming);
|
||||
iscsi_free_iscsi_in_pdu(old_iscsi, old_iscsi->incoming);
|
||||
}
|
||||
if (old_iscsi->inqueue != NULL) {
|
||||
iscsi_free_iscsi_inqueue(old_iscsi->inqueue);
|
||||
iscsi_free_iscsi_inqueue(old_iscsi, old_iscsi->inqueue);
|
||||
}
|
||||
|
||||
close(iscsi->fd);
|
||||
iscsi->fd = old_iscsi->fd;
|
||||
int _mallocs = old_iscsi->mallocs;
|
||||
int _frees = old_iscsi->frees;
|
||||
memcpy(old_iscsi, iscsi, sizeof(struct iscsi_context));
|
||||
free(iscsi);
|
||||
|
||||
old_iscsi->is_reconnecting = 0;
|
||||
old_iscsi->mallocs+=_mallocs;
|
||||
old_iscsi->frees+=_frees;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user