test-tool: Make it possible to disable memory caching

Disable memory caching in libiscsi if the environment variable
LIBISCSI_CACHE_ALLOCATIONS has been set to zero. This makes
Valgrind reports more meaningful.

Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
This commit is contained in:
Bart Van Assche
2015-04-20 09:56:35 +02:00
committed by Ronnie Sahlberg
parent d8de6531f8
commit cf4076dba9
5 changed files with 37 additions and 11 deletions

View File

@@ -33,6 +33,17 @@
#include "iscsi-private.h"
#include "slist.h"
int cache_allocations = 1;
/**
* Whether or not the internal memory allocator caches allocations. Disable
* memory allocation caching to improve the accuracy of Valgrind reports.
*/
void iscsi_set_cache_allocations(int ca)
{
cache_allocations = ca;
}
void* iscsi_malloc(struct iscsi_context *iscsi, size_t size) {
void * ptr = malloc(size);
if (ptr != NULL) iscsi->mallocs++;
@@ -94,19 +105,25 @@ void iscsi_sfree(struct iscsi_context *iscsi, void* ptr) {
if (ptr == NULL) {
return;
}
if (iscsi->smalloc_free == SMALL_ALLOC_MAX_FREE) {
int i;
/* SMALL_ALLOC_MAX_FREE should be adjusted that this happens rarely */
ISCSI_LOG(iscsi, 6, "smalloc free == SMALLOC_MAX_FREE");
/* remove oldest half of free pointers and copy
* upper half to lower half */
iscsi->smalloc_free >>= 1;
for (i = 0; i < iscsi->smalloc_free; i++) {
iscsi_free(iscsi, iscsi->smalloc_ptrs[i]);
iscsi->smalloc_ptrs[i] = iscsi->smalloc_ptrs[i + iscsi->smalloc_free];
if (cache_allocations) {
if (iscsi->smalloc_free == SMALL_ALLOC_MAX_FREE) {
int i;
/* SMALL_ALLOC_MAX_FREE should be adjusted that this */
/* happens rarely */
ISCSI_LOG(iscsi, 6, "smalloc free == SMALLOC_MAX_FREE");
/* remove oldest half of free pointers and copy
* upper half to lower half */
iscsi->smalloc_free >>= 1;
for (i = 0; i < iscsi->smalloc_free; i++) {
iscsi_free(iscsi, iscsi->smalloc_ptrs[i]);
iscsi->smalloc_ptrs[i] =
iscsi->smalloc_ptrs[i + iscsi->smalloc_free];
}
}
iscsi->smalloc_ptrs[iscsi->smalloc_free++] = ptr;
} else {
iscsi_free(iscsi, ptr);
}
iscsi->smalloc_ptrs[iscsi->smalloc_free++] = ptr;
}
struct iscsi_context *