From f25d9a8e4056e68fbe826c4234b8739e92b015a6 Mon Sep 17 00:00:00 2001 From: Jan Musial Date: Wed, 9 Jun 2021 09:48:17 +0200 Subject: [PATCH] Use new non-zeroing allocator APIs Signed-off-by: Jan Musial --- env/posix/ocf_env.c | 40 ++++++++++---------- env/posix/ocf_env.h | 6 +-- env/posix/utils_mpool.c | 6 ++- env/posix/utils_mpool.h | 2 +- src/concurrency/ocf_cache_line_concurrency.c | 2 +- src/metadata/metadata_io.c | 3 +- src/ocf_io.c | 3 +- src/ocf_request.c | 2 +- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/env/posix/ocf_env.c b/env/posix/ocf_env.c index 406d520..ba8c6c0 100644 --- a/env/posix/ocf_env.c +++ b/env/posix/ocf_env.c @@ -17,6 +17,9 @@ struct _env_allocator { /*!< Number of currently allocated items in pool */ env_atomic count; + + /*!< Should buffer be zeroed while allocating */ + bool zero; }; static inline size_t env_allocator_align(size_t size) @@ -36,21 +39,26 @@ void *env_allocator_new(env_allocator *allocator) { struct _env_allocator_item *item = NULL; - item = calloc(1, allocator->item_size); + item = malloc(allocator->item_size); - if (item) { - item->cpu = 0; - env_atomic_inc(&allocator->count); + if (!item) { + return NULL; } + if (allocator->zero) { + memset(item, 0, allocator->item_size); + } + + item->cpu = 0; + item->flags = 0; + env_atomic_inc(&allocator->count); + return &item->data; } -env_allocator *env_allocator_create(uint32_t size, const char *fmt_name, ...) +env_allocator *env_allocator_create(uint32_t size, const char *name, bool zero) { - char name[OCF_ALLOCATOR_NAME_MAX] = { '\0' }; - int result, error = -1; - va_list args; + int error = -1; env_allocator *allocator = calloc(1, sizeof(*allocator)); if (!allocator) { @@ -59,21 +67,11 @@ env_allocator *env_allocator_create(uint32_t size, const char *fmt_name, ...) } allocator->item_size = size + sizeof(struct _env_allocator_item); + allocator->zero = zero; - /* Format allocator name */ - va_start(args, fmt_name); - result = vsnprintf(name, sizeof(name), fmt_name, args); - va_end(args); + allocator->name = strdup(name); - if ((result > 0) && (result < sizeof(name))) { - allocator->name = strdup(name); - - if (!allocator->name) { - error = __LINE__; - goto err; - } - } else { - /* Formated string name exceed max allowed size of name */ + if (!allocator->name) { error = __LINE__; goto err; } diff --git a/env/posix/ocf_env.h b/env/posix/ocf_env.h index 2434efa..91bd8b0 100644 --- a/env/posix/ocf_env.h +++ b/env/posix/ocf_env.h @@ -200,10 +200,10 @@ static inline uint64_t env_get_free_memory(void) /* ALLOCATOR */ typedef struct _env_allocator env_allocator; -env_allocator *env_allocator_create(uint32_t size, const char *fmt_name, ...); +env_allocator *env_allocator_create(uint32_t size, const char *name, bool zero); -#define env_allocator_create_extended(size, fmt_name, limit) \ - env_allocator_create(size, fmt_name) +#define env_allocator_create_extended(size, name, limit, zero) \ + env_allocator_create(size, name, zero) void env_allocator_destroy(env_allocator *allocator); diff --git a/env/posix/utils_mpool.c b/env/posix/utils_mpool.c index 2b04304..50dfe86 100644 --- a/env/posix/utils_mpool.c +++ b/env/posix/utils_mpool.c @@ -29,7 +29,8 @@ struct env_mpool { struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size, int flags, int mpool_max, bool fallback, const uint32_t limits[env_mpool_max], - const char *name_perfix) + const char *name_perfix, + bool zero) { uint32_t i; char name[MPOOL_ALLOCATOR_NAME_MAX] = { '\0' }; @@ -56,7 +57,8 @@ struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size, size = hdr_size + (elem_size * (1 << i)); mpool->allocator[i] = env_allocator_create_extended( - size, name, limits ? limits[i] : -1); + size, name, limits ? limits[i] : -1, + zero); if (!mpool->allocator[i]) goto err; diff --git a/env/posix/utils_mpool.h b/env/posix/utils_mpool.h index 00c7ff5..5b122db 100644 --- a/env/posix/utils_mpool.h +++ b/env/posix/utils_mpool.h @@ -44,7 +44,7 @@ struct env_mpool; struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size, int flags, int mpool_max, bool fallback, const uint32_t limits[env_mpool_max], - const char *name_perfix); + const char *name_perfix, bool zero); /** * @brief Destroy existing memory pool diff --git a/src/concurrency/ocf_cache_line_concurrency.c b/src/concurrency/ocf_cache_line_concurrency.c index 4c662de..2137359 100644 --- a/src/concurrency/ocf_cache_line_concurrency.c +++ b/src/concurrency/ocf_cache_line_concurrency.c @@ -108,7 +108,7 @@ int ocf_cache_line_concurrency_init(struct ocf_cache_line_concurrency **self, goto allocation_err; } - c->allocator = env_allocator_create(sizeof(struct __waiter), name); + c->allocator = env_allocator_create(sizeof(struct __waiter), name, false); if (!c->allocator) { error = __LINE__; goto allocation_err; diff --git a/src/metadata/metadata_io.c b/src/metadata/metadata_io.c index 06377b5..460e34d 100644 --- a/src/metadata/metadata_io.c +++ b/src/metadata/metadata_io.c @@ -492,7 +492,8 @@ int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx) sizeof(struct metadata_io_request), ENV_MEM_NOIO, ocf_mio_size_max - 1, true, limits, - "ocf_mio"); + "ocf_mio", + true); if (ocf_ctx->resources.mio == NULL) return -1; diff --git a/src/ocf_io.c b/src/ocf_io.c index e580e9f..69f1bde 100644 --- a/src/ocf_io.c +++ b/src/ocf_io.c @@ -34,7 +34,8 @@ static int ocf_io_allocator_default_init(ocf_io_allocator_t allocator, uint32_t priv_size, const char *name) { - allocator->priv = env_allocator_create(OCF_IO_TOTAL(priv_size), name); + allocator->priv = env_allocator_create(OCF_IO_TOTAL(priv_size), name, + true); if (!allocator->priv) return -OCF_ERR_NO_MEM; diff --git a/src/ocf_request.c b/src/ocf_request.c index bd2e6fc..687bd21 100644 --- a/src/ocf_request.c +++ b/src/ocf_request.c @@ -47,7 +47,7 @@ int ocf_req_allocator_init(struct ocf_ctx *ocf_ctx) { ocf_ctx->resources.req = env_mpool_create(sizeof(struct ocf_request), sizeof(struct ocf_map_info), ENV_MEM_NORMAL, ocf_req_size_128, - false, NULL, "ocf_req"); + false, NULL, "ocf_req", true); if (ocf_ctx->resources.req == NULL) return -1;