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 */
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;
}

6
env/posix/ocf_env.h vendored
View File

@@ -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);

View File

@@ -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;

View File

@@ -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