2010-07-26 08:58:18 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2010 Cedric BAIL, Vincent Torri
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef EFL_HAVE_POSIX_THREADS
|
2010-10-23 09:56:36 -07:00
|
|
|
# include <pthread.h>
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
|
|
|
|
2010-10-23 09:56:36 -07:00
|
|
|
#include <assert.h>
|
|
|
|
|
2010-07-26 08:58:18 -07:00
|
|
|
#ifdef EFL_HAVE_WIN32_THREADS
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
|
|
|
# undef WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
|
2012-10-10 14:48:34 -07:00
|
|
|
#include "eina_config.h"
|
2010-07-26 08:58:18 -07:00
|
|
|
#include "eina_mempool.h"
|
|
|
|
#include "eina_trash.h"
|
2010-10-22 20:45:06 -07:00
|
|
|
#include "eina_inlist.h"
|
|
|
|
#include "eina_log.h"
|
2011-04-24 09:49:48 -07:00
|
|
|
#include "eina_lock.h"
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
2012-03-16 04:25:14 -07:00
|
|
|
# include <memcheck.h>
|
2010-10-14 12:28:32 -07:00
|
|
|
#endif
|
|
|
|
|
2010-07-26 08:58:18 -07:00
|
|
|
#include "eina_private.h"
|
|
|
|
|
|
|
|
#ifdef INF
|
|
|
|
#undef INF
|
|
|
|
#endif
|
|
|
|
#define INF(...) EINA_LOG_DOM_INFO(_eina_mempool_log_dom, __VA_ARGS__)
|
2010-10-22 20:45:06 -07:00
|
|
|
|
|
|
|
#ifdef WRN
|
|
|
|
#undef WRN
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
2010-10-23 13:57:08 -07:00
|
|
|
#define WRN(...) EINA_LOG_DOM_WARN(_eina_one_big_mp_log_dom, __VA_ARGS__)
|
2010-10-22 20:45:06 -07:00
|
|
|
|
2010-10-23 13:57:08 -07:00
|
|
|
static int _eina_one_big_mp_log_dom = -1;
|
2010-07-26 08:58:18 -07:00
|
|
|
|
|
|
|
typedef struct _One_Big One_Big;
|
|
|
|
struct _One_Big
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
int item_size;
|
|
|
|
|
|
|
|
int usage;
|
|
|
|
int over;
|
|
|
|
|
|
|
|
int served;
|
|
|
|
int max;
|
|
|
|
unsigned char *base;
|
|
|
|
|
|
|
|
Eina_Trash *empty;
|
2010-10-22 20:45:06 -07:00
|
|
|
Eina_Inlist *over_list;
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2012-10-10 14:48:34 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2010-10-23 09:56:36 -07:00
|
|
|
pthread_t self;
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
2011-04-24 09:49:48 -07:00
|
|
|
Eina_Lock mutex;
|
2010-07-26 08:58:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
2012-10-05 13:09:47 -07:00
|
|
|
eina_one_big_malloc(void *data, EINA_UNUSED unsigned int size)
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
|
|
|
One_Big *pool = data;
|
2010-07-28 11:10:53 -07:00
|
|
|
unsigned char *mem = NULL;
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2011-04-24 09:49:48 -07:00
|
|
|
if (!eina_lock_take(&pool->mutex))
|
2010-10-23 09:56:36 -07:00
|
|
|
{
|
2012-10-10 14:48:34 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-04-24 09:49:48 -07:00
|
|
|
assert(pthread_equal(pool->self, pthread_self()));
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
2011-04-24 09:49:48 -07:00
|
|
|
}
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-07-28 11:10:53 -07:00
|
|
|
if (pool->empty)
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(pool->empty, pool->item_size);
|
|
|
|
#endif
|
2010-07-27 19:37:05 -07:00
|
|
|
mem = eina_trash_pop(&pool->empty);
|
|
|
|
pool->usage++;
|
|
|
|
goto on_exit;
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
|
2010-09-21 08:18:15 -07:00
|
|
|
if (!pool->base)
|
|
|
|
{
|
|
|
|
pool->base = malloc(pool->item_size * pool->max);
|
|
|
|
if (!pool->base)
|
|
|
|
{
|
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
|
|
|
goto retry_smaller;
|
|
|
|
}
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS(pool->base, pool->item_size * pool->max);
|
|
|
|
#endif
|
2010-09-21 08:18:15 -07:00
|
|
|
}
|
|
|
|
|
2010-07-26 08:58:18 -07:00
|
|
|
if (pool->served < pool->max)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
mem = pool->base + (pool->served++ *pool->item_size);
|
|
|
|
pool->usage++;
|
|
|
|
goto on_exit;
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
|
2010-09-21 08:18:15 -07:00
|
|
|
retry_smaller:
|
|
|
|
eina_error_set(0);
|
2010-10-22 20:45:06 -07:00
|
|
|
mem = malloc(sizeof(Eina_Inlist) + pool->item_size);
|
2010-07-26 08:58:18 -07:00
|
|
|
if (!mem)
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
|
2010-07-26 08:58:18 -07:00
|
|
|
else
|
2010-10-22 20:45:06 -07:00
|
|
|
{
|
|
|
|
pool->over++;
|
|
|
|
memset(mem, 0, sizeof(Eina_Inlist));
|
|
|
|
pool->over_list = eina_inlist_append(pool->over_list,
|
|
|
|
(Eina_Inlist *)mem);
|
|
|
|
mem = ((unsigned char *)mem) + sizeof(Eina_Inlist);
|
|
|
|
}
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS(mem, pool->item_size);
|
|
|
|
#endif
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
on_exit:
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_release(&pool->mutex);
|
2010-10-14 12:28:32 -07:00
|
|
|
|
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_MEMPOOL_ALLOC(pool, mem, pool->item_size);
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eina_one_big_free(void *data, void *ptr)
|
|
|
|
{
|
|
|
|
One_Big *pool = data;
|
|
|
|
|
2011-04-24 09:49:48 -07:00
|
|
|
if (!eina_lock_take(&pool->mutex))
|
2010-10-23 09:56:36 -07:00
|
|
|
{
|
2012-10-10 14:48:34 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-04-24 09:49:48 -07:00
|
|
|
assert(pthread_equal(pool->self, pthread_self()));
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
2011-04-24 09:49:48 -07:00
|
|
|
}
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
if ((void *)pool->base <= ptr
|
|
|
|
&& ptr < (void *)(pool->base + (pool->max * pool->item_size)))
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
eina_trash_push(&pool->empty, ptr);
|
|
|
|
pool->usage--;
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-23 09:56:36 -07:00
|
|
|
#ifndef NDEBUG
|
|
|
|
Eina_Inlist *it;
|
|
|
|
#endif
|
2010-10-22 21:36:52 -07:00
|
|
|
Eina_Inlist *il;
|
2010-10-23 09:56:36 -07:00
|
|
|
|
2010-10-22 21:36:52 -07:00
|
|
|
il = (Eina_Inlist *)(((unsigned char *)ptr) - sizeof(Eina_Inlist));
|
2010-10-23 09:56:36 -07:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (it = pool->over_list; it != NULL; it = it->next)
|
|
|
|
if (it == il) break;
|
|
|
|
|
|
|
|
assert(it != NULL);
|
|
|
|
#endif
|
|
|
|
|
2010-10-22 20:45:06 -07:00
|
|
|
pool->over_list = eina_inlist_remove(pool->over_list, il);
|
|
|
|
free(il);
|
2010-07-27 19:37:05 -07:00
|
|
|
pool->over--;
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_MEMPOOL_FREE(pool, ptr);
|
|
|
|
#endif
|
|
|
|
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_release(&pool->mutex);
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
static void *
|
2012-10-05 13:09:47 -07:00
|
|
|
eina_one_big_realloc(EINA_UNUSED void *data,
|
|
|
|
EINA_UNUSED void *element,
|
|
|
|
EINA_UNUSED unsigned int size)
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-27 19:37:05 -07:00
|
|
|
static void *
|
|
|
|
eina_one_big_init(const char *context,
|
2012-10-05 13:09:47 -07:00
|
|
|
EINA_UNUSED const char *option,
|
2010-07-27 19:37:05 -07:00
|
|
|
va_list args)
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
|
|
|
One_Big *pool;
|
|
|
|
int item_size;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
length = context ? strlen(context) + 1 : 0;
|
|
|
|
|
|
|
|
pool = calloc(1, sizeof (One_Big) + length);
|
2010-07-27 19:37:05 -07:00
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
2010-07-26 08:58:18 -07:00
|
|
|
|
|
|
|
item_size = va_arg(args, int);
|
|
|
|
|
|
|
|
pool->item_size = eina_mempool_alignof(item_size);
|
|
|
|
pool->max = va_arg(args, int);
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
pool->name = (const char *)(pool + 1);
|
|
|
|
memcpy((char *)pool->name, context, length);
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
|
|
|
|
2012-10-10 14:48:34 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2010-10-23 09:56:36 -07:00
|
|
|
pool->self = pthread_self();
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_new(&pool->mutex);
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-10-14 12:28:32 -07:00
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_CREATE_MEMPOOL(pool, 0, 1);
|
|
|
|
#endif
|
|
|
|
|
2010-07-26 08:58:18 -07:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eina_one_big_shutdown(void *data)
|
|
|
|
{
|
2010-10-22 21:36:52 -07:00
|
|
|
One_Big *pool = data;
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-10-22 21:36:52 -07:00
|
|
|
if (!pool) return;
|
2011-04-24 09:49:48 -07:00
|
|
|
if (!eina_lock_take(&pool->mutex))
|
2010-10-23 09:56:36 -07:00
|
|
|
{
|
2012-10-10 14:48:34 -07:00
|
|
|
#ifdef EINA_HAVE_DEBUG_THREADS
|
2011-04-24 09:49:48 -07:00
|
|
|
assert(pthread_equal(pool->self, pthread_self()));
|
2010-10-22 21:36:52 -07:00
|
|
|
#endif
|
2011-04-24 09:49:48 -07:00
|
|
|
}
|
2010-08-05 05:36:36 -07:00
|
|
|
|
2010-10-22 20:45:06 -07:00
|
|
|
if (pool->over > 0)
|
|
|
|
{
|
|
|
|
// FIXME: should we warn here? one_big mempool exceeded its alloc and now
|
|
|
|
// mempool is cleaning up the mess created. be quiet for now as we were before
|
|
|
|
// but edje seems to be a big offender at the moment! bad cedric! :)
|
|
|
|
// WRN(
|
|
|
|
// "Pool [%s] over by %i. cleaning up for you",
|
|
|
|
// pool->name, pool->over);
|
|
|
|
while (pool->over_list)
|
|
|
|
{
|
|
|
|
Eina_Inlist *il = pool->over_list;
|
|
|
|
pool->over_list = eina_inlist_remove(pool->over_list, il);
|
|
|
|
free(il);
|
|
|
|
pool->over--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pool->over > 0)
|
|
|
|
{
|
|
|
|
WRN(
|
|
|
|
"Pool [%s] still over by %i\n",
|
|
|
|
pool->name, pool->over);
|
|
|
|
}
|
2010-10-14 12:28:32 -07:00
|
|
|
|
|
|
|
#ifndef NVALGRIND
|
|
|
|
VALGRIND_DESTROY_MEMPOOL(pool);
|
|
|
|
#endif
|
2010-07-26 08:58:18 -07:00
|
|
|
|
2010-10-22 21:40:30 -07:00
|
|
|
if (pool->base) free(pool->base);
|
2010-10-23 09:56:36 -07:00
|
|
|
|
2011-04-24 09:49:48 -07:00
|
|
|
eina_lock_release(&pool->mutex);
|
|
|
|
eina_lock_free(&pool->mutex);
|
2010-07-26 08:58:18 -07:00
|
|
|
free(pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Eina_Mempool_Backend _eina_one_big_mp_backend = {
|
2010-07-27 19:37:05 -07:00
|
|
|
"one_big",
|
|
|
|
&eina_one_big_init,
|
|
|
|
&eina_one_big_free,
|
|
|
|
&eina_one_big_malloc,
|
|
|
|
&eina_one_big_realloc,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2011-04-11 07:07:42 -07:00
|
|
|
&eina_one_big_shutdown,
|
|
|
|
NULL
|
2010-07-26 08:58:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Eina_Bool one_big_init(void)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-10-23 13:57:08 -07:00
|
|
|
_eina_one_big_mp_log_dom = eina_log_domain_register("eina_one_big_mempool",
|
|
|
|
EINA_LOG_COLOR_DEFAULT);
|
|
|
|
if (_eina_one_big_mp_log_dom < 0)
|
2010-07-26 08:58:18 -07:00
|
|
|
{
|
2010-07-27 19:37:05 -07:00
|
|
|
EINA_LOG_ERR("Could not register log domain: eina_one_big_mempool");
|
|
|
|
return EINA_FALSE;
|
2010-07-26 08:58:18 -07:00
|
|
|
}
|
2010-07-27 19:37:05 -07:00
|
|
|
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
|
|
|
return eina_mempool_register(&_eina_one_big_mp_backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void one_big_shutdown(void)
|
|
|
|
{
|
|
|
|
eina_mempool_unregister(&_eina_one_big_mp_backend);
|
|
|
|
#ifdef DEBUG
|
2010-10-23 13:57:08 -07:00
|
|
|
eina_log_domain_unregister(_eina_one_big_mp_log_dom);
|
|
|
|
_eina_one_big_mp_log_dom = -1;
|
2010-07-26 08:58:18 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef EINA_STATIC_BUILD_ONE_BIG
|
|
|
|
|
|
|
|
EINA_MODULE_INIT(one_big_init);
|
|
|
|
EINA_MODULE_SHUTDOWN(one_big_shutdown);
|
|
|
|
|
|
|
|
#endif /* ! EINA_STATIC_BUILD_ONE_BIG */
|
|
|
|
|