MEMORY introduce a small allocation pool

This patch finally introduces a small allocation pool
which recycles all the small portions of memory that
are used for headers and pdu structures. This was
the initial idea behind wrapping all memory functions
in libiscsi.

The results of booting are test system up to the login
prompt are quite impressive:

BEFORE:
libiscsi:5 memory is clean at iscsi_destroy_context() after 10712 mallocs, 18 realloc(s) and 10712 free(s)

AFTER:
libiscsi:5 memory is clean at iscsi_destroy_context() after 41 mallocs, 18 realloc(s), 41 free(s) and 10584 reused small allocations

Signed-off-by: Peter Lieven <pl@kamp.de>
This commit is contained in:
Peter Lieven
2013-07-18 11:15:56 +02:00
parent cc02faabb0
commit d429276907
4 changed files with 77 additions and 13 deletions

View File

@@ -39,6 +39,7 @@ extern "C" {
#define ISCSI_HEADER_SIZE (ISCSI_RAW_HEADER_SIZE \
+ (iscsi->header_digest == ISCSI_HEADER_DIGEST_NONE?0:ISCSI_DIGEST_SIZE))
#define SMALL_ALLOC_MAX_FREE (128) /* must be power of 2 */
struct iscsi_in_pdu {
struct iscsi_in_pdu *next;
@@ -131,6 +132,10 @@ struct iscsi_context {
int mallocs;
int reallocs;
int frees;
int smallocs;
void* smalloc_ptrs[SMALL_ALLOC_MAX_FREE];
int smalloc_free;
size_t smalloc_size;
time_t last_reconnect;
int scsi_timeout;
@@ -301,6 +306,8 @@ inline void* iscsi_zmalloc(struct iscsi_context *iscsi, size_t size);
inline void* iscsi_realloc(struct iscsi_context *iscsi, void* ptr, size_t size);
inline void iscsi_free(struct iscsi_context *iscsi, void* ptr);
inline char* iscsi_strdup(struct iscsi_context *iscsi, const char* str);
inline void* iscsi_szmalloc(struct iscsi_context *iscsi, size_t size);
inline void iscsi_sfree(struct iscsi_context *iscsi, void* ptr);
unsigned long crc32c(char *buf, int len);