From c68d2c0ddb1739b8c589e329e624fb5032513159 Mon Sep 17 00:00:00 2001 From: Peter Lieven Date: Tue, 3 Jan 2017 12:03:07 +0100 Subject: [PATCH] init: introduce iscsi_smalloc Signed-off-by: Peter Lieven --- include/iscsi-private.h | 1 + lib/init.c | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/iscsi-private.h b/include/iscsi-private.h index b91af60..60d216e 100644 --- a/include/iscsi-private.h +++ b/include/iscsi-private.h @@ -351,6 +351,7 @@ void* iscsi_zmalloc(struct iscsi_context *iscsi, size_t size); void* iscsi_realloc(struct iscsi_context *iscsi, void* ptr, size_t size); void iscsi_free(struct iscsi_context *iscsi, void* ptr); char* iscsi_strdup(struct iscsi_context *iscsi, const char* str); +void* iscsi_smalloc(struct iscsi_context *iscsi, size_t size); void* iscsi_szmalloc(struct iscsi_context *iscsi, size_t size); void iscsi_sfree(struct iscsi_context *iscsi, void* ptr); diff --git a/lib/init.c b/lib/init.c index 9941ceb..1b063cd 100644 --- a/lib/init.c +++ b/lib/init.c @@ -111,15 +111,22 @@ char* iscsi_strdup(struct iscsi_context *iscsi, const char* str) { return str2; } -void* iscsi_szmalloc(struct iscsi_context *iscsi, size_t size) { +void* iscsi_smalloc(struct iscsi_context *iscsi, size_t size) { void *ptr; if (size > iscsi->smalloc_size) return NULL; if (iscsi->smalloc_free > 0) { ptr = iscsi->smalloc_ptrs[--iscsi->smalloc_free]; - memset(ptr, 0, iscsi->smalloc_size); iscsi->smallocs++; } else { - ptr = iscsi_zmalloc(iscsi, iscsi->smalloc_size); + ptr = iscsi_malloc(iscsi, iscsi->smalloc_size); + } + return ptr; +} + +void* iscsi_szmalloc(struct iscsi_context *iscsi, size_t size) { + void *ptr = iscsi_smalloc(iscsi, size); + if (ptr) { + memset(ptr, 0, size); } return ptr; }