Add utils_mpool
Signed-off-by: Robert Baldyga <robert.baldyga@intel.com>
This commit is contained in:
114
modules/cas_cache/utils/utils_mpool.c
Normal file
114
modules/cas_cache/utils/utils_mpool.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright(c) 2012-2018 Intel Corporation
|
||||
* SPDX-License-Identifier: BSD-3-Clause-Clear
|
||||
*/
|
||||
|
||||
#include "ocf_env.h"
|
||||
#include "utils_mpool.h"
|
||||
|
||||
|
||||
struct cas_mpool *cas_mpool_create(uint32_t hdr_size, uint32_t size, int flags,
|
||||
int mpool_max, const char *name_perfix)
|
||||
{
|
||||
uint32_t i;
|
||||
char name[ALLOCATOR_NAME_MAX] = { '\0' };
|
||||
int result;
|
||||
struct cas_mpool *mpool;
|
||||
|
||||
mpool = env_zalloc(sizeof(*mpool), ENV_MEM_NORMAL);
|
||||
if (!mpool)
|
||||
return NULL;
|
||||
|
||||
mpool->item_size = size;
|
||||
mpool->hdr_size = hdr_size;
|
||||
mpool->flags = flags;
|
||||
|
||||
for (i = 0; i < min(cas_mpool_max, mpool_max + 1); i++) {
|
||||
result = snprintf(name, sizeof(name), "%s_%u", name_perfix,
|
||||
(1 << i));
|
||||
if (result < 0 || result >= sizeof(name))
|
||||
goto cas_multi_allocator_create_ERROR;
|
||||
|
||||
mpool->allocator[i] = env_allocator_create(
|
||||
hdr_size + (size * (1 << i)), name);
|
||||
|
||||
if (!mpool->allocator[i])
|
||||
goto err;
|
||||
}
|
||||
|
||||
return mpool;
|
||||
|
||||
err:
|
||||
cas_mpool_destroy(mpool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void cas_mpool_destroy(struct cas_mpool *mallocator)
|
||||
{
|
||||
if (mallocator) {
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < cas_mpool_max; i++)
|
||||
if (mallocator->allocator[i])
|
||||
env_allocator_destroy(mallocator->allocator[i]);
|
||||
|
||||
env_free(mallocator);
|
||||
}
|
||||
}
|
||||
|
||||
static env_allocator *cas_mpool_get_allocator(
|
||||
struct cas_mpool *mallocator, uint32_t count)
|
||||
{
|
||||
unsigned int idx;
|
||||
|
||||
if (unlikely(count == 0))
|
||||
return cas_mpool_1;
|
||||
|
||||
idx = 31 - __builtin_clz(count);
|
||||
|
||||
if (__builtin_ffs(count) <= idx)
|
||||
idx++;
|
||||
|
||||
if (idx >= cas_mpool_max)
|
||||
return NULL;
|
||||
|
||||
return mallocator->allocator[idx];
|
||||
}
|
||||
|
||||
void *cas_mpool_new_f(struct cas_mpool *mpool, uint32_t count, int flags)
|
||||
{
|
||||
void *items = NULL;
|
||||
env_allocator *allocator;
|
||||
|
||||
allocator = cas_mpool_get_allocator(mpool, count);
|
||||
|
||||
if (allocator)
|
||||
items = env_allocator_new(allocator);
|
||||
else
|
||||
items = env_zalloc(mpool->hdr_size + (mpool->item_size * count), flags);
|
||||
|
||||
#ifdef ZERO_OR_NULL_PTR
|
||||
if (ZERO_OR_NULL_PTR(items))
|
||||
return NULL;
|
||||
#endif
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
void *cas_mpool_new(struct cas_mpool *mpool, uint32_t count)
|
||||
{
|
||||
return cas_mpool_new_f(mpool, count, mpool->flags);
|
||||
}
|
||||
|
||||
void cas_mpool_del(struct cas_mpool *mpool,
|
||||
void *items, uint32_t count)
|
||||
{
|
||||
env_allocator *allocator;
|
||||
|
||||
allocator = cas_mpool_get_allocator(mpool, count);
|
||||
|
||||
if (allocator)
|
||||
env_allocator_del(allocator, items);
|
||||
else
|
||||
env_free(items);
|
||||
}
|
92
modules/cas_cache/utils/utils_mpool.h
Normal file
92
modules/cas_cache/utils/utils_mpool.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright(c) 2012-2019 Intel Corporation
|
||||
* SPDX-License-Identifier: BSD-3-Clause-Clear
|
||||
*/
|
||||
|
||||
#ifndef UTILS_MPOOL_H_
|
||||
#define UTILS_MPOOL_H_
|
||||
|
||||
#define ALLOCATOR_NAME_MAX 128
|
||||
|
||||
enum {
|
||||
cas_mpool_1,
|
||||
cas_mpool_2,
|
||||
cas_mpool_4,
|
||||
cas_mpool_8,
|
||||
cas_mpool_16,
|
||||
cas_mpool_32,
|
||||
cas_mpool_64,
|
||||
cas_mpool_128,
|
||||
|
||||
cas_mpool_max
|
||||
};
|
||||
|
||||
struct cas_mpool {
|
||||
uint32_t item_size;
|
||||
/*!< Size of specific item of memory pool */
|
||||
|
||||
uint32_t hdr_size;
|
||||
/*!< Header size before items */
|
||||
|
||||
env_allocator *allocator[cas_mpool_max];
|
||||
/*!< OS handle to memory pool */
|
||||
|
||||
int flags;
|
||||
/*!< Allocation flags */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Create CAS memory pool
|
||||
*
|
||||
* @param hdr_size Header size before array of items
|
||||
* @param size Size of particular item
|
||||
* @param flags Allocation flags
|
||||
* @param mpool_max Maximal allocator size (power of two)
|
||||
* @param name_prefix Format name prefix
|
||||
*
|
||||
* @return CAS memory pool
|
||||
*/
|
||||
struct cas_mpool *cas_mpool_create(uint32_t hdr_size, uint32_t size, int flags,
|
||||
int mpool_max, const char *name_perfix);
|
||||
|
||||
/**
|
||||
* @brief Destroy existing memory pool
|
||||
*
|
||||
* @param mpool memory pool
|
||||
*/
|
||||
void cas_mpool_destroy(struct cas_mpool *mpool);
|
||||
|
||||
/**
|
||||
* @brief Allocate new items of memory pool
|
||||
*
|
||||
* @note Allocation based on ATOMIC memory pool and this function can be called
|
||||
* when IRQ disable
|
||||
*
|
||||
* @param mpool CAS memory pool reference
|
||||
* @param count Count of elements to be allocated
|
||||
*
|
||||
* @return Pointer to the new items
|
||||
*/
|
||||
void *cas_mpool_new(struct cas_mpool *mpool, uint32_t count);
|
||||
|
||||
/**
|
||||
* @brief Allocate new items of memory pool with specified allocation flag
|
||||
*
|
||||
* @param mpool CAS memory pool reference
|
||||
* @param count Count of elements to be allocated
|
||||
* @param flags Kernel allocation falgs
|
||||
*
|
||||
* @return Pointer to the new items
|
||||
*/
|
||||
void *cas_mpool_new_f(struct cas_mpool *mpool, uint32_t count, int flags);
|
||||
|
||||
/**
|
||||
* @brief Free existing items of memory pool
|
||||
*
|
||||
* @param mpool CAS memory pool reference
|
||||
* @param items Items to be freed
|
||||
* @param count - Count of elements to be free
|
||||
*/
|
||||
void cas_mpool_del(struct cas_mpool *mpool, void *items, uint32_t count);
|
||||
|
||||
#endif /* UTILS_MPOOL_H_ */
|
Reference in New Issue
Block a user