From 4423005ab43efd1c0d1a846957b3a34bc129efbb Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Thu, 18 Sep 2008 14:16:47 +0000 Subject: [PATCH] Avoid some dereferencing and provide inline for mempool alloc/free/realloc. SVN revision: 36078 --- legacy/eina/src/include/Makefile.am | 1 + legacy/eina/src/include/eina_inline_mempool.x | 60 +++++++++++++++ legacy/eina/src/include/eina_mempool.h | 8 +- legacy/eina/src/lib/eina_mempool.c | 77 ++++--------------- 4 files changed, 82 insertions(+), 64 deletions(-) create mode 100644 legacy/eina/src/include/eina_inline_mempool.x diff --git a/legacy/eina/src/include/Makefile.am b/legacy/eina/src/include/Makefile.am index 1a221219f9..fa4289795b 100644 --- a/legacy/eina/src/include/Makefile.am +++ b/legacy/eina/src/include/Makefile.am @@ -24,6 +24,7 @@ eina_convert.h \ eina_rbtree.h \ eina_benchmark.h \ eina_inline_rbtree.x \ +eina_inline_mempool.x \ eina_iterator.h installed_mainheaderdir = $(prefix)/include/eina-@VMAJ@ diff --git a/legacy/eina/src/include/eina_inline_mempool.x b/legacy/eina/src/include/eina_inline_mempool.x new file mode 100644 index 0000000000..b13a39bb4d --- /dev/null +++ b/legacy/eina/src/include/eina_inline_mempool.x @@ -0,0 +1,60 @@ +/* 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 . + */ + +#ifndef EINA_INLINE_MEMPOOL_X_ +#define EINA_INLINE_MEMPOOL_X_ + +/* Memory Pool */ +typedef struct _Eina_Mempool_Backend Eina_Mempool_Backend; +struct _Eina_Mempool_Backend +{ + void *(*init)(const char *context, const char *options, va_list args); + void (*free)(void *data, void *element); + void *(*alloc)(void *data, unsigned int size); + void *(*realloc)(void *data, void *element, unsigned int size); + void (*garbage_collect)(void *data); + void (*statistics)(void *data); + void (*shutdown)(void *data); +}; + +struct _Eina_Mempool +{ + Eina_Module *module; + Eina_Mempool_Backend backend; + void *backend_data; +}; + +static inline void * +eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size) +{ + return mp->backend.realloc(mp->backend_data, element, size); +} + +static inline void * +eina_mempool_alloc(Eina_Mempool *mp, unsigned int size) +{ + return mp->backend.alloc(mp->backend_data, size); +} + +static inline void +eina_mempool_free(Eina_Mempool *mp, void *element) +{ + mp->backend.free(mp->backend_data, element); +} + +#endif diff --git a/legacy/eina/src/include/eina_mempool.h b/legacy/eina/src/include/eina_mempool.h index 8ea805bf80..801fbcbe99 100644 --- a/legacy/eina/src/include/eina_mempool.h +++ b/legacy/eina/src/include/eina_mempool.h @@ -38,13 +38,15 @@ EAPI Eina_Module_Group * eina_mempool_module_group_get(void); EAPI Eina_Mempool * eina_mempool_new(const char *module, const char *context, const char *options, ...); EAPI void eina_mempool_delete(Eina_Mempool *mp); -EAPI void * eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size); -EAPI void * eina_mempool_alloc(Eina_Mempool *mp, unsigned int size); -EAPI void eina_mempool_free(Eina_Mempool *mp, void *element); +static inline void * eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size); +static inline void * eina_mempool_alloc(Eina_Mempool *mp, unsigned int size); +static inline void eina_mempool_free(Eina_Mempool *mp, void *element); EAPI void eina_mempool_gc(Eina_Mempool *mp); EAPI void eina_mempool_statistics(Eina_Mempool *mp); +#include "eina_inline_mempool.x" + /** @} */ #endif /* EINA_MEMPOOL_H_ */ diff --git a/legacy/eina/src/lib/eina_mempool.c b/legacy/eina/src/lib/eina_mempool.c index 10d64f3ea6..c9f50bbe33 100644 --- a/legacy/eina/src/lib/eina_mempool.c +++ b/legacy/eina/src/lib/eina_mempool.c @@ -34,16 +34,6 @@ static Eina_Module_Group *_group; static int _init_count = 0; -struct _Eina_Mempool -{ -#ifdef DEBUG - unsigned int magic; -#endif - Eina_Module *module; - Eina_Mempool_Backend *backend; - void *backend_data; -}; - static Eina_Mempool * _new_from_buffer(const char *module, const char *context, const char *options, va_list args) { @@ -60,8 +50,8 @@ _new_from_buffer(const char *module, const char *context, const char *options, v mp = malloc(sizeof(Eina_Mempool)); if (!mp) goto on_error; mp->module = m; - mp->backend = eina_module_export_object_get(m); - mp->backend_data = mp->backend->init(context, options, args); + mp->backend = *(Eina_Mempool_Backend *)eina_module_export_object_get(m); + mp->backend_data = mp->backend.init(context, options, args); return mp; @@ -108,16 +98,14 @@ eina_mempool_init(void) EAPI int eina_mempool_shutdown(void) { - if (!_init_count) - return _init_count; _init_count--; - if (!_init_count) - { - /* remove the list of modules */ - eina_module_group_delete(_group); - eina_module_shutdown(); - } - return _init_count; + if (_init_count != 0) return _init_count; + + /* remove the list of modules */ + eina_module_group_delete(_group); + eina_module_shutdown(); + + return 0; } EAPI Eina_Module_Group * @@ -135,7 +123,7 @@ eina_mempool_new(const char *name, const char *context, const char *options, ... Eina_Mempool *mp; va_list args; - assert(name); + if (!name) return NULL; va_start(args, options); mp = _new_from_buffer(name, context, options, args); @@ -149,58 +137,25 @@ eina_mempool_new(const char *name, const char *context, const char *options, ... */ EAPI void eina_mempool_delete(Eina_Mempool *mp) { - assert(mp); + if (!mp) return ; - mp->backend->shutdown(mp->backend_data); + mp->backend.shutdown(mp->backend_data); eina_module_unload(mp->module); free(mp); } -/** - * - */ -EAPI void * eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size) -{ - assert(mp); - assert(mp->backend->realloc); - - return mp->backend->realloc(mp->backend_data, element, size); -} - -/** - * - */ -EAPI void * eina_mempool_alloc(Eina_Mempool *mp, unsigned int size) -{ - assert(mp); - assert(mp->backend->alloc); - - return mp->backend->alloc(mp->backend_data, size); -} - -/** - * - */ -EAPI void eina_mempool_free(Eina_Mempool *mp, void *element) -{ - assert(mp); - assert(mp->backend->free); - - mp->backend->free(mp->backend_data, element); -} - EAPI void eina_mempool_gc(Eina_Mempool *mp) { assert(mp); - assert(mp->backend->garbage_collect); + assert(mp->backend.garbage_collect); - mp->backend->garbage_collect(mp->backend_data); + mp->backend.garbage_collect(mp->backend_data); } EAPI void eina_mempool_statistics(Eina_Mempool *mp) { assert(mp); - assert(mp->backend->statistics); + assert(mp->backend.statistics); - mp->backend->statistics(mp->backend_data); + mp->backend.statistics(mp->backend_data); }