summaryrefslogtreecommitdiffstats
path: root/src/mem_pool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mem_pool.c')
-rw-r--r--src/mem_pool.c213
1 files changed, 107 insertions, 106 deletions
diff --git a/src/mem_pool.c b/src/mem_pool.c
index ce6eab86e..23907d260 100644
--- a/src/mem_pool.c
+++ b/src/mem_pool.c
@@ -31,12 +31,12 @@
#define MUTEX_SPIN_COUNT 100
#ifdef _THREAD_SAFE
-pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
-#define STAT_LOCK() do { pthread_mutex_lock (&stat_mtx); } while (0)
-#define STAT_UNLOCK() do { pthread_mutex_unlock (&stat_mtx); } while (0)
+pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
+# define STAT_LOCK() do { pthread_mutex_lock (&stat_mtx); } while (0)
+# define STAT_UNLOCK() do { pthread_mutex_unlock (&stat_mtx); } while (0)
#else
-#define STAT_LOCK() do {} while (0)
-#define STAT_UNLOCK() do {} while (0)
+# define STAT_LOCK() do {} while (0)
+# define STAT_UNLOCK() do {} while (0)
#endif
/*
@@ -50,65 +50,65 @@ pthread_mutex_t stat_mtx = PTHREAD_MUTEX_INITIALIZER;
#undef MEMORY_GREEDY
/* Internal statistic */
-static memory_pool_stat_t *mem_pool_stat = NULL;
+static memory_pool_stat_t *mem_pool_stat = NULL;
-static struct _pool_chain *
-pool_chain_new (memory_pool_ssize_t size)
+static struct _pool_chain *
+pool_chain_new (memory_pool_ssize_t size)
{
- struct _pool_chain *chain;
+ struct _pool_chain *chain;
g_assert (size > 0);
chain = g_slice_alloc (sizeof (struct _pool_chain));
- g_assert (chain != NULL);
+ g_assert (chain != NULL);
chain->begin = g_slice_alloc (size);
- g_assert (chain->begin != NULL);
+ g_assert (chain->begin != NULL);
chain->len = size;
chain->pos = chain->begin;
chain->next = NULL;
STAT_LOCK ();
- mem_pool_stat->chunks_allocated ++;
+ mem_pool_stat->chunks_allocated++;
STAT_UNLOCK ();
-
+
return chain;
}
static struct _pool_chain_shared *
-pool_chain_new_shared (memory_pool_ssize_t size)
+pool_chain_new_shared (memory_pool_ssize_t size)
{
- struct _pool_chain_shared *chain;
+ struct _pool_chain_shared *chain;
#if defined(HAVE_MMAP_ANON)
- chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
+ chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
g_assert (chain != MAP_FAILED);
- chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared);
+ chain->begin = ((u_char *) chain) + sizeof (struct _pool_chain_shared);
g_assert (chain->begin != MAP_FAILED);
#elif defined(HAVE_MMAP_ZERO)
- int fd;
+ int fd;
fd = open ("/dev/zero", O_RDWR);
if (fd == -1) {
return NULL;
}
- chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ chain = mmap (NULL, size + sizeof (struct _pool_chain_shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
g_assert (chain != MAP_FAILED);
- chain->begin = ((u_char *)chain) + sizeof (struct _pool_chain_shared);
+ chain->begin = ((u_char *) chain) + sizeof (struct _pool_chain_shared);
g_assert (chain->begin != MAP_FAILED);
#else
-# error No mmap methods are defined
+# error No mmap methods are defined
#endif
chain->len = size;
chain->pos = chain->begin;
chain->lock = 0;
chain->next = NULL;
STAT_LOCK ();
- mem_pool_stat->shared_chunks_allocated ++;
+ mem_pool_stat->shared_chunks_allocated++;
STAT_UNLOCK ();
-
+
return chain;
}
@@ -118,47 +118,47 @@ pool_chain_new_shared (memory_pool_ssize_t size)
* @param size size of pool's page
* @return new memory pool object
*/
-memory_pool_t*
+memory_pool_t *
memory_pool_new (memory_pool_ssize_t size)
{
- memory_pool_t *new;
-
+ memory_pool_t *new;
+
g_assert (size > 0);
/* Allocate statistic structure if it is not allocated before */
if (mem_pool_stat == NULL) {
#if defined(HAVE_MMAP_ANON)
- mem_pool_stat = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
+ mem_pool_stat = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
g_assert (stat != MAP_FAILED);
#elif defined(HAVE_MMAP_ZERO)
- int fd;
+ int fd;
fd = open ("/dev/zero", O_RDWR);
g_assert (fd != -1);
- mem_pool_stat = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ mem_pool_stat = mmap (NULL, sizeof (memory_pool_stat_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
g_assert (chain != MAP_FAILED);
#else
-# error No mmap methods are defined
+# error No mmap methods are defined
#endif
}
new = g_slice_alloc (sizeof (memory_pool_t));
- g_assert (new != NULL);
+ g_assert (new != NULL);
new->cur_pool = pool_chain_new (size);
new->shared_pool = NULL;
new->first_pool = new->cur_pool;
new->destructors = NULL;
- mem_pool_stat->pools_allocated ++;
+ mem_pool_stat->pools_allocated++;
return new;
}
-void *
-memory_pool_alloc (memory_pool_t *pool, memory_pool_ssize_t size)
+void *
+memory_pool_alloc (memory_pool_t * pool, memory_pool_ssize_t size)
{
- u_char *tmp;
- struct _pool_chain *new, *cur;
+ u_char *tmp;
+ struct _pool_chain *new, *cur;
if (pool) {
#ifdef MEMORY_GREEDY
@@ -176,7 +176,7 @@ memory_pool_alloc (memory_pool_t *pool, memory_pool_ssize_t size)
new = pool_chain_new (cur->len);
}
else {
- mem_pool_stat->oversized_chunks ++;
+ mem_pool_stat->oversized_chunks++;
new = pool_chain_new (size + cur->len);
}
/* Attach new pool to chain */
@@ -187,7 +187,7 @@ memory_pool_alloc (memory_pool_t *pool, memory_pool_ssize_t size)
mem_pool_stat->bytes_allocated += size;
STAT_UNLOCK ();
return new->begin;
- }
+ }
tmp = cur->pos;
cur->pos += size;
STAT_LOCK ();
@@ -198,31 +198,31 @@ memory_pool_alloc (memory_pool_t *pool, memory_pool_ssize_t size)
return NULL;
}
-void *
-memory_pool_alloc0 (memory_pool_t *pool, memory_pool_ssize_t size)
+void *
+memory_pool_alloc0 (memory_pool_t * pool, memory_pool_ssize_t size)
{
- void *pointer = memory_pool_alloc (pool, size);
+ void *pointer = memory_pool_alloc (pool, size);
if (pointer) {
bzero (pointer, size);
}
return pointer;
}
-void *
-memory_pool_alloc0_shared (memory_pool_t *pool, memory_pool_ssize_t size)
+void *
+memory_pool_alloc0_shared (memory_pool_t * pool, memory_pool_ssize_t size)
{
- void *pointer = memory_pool_alloc_shared (pool, size);
+ void *pointer = memory_pool_alloc_shared (pool, size);
if (pointer) {
bzero (pointer, size);
}
return pointer;
}
-char *
-memory_pool_strdup (memory_pool_t *pool, const char *src)
+char *
+memory_pool_strdup (memory_pool_t * pool, const char *src)
{
- memory_pool_ssize_t len;
- char *newstr;
+ memory_pool_ssize_t len;
+ char *newstr;
if (src == NULL) {
return NULL;
@@ -235,10 +235,10 @@ memory_pool_strdup (memory_pool_t *pool, const char *src)
return newstr;
}
-char *
-memory_pool_fstrdup (memory_pool_t *pool, const struct f_str_s *src)
+char *
+memory_pool_fstrdup (memory_pool_t * pool, const struct f_str_s *src)
{
- char *newstr;
+ char *newstr;
if (src == NULL) {
return NULL;
@@ -251,11 +251,11 @@ memory_pool_fstrdup (memory_pool_t *pool, const struct f_str_s *src)
}
-char *
-memory_pool_strdup_shared (memory_pool_t *pool, const char *src)
+char *
+memory_pool_strdup_shared (memory_pool_t * pool, const char *src)
{
- memory_pool_ssize_t len;
- char *newstr;
+ memory_pool_ssize_t len;
+ char *newstr;
if (src == NULL) {
return NULL;
@@ -269,11 +269,11 @@ memory_pool_strdup_shared (memory_pool_t *pool, const char *src)
}
-void *
-memory_pool_alloc_shared (memory_pool_t *pool, memory_pool_ssize_t size)
+void *
+memory_pool_alloc_shared (memory_pool_t * pool, memory_pool_ssize_t size)
{
- u_char *tmp;
- struct _pool_chain_shared *new, *cur;
+ u_char *tmp;
+ struct _pool_chain_shared *new, *cur;
if (pool) {
g_assert (size > 0);
@@ -293,7 +293,7 @@ memory_pool_alloc_shared (memory_pool_t *pool, memory_pool_ssize_t size)
new = pool_chain_new_shared (cur->len);
}
else {
- mem_pool_stat->oversized_chunks ++;
+ mem_pool_stat->oversized_chunks++;
new = pool_chain_new_shared (size + cur->len);
}
/* Attach new pool to chain */
@@ -316,12 +316,12 @@ memory_pool_alloc_shared (memory_pool_t *pool, memory_pool_ssize_t size)
/* Find pool for a pointer, returns NULL if pointer is not in pool */
static struct _pool_chain_shared *
-memory_pool_find_pool (memory_pool_t *pool, void *pointer)
+memory_pool_find_pool (memory_pool_t * pool, void *pointer)
{
- struct _pool_chain_shared *cur = pool->shared_pool;
+ struct _pool_chain_shared *cur = pool->shared_pool;
while (cur) {
- if ((u_char *)pointer >= cur->begin && (u_char *)pointer <= (cur->begin + cur->len)) {
+ if ((u_char *) pointer >= cur->begin && (u_char *) pointer <= (cur->begin + cur->len)) {
return cur;
}
cur = cur->next;
@@ -331,7 +331,7 @@ memory_pool_find_pool (memory_pool_t *pool, void *pointer)
}
static inline int
-__mutex_spin (memory_pool_mutex_t *mutex)
+__mutex_spin (memory_pool_mutex_t * mutex)
{
/* check spin count */
if (g_atomic_int_dec_and_test (&mutex->spin)) {
@@ -350,23 +350,23 @@ __mutex_spin (memory_pool_mutex_t *mutex)
g_atomic_int_set (&mutex->spin, MUTEX_SPIN_COUNT);
}
#ifdef HAVE_ASM_PAUSE
- __asm __volatile("pause");
+ __asm __volatile ("pause");
#elif defined(HAVE_SCHED_YIELD)
(void)sched_yield ();
#elif defined(HAVE_NANOSLEEP)
- struct timespec ts;
+ struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = MUTEX_SLEEP_TIME;
/* Spin */
while (nanosleep (&ts, &ts) == -1 && errno == EINTR);
#else
-# error No methods to spin are defined
+# error No methods to spin are defined
#endif
return 1;
}
static void
-memory_pool_mutex_spin (memory_pool_mutex_t *mutex)
+memory_pool_mutex_spin (memory_pool_mutex_t * mutex)
{
while (!g_atomic_int_compare_and_exchange (&mutex->lock, 0, 1)) {
if (!__mutex_spin (mutex)) {
@@ -377,34 +377,35 @@ memory_pool_mutex_spin (memory_pool_mutex_t *mutex)
/* Simple implementation of spinlock */
void
-memory_pool_lock_shared (memory_pool_t *pool, void *pointer)
+memory_pool_lock_shared (memory_pool_t * pool, void *pointer)
{
- struct _pool_chain_shared *chain;
+ struct _pool_chain_shared *chain;
chain = memory_pool_find_pool (pool, pointer);
if (chain == NULL) {
return;
}
-
+
memory_pool_lock_mutex (chain->lock);
}
-void memory_pool_unlock_shared (memory_pool_t *pool, void *pointer)
+void
+memory_pool_unlock_shared (memory_pool_t * pool, void *pointer)
{
- struct _pool_chain_shared *chain;
+ struct _pool_chain_shared *chain;
chain = memory_pool_find_pool (pool, pointer);
if (chain == NULL) {
return;
}
-
+
memory_pool_unlock_mutex (chain->lock);
}
void
-memory_pool_add_destructor (memory_pool_t *pool, pool_destruct_func func, void *data)
+memory_pool_add_destructor (memory_pool_t * pool, pool_destruct_func func, void *data)
{
- struct _pool_destructors *cur, *tmp;
+ struct _pool_destructors *cur, *tmp;
cur = memory_pool_alloc (pool, sizeof (struct _pool_destructors));
if (cur) {
@@ -426,16 +427,16 @@ memory_pool_add_destructor (memory_pool_t *pool, pool_destruct_func func, void *
}
void
-memory_pool_delete (memory_pool_t *pool)
+memory_pool_delete (memory_pool_t * pool)
{
- struct _pool_chain *cur = pool->first_pool, *tmp;
- struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared;
- struct _pool_destructors *destructor = pool->destructors;
-
+ struct _pool_chain *cur = pool->first_pool, *tmp;
+ struct _pool_chain_shared *cur_shared = pool->shared_pool, *tmp_shared;
+ struct _pool_destructors *destructor = pool->destructors;
+
/* Call all pool destructors */
while (destructor) {
/* Avoid calling destructors for NULL pointers */
- if (destructor->data != NULL) {
+ if (destructor->data != NULL) {
destructor->func (destructor->data);
}
destructor = destructor->prev;
@@ -447,7 +448,7 @@ memory_pool_delete (memory_pool_t *pool)
g_slice_free1 (tmp->len, tmp->begin);
g_slice_free (struct _pool_chain, tmp);
STAT_LOCK ();
- mem_pool_stat->chunks_freed ++;
+ mem_pool_stat->chunks_freed++;
STAT_UNLOCK ();
}
/* Unmap shared memory */
@@ -456,16 +457,16 @@ memory_pool_delete (memory_pool_t *pool)
cur_shared = cur_shared->next;
munmap (tmp_shared, tmp_shared->len + sizeof (struct _pool_chain_shared));
STAT_LOCK ();
- mem_pool_stat->chunks_freed ++;
+ mem_pool_stat->chunks_freed++;
STAT_UNLOCK ();
}
- mem_pool_stat->pools_freed ++;
+ mem_pool_stat->pools_freed++;
g_slice_free (memory_pool_t, pool);
}
void
-memory_pool_stat (memory_pool_stat_t *st)
+memory_pool_stat (memory_pool_stat_t * st)
{
st->pools_allocated = mem_pool_stat->pools_allocated;
st->pools_freed = mem_pool_stat->pools_freed;
@@ -483,16 +484,16 @@ memory_pool_ssize_t
memory_pool_get_size ()
{
#ifdef HAVE_GETPAGESIZE
- return MAX (getpagesize (), FIXED_POOL_SIZE);
+ return MAX (getpagesize (), FIXED_POOL_SIZE);
#else
return MAX (sysconf (_SC_PAGESIZE), FIXED_POOL_SIZE);
#endif
}
-memory_pool_mutex_t*
-memory_pool_get_mutex (memory_pool_t *pool)
+memory_pool_mutex_t *
+memory_pool_get_mutex (memory_pool_t * pool)
{
- memory_pool_mutex_t *res;
+ memory_pool_mutex_t *res;
if (pool != NULL) {
res = memory_pool_alloc_shared (pool, sizeof (memory_pool_mutex_t));
res->lock = 0;
@@ -503,24 +504,24 @@ memory_pool_get_mutex (memory_pool_t *pool)
return NULL;
}
-void
-memory_pool_lock_mutex (memory_pool_mutex_t *mutex)
+void
+memory_pool_lock_mutex (memory_pool_mutex_t * mutex)
{
memory_pool_mutex_spin (mutex);
mutex->owner = getpid ();
}
-void
-memory_pool_unlock_mutex (memory_pool_mutex_t *mutex)
+void
+memory_pool_unlock_mutex (memory_pool_mutex_t * mutex)
{
mutex->owner = 0;
(void)g_atomic_int_compare_and_exchange (&mutex->lock, 1, 0);
}
-memory_pool_rwlock_t*
-memory_pool_get_rwlock (memory_pool_t *pool)
+memory_pool_rwlock_t *
+memory_pool_get_rwlock (memory_pool_t * pool)
{
- memory_pool_rwlock_t *lock;
+ memory_pool_rwlock_t *lock;
lock = memory_pool_alloc_shared (pool, sizeof (memory_pool_rwlock_t));
lock->__r_lock = memory_pool_get_mutex (pool);
@@ -529,8 +530,8 @@ memory_pool_get_rwlock (memory_pool_t *pool)
return lock;
}
-void
-memory_pool_rlock_rwlock (memory_pool_rwlock_t *lock)
+void
+memory_pool_rlock_rwlock (memory_pool_rwlock_t * lock)
{
/* Spin on write lock */
while (g_atomic_int_get (&lock->__w_lock->lock)) {
@@ -538,13 +539,13 @@ memory_pool_rlock_rwlock (memory_pool_rwlock_t *lock)
break;
}
}
-
+
g_atomic_int_inc (&lock->__r_lock->lock);
lock->__r_lock->owner = getpid ();
}
-void
-memory_pool_wlock_rwlock (memory_pool_rwlock_t *lock)
+void
+memory_pool_wlock_rwlock (memory_pool_rwlock_t * lock)
{
/* Spin on write lock first */
memory_pool_lock_mutex (lock->__w_lock);
@@ -555,16 +556,16 @@ memory_pool_wlock_rwlock (memory_pool_rwlock_t *lock)
}
}
-void
-memory_pool_runlock_rwlock (memory_pool_rwlock_t *lock)
+void
+memory_pool_runlock_rwlock (memory_pool_rwlock_t * lock)
{
if (g_atomic_int_get (&lock->__r_lock->lock)) {
(void)g_atomic_int_dec_and_test (&lock->__r_lock->lock);
}
}
-void
-memory_pool_wunlock_rwlock (memory_pool_rwlock_t *lock)
+void
+memory_pool_wunlock_rwlock (memory_pool_rwlock_t * lock)
{
memory_pool_unlock_mutex (lock->__w_lock);
}