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-10-29 16:12:56 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2009-08-28 05:03:34 -07:00
|
|
|
#include "eina_config.h"
|
|
|
|
#include "eina_private.h"
|
|
|
|
#include "eina_safety_checks.h"
|
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-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
|
|
|
*============================================================================*/
|
2009-01-20 00:29:41 -08:00
|
|
|
|
2009-06-22 13:03:58 -07:00
|
|
|
/**
|
|
|
|
* @cond LOCAL
|
|
|
|
*/
|
|
|
|
|
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;
|
2009-01-20 00:29:41 -08: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
|
|
|
|
|
2009-06-22 13:03:58 -07:00
|
|
|
/**
|
|
|
|
* @endcond
|
|
|
|
*/
|
|
|
|
|
2008-08-28 00:46:42 -07:00
|
|
|
/*============================================================================*
|
|
|
|
* Global *
|
|
|
|
*============================================================================*/
|
2009-01-20 00:29:41 -08:00
|
|
|
|
2008-10-20 04:40:01 -07:00
|
|
|
EAPI Eina_Bool eina_mempool_register(Eina_Mempool_Backend *be)
|
2008-09-24 05:55:31 -07:00
|
|
|
{
|
2008-10-20 04:40:01 -07:00
|
|
|
return eina_hash_add(_backends, be->name, be);
|
2008-09-24 05:55:31 -07:00
|
|
|
}
|
2008-08-28 00:46:42 -07:00
|
|
|
|
2008-10-20 04:40:01 -07:00
|
|
|
EAPI void eina_mempool_unregister(Eina_Mempool_Backend *be)
|
2008-09-24 05:55:31 -07:00
|
|
|
{
|
|
|
|
eina_hash_del(_backends, be->name, be);
|
|
|
|
}
|
2009-01-20 00:29:41 -08:00
|
|
|
|
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
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
/**
|
2009-06-22 13:03:58 -07:00
|
|
|
* @addtogroup Eina_Memory_Pool_Group Memory Pool
|
|
|
|
*
|
|
|
|
* @brief These functions provide memory pool management.
|
2008-08-11 09:32:19 -07:00
|
|
|
*
|
2009-06-22 13:03:58 -07:00
|
|
|
* @{
|
2008-07-30 05:46:55 -07:00
|
|
|
*/
|
2009-06-22 13:03:58 -07:00
|
|
|
|
|
|
|
EAPI Eina_Error EINA_ERROR_NOT_MEMPOOL_MODULE = 0;
|
|
|
|
|
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-11-01 08:39:53 -07:00
|
|
|
char *path;
|
|
|
|
|
2009-09-03 17:53:19 -07:00
|
|
|
if (!eina_safety_checks_init())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not initialize eina safety checks.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-09 09:02:20 -07:00
|
|
|
if (!eina_hash_init())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not initialize eina hash module.\n");
|
2009-09-03 17:53:19 -07:00
|
|
|
goto hash_init_error;
|
2009-06-09 09:02:20 -07:00
|
|
|
}
|
|
|
|
if (!eina_module_init())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Could not initialize eina module module.\n");
|
|
|
|
goto module_init_error;
|
|
|
|
}
|
2008-08-18 01:18:16 -07:00
|
|
|
|
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-11-01 08:39:53 -07:00
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
/* dynamic backends */
|
2008-11-01 08:39:53 -07:00
|
|
|
_modules = eina_module_list_get(NULL, PACKAGE_LIB_DIR "/eina/mp/", 0, NULL, NULL);
|
|
|
|
|
|
|
|
path = eina_module_environment_path_get("HOME", "/.eina/mp/");
|
|
|
|
_modules = eina_module_list_get(_modules, path, 0, NULL, NULL);
|
|
|
|
if (path) free(path);
|
|
|
|
|
|
|
|
path = eina_module_environment_path_get("EINA_MODULES_MEMPOOL_DIR", "/eina/mp/");
|
|
|
|
_modules = eina_module_list_get(_modules, path, 0, NULL, NULL);
|
|
|
|
if (path) free(path);
|
|
|
|
|
|
|
|
path = eina_module_symbol_path_get(eina_mempool_init, "/eina/mp/");
|
|
|
|
_modules = eina_module_list_get(_modules, path, 0, NULL, NULL);
|
|
|
|
if (path) free(path);
|
|
|
|
|
2008-10-29 16:12:56 -07:00
|
|
|
if (!_modules)
|
|
|
|
{
|
2008-10-29 16:41:55 -07:00
|
|
|
EINA_ERROR_PERR("ERROR: no mempool modules able to be loaded.\n");
|
2009-06-09 09:02:20 -07:00
|
|
|
eina_hash_free(_backends);
|
|
|
|
goto mempool_init_error;
|
2008-10-29 16:12:56 -07:00
|
|
|
}
|
2008-09-24 05:55:31 -07:00
|
|
|
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();
|
2009-08-06 11:08:14 -07:00
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_FIXED_BITMAP
|
|
|
|
fixed_bitmap_init();
|
2008-09-24 05:55:31 -07:00
|
|
|
#endif
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
|
|
|
return ++_init_count;
|
2009-06-09 09:02:20 -07:00
|
|
|
|
|
|
|
mempool_init_error:
|
|
|
|
eina_module_shutdown();
|
|
|
|
module_init_error:
|
|
|
|
eina_hash_shutdown();
|
2009-09-03 17:53:19 -07:00
|
|
|
hash_init_error:
|
|
|
|
eina_safety_checks_shutdown();
|
2009-06-09 09:02:20 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2008-07-30 05:46:55 -07:00
|
|
|
}
|
2008-08-18 01:18:16 -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();
|
2009-08-06 11:08:14 -07:00
|
|
|
#endif
|
|
|
|
#ifdef EINA_STATIC_BUILD_FIXED_BITMAP
|
|
|
|
fixed_bitmap_shutdown();
|
2008-09-24 05:55:31 -07:00
|
|
|
#endif
|
|
|
|
/* dynamic backends */
|
2009-07-17 04:13:38 -07:00
|
|
|
eina_module_list_flush(_modules);
|
2009-06-09 09:02:20 -07:00
|
|
|
if (_modules)
|
|
|
|
eina_array_free(_modules);
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
eina_module_shutdown();
|
2009-06-09 09:02:20 -07:00
|
|
|
|
|
|
|
if (_backends)
|
|
|
|
eina_hash_free(_backends);
|
|
|
|
|
2008-09-24 05:55:31 -07:00
|
|
|
eina_hash_shutdown();
|
2009-09-03 17:53:19 -07:00
|
|
|
eina_safety_checks_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-31 10:00:56 -07:00
|
|
|
EAPI Eina_Mempool *
|
2009-06-24 09:56:49 -07:00
|
|
|
eina_mempool_add(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-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN_VAL(name, 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
|
|
|
|
2009-06-24 09:56:49 -07:00
|
|
|
EAPI void eina_mempool_del(Eina_Mempool *mp)
|
2008-07-30 05:46:55 -07:00
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp->backend.shutdown);
|
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)
|
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp->backend.garbage_collect);
|
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)
|
|
|
|
{
|
2008-12-26 10:31:14 -08:00
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp);
|
|
|
|
EINA_SAFETY_ON_NULL_RETURN(mp->backend.statistics);
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend.statistics(mp->backend_data);
|
2008-09-17 08:20:35 -07:00
|
|
|
}
|
2009-06-22 13:03:58 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|