2008-08-06 11:15:24 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2007-2008 Jorge Luis Zapata Muga
|
|
|
|
*
|
|
|
|
* 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-08-28 00:46:42 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
#include "eina_mempool.h"
|
|
|
|
#include "eina_list.h"
|
|
|
|
#include "eina_module.h"
|
2008-07-30 05:46:55 -07:00
|
|
|
#include "eina_private.h"
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2008-08-11 09:32:19 -07:00
|
|
|
* Local *
|
2008-07-30 05:46:55 -07:00
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
static Eina_Module_Group *_group;
|
2008-07-30 05:46:55 -07:00
|
|
|
static int _init_count = 0;
|
|
|
|
|
2008-07-31 10:00:56 -07:00
|
|
|
static Eina_Mempool *
|
|
|
|
_new_from_buffer(const char *module, const char *context, const char *options, va_list args)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-18 01:18:16 -07:00
|
|
|
Eina_Mempool *mp;
|
|
|
|
Eina_Module *m;
|
2008-09-18 03:04:01 -07:00
|
|
|
Eina_Error err = EINA_ERROR_NOT_MEMPOOL_MODULE;
|
2008-07-31 10:00:56 -07:00
|
|
|
|
2008-08-18 01:18:16 -07:00
|
|
|
eina_error_set(0);
|
|
|
|
m = eina_module_new(_group, module);
|
|
|
|
if (!m) return NULL;
|
2008-09-18 03:04:01 -07:00
|
|
|
if (eina_module_load(m) == EINA_FALSE) goto on_error;
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-09-18 03:04:01 -07:00
|
|
|
err = EINA_ERROR_OUT_OF_MEMORY;
|
2008-08-18 01:18:16 -07:00
|
|
|
mp = malloc(sizeof(Eina_Mempool));
|
2008-09-18 03:04:01 -07:00
|
|
|
if (!mp) goto on_error;
|
2008-08-18 01:18:16 -07:00
|
|
|
mp->module = m;
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend = *(Eina_Mempool_Backend *)eina_module_export_object_get(m);
|
|
|
|
mp->backend_data = mp->backend.init(context, options, args);
|
2008-08-18 01:18:16 -07:00
|
|
|
|
|
|
|
return mp;
|
|
|
|
|
|
|
|
on_error:
|
2008-09-18 03:04:01 -07:00
|
|
|
eina_error_set(err);
|
2008-08-18 01:18:16 -07:00
|
|
|
if (m) eina_module_delete(m);
|
2008-07-30 05:46:55 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/*============================================================================*
|
2008-08-11 09:32:19 -07:00
|
|
|
* API *
|
2008-07-30 05:46:55 -07:00
|
|
|
*============================================================================*/
|
2008-08-28 00:46:42 -07:00
|
|
|
|
|
|
|
EAPI Eina_Error EINA_ERROR_NOT_MEMPOOL_MODULE = 0;
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
2008-08-11 09:32:19 -07:00
|
|
|
*
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-07-31 10:00:56 -07:00
|
|
|
EAPI int
|
|
|
|
eina_mempool_init(void)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-11 09:32:19 -07:00
|
|
|
if (!_init_count)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-08-18 01:18:16 -07:00
|
|
|
eina_module_init();
|
|
|
|
|
|
|
|
_group = eina_module_group_new();
|
|
|
|
if (!_group) return 0;
|
|
|
|
|
|
|
|
eina_module_app_register(_group, "eina", "mp", NULL);
|
|
|
|
|
2008-09-05 00:58:38 -07:00
|
|
|
EINA_ERROR_NOT_MEMPOOL_MODULE = eina_error_msg_register("Not a memory pool module.");
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
/* get all the modules */
|
|
|
|
return ++_init_count;
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
2008-08-11 09:32:19 -07:00
|
|
|
*
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2008-07-31 10:00:56 -07:00
|
|
|
EAPI int
|
|
|
|
eina_mempool_shutdown(void)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
_init_count--;
|
2008-09-18 07:16:47 -07:00
|
|
|
if (_init_count != 0) return _init_count;
|
|
|
|
|
|
|
|
/* remove the list of modules */
|
|
|
|
eina_module_group_delete(_group);
|
|
|
|
eina_module_shutdown();
|
|
|
|
|
|
|
|
return 0;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-09-17 08:20:35 -07:00
|
|
|
EAPI Eina_Module_Group *
|
|
|
|
eina_mempool_module_group_get(void)
|
|
|
|
{
|
|
|
|
return _group;
|
|
|
|
}
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2008-07-31 10:00:56 -07:00
|
|
|
EAPI Eina_Mempool *
|
|
|
|
eina_mempool_new(const char *name, const char *context, const char *options, ...)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
|
|
|
Eina_Mempool *mp;
|
|
|
|
va_list args;
|
2008-07-31 10:00:56 -07:00
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
if (!name) return NULL;
|
2008-07-31 10:00:56 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
va_start(args, options);
|
2008-07-31 10:00:56 -07:00
|
|
|
mp = _new_from_buffer(name, context, options, args);
|
2008-07-30 05:46:55 -07:00
|
|
|
va_end(args);
|
2008-07-31 10:00:56 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
return mp;
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
EAPI void eina_mempool_delete(Eina_Mempool *mp)
|
|
|
|
{
|
2008-09-18 07:16:47 -07:00
|
|
|
if (!mp) return ;
|
2008-07-31 10:00:56 -07:00
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend.shutdown(mp->backend_data);
|
2008-07-30 05:46:55 -07:00
|
|
|
eina_module_unload(mp->module);
|
|
|
|
free(mp);
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-09-17 08:20:35 -07:00
|
|
|
EAPI void eina_mempool_gc(Eina_Mempool *mp)
|
|
|
|
{
|
|
|
|
assert(mp);
|
2008-09-18 07:16:47 -07:00
|
|
|
assert(mp->backend.garbage_collect);
|
2008-09-17 08:20:35 -07:00
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend.garbage_collect(mp->backend_data);
|
2008-09-17 08:20:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void eina_mempool_statistics(Eina_Mempool *mp)
|
|
|
|
{
|
|
|
|
assert(mp);
|
2008-09-18 07:16:47 -07:00
|
|
|
assert(mp->backend.statistics);
|
2008-09-17 08:20:35 -07:00
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend.statistics(mp->backend_data);
|
2008-09-17 08:20:35 -07:00
|
|
|
}
|