2008-09-18 07:16:47 -07:00
|
|
|
/* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EINA_INLINE_MEMPOOL_X_
|
|
|
|
#define EINA_INLINE_MEMPOOL_X_
|
|
|
|
|
2011-12-02 07:27:35 -08:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
/* Memory Pool */
|
2011-04-11 10:45:01 -07:00
|
|
|
typedef struct _Eina_Mempool_Backend_ABI1 Eina_Mempool_Backend_ABI1;
|
|
|
|
typedef struct _Eina_Mempool_Backend_ABI2 Eina_Mempool_Backend_ABI2;
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
struct _Eina_Mempool_Backend
|
|
|
|
{
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Name of the mempool backend */
|
2008-09-24 05:55:31 -07:00
|
|
|
const char *name;
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to initialize the backend. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void *(*init)(const char *context, const char *options, va_list args);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to free memory back to the mempool. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void (*free)(void *data, void *element);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to allocate memory from the mempool. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void *(*alloc)(void *data, unsigned int size);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to change the size of a block of memory that is currently
|
|
|
|
* allocated. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void *(*realloc)(void *data, void *element, unsigned int size);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to trigger a garbage collection; can be NULL if the feature
|
|
|
|
* isn't available in the backend. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void (*garbage_collect)(void *data);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Report statistics on the content of the mempool; can be NULL if the
|
|
|
|
* feature isn't available in the backend. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void (*statistics)(void *data);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to destroy the backend, freeing memory back to the operating
|
|
|
|
* system. */
|
2008-09-18 07:16:47 -07:00
|
|
|
void (*shutdown)(void *data);
|
2014-10-20 05:51:37 -07:00
|
|
|
/** Function to optimize the placement of objects in the mempool (it's
|
|
|
|
* different from garbage_collect); can be NULL if the feature isn't
|
|
|
|
* available in the backend.
|
|
|
|
* @see Eina_Mempool_Repack_Cb */
|
2011-04-11 07:07:42 -07:00
|
|
|
void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
|
2017-08-15 13:46:34 -07:00
|
|
|
/** Function to check is a valid element from a mempool.
|
|
|
|
* @see eina_mempool_from
|
|
|
|
*/
|
|
|
|
Eina_Bool (*from)(void *data, void *element);
|
2019-09-14 10:54:05 -07:00
|
|
|
/** Function to get an Eina_Iterator that will walk every allocated element
|
|
|
|
* in the pool.
|
|
|
|
* @use eina_mempool_iterator_new
|
|
|
|
*/
|
|
|
|
Eina_Iterator *(*iterator)(void *data);
|
2008-09-18 07:16:47 -07:00
|
|
|
};
|
|
|
|
|
2011-04-11 10:45:01 -07:00
|
|
|
struct _Eina_Mempool_Backend_ABI1
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
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_Backend_ABI2
|
|
|
|
{
|
|
|
|
void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
|
2017-08-15 13:46:34 -07:00
|
|
|
Eina_Bool (*from)(void *data, void *element);
|
2019-09-14 10:54:05 -07:00
|
|
|
Eina_Iterator *(*iterator)(void *data);
|
2011-04-11 10:45:01 -07:00
|
|
|
};
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
struct _Eina_Mempool
|
|
|
|
{
|
2011-04-11 10:45:01 -07:00
|
|
|
Eina_Mempool_Backend_ABI1 backend;
|
2008-09-18 07:16:47 -07:00
|
|
|
void *backend_data;
|
2011-04-12 01:52:59 -07:00
|
|
|
Eina_Mempool_Backend_ABI2 *backend2;
|
2008-09-18 07:16:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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 *
|
2009-06-24 09:56:49 -07:00
|
|
|
eina_mempool_malloc(Eina_Mempool *mp, unsigned int size)
|
2008-09-18 07:16:47 -07:00
|
|
|
{
|
|
|
|
return mp->backend.alloc(mp->backend_data, size);
|
|
|
|
}
|
|
|
|
|
2011-12-02 07:27:35 -08:00
|
|
|
static inline void *
|
|
|
|
eina_mempool_calloc(Eina_Mempool *mp, unsigned int size)
|
|
|
|
{
|
|
|
|
void *r = mp->backend.alloc(mp->backend_data, size);
|
2016-09-18 16:58:56 -07:00
|
|
|
if (r) memset(r, 0, size);
|
2011-12-02 07:27:35 -08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
static inline void
|
|
|
|
eina_mempool_free(Eina_Mempool *mp, void *element)
|
|
|
|
{
|
2016-09-18 16:58:56 -07:00
|
|
|
if (element) mp->backend.free(mp->backend_data, element);
|
2008-09-18 07:16:47 -07:00
|
|
|
}
|
|
|
|
|
2017-08-15 13:46:34 -07:00
|
|
|
static inline Eina_Bool
|
|
|
|
eina_mempool_from(Eina_Mempool *mp, void *element)
|
|
|
|
{
|
|
|
|
if (!element) return EINA_FALSE;
|
|
|
|
return mp->backend2->from(mp->backend_data, element);
|
|
|
|
}
|
|
|
|
|
2019-09-14 10:54:05 -07:00
|
|
|
static inline Eina_Iterator *
|
|
|
|
eina_mempool_iterator_new(Eina_Mempool *mp)
|
|
|
|
{
|
|
|
|
if (!mp->backend2->iterator) return NULL;
|
|
|
|
return mp->backend2->iterator(mp->backend_data);
|
|
|
|
}
|
|
|
|
|
2013-04-10 19:58:22 -07:00
|
|
|
static inline unsigned int
|
|
|
|
eina_mempool_alignof(unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned int align;
|
|
|
|
|
|
|
|
#if __WORDSIZE == 32
|
2016-09-18 16:58:56 -07:00
|
|
|
if (size >= 8)
|
2013-04-10 19:58:22 -07:00
|
|
|
{
|
|
|
|
align = 8;
|
2016-09-18 16:58:56 -07:00
|
|
|
calc:
|
|
|
|
return ((size / align) + (size % align ? 1 : 0)) * align;
|
2013-04-10 19:58:22 -07:00
|
|
|
}
|
2016-09-18 16:58:56 -07:00
|
|
|
#else // __WORDSIZE == 64
|
|
|
|
if (size >= 16)
|
2013-04-10 19:58:22 -07:00
|
|
|
{
|
2016-09-18 16:58:56 -07:00
|
|
|
align = 16;
|
|
|
|
calc:
|
|
|
|
return ((size / align) + (size % align ? 1 : 0)) * align;
|
2013-04-10 19:58:22 -07:00
|
|
|
}
|
2016-09-18 16:58:56 -07:00
|
|
|
else if (size >= 8)
|
2013-04-10 19:58:22 -07:00
|
|
|
{
|
2016-09-18 16:58:56 -07:00
|
|
|
align = 8;
|
|
|
|
goto calc;
|
2013-04-10 19:58:22 -07:00
|
|
|
}
|
|
|
|
#endif
|
2016-09-18 16:58:56 -07:00
|
|
|
else if (size >= 4)
|
|
|
|
{
|
|
|
|
align = 4;
|
|
|
|
goto calc;
|
|
|
|
}
|
|
|
|
align = 2;
|
|
|
|
goto calc;
|
2013-04-10 19:58:22 -07:00
|
|
|
}
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
#endif
|