Use new non-zeroing allocator APIs

Signed-off-by: Jan Musial <jan.musial@intel.com>
This commit is contained in:
Jan Musial 2021-06-09 09:48:17 +02:00
parent 68fccaf9ae
commit f25d9a8e40
8 changed files with 33 additions and 31 deletions

40
env/posix/ocf_env.c vendored
View File

@ -17,6 +17,9 @@ struct _env_allocator {
/*!< Number of currently allocated items in pool */ /*!< Number of currently allocated items in pool */
env_atomic count; env_atomic count;
/*!< Should buffer be zeroed while allocating */
bool zero;
}; };
static inline size_t env_allocator_align(size_t size) 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; struct _env_allocator_item *item = NULL;
item = calloc(1, allocator->item_size); item = malloc(allocator->item_size);
if (item) { if (!item) {
item->cpu = 0; return NULL;
env_atomic_inc(&allocator->count);
} }
if (allocator->zero) {
memset(item, 0, allocator->item_size);
}
item->cpu = 0;
item->flags = 0;
env_atomic_inc(&allocator->count);
return &item->data; 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 error = -1;
int result, error = -1;
va_list args;
env_allocator *allocator = calloc(1, sizeof(*allocator)); env_allocator *allocator = calloc(1, sizeof(*allocator));
if (!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->item_size = size + sizeof(struct _env_allocator_item);
allocator->zero = zero;
/* Format allocator name */ allocator->name = strdup(name);
va_start(args, fmt_name);
result = vsnprintf(name, sizeof(name), fmt_name, args);
va_end(args);
if ((result > 0) && (result < sizeof(name))) { if (!allocator->name) {
allocator->name = strdup(name);
if (!allocator->name) {
error = __LINE__;
goto err;
}
} else {
/* Formated string name exceed max allowed size of name */
error = __LINE__; error = __LINE__;
goto err; goto err;
} }

6
env/posix/ocf_env.h vendored
View File

@ -200,10 +200,10 @@ static inline uint64_t env_get_free_memory(void)
/* ALLOCATOR */ /* ALLOCATOR */
typedef struct _env_allocator env_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) \ #define env_allocator_create_extended(size, name, limit, zero) \
env_allocator_create(size, fmt_name) env_allocator_create(size, name, zero)
void env_allocator_destroy(env_allocator *allocator); void env_allocator_destroy(env_allocator *allocator);

View File

@ -29,7 +29,8 @@ struct env_mpool {
struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size, struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size,
int flags, int mpool_max, bool fallback, int flags, int mpool_max, bool fallback,
const uint32_t limits[env_mpool_max], const uint32_t limits[env_mpool_max],
const char *name_perfix) const char *name_perfix,
bool zero)
{ {
uint32_t i; uint32_t i;
char name[MPOOL_ALLOCATOR_NAME_MAX] = { '\0' }; 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)); size = hdr_size + (elem_size * (1 << i));
mpool->allocator[i] = env_allocator_create_extended( mpool->allocator[i] = env_allocator_create_extended(
size, name, limits ? limits[i] : -1); size, name, limits ? limits[i] : -1,
zero);
if (!mpool->allocator[i]) if (!mpool->allocator[i])
goto err; goto err;

View File

@ -44,7 +44,7 @@ struct env_mpool;
struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size, struct env_mpool *env_mpool_create(uint32_t hdr_size, uint32_t elem_size,
int flags, int mpool_max, bool fallback, int flags, int mpool_max, bool fallback,
const uint32_t limits[env_mpool_max], const uint32_t limits[env_mpool_max],
const char *name_perfix); const char *name_perfix, bool zero);
/** /**
* @brief Destroy existing memory pool * @brief Destroy existing memory pool

View File

@ -108,7 +108,7 @@ int ocf_cache_line_concurrency_init(struct ocf_cache_line_concurrency **self,
goto allocation_err; 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) { if (!c->allocator) {
error = __LINE__; error = __LINE__;
goto allocation_err; goto allocation_err;

View File

@ -492,7 +492,8 @@ int ocf_metadata_io_ctx_init(struct ocf_ctx *ocf_ctx)
sizeof(struct metadata_io_request), sizeof(struct metadata_io_request),
ENV_MEM_NOIO, ocf_mio_size_max - 1, true, ENV_MEM_NOIO, ocf_mio_size_max - 1, true,
limits, limits,
"ocf_mio"); "ocf_mio",
true);
if (ocf_ctx->resources.mio == NULL) if (ocf_ctx->resources.mio == NULL)
return -1; return -1;

View File

@ -34,7 +34,8 @@
static int ocf_io_allocator_default_init(ocf_io_allocator_t allocator, static int ocf_io_allocator_default_init(ocf_io_allocator_t allocator,
uint32_t priv_size, const char *name) 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) if (!allocator->priv)
return -OCF_ERR_NO_MEM; return -OCF_ERR_NO_MEM;

View File

@ -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), ocf_ctx->resources.req = env_mpool_create(sizeof(struct ocf_request),
sizeof(struct ocf_map_info), ENV_MEM_NORMAL, ocf_req_size_128, 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) if (ocf_ctx->resources.req == NULL)
return -1; return -1;