*/
#define __rseq_percpu
-struct rseq_mmap_attr;
+struct rseq_pool_attr;
struct rseq_percpu_pool;
/*
* next power of two). The reserved allocation size is @percpu_len, and
* the maximum CPU value expected is (@max_nr_cpus - 1).
*
- * The @mmap_attr pointer used to specify the memory allocator callbacks
- * to use to manage the memory for the pool. If NULL, use a default
- * internal implementation. The @mmap_attr can be destroyed immediately
- * after rseq_percpu_pool_create() returns. The caller keeps ownership
- * of @mmap_attr.
+ * The @pool_attr pointer used to specify the pool attributes. If NULL,
+ * use a default attribute values. The @pool_attr can be destroyed
+ * immediately after rseq_percpu_pool_create() returns. The caller keeps
+ * ownership of @pool_attr.
*
* The argument @pool_name can be used to given a name to the pool for
* debugging purposes. It can be NULL if no name is given.
* ENOMEM: Not enough resources (memory or pool indexes) available to
* allocate pool.
*
- * In addition, if the mmap_attr mmap callback fails, NULL is returned
- * and errno is propagated from the callback. The default callback can
+ * In addition, if the attr mmap callback fails, NULL is returned and
+ * errno is propagated from the callback. The default callback can
* return errno=ENOMEM.
*
* This API is MT-safe.
*/
struct rseq_percpu_pool *rseq_percpu_pool_create(const char *pool_name,
size_t item_len, size_t percpu_len, int max_nr_cpus,
- const struct rseq_mmap_attr *mmap_attr,
+ const struct rseq_pool_attr *attr,
int flags);
/*
int rseq_percpu_pool_init_numa(struct rseq_percpu_pool *pool, int numa_flags);
/*
- * rseq_mmap_attr_create: Create a mmap attribute structure.
+ * rseq_pool_attr_create: Create a pool attribute structure.
+ */
+struct rseq_pool_attr *rseq_pool_attr_create(void);
+
+/*
+ * rseq_pool_attr_destroy: Destroy a pool attribute structure.
+ */
+void rseq_pool_attr_destroy(struct rseq_pool_attr *attr);
+
+/*
+ * rseq_pool_attr_set_mmap: Set pool attribute structure mmap functions.
*
* The @mmap_func callback used to map the memory for the pool.
*
* The @mmap_priv argument is a private data pointer passed to both
* @mmap_func and @munmap_func callbacks.
*/
-struct rseq_mmap_attr *rseq_mmap_attr_create(void *(*mmap_func)(void *priv, size_t len),
+void rseq_pool_attr_set_mmap(struct rseq_pool_attr *attr,
+ void *(*mmap_func)(void *priv, size_t len),
int (*munmap_func)(void *priv, void *ptr, size_t len),
void *mmap_priv);
-/*
- * rseq_mmap_attr_destroy: Destroy a mmap attribute structure.
- */
-void rseq_mmap_attr_destroy(struct rseq_mmap_attr *attr);
-
#ifdef __cplusplus
}
#endif
/* This lock protects pool create/destroy. */
static pthread_mutex_t pool_lock = PTHREAD_MUTEX_INITIALIZER;
-struct rseq_mmap_attr {
+struct rseq_pool_attr {
+ bool mmap_set;
void *(*mmap_func)(void *priv, size_t len);
int (*munmap_func)(void *priv, void *ptr, size_t len);
void *mmap_priv;
/* This lock protects allocation/free within the pool. */
pthread_mutex_t lock;
- struct rseq_mmap_attr mmap_attr;
+ struct rseq_pool_attr attr;
char *name;
/* Track alloc/free. */
* free-list.
*/
destroy_alloc_bitmap(pool);
- ret = pool->mmap_attr.munmap_func(pool->mmap_attr.mmap_priv, pool->base,
+ ret = pool->attr.munmap_func(pool->attr.mmap_priv, pool->base,
pool->percpu_len * pool->max_nr_cpus);
if (ret)
goto end;
struct rseq_percpu_pool *rseq_percpu_pool_create(const char *pool_name,
size_t item_len, size_t percpu_len, int max_nr_cpus,
- const struct rseq_mmap_attr *mmap_attr,
+ const struct rseq_pool_attr *_attr,
int flags)
{
- void *(*mmap_func)(void *priv, size_t len);
- int (*munmap_func)(void *priv, void *ptr, size_t len);
- void *mmap_priv;
struct rseq_percpu_pool *pool;
+ struct rseq_pool_attr attr = {};
void *base;
unsigned int i;
int order;
return NULL;
}
- if (mmap_attr) {
- mmap_func = mmap_attr->mmap_func;
- munmap_func = mmap_attr->munmap_func;
- mmap_priv = mmap_attr->mmap_priv;
- } else {
- mmap_func = default_mmap_func;
- munmap_func = default_munmap_func;
- mmap_priv = NULL;
+ if (_attr)
+ memcpy(&attr, _attr, sizeof(attr));
+ if (!attr.mmap_set) {
+ attr.mmap_func = default_mmap_func;
+ attr.munmap_func = default_munmap_func;
+ attr.mmap_priv = NULL;
}
+
pthread_mutex_lock(&pool_lock);
/* Linear scan in array of pools to find empty spot. */
for (i = FIRST_POOL; i < MAX_NR_POOLS; i++) {
goto end;
found_empty:
- base = mmap_func(mmap_priv, percpu_len * max_nr_cpus);
+ base = attr.mmap_func(attr.mmap_priv, percpu_len * max_nr_cpus);
if (!base)
goto error_alloc;
pthread_mutex_init(&pool->lock, NULL);
pool->index = i;
pool->item_len = item_len;
pool->item_order = order;
- pool->mmap_attr.mmap_func = mmap_func;
- pool->mmap_attr.munmap_func = munmap_func;
- pool->mmap_attr.mmap_priv = mmap_priv;
+ memcpy(&pool->attr, &attr, sizeof(attr));
if (pool_name) {
pool->name = strdup(pool_name);
return __rseq_percpu_pool_set_malloc(pool_set, len, true);
}
-struct rseq_mmap_attr *rseq_mmap_attr_create(void *(*mmap_func)(void *priv, size_t len),
+struct rseq_pool_attr *rseq_pool_attr_create(void)
+{
+ return calloc(1, sizeof(struct rseq_pool_attr));
+}
+
+void rseq_pool_attr_destroy(struct rseq_pool_attr *attr)
+{
+ free(attr);
+}
+
+void rseq_pool_attr_set_mmap(struct rseq_pool_attr *attr,
+ void *(*mmap_func)(void *priv, size_t len),
int (*munmap_func)(void *priv, void *ptr, size_t len),
void *mmap_priv)
{
- struct rseq_mmap_attr *attr = calloc(1, sizeof(struct rseq_mmap_attr));
-
- if (!attr)
- return NULL;
+ attr->mmap_set = true;
attr->mmap_func = mmap_func;
attr->munmap_func = munmap_func;
attr->mmap_priv = mmap_priv;
- return attr;
-}
-
-void rseq_mmap_attr_destroy(struct rseq_mmap_attr *attr)
-{
- free(attr);
}