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-09-29 06:28:42 -07:00
|
|
|
#include <stdlib.h>
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-07-30 06:35:49 -07:00
|
|
|
#include "eina_mempool.h"
|
2008-09-24 05:55:31 -07:00
|
|
|
#include "eina_hash.h"
|
2008-07-30 06:35:49 -07:00
|
|
|
#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-09-24 05:55:31 -07:00
|
|
|
static Eina_Hash *_backends;
|
2008-09-29 06:28:42 -07:00
|
|
|
static Eina_Array *_modules;
|
2008-07-30 05:46:55 -07:00
|
|
|
static int _init_count = 0;
|
|
|
|
|
2008-07-31 10:00:56 -07:00
|
|
|
static Eina_Mempool *
|
2008-09-24 05:55:31 -07:00
|
|
|
_new_from_buffer(const char *name, const char *context, const char *options, va_list args)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-09-24 05:55:31 -07:00
|
|
|
Eina_Mempool_Backend *be;
|
2008-08-18 01:18:16 -07:00
|
|
|
Eina_Mempool *mp;
|
2008-09-24 05:55:31 -07:00
|
|
|
|
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);
|
2008-09-24 05:55:31 -07:00
|
|
|
be = eina_hash_find(_backends, name);
|
|
|
|
if (!be)
|
|
|
|
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-09-24 05:55:31 -07:00
|
|
|
|
|
|
|
/* FIXME why backend is not a pointer? */
|
|
|
|
mp->backend = *be;
|
2008-09-18 07:16:47 -07:00
|
|
|
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-07-30 05:46:55 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
/* Built-in backend's prototypes */
|
|
|
|
#ifdef EINA_STATIC_BUILD_CHAINED_POOL
|
|
|
|
Eina_Bool chained_init(void);
|
|
|
|
void chained_shutdown(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINA_STATIC_BUILD_PASS_THROUGH
|
|
|
|
Eina_Bool pass_through_init(void);
|
|
|
|
void pass_through_shutdown(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_UNKNOWN
|
|
|
|
Eina_Bool ememoa_unknown_init(void);
|
|
|
|
void ememoa_unknown_shutdown(void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_FIXED
|
|
|
|
Eina_Bool ememoa_fixed_init(void);
|
|
|
|
void ememoa_fixed_shutdown(void);
|
|
|
|
#endif
|
|
|
|
|
2008-09-26 08:27:46 -07:00
|
|
|
#ifdef EINA_STATIC_BUILD_FIXED_BITMAP
|
|
|
|
Eina_Bool fixed_bitmap_init(void);
|
|
|
|
void fixed_bitmap_shutdown(void);
|
|
|
|
#endif
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2008-09-24 05:55:31 -07:00
|
|
|
Eina_Bool eina_mempool_register(Eina_Mempool_Backend *be)
|
|
|
|
{
|
|
|
|
return eina_hash_add(_backends, be->name, be);
|
|
|
|
}
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
void eina_mempool_unregister(Eina_Mempool_Backend *be)
|
|
|
|
{
|
|
|
|
eina_hash_del(_backends, be->name, be);
|
|
|
|
}
|
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-09-24 05:55:31 -07:00
|
|
|
eina_hash_init();
|
2008-08-18 01:18:16 -07:00
|
|
|
eina_module_init();
|
|
|
|
|
2008-09-05 00:58:38 -07:00
|
|
|
EINA_ERROR_NOT_MEMPOOL_MODULE = eina_error_msg_register("Not a memory pool module.");
|
2008-09-24 10:14:29 -07:00
|
|
|
_backends = eina_hash_string_superfast_new(NULL);
|
2008-09-24 05:55:31 -07:00
|
|
|
/* dynamic backends */
|
|
|
|
_modules = eina_module_list_get(PACKAGE_LIB_DIR "/eina/mp/", 0, NULL, NULL);
|
|
|
|
eina_module_list_load(_modules);
|
|
|
|
/* builtin backends */
|
|
|
|
#ifdef EINA_STATIC_BUILD_CHAINED_POOL
|
|
|
|
chained_init();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_PASS_THROUGH
|
|
|
|
pass_through_init();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_UNKNOWN
|
|
|
|
ememoa_unknown_init();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_FIXED
|
|
|
|
ememoa_fixed_init();
|
|
|
|
#endif
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
/* builtin backends */
|
|
|
|
#ifdef EINA_STATIC_BUILD_CHAINED_POOL
|
|
|
|
chained_shutdown();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_PASS_THROUGH
|
|
|
|
pass_through_shutdown();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_UNKNOWN
|
|
|
|
ememoa_unknown_shutdown();
|
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_EMEMOA_FIXED
|
|
|
|
ememoa_fixed_shutdown();
|
|
|
|
#endif
|
|
|
|
/* dynamic backends */
|
|
|
|
eina_module_list_unload(_modules);
|
2008-09-18 07:16:47 -07:00
|
|
|
eina_module_shutdown();
|
2008-09-24 05:55:31 -07:00
|
|
|
/* TODO delete the _modules list */
|
|
|
|
eina_hash_shutdown();
|
2008-09-18 07:16:47 -07:00
|
|
|
return 0;
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
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
|
|
|
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
|
|
|
}
|