2008-07-31 08:11:05 -07:00
|
|
|
/*
|
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
|
|
|
*/
|
2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2008 Cedric BAIL
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2008-07-31 08:11:05 -07:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-31 08:11:05 -07:00
|
|
|
#include "eina_inlist.h"
|
|
|
|
#include "eina_error.h"
|
2008-08-18 01:18:16 -07:00
|
|
|
#include "eina_module.h"
|
2008-09-18 07:22:48 -07:00
|
|
|
#include "eina_mempool.h"
|
2009-07-15 06:13:52 -07:00
|
|
|
#include "eina_trash.h"
|
2008-07-31 08:11:05 -07:00
|
|
|
|
|
|
|
#include "eina_private.h"
|
|
|
|
|
|
|
|
typedef struct _Chained_Mempool Chained_Mempool;
|
|
|
|
struct _Chained_Mempool
|
|
|
|
{
|
|
|
|
Eina_Inlist *first;
|
|
|
|
const char *name;
|
|
|
|
int item_size;
|
|
|
|
int pool_size;
|
|
|
|
int usage;
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
#endif
|
2008-07-31 08:11:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _Chained_Pool Chained_Pool;
|
|
|
|
struct _Chained_Pool
|
|
|
|
{
|
2008-09-18 07:22:48 -07:00
|
|
|
EINA_INLIST;
|
2009-07-15 06:13:52 -07:00
|
|
|
Eina_Trash *base;
|
2008-07-31 08:11:05 -07:00
|
|
|
int usage;
|
|
|
|
};
|
|
|
|
|
2008-10-01 09:30:30 -07:00
|
|
|
static inline Chained_Pool *
|
2008-07-31 08:11:05 -07:00
|
|
|
_eina_chained_mp_pool_new(Chained_Mempool *pool)
|
|
|
|
{
|
|
|
|
Chained_Pool *p;
|
2009-07-15 06:13:52 -07:00
|
|
|
unsigned char *ptr;
|
2008-07-31 08:11:05 -07:00
|
|
|
int item_alloc, i;
|
|
|
|
|
|
|
|
item_alloc = ((pool->item_size + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
|
|
|
|
p = malloc(sizeof(Chained_Pool) + (pool->pool_size * item_alloc));
|
2009-07-15 06:13:52 -07:00
|
|
|
ptr = (unsigned char *) (p + 1);
|
2008-07-31 08:11:05 -07:00
|
|
|
p->usage = 0;
|
2009-07-15 06:13:52 -07:00
|
|
|
p->base = NULL;
|
|
|
|
for (i = 0; i < pool->pool_size; ++i, ptr += item_alloc)
|
|
|
|
eina_trash_push(&p->base, ptr);
|
2008-07-31 08:11:05 -07:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-10-01 09:30:30 -07:00
|
|
|
static inline void
|
2008-07-31 08:11:05 -07:00
|
|
|
_eina_chained_mp_pool_free(Chained_Pool *p)
|
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
|
|
|
|
{
|
|
|
|
Chained_Mempool *pool = data;
|
2008-09-17 08:18:35 -07:00
|
|
|
Chained_Pool *p = NULL;
|
2008-07-31 08:11:05 -07:00
|
|
|
void *mem;
|
|
|
|
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_lock(&pool->mutex);
|
|
|
|
#endif
|
|
|
|
|
2008-07-31 08:11:05 -07:00
|
|
|
// look 4 pool from 2nd bucket on
|
2008-10-21 05:19:57 -07:00
|
|
|
EINA_INLIST_FOREACH(pool->first, p)
|
2008-07-31 08:11:05 -07:00
|
|
|
{
|
|
|
|
// base is not NULL - has a free slot
|
|
|
|
if (p->base)
|
|
|
|
{
|
2008-10-01 09:30:30 -07:00
|
|
|
pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
|
2008-07-31 08:11:05 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-02-17 06:18:14 -08:00
|
|
|
|
2008-07-31 08:11:05 -07:00
|
|
|
// we have reached the end of the list - no free pools
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
p = _eina_chained_mp_pool_new(pool);
|
2009-02-17 06:18:14 -08:00
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_unlock(&pool->mutex);
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-18 07:22:48 -07:00
|
|
|
pool->first = eina_inlist_prepend(pool->first, EINA_INLIST_GET(p));
|
2008-07-31 08:11:05 -07:00
|
|
|
}
|
2009-07-15 06:13:52 -07:00
|
|
|
// Request a free pointer
|
|
|
|
mem = eina_trash_pop(&p->base);
|
2008-07-31 08:11:05 -07:00
|
|
|
// move to end - it just filled up
|
|
|
|
if (!p->base)
|
2009-02-17 06:18:14 -08:00
|
|
|
{
|
|
|
|
pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
|
|
|
|
}
|
2008-07-31 08:11:05 -07:00
|
|
|
p->usage++;
|
|
|
|
pool->usage++;
|
2009-02-17 06:18:14 -08:00
|
|
|
|
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_unlock(&pool->mutex);
|
|
|
|
#endif
|
2008-07-31 08:11:05 -07:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eina_chained_mempool_free(void *data, void *ptr)
|
|
|
|
{
|
|
|
|
Chained_Mempool *pool = data;
|
|
|
|
Chained_Pool *p;
|
|
|
|
void *pmem;
|
|
|
|
int item_alloc, psize;
|
|
|
|
|
|
|
|
item_alloc = ((pool->item_size + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
|
|
|
|
psize = item_alloc * pool->pool_size;
|
|
|
|
// look 4 pool
|
2008-09-30 05:55:33 -07:00
|
|
|
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_lock(&pool->mutex);
|
|
|
|
#endif
|
|
|
|
|
2008-10-21 05:19:57 -07:00
|
|
|
EINA_INLIST_FOREACH(pool->first, p)
|
2008-07-31 08:11:05 -07:00
|
|
|
{
|
|
|
|
// pool mem base
|
|
|
|
pmem = (void *)(((unsigned char *)p) + sizeof(Chained_Pool));
|
|
|
|
// is it in pool mem?
|
|
|
|
if ((ptr >= pmem) && ((unsigned char *)ptr < (((unsigned char *)pmem) + psize)))
|
|
|
|
{
|
|
|
|
// freed node points to prev free node
|
2009-07-15 06:13:52 -07:00
|
|
|
eina_trash_push(&p->base, ptr);
|
2008-07-31 08:11:05 -07:00
|
|
|
// next free node is now the one we freed
|
|
|
|
p->usage--;
|
|
|
|
pool->usage--;
|
|
|
|
if (p->usage == 0)
|
2008-10-01 09:30:30 -07:00
|
|
|
{
|
|
|
|
// free bucket
|
|
|
|
pool->first = eina_inlist_remove(pool->first, EINA_INLIST_GET(p));
|
|
|
|
_eina_chained_mp_pool_free(p);
|
|
|
|
}
|
2008-07-31 08:11:05 -07:00
|
|
|
else
|
2008-10-01 09:30:30 -07:00
|
|
|
{
|
|
|
|
// move to front
|
|
|
|
pool->first = eina_inlist_promote(pool->first, EINA_INLIST_GET(p));
|
|
|
|
}
|
2008-07-31 08:11:05 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-02-17 06:18:14 -08:00
|
|
|
|
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_unlock(&pool->mutex);
|
|
|
|
#endif
|
2008-07-31 08:11:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
eina_chained_mempool_realloc(__UNUSED__ void *data, __UNUSED__ void *element, __UNUSED__ unsigned int size)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
eina_chained_mempool_init(const char *context, __UNUSED__ const char *option, va_list args)
|
|
|
|
{
|
|
|
|
Chained_Mempool *mp;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = context ? strlen(context) + 1 : 0;
|
|
|
|
|
|
|
|
mp = calloc(1, sizeof(Chained_Mempool) + length);
|
|
|
|
if (!mp) return NULL;
|
|
|
|
|
|
|
|
mp->item_size = va_arg(args, int);
|
|
|
|
mp->pool_size = va_arg(args, int);
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
|
|
|
mp->name = (const char*) (mp + 1);
|
|
|
|
memcpy((char*) mp->name, context, length);
|
|
|
|
}
|
|
|
|
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_init(&mp->mutex, NULL);
|
|
|
|
#endif
|
|
|
|
|
2008-07-31 08:11:05 -07:00
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eina_chained_mempool_shutdown(void *data)
|
|
|
|
{
|
|
|
|
Chained_Mempool *mp;
|
|
|
|
|
|
|
|
mp = (Chained_Mempool *) data;
|
|
|
|
|
|
|
|
while (mp->first)
|
|
|
|
{
|
|
|
|
Chained_Pool *p = (Chained_Pool *) mp->first;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (p->usage > 0)
|
|
|
|
EINA_ERROR_PINFO("Bad news we are destroying not an empty mempool [%s]\n", mp->name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mp->first = eina_inlist_remove(mp->first, mp->first);
|
|
|
|
_eina_chained_mp_pool_free(p);
|
|
|
|
}
|
|
|
|
|
2009-02-17 06:18:14 -08:00
|
|
|
#ifdef EFL_HAVE_PTHREAD
|
|
|
|
pthread_mutex_destroy(&mp->mutex);
|
|
|
|
#endif
|
|
|
|
|
2008-07-31 08:11:05 -07:00
|
|
|
free(mp);
|
|
|
|
}
|
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
static Eina_Mempool_Backend mp_backend = {
|
2008-09-24 05:55:31 -07:00
|
|
|
.name ="chained_mempool",
|
2008-07-31 08:11:05 -07:00
|
|
|
.init = &eina_chained_mempool_init,
|
|
|
|
.shutdown = &eina_chained_mempool_shutdown,
|
|
|
|
.realloc = &eina_chained_mempool_realloc,
|
|
|
|
.alloc = &eina_chained_mempool_malloc,
|
|
|
|
.free = &eina_chained_mempool_free
|
|
|
|
};
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
Eina_Bool chained_init(void)
|
|
|
|
{
|
|
|
|
return eina_mempool_register(&mp_backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void chained_shutdown(void)
|
|
|
|
{
|
|
|
|
eina_mempool_unregister(&mp_backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef EINA_STATIC_BUILD_CHAINED_POOL
|
|
|
|
|
|
|
|
EINA_MODULE_INIT(chained_init);
|
|
|
|
EINA_MODULE_SHUTDOWN(chained_shutdown);
|
2008-09-22 22:17:49 -07:00
|
|
|
|
|
|
|
#endif /* ! EINA_STATIC_BUILD_CHAINED_POOL */
|